Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env perl
0002 # SPDX-License-Identifier: GPL-2.0-or-later
0003 use strict;
0004 
0005 # Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
0006 #
0007 
0008 my $prefix = "./";
0009 $prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
0010 
0011 my $conf = $prefix . "Documentation/conf.py";
0012 my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
0013 my $virtenv_prefix = "sphinx_";
0014 
0015 #
0016 # Static vars
0017 #
0018 
0019 my %missing;
0020 my $system_release;
0021 my $need = 0;
0022 my $optional = 0;
0023 my $need_symlink = 0;
0024 my $need_sphinx = 0;
0025 my $need_pip = 0;
0026 my $need_virtualenv = 0;
0027 my $rec_sphinx_upgrade = 0;
0028 my $verbose_warn_install = 1;
0029 my $install = "";
0030 my $virtenv_dir = "";
0031 my $python_cmd = "";
0032 my $activate_cmd;
0033 my $min_version;
0034 my $cur_version;
0035 my $rec_version = "1.7.9";      # PDF won't build here
0036 my $min_pdf_version = "2.4.4";  # Min version where pdf builds
0037 my $latest_avail_ver;
0038 
0039 #
0040 # Command line arguments
0041 #
0042 
0043 my $pdf = 1;
0044 my $virtualenv = 1;
0045 my $version_check = 0;
0046 
0047 #
0048 # List of required texlive packages on Fedora and OpenSuse
0049 #
0050 
0051 my %texlive = (
0052         'amsfonts.sty'       => 'texlive-amsfonts',
0053         'amsmath.sty'        => 'texlive-amsmath',
0054         'amssymb.sty'        => 'texlive-amsfonts',
0055         'amsthm.sty'         => 'texlive-amscls',
0056         'anyfontsize.sty'    => 'texlive-anyfontsize',
0057         'atbegshi.sty'       => 'texlive-oberdiek',
0058         'bm.sty'             => 'texlive-tools',
0059         'capt-of.sty'        => 'texlive-capt-of',
0060         'cmap.sty'           => 'texlive-cmap',
0061         'ecrm1000.tfm'       => 'texlive-ec',
0062         'eqparbox.sty'       => 'texlive-eqparbox',
0063         'eu1enc.def'         => 'texlive-euenc',
0064         'fancybox.sty'       => 'texlive-fancybox',
0065         'fancyvrb.sty'       => 'texlive-fancyvrb',
0066         'float.sty'          => 'texlive-float',
0067         'fncychap.sty'       => 'texlive-fncychap',
0068         'footnote.sty'       => 'texlive-mdwtools',
0069         'framed.sty'         => 'texlive-framed',
0070         'luatex85.sty'       => 'texlive-luatex85',
0071         'multirow.sty'       => 'texlive-multirow',
0072         'needspace.sty'      => 'texlive-needspace',
0073         'palatino.sty'       => 'texlive-psnfss',
0074         'parskip.sty'        => 'texlive-parskip',
0075         'polyglossia.sty'    => 'texlive-polyglossia',
0076         'tabulary.sty'       => 'texlive-tabulary',
0077         'threeparttable.sty' => 'texlive-threeparttable',
0078         'titlesec.sty'       => 'texlive-titlesec',
0079         'ucs.sty'            => 'texlive-ucs',
0080         'upquote.sty'        => 'texlive-upquote',
0081         'wrapfig.sty'        => 'texlive-wrapfig',
0082         'ctexhook.sty'       => 'texlive-ctex',
0083 );
0084 
0085 #
0086 # Subroutines that checks if a feature exists
0087 #
0088 
0089 sub check_missing(%)
0090 {
0091         my %map = %{$_[0]};
0092 
0093         foreach my $prog (sort keys %missing) {
0094                 my $is_optional = $missing{$prog};
0095 
0096                 # At least on some LTS distros like CentOS 7, texlive doesn't
0097                 # provide all packages we need. When such distros are
0098                 # detected, we have to disable PDF output.
0099                 #
0100                 # So, we need to ignore the packages that distros would
0101                 # need for LaTeX to work
0102                 if ($is_optional == 2 && !$pdf) {
0103                         $optional--;
0104                         next;
0105                 }
0106 
0107                 if ($verbose_warn_install) {
0108                         if ($is_optional) {
0109                                 print "Warning: better to also install \"$prog\".\n";
0110                         } else {
0111                                 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
0112                         }
0113                 }
0114                 if (defined($map{$prog})) {
0115                         $install .= " " . $map{$prog};
0116                 } else {
0117                         $install .= " " . $prog;
0118                 }
0119         }
0120 
0121         $install =~ s/^\s//;
0122 }
0123 
0124 sub add_package($$)
0125 {
0126         my $package = shift;
0127         my $is_optional = shift;
0128 
0129         $missing{$package} = $is_optional;
0130         if ($is_optional) {
0131                 $optional++;
0132         } else {
0133                 $need++;
0134         }
0135 }
0136 
0137 sub check_missing_file($$$)
0138 {
0139         my $files = shift;
0140         my $package = shift;
0141         my $is_optional = shift;
0142 
0143         for (@$files) {
0144                 return if(-e $_);
0145         }
0146 
0147         add_package($package, $is_optional);
0148 }
0149 
0150 sub findprog($)
0151 {
0152         foreach(split(/:/, $ENV{PATH})) {
0153                 return "$_/$_[0]" if(-x "$_/$_[0]");
0154         }
0155 }
0156 
0157 sub find_python_no_venv()
0158 {
0159         my $prog = shift;
0160 
0161         my $cur_dir = qx(pwd);
0162         $cur_dir =~ s/\s+$//;
0163 
0164         foreach my $dir (split(/:/, $ENV{PATH})) {
0165                 next if ($dir =~ m,($cur_dir)/sphinx,);
0166                 return "$dir/python3" if(-x "$dir/python3");
0167         }
0168         foreach my $dir (split(/:/, $ENV{PATH})) {
0169                 next if ($dir =~ m,($cur_dir)/sphinx,);
0170                 return "$dir/python" if(-x "$dir/python");
0171         }
0172         return "python";
0173 }
0174 
0175 sub check_program($$)
0176 {
0177         my $prog = shift;
0178         my $is_optional = shift;
0179 
0180         return $prog if findprog($prog);
0181 
0182         add_package($prog, $is_optional);
0183 }
0184 
0185 sub check_perl_module($$)
0186 {
0187         my $prog = shift;
0188         my $is_optional = shift;
0189 
0190         my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
0191         return if ($err == 0);
0192 
0193         add_package($prog, $is_optional);
0194 }
0195 
0196 sub check_python_module($$)
0197 {
0198         my $prog = shift;
0199         my $is_optional = shift;
0200 
0201         return if (!$python_cmd);
0202 
0203         my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
0204         return if ($err == 0);
0205 
0206         add_package($prog, $is_optional);
0207 }
0208 
0209 sub check_rpm_missing($$)
0210 {
0211         my @pkgs = @{$_[0]};
0212         my $is_optional = $_[1];
0213 
0214         foreach my $prog(@pkgs) {
0215                 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
0216                 add_package($prog, $is_optional) if ($err);
0217         }
0218 }
0219 
0220 sub check_pacman_missing($$)
0221 {
0222         my @pkgs = @{$_[0]};
0223         my $is_optional = $_[1];
0224 
0225         foreach my $prog(@pkgs) {
0226                 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
0227                 add_package($prog, $is_optional) if ($err);
0228         }
0229 }
0230 
0231 sub check_missing_tex($)
0232 {
0233         my $is_optional = shift;
0234         my $kpsewhich = findprog("kpsewhich");
0235 
0236         foreach my $prog(keys %texlive) {
0237                 my $package = $texlive{$prog};
0238                 if (!$kpsewhich) {
0239                         add_package($package, $is_optional);
0240                         next;
0241                 }
0242                 my $file = qx($kpsewhich $prog);
0243                 add_package($package, $is_optional) if ($file =~ /^\s*$/);
0244         }
0245 }
0246 
0247 sub get_sphinx_fname()
0248 {
0249         my $fname = "sphinx-build";
0250         return $fname if findprog($fname);
0251 
0252         $fname = "sphinx-build-3";
0253         if (findprog($fname)) {
0254                 $need_symlink = 1;
0255                 return $fname;
0256         }
0257 
0258         return "";
0259 }
0260 
0261 sub get_sphinx_version($)
0262 {
0263         my $cmd = shift;
0264         my $ver;
0265 
0266         open IN, "$cmd --version 2>&1 |";
0267         while (<IN>) {
0268                 if (m/^\s*sphinx-build\s+([\d\.]+)((\+\/[\da-f]+)|(b\d+))?$/) {
0269                         $ver=$1;
0270                         last;
0271                 }
0272                 # Sphinx 1.2.x uses a different format
0273                 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
0274                         $ver=$1;
0275                         last;
0276                 }
0277         }
0278         close IN;
0279         return $ver;
0280 }
0281 
0282 sub check_sphinx()
0283 {
0284         my $default_version;
0285 
0286         open IN, $conf or die "Can't open $conf";
0287         while (<IN>) {
0288                 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
0289                         $min_version=$1;
0290                         last;
0291                 }
0292         }
0293         close IN;
0294 
0295         die "Can't get needs_sphinx version from $conf" if (!$min_version);
0296 
0297         open IN, $requirement_file or die "Can't open $requirement_file";
0298         while (<IN>) {
0299                 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
0300                         $default_version=$1;
0301                         last;
0302                 }
0303         }
0304         close IN;
0305 
0306         die "Can't get default sphinx version from $requirement_file" if (!$default_version);
0307 
0308         $virtenv_dir = $virtenv_prefix . $default_version;
0309 
0310         my $sphinx = get_sphinx_fname();
0311         if ($sphinx eq "") {
0312                 $need_sphinx = 1;
0313                 return;
0314         }
0315 
0316         $cur_version = get_sphinx_version($sphinx);
0317         die ("$sphinx returned an error") if (!$cur_version);
0318 
0319         die "$sphinx didn't return its version" if (!$cur_version);
0320 
0321         if ($cur_version lt $min_version) {
0322                 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
0323                        $cur_version, $min_version, $default_version;
0324                 $need_sphinx = 1;
0325                 return;
0326         }
0327 
0328         return if ($cur_version lt $rec_version);
0329 
0330         # On version check mode, just assume Sphinx has all mandatory deps
0331         exit (0) if ($version_check);
0332 }
0333 
0334 #
0335 # Ancillary subroutines
0336 #
0337 
0338 sub catcheck($)
0339 {
0340   my $res = "";
0341   $res = qx(cat $_[0]) if (-r $_[0]);
0342   return $res;
0343 }
0344 
0345 sub which($)
0346 {
0347         my $file = shift;
0348         my @path = split ":", $ENV{PATH};
0349 
0350         foreach my $dir(@path) {
0351                 my $name = $dir.'/'.$file;
0352                 return $name if (-x $name );
0353         }
0354         return undef;
0355 }
0356 
0357 #
0358 # Subroutines that check distro-specific hints
0359 #
0360 
0361 sub give_debian_hints()
0362 {
0363         my %map = (
0364                 "python-sphinx"         => "python3-sphinx",
0365                 "sphinx_rtd_theme"      => "python3-sphinx-rtd-theme",
0366                 "ensurepip"             => "python3-venv",
0367                 "virtualenv"            => "virtualenv",
0368                 "dot"                   => "graphviz",
0369                 "convert"               => "imagemagick",
0370                 "Pod::Usage"            => "perl-modules",
0371                 "xelatex"               => "texlive-xetex",
0372                 "rsvg-convert"          => "librsvg2-bin",
0373         );
0374 
0375         if ($pdf) {
0376                 check_missing_file(["/usr/share/texlive/texmf-dist/tex/latex/ctex/ctexhook.sty"],
0377                                    "texlive-lang-chinese", 2);
0378 
0379                 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
0380                                    "fonts-dejavu", 2);
0381 
0382                 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
0383                                     "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
0384                                     "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
0385                                    "fonts-noto-cjk", 2);
0386         }
0387 
0388         check_program("dvipng", 2) if ($pdf);
0389         check_missing(\%map);
0390 
0391         return if (!$need && !$optional);
0392         printf("You should run:\n") if ($verbose_warn_install);
0393         printf("\n\tsudo apt-get install $install\n");
0394 }
0395 
0396 sub give_redhat_hints()
0397 {
0398         my %map = (
0399                 "python-sphinx"         => "python3-sphinx",
0400                 "sphinx_rtd_theme"      => "python3-sphinx_rtd_theme",
0401                 "virtualenv"            => "python3-virtualenv",
0402                 "dot"                   => "graphviz",
0403                 "convert"               => "ImageMagick",
0404                 "Pod::Usage"            => "perl-Pod-Usage",
0405                 "xelatex"               => "texlive-xetex-bin",
0406                 "rsvg-convert"          => "librsvg2-tools",
0407         );
0408 
0409         my @fedora26_opt_pkgs = (
0410                 "graphviz-gd",          # Fedora 26: needed for PDF support
0411         );
0412 
0413         my @fedora_tex_pkgs = (
0414                 "texlive-collection-fontsrecommended",
0415                 "texlive-collection-latex",
0416                 "texlive-xecjk",
0417                 "dejavu-sans-fonts",
0418                 "dejavu-serif-fonts",
0419                 "dejavu-sans-mono-fonts",
0420         );
0421 
0422         #
0423         # Checks valid for RHEL/CentOS version 7.x.
0424         #
0425         my $old = 0;
0426         my $rel;
0427         $rel = $1 if ($system_release =~ /release\s+(\d+)/);
0428 
0429         if (!($system_release =~ /Fedora/)) {
0430                 $map{"virtualenv"} = "python-virtualenv";
0431 
0432                 if ($rel && $rel < 8) {
0433                         $old = 1;
0434                         $pdf = 0;
0435 
0436                         printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
0437                         printf("If you want to build PDF, please read:\n");
0438                         printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
0439                 }
0440         } else {
0441                 if ($rel && $rel < 26) {
0442                         $old = 1;
0443                 }
0444         }
0445         if (!$rel) {
0446                 printf("Couldn't identify release number\n");
0447                 $old = 1;
0448                 $pdf = 0;
0449         }
0450 
0451         if ($pdf) {
0452                 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
0453                                    "google-noto-sans-cjk-ttc-fonts", 2);
0454         }
0455 
0456         check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
0457         check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
0458         check_missing_tex(2) if ($pdf);
0459         check_missing(\%map);
0460 
0461         return if (!$need && !$optional);
0462 
0463         if (!$old) {
0464                 # dnf, for Fedora 18+
0465                 printf("You should run:\n") if ($verbose_warn_install);
0466                 printf("\n\tsudo dnf install -y $install\n");
0467         } else {
0468                 # yum, for RHEL (and clones) or Fedora version < 18
0469                 printf("You should run:\n") if ($verbose_warn_install);
0470                 printf("\n\tsudo yum install -y $install\n");
0471         }
0472 }
0473 
0474 sub give_opensuse_hints()
0475 {
0476         my %map = (
0477                 "python-sphinx"         => "python3-sphinx",
0478                 "sphinx_rtd_theme"      => "python3-sphinx_rtd_theme",
0479                 "virtualenv"            => "python3-virtualenv",
0480                 "dot"                   => "graphviz",
0481                 "convert"               => "ImageMagick",
0482                 "Pod::Usage"            => "perl-Pod-Usage",
0483                 "xelatex"               => "texlive-xetex-bin",
0484         );
0485 
0486         # On Tumbleweed, this package is also named rsvg-convert
0487         $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
0488 
0489         my @suse_tex_pkgs = (
0490                 "texlive-babel-english",
0491                 "texlive-caption",
0492                 "texlive-colortbl",
0493                 "texlive-courier",
0494                 "texlive-dvips",
0495                 "texlive-helvetic",
0496                 "texlive-makeindex",
0497                 "texlive-metafont",
0498                 "texlive-metapost",
0499                 "texlive-palatino",
0500                 "texlive-preview",
0501                 "texlive-times",
0502                 "texlive-zapfchan",
0503                 "texlive-zapfding",
0504         );
0505 
0506         $map{"latexmk"} = "texlive-latexmk-bin";
0507 
0508         # FIXME: add support for installing CJK fonts
0509         #
0510         # I tried hard, but was unable to find a way to install
0511         # "Noto Sans CJK SC" on openSUSE
0512 
0513         check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
0514         check_missing_tex(2) if ($pdf);
0515         check_missing(\%map);
0516 
0517         return if (!$need && !$optional);
0518         printf("You should run:\n") if ($verbose_warn_install);
0519         printf("\n\tsudo zypper install --no-recommends $install\n");
0520 }
0521 
0522 sub give_mageia_hints()
0523 {
0524         my %map = (
0525                 "python-sphinx"         => "python3-sphinx",
0526                 "sphinx_rtd_theme"      => "python3-sphinx_rtd_theme",
0527                 "virtualenv"            => "python3-virtualenv",
0528                 "dot"                   => "graphviz",
0529                 "convert"               => "ImageMagick",
0530                 "Pod::Usage"            => "perl-Pod-Usage",
0531                 "xelatex"               => "texlive",
0532                 "rsvg-convert"          => "librsvg2",
0533         );
0534 
0535         my @tex_pkgs = (
0536                 "texlive-fontsextra",
0537         );
0538 
0539         $map{"latexmk"} = "texlive-collection-basic";
0540 
0541         my $packager_cmd;
0542         my $noto_sans;
0543         if ($system_release =~ /OpenMandriva/) {
0544                 $packager_cmd = "dnf install";
0545                 $noto_sans = "noto-sans-cjk-fonts";
0546                 @tex_pkgs = ( "texlive-collection-fontsextra" );
0547         } else {
0548                 $packager_cmd = "urpmi";
0549                 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
0550         }
0551 
0552 
0553         if ($pdf) {
0554                 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
0555                                     "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
0556                                    $noto_sans, 2);
0557         }
0558 
0559         check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
0560         check_missing(\%map);
0561 
0562         return if (!$need && !$optional);
0563         printf("You should run:\n") if ($verbose_warn_install);
0564         printf("\n\tsudo $packager_cmd $install\n");
0565 }
0566 
0567 sub give_arch_linux_hints()
0568 {
0569         my %map = (
0570                 "sphinx_rtd_theme"      => "python-sphinx_rtd_theme",
0571                 "virtualenv"            => "python-virtualenv",
0572                 "dot"                   => "graphviz",
0573                 "convert"               => "imagemagick",
0574                 "xelatex"               => "texlive-bin",
0575                 "latexmk"               => "texlive-core",
0576                 "rsvg-convert"          => "extra/librsvg",
0577         );
0578 
0579         my @archlinux_tex_pkgs = (
0580                 "texlive-core",
0581                 "texlive-latexextra",
0582                 "ttf-dejavu",
0583         );
0584         check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
0585 
0586         if ($pdf) {
0587                 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
0588                                    "noto-fonts-cjk", 2);
0589         }
0590 
0591         check_missing(\%map);
0592 
0593         return if (!$need && !$optional);
0594         printf("You should run:\n") if ($verbose_warn_install);
0595         printf("\n\tsudo pacman -S $install\n");
0596 }
0597 
0598 sub give_gentoo_hints()
0599 {
0600         my %map = (
0601                 "sphinx_rtd_theme"      => "dev-python/sphinx_rtd_theme",
0602                 "virtualenv"            => "dev-python/virtualenv",
0603                 "dot"                   => "media-gfx/graphviz",
0604                 "convert"               => "media-gfx/imagemagick",
0605                 "xelatex"               => "dev-texlive/texlive-xetex media-fonts/dejavu",
0606                 "rsvg-convert"          => "gnome-base/librsvg",
0607         );
0608 
0609         check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
0610                            "media-fonts/dejavu", 2) if ($pdf);
0611 
0612         if ($pdf) {
0613                 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
0614                                     "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
0615                                    "media-fonts/noto-cjk", 2);
0616         }
0617 
0618         check_missing(\%map);
0619 
0620         return if (!$need && !$optional);
0621 
0622         printf("You should run:\n") if ($verbose_warn_install);
0623         printf("\n");
0624 
0625         my $imagemagick = "media-gfx/imagemagick svg png";
0626         my $cairo = "media-gfx/graphviz cairo pdf";
0627         my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
0628         my $portage_cairo = "/etc/portage/package.use/graphviz";
0629 
0630         if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
0631                 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
0632         }
0633         if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
0634                 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
0635         }
0636 
0637         printf("\tsudo emerge --ask $install\n");
0638 
0639 }
0640 
0641 sub check_distros()
0642 {
0643         # Distro-specific hints
0644         if ($system_release =~ /Red Hat Enterprise Linux/) {
0645                 give_redhat_hints;
0646                 return;
0647         }
0648         if ($system_release =~ /CentOS/) {
0649                 give_redhat_hints;
0650                 return;
0651         }
0652         if ($system_release =~ /Scientific Linux/) {
0653                 give_redhat_hints;
0654                 return;
0655         }
0656         if ($system_release =~ /Oracle Linux Server/) {
0657                 give_redhat_hints;
0658                 return;
0659         }
0660         if ($system_release =~ /Fedora/) {
0661                 give_redhat_hints;
0662                 return;
0663         }
0664         if ($system_release =~ /Ubuntu/) {
0665                 give_debian_hints;
0666                 return;
0667         }
0668         if ($system_release =~ /Debian/) {
0669                 give_debian_hints;
0670                 return;
0671         }
0672         if ($system_release =~ /openSUSE/) {
0673                 give_opensuse_hints;
0674                 return;
0675         }
0676         if ($system_release =~ /Mageia/) {
0677                 give_mageia_hints;
0678                 return;
0679         }
0680         if ($system_release =~ /OpenMandriva/) {
0681                 give_mageia_hints;
0682                 return;
0683         }
0684         if ($system_release =~ /Arch Linux/) {
0685                 give_arch_linux_hints;
0686                 return;
0687         }
0688         if ($system_release =~ /Gentoo/) {
0689                 give_gentoo_hints;
0690                 return;
0691         }
0692 
0693         #
0694         # Fall-back to generic hint code for other distros
0695         # That's far from ideal, specially for LaTeX dependencies.
0696         #
0697         my %map = (
0698                 "sphinx-build" => "sphinx"
0699         );
0700         check_missing_tex(2) if ($pdf);
0701         check_missing(\%map);
0702         print "I don't know distro $system_release.\n";
0703         print "So, I can't provide you a hint with the install procedure.\n";
0704         print "There are likely missing dependencies.\n";
0705 }
0706 
0707 #
0708 # Common dependencies
0709 #
0710 
0711 sub deactivate_help()
0712 {
0713         printf "\n    If you want to exit the virtualenv, you can use:\n";
0714         printf "\tdeactivate\n";
0715 }
0716 
0717 sub get_virtenv()
0718 {
0719         my $ver;
0720         my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
0721         my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
0722 
0723         @activates = sort {$b cmp $a} @activates;
0724 
0725         foreach my $f (@activates) {
0726                 next if ($f lt $min_activate);
0727 
0728                 my $sphinx_cmd = $f;
0729                 $sphinx_cmd =~ s/activate/sphinx-build/;
0730                 next if (! -f $sphinx_cmd);
0731 
0732                 my $ver = get_sphinx_version($sphinx_cmd);
0733 
0734                 if (!$ver) {
0735                         $f =~ s#/bin/activate##;
0736                         print("Warning: virtual environment $f is not working.\nPython version upgrade? Remove it with:\n\n\trm -rf $f\n\n");
0737                 }
0738 
0739                 if ($need_sphinx && ($ver ge $min_version)) {
0740                         return ($f, $ver);
0741                 } elsif ($ver gt $cur_version) {
0742                         return ($f, $ver);
0743                 }
0744         }
0745         return ("", "");
0746 }
0747 
0748 sub recommend_sphinx_upgrade()
0749 {
0750         my $venv_ver;
0751 
0752         # Avoid running sphinx-builds from venv if $cur_version is good
0753         if ($cur_version && ($cur_version ge $rec_version)) {
0754                 $latest_avail_ver = $cur_version;
0755                 return;
0756         }
0757 
0758         # Get the highest version from sphinx_*/bin/sphinx-build and the
0759         # corresponding command to activate the venv/virtenv
0760         ($activate_cmd, $venv_ver) = get_virtenv();
0761 
0762         # Store the highest version from Sphinx existing virtualenvs
0763         if (($activate_cmd ne "") && ($venv_ver gt $cur_version)) {
0764                 $latest_avail_ver = $venv_ver;
0765         } else {
0766                 $latest_avail_ver = $cur_version if ($cur_version);
0767         }
0768 
0769         # As we don't know package version of Sphinx, and there's no
0770         # virtual environments, don't check if upgrades are needed
0771         if (!$virtualenv) {
0772                 return if (!$latest_avail_ver);
0773         }
0774 
0775         # Either there are already a virtual env or a new one should be created
0776         $need_pip = 1;
0777 
0778         return if (!$latest_avail_ver);
0779 
0780         # Return if the reason is due to an upgrade or not
0781         if ($latest_avail_ver lt $rec_version) {
0782                 $rec_sphinx_upgrade = 1;
0783         }
0784 
0785         return $latest_avail_ver;
0786 }
0787 
0788 #
0789 # The logic here is complex, as it have to deal with different versions:
0790 #       - minimal supported version;
0791 #       - minimal PDF version;
0792 #       - recommended version.
0793 # It also needs to work fine with both distro's package and venv/virtualenv
0794 sub recommend_sphinx_version($)
0795 {
0796         my $virtualenv_cmd = shift;
0797 
0798         # Version is OK. Nothing to do.
0799         if ($cur_version && ($cur_version ge $rec_version)) {
0800                 if ($cur_version lt $min_pdf_version) {
0801                         print "note: If you want pdf, you need at least Sphinx $min_pdf_version.\n";
0802                 }
0803                 return;
0804         };
0805 
0806         if (!$need_sphinx) {
0807                 # sphinx-build is present and its version is >= $min_version
0808 
0809                 #only recommend enabling a newer virtenv version if makes sense.
0810                 if ($latest_avail_ver gt $cur_version) {
0811                         printf "\nYou may also use the newer Sphinx version $latest_avail_ver with:\n";
0812                         printf "\tdeactivate\n"  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
0813                         printf "\t. $activate_cmd\n";
0814                         deactivate_help();
0815 
0816                         return;
0817                 }
0818                 return if ($latest_avail_ver ge $rec_version);
0819         }
0820 
0821         if (!$virtualenv) {
0822                 # No sphinx either via package or via virtenv. As we can't
0823                 # Compare the versions here, just return, recommending the
0824                 # user to install it from the package distro.
0825                 return if (!$latest_avail_ver);
0826 
0827                 # User doesn't want a virtenv recommendation, but he already
0828                 # installed one via virtenv with a newer version.
0829                 # So, print commands to enable it
0830                 if ($latest_avail_ver gt $cur_version) {
0831                         printf "\nYou may also use the Sphinx virtualenv version $latest_avail_ver with:\n";
0832                         printf "\tdeactivate\n"  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
0833                         printf "\t. $activate_cmd\n";
0834                         deactivate_help();
0835 
0836                         return;
0837                 }
0838                 print "\n";
0839         } else {
0840                 $need++ if ($need_sphinx);
0841         }
0842 
0843         # Suggest newer versions if current ones are too old
0844         if ($latest_avail_ver && $latest_avail_ver ge $min_version) {
0845                 # If there's a good enough version, ask the user to enable it
0846                 if ($latest_avail_ver ge $rec_version) {
0847                         printf "\nNeed to activate Sphinx (version $latest_avail_ver) on virtualenv with:\n";
0848                         printf "\t. $activate_cmd\n";
0849                         deactivate_help();
0850 
0851                         if ($latest_avail_ver lt $min_pdf_version) {
0852                                 print "note: If you want pdf, you need at least Sphinx $min_pdf_version.\n";
0853                         }
0854 
0855                         return;
0856                 }
0857 
0858                 # Version is above the minimal required one, but may be
0859                 # below the recommended one. So, print warnings/notes
0860 
0861                 if ($latest_avail_ver lt $rec_version) {
0862                         print "Warning: It is recommended at least Sphinx version $rec_version.\n";
0863                 }
0864         }
0865 
0866         # At this point, either it needs Sphinx or upgrade is recommended,
0867         # both via pip
0868 
0869         if ($rec_sphinx_upgrade) {
0870                 if (!$virtualenv) {
0871                         print "Instead of install/upgrade Python Sphinx pkg, you could use pip/pypi with:\n\n";
0872                 } else {
0873                         print "To upgrade Sphinx, use:\n\n";
0874                 }
0875         } else {
0876                 print "\nSphinx needs to be installed either:\n1) via pip/pypi with:\n\n";
0877         }
0878 
0879         $python_cmd = find_python_no_venv();
0880 
0881         printf "\t$virtualenv_cmd $virtenv_dir\n";
0882 
0883         printf "\t. $virtenv_dir/bin/activate\n";
0884         printf "\tpip install -r $requirement_file\n";
0885         deactivate_help();
0886 
0887         printf "\n2) As a package with:\n";
0888 
0889         my $old_need = $need;
0890         my $old_optional = $optional;
0891         %missing = ();
0892         $pdf = 0;
0893         $optional = 0;
0894         $install = "";
0895         $verbose_warn_install = 0;
0896 
0897         add_package("python-sphinx", 0);
0898         check_python_module("sphinx_rtd_theme", 1);
0899 
0900         check_distros();
0901 
0902         $need = $old_need;
0903         $optional = $old_optional;
0904 
0905         printf "\n    Please note that Sphinx >= 3.0 will currently produce false-positive\n";
0906         printf "   warning when the same name is used for more than one type (functions,\n";
0907         printf "   structs, enums,...). This is known Sphinx bug. For more details, see:\n";
0908         printf "\thttps://github.com/sphinx-doc/sphinx/pull/8313\n";
0909 }
0910 
0911 sub check_needs()
0912 {
0913         # Check if Sphinx is already accessible from current environment
0914         check_sphinx();
0915 
0916         if ($system_release) {
0917                 print "Detected OS: $system_release.\n";
0918         } else {
0919                 print "Unknown OS\n";
0920         }
0921         printf "Sphinx version: %s\n\n", $cur_version if ($cur_version);
0922 
0923         # Check python command line, trying first python3
0924         $python_cmd = findprog("python3");
0925         $python_cmd = check_program("python", 0) if (!$python_cmd);
0926 
0927         # Check the type of virtual env, depending on Python version
0928         if ($python_cmd) {
0929                 if ($virtualenv) {
0930                         my $tmp = qx($python_cmd --version 2>&1);
0931                         if ($tmp =~ m/(\d+\.)(\d+\.)/) {
0932                                 if ($1 < 3) {
0933                                         # Fail if it finds python2 (or worse)
0934                                         die "Python 3 is required to build the kernel docs\n";
0935                                 }
0936                                 if ($1 == 3 && $2 < 3) {
0937                                         # Need Python 3.3 or upper for venv
0938                                         $need_virtualenv = 1;
0939                                 }
0940                         } else {
0941                                 die "Warning: couldn't identify $python_cmd version!";
0942                         }
0943                 } else {
0944                         add_package("python-sphinx", 0);
0945                 }
0946         }
0947 
0948         my $venv_ver = recommend_sphinx_upgrade();
0949 
0950         my $virtualenv_cmd;
0951 
0952         if ($need_pip) {
0953                 # Set virtualenv command line, if python < 3.3
0954                 if ($need_virtualenv) {
0955                         $virtualenv_cmd = findprog("virtualenv-3");
0956                         $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
0957                         if (!$virtualenv_cmd) {
0958                                 check_program("virtualenv", 0);
0959                                 $virtualenv_cmd = "virtualenv";
0960                         }
0961                 } else {
0962                         $virtualenv_cmd = "$python_cmd -m venv";
0963                         check_python_module("ensurepip", 0);
0964                 }
0965         }
0966 
0967         # Check for needed programs/tools
0968         check_perl_module("Pod::Usage", 0);
0969         check_program("make", 0);
0970         check_program("gcc", 0);
0971         check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
0972         check_program("dot", 1);
0973         check_program("convert", 1);
0974 
0975         # Extra PDF files - should use 2 for is_optional
0976         check_program("xelatex", 2) if ($pdf);
0977         check_program("rsvg-convert", 2) if ($pdf);
0978         check_program("latexmk", 2) if ($pdf);
0979 
0980         # Do distro-specific checks and output distro-install commands
0981         check_distros();
0982 
0983         if (!$python_cmd) {
0984                 if ($need == 1) {
0985                         die "Can't build as $need mandatory dependency is missing";
0986                 } elsif ($need) {
0987                         die "Can't build as $need mandatory dependencies are missing";
0988                 }
0989         }
0990 
0991         # Check if sphinx-build is called sphinx-build-3
0992         if ($need_symlink) {
0993                 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
0994                        which("sphinx-build-3");
0995         }
0996 
0997         recommend_sphinx_version($virtualenv_cmd);
0998         printf "\n";
0999 
1000         print "All optional dependencies are met.\n" if (!$optional);
1001 
1002         if ($need == 1) {
1003                 die "Can't build as $need mandatory dependency is missing";
1004         } elsif ($need) {
1005                 die "Can't build as $need mandatory dependencies are missing";
1006         }
1007 
1008         print "Needed package dependencies are met.\n";
1009 }
1010 
1011 #
1012 # Main
1013 #
1014 
1015 while (@ARGV) {
1016         my $arg = shift(@ARGV);
1017 
1018         if ($arg eq "--no-virtualenv") {
1019                 $virtualenv = 0;
1020         } elsif ($arg eq "--no-pdf"){
1021                 $pdf = 0;
1022         } elsif ($arg eq "--version-check"){
1023                 $version_check = 1;
1024         } else {
1025                 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
1026                 print "Where:\n";
1027                 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
1028                 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
1029                 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
1030                 exit -1;
1031         }
1032 }
1033 
1034 #
1035 # Determine the system type. There's no standard unique way that would
1036 # work with all distros with a minimal package install. So, several
1037 # methods are used here.
1038 #
1039 # By default, it will use lsb_release function. If not available, it will
1040 # fail back to reading the known different places where the distro name
1041 # is stored
1042 #
1043 
1044 $system_release = qx(lsb_release -d) if which("lsb_release");
1045 $system_release =~ s/Description:\s*// if ($system_release);
1046 $system_release = catcheck("/etc/system-release") if !$system_release;
1047 $system_release = catcheck("/etc/redhat-release") if !$system_release;
1048 $system_release = catcheck("/etc/lsb-release") if !$system_release;
1049 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
1050 
1051 # This seems more common than LSB these days
1052 if (!$system_release) {
1053         my %os_var;
1054         if (open IN, "cat /etc/os-release|") {
1055                 while (<IN>) {
1056                         if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
1057                                 $os_var{$1}=$2;
1058                         }
1059                 }
1060                 $system_release = $os_var{"NAME"};
1061                 if (defined($os_var{"VERSION_ID"})) {
1062                         $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
1063                 } else {
1064                         $system_release .= " " . $os_var{"VERSION"};
1065                 }
1066         }
1067 }
1068 $system_release = catcheck("/etc/issue") if !$system_release;
1069 $system_release =~ s/\s+$//;
1070 
1071 check_needs;