2 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
9 $prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
11 my $conf = $prefix . "Documentation/conf.py";
12 my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
13 my $virtenv_prefix = "sphinx_";
26 my $need_virtualenv = 0;
27 my $rec_sphinx_upgrade = 0;
34 # Command line arguments
39 my $version_check = 0;
42 # List of required texlive packages on Fedora and OpenSuse
46 'amsfonts.sty' => 'texlive-amsfonts',
47 'amsmath.sty' => 'texlive-amsmath',
48 'amssymb.sty' => 'texlive-amsfonts',
49 'amsthm.sty' => 'texlive-amscls',
50 'anyfontsize.sty' => 'texlive-anyfontsize',
51 'atbegshi.sty' => 'texlive-oberdiek',
52 'bm.sty' => 'texlive-tools',
53 'capt-of.sty' => 'texlive-capt-of',
54 'cmap.sty' => 'texlive-cmap',
55 'ecrm1000.tfm' => 'texlive-ec',
56 'eqparbox.sty' => 'texlive-eqparbox',
57 'eu1enc.def' => 'texlive-euenc',
58 'fancybox.sty' => 'texlive-fancybox',
59 'fancyvrb.sty' => 'texlive-fancyvrb',
60 'float.sty' => 'texlive-float',
61 'fncychap.sty' => 'texlive-fncychap',
62 'footnote.sty' => 'texlive-mdwtools',
63 'framed.sty' => 'texlive-framed',
64 'luatex85.sty' => 'texlive-luatex85',
65 'multirow.sty' => 'texlive-multirow',
66 'needspace.sty' => 'texlive-needspace',
67 'palatino.sty' => 'texlive-psnfss',
68 'parskip.sty' => 'texlive-parskip',
69 'polyglossia.sty' => 'texlive-polyglossia',
70 'tabulary.sty' => 'texlive-tabulary',
71 'threeparttable.sty' => 'texlive-threeparttable',
72 'titlesec.sty' => 'texlive-titlesec',
73 'ucs.sty' => 'texlive-ucs',
74 'upquote.sty' => 'texlive-upquote',
75 'wrapfig.sty' => 'texlive-wrapfig',
79 # Subroutines that checks if a feature exists
86 foreach my $prog (sort keys %missing) {
87 my $is_optional = $missing{$prog};
89 # At least on some LTS distros like CentOS 7, texlive doesn't
90 # provide all packages we need. When such distros are
91 # detected, we have to disable PDF output.
93 # So, we need to ignore the packages that distros would
94 # need for LaTeX to work
95 if ($is_optional == 2 && !$pdf) {
101 print "Warning: better to also install \"$prog\".\n";
103 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
105 if (defined($map{$prog})) {
106 $install .= " " . $map{$prog};
108 $install .= " " . $prog;
118 my $is_optional = shift;
120 $missing{$package} = $is_optional;
128 sub check_missing_file($$$)
132 my $is_optional = shift;
138 add_package($package, $is_optional);
143 foreach(split(/:/, $ENV{PATH})) {
144 return "$_/$_[0]" if(-x "$_/$_[0]");
148 sub check_program($$)
151 my $is_optional = shift;
153 return $prog if findprog($prog);
155 add_package($prog, $is_optional);
158 sub check_perl_module($$)
161 my $is_optional = shift;
163 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
164 return if ($err == 0);
166 add_package($prog, $is_optional);
169 sub check_python_module($$)
172 my $is_optional = shift;
174 return if (!$python_cmd);
176 my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
177 return if ($err == 0);
179 add_package($prog, $is_optional);
182 sub check_rpm_missing($$)
185 my $is_optional = $_[1];
187 foreach my $prog(@pkgs) {
188 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
189 add_package($prog, $is_optional) if ($err);
193 sub check_pacman_missing($$)
196 my $is_optional = $_[1];
198 foreach my $prog(@pkgs) {
199 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
200 add_package($prog, $is_optional) if ($err);
204 sub check_missing_tex($)
206 my $is_optional = shift;
207 my $kpsewhich = findprog("kpsewhich");
209 foreach my $prog(keys %texlive) {
210 my $package = $texlive{$prog};
212 add_package($package, $is_optional);
215 my $file = qx($kpsewhich $prog);
216 add_package($package, $is_optional) if ($file =~ /^\s*$/);
220 sub get_sphinx_fname()
222 my $fname = "sphinx-build";
223 return $fname if findprog($fname);
225 $fname = "sphinx-build-3";
226 if (findprog($fname)) {
239 open IN, $conf or die "Can't open $conf";
241 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
248 die "Can't get needs_sphinx version from $conf" if (!$min_version);
250 open IN, $requirement_file or die "Can't open $requirement_file";
252 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
259 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
261 $virtenv_dir = $virtenv_prefix . $rec_version;
263 my $sphinx = get_sphinx_fname();
269 open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
271 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
275 # Sphinx 1.2.x uses a different format
276 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
283 die "$sphinx didn't return its version" if (!$cur_version);
285 if ($cur_version lt $min_version) {
286 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
287 $cur_version, $min_version, $rec_version;;
292 if ($cur_version lt $rec_version) {
293 printf "Sphinx version %s\n", $cur_version;
294 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
295 $rec_sphinx_upgrade = 1;
299 # On version check mode, just assume Sphinx has all mandatory deps
300 exit (0) if ($version_check);
304 # Ancillary subroutines
310 $res = qx(cat $_[0]) if (-r $_[0]);
317 my @path = split ":", $ENV{PATH};
319 foreach my $dir(@path) {
320 my $name = $dir.'/'.$file;
321 return $name if (-x $name );
327 # Subroutines that check distro-specific hints
330 sub give_debian_hints()
333 "python-sphinx" => "python3-sphinx",
334 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
335 "ensurepip" => "python3-venv",
336 "virtualenv" => "virtualenv",
338 "convert" => "imagemagick",
339 "Pod::Usage" => "perl-modules",
340 "xelatex" => "texlive-xetex",
341 "rsvg-convert" => "librsvg2-bin",
345 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
348 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
349 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
350 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
351 "fonts-noto-cjk", 2);
354 check_program("dvipng", 2) if ($pdf);
355 check_missing(\%map);
357 return if (!$need && !$optional);
358 printf("You should run:\n\n\tsudo apt-get install $install\n");
361 sub give_redhat_hints()
364 "python-sphinx" => "python3-sphinx",
365 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
366 "virtualenv" => "python3-virtualenv",
368 "convert" => "ImageMagick",
369 "Pod::Usage" => "perl-Pod-Usage",
370 "xelatex" => "texlive-xetex-bin",
371 "rsvg-convert" => "librsvg2-tools",
374 my @fedora26_opt_pkgs = (
375 "graphviz-gd", # Fedora 26: needed for PDF support
378 my @fedora_tex_pkgs = (
379 "texlive-collection-fontsrecommended",
380 "texlive-collection-latex",
383 "dejavu-serif-fonts",
384 "dejavu-sans-mono-fonts",
388 # Checks valid for RHEL/CentOS version 7.x.
392 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
394 if (!($system_release =~ /Fedora/)) {
395 $map{"virtualenv"} = "python-virtualenv";
397 if ($rel && $rel < 8) {
401 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
402 printf("If you want to build PDF, please read:\n");
403 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
406 if ($rel && $rel < 26) {
411 printf("Couldn't identify release number\n");
417 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
418 "google-noto-sans-cjk-ttc-fonts", 2);
421 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
422 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
423 check_missing_tex(2) if ($pdf);
424 check_missing(\%map);
426 return if (!$need && !$optional);
429 # dnf, for Fedora 18+
430 printf("You should run:\n\n\tsudo dnf install -y $install\n");
432 # yum, for RHEL (and clones) or Fedora version < 18
433 printf("You should run:\n\n\tsudo yum install -y $install\n");
437 sub give_opensuse_hints()
440 "python-sphinx" => "python3-sphinx",
441 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
442 "virtualenv" => "python3-virtualenv",
444 "convert" => "ImageMagick",
445 "Pod::Usage" => "perl-Pod-Usage",
446 "xelatex" => "texlive-xetex-bin",
449 # On Tumbleweed, this package is also named rsvg-convert
450 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
452 my @suse_tex_pkgs = (
453 "texlive-babel-english",
469 $map{"latexmk"} = "texlive-latexmk-bin";
471 # FIXME: add support for installing CJK fonts
473 # I tried hard, but was unable to find a way to install
474 # "Noto Sans CJK SC" on openSUSE
476 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
477 check_missing_tex(2) if ($pdf);
478 check_missing(\%map);
480 return if (!$need && !$optional);
481 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
484 sub give_mageia_hints()
487 "python-sphinx" => "python3-sphinx",
488 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
489 "virtualenv" => "python3-virtualenv",
491 "convert" => "ImageMagick",
492 "Pod::Usage" => "perl-Pod-Usage",
493 "xelatex" => "texlive",
494 "rsvg-convert" => "librsvg2",
498 "texlive-fontsextra",
501 $map{"latexmk"} = "texlive-collection-basic";
505 if ($system_release =~ /OpenMandriva/) {
506 $packager_cmd = "dnf install";
507 $noto_sans = "noto-sans-cjk-fonts";
508 @tex_pkgs = ( "texlive-collection-fontsextra" );
510 $packager_cmd = "urpmi";
511 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
516 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
517 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
521 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
522 check_missing(\%map);
524 return if (!$need && !$optional);
525 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
528 sub give_arch_linux_hints()
531 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
532 "virtualenv" => "python-virtualenv",
534 "convert" => "imagemagick",
535 "xelatex" => "texlive-bin",
536 "latexmk" => "texlive-core",
537 "rsvg-convert" => "extra/librsvg",
540 my @archlinux_tex_pkgs = (
542 "texlive-latexextra",
545 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
548 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
549 "noto-fonts-cjk", 2);
552 check_missing(\%map);
554 return if (!$need && !$optional);
555 printf("You should run:\n\n\tsudo pacman -S $install\n");
558 sub give_gentoo_hints()
561 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
562 "virtualenv" => "dev-python/virtualenv",
563 "dot" => "media-gfx/graphviz",
564 "convert" => "media-gfx/imagemagick",
565 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
566 "rsvg-convert" => "gnome-base/librsvg",
569 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
570 "media-fonts/dejavu", 2) if ($pdf);
573 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
574 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
575 "media-fonts/noto-cjk", 2);
578 check_missing(\%map);
580 return if (!$need && !$optional);
582 printf("You should run:\n\n");
584 my $imagemagick = "media-gfx/imagemagick svg png";
585 my $cairo = "media-gfx/graphviz cairo pdf";
586 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
587 my $portage_cairo = "/etc/portage/package.use/graphviz";
589 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
590 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
592 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
593 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
596 printf("\tsudo emerge --ask $install\n");
602 # Distro-specific hints
603 if ($system_release =~ /Red Hat Enterprise Linux/) {
607 if ($system_release =~ /CentOS/) {
611 if ($system_release =~ /Scientific Linux/) {
615 if ($system_release =~ /Oracle Linux Server/) {
619 if ($system_release =~ /Fedora/) {
623 if ($system_release =~ /Ubuntu/) {
627 if ($system_release =~ /Debian/) {
631 if ($system_release =~ /openSUSE/) {
635 if ($system_release =~ /Mageia/) {
639 if ($system_release =~ /OpenMandriva/) {
643 if ($system_release =~ /Arch Linux/) {
644 give_arch_linux_hints;
647 if ($system_release =~ /Gentoo/) {
653 # Fall-back to generic hint code for other distros
654 # That's far from ideal, specially for LaTeX dependencies.
657 "sphinx-build" => "sphinx"
659 check_missing_tex(2) if ($pdf);
660 check_missing(\%map);
661 print "I don't know distro $system_release.\n";
662 print "So, I can't provide you a hint with the install procedure.\n";
663 print "There are likely missing dependencies.\n";
667 # Common dependencies
670 sub deactivate_help()
672 printf "\nIf you want to exit the virtualenv, you can use:\n";
673 printf "\tdeactivate\n";
678 # Check if Sphinx is already accessible from current environment
681 if ($system_release) {
682 print "Detected OS: $system_release.\n\n";
684 print "Unknown OS\n\n";
687 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
689 # Check python command line, trying first python3
690 $python_cmd = findprog("python3");
691 $python_cmd = check_program("python", 0) if (!$python_cmd);
693 # Check the type of virtual env, depending on Python version
696 my $tmp = qx($python_cmd --version 2>&1);
697 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
698 if ($1 >= 3 && $2 >= 3) {
699 $need_venv = 1; # python 3.3 or upper
701 $need_virtualenv = 1;
704 # Complain if it finds python2 (or worse)
705 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
708 die "Warning: couldn't identify $python_cmd version!";
711 add_package("python-sphinx", 0);
715 # Set virtualenv command line, if python < 3.3
717 if ($need_virtualenv) {
718 $virtualenv_cmd = findprog("virtualenv-3");
719 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
720 if (!$virtualenv_cmd) {
721 check_program("virtualenv", 0);
722 $virtualenv_cmd = "virtualenv";
726 # Check for needed programs/tools
727 check_perl_module("Pod::Usage", 0);
728 check_program("make", 0);
729 check_program("gcc", 0);
730 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
731 check_program("dot", 1);
732 check_program("convert", 1);
734 # Extra PDF files - should use 2 for is_optional
735 check_program("xelatex", 2) if ($pdf);
736 check_program("rsvg-convert", 2) if ($pdf);
737 check_program("latexmk", 2) if ($pdf);
739 if ($need_sphinx || $rec_sphinx_upgrade) {
740 check_python_module("ensurepip", 0) if ($need_venv);
743 # Do distro-specific checks and output distro-install commands
748 die "Can't build as $need mandatory dependency is missing";
750 die "Can't build as $need mandatory dependencies are missing";
754 # Check if sphinx-build is called sphinx-build-3
756 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
757 which("sphinx-build-3");
760 # NOTE: if the system has a too old Sphinx version installed,
761 # it will recommend installing a newer version using virtualenv
763 if ($need_sphinx || $rec_sphinx_upgrade) {
764 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
765 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
767 @activates = sort {$b cmp $a} @activates;
769 if ($need_sphinx && scalar @activates > 0 && $activates[0] ge $min_activate) {
770 printf "\nNeed to activate a compatible Sphinx version on virtualenv with:\n";
771 printf "\t. $activates[0]\n";
775 my $rec_activate = "$virtenv_dir/bin/activate";
778 printf "\t$python_cmd -m venv $virtenv_dir\n";
780 printf "\t$virtualenv_cmd $virtenv_dir\n";
782 printf "\t. $rec_activate\n";
783 printf "\tpip install -r $requirement_file\n";
786 $need++ if (!$rec_sphinx_upgrade);
791 print "All optional dependencies are met.\n" if (!$optional);
794 die "Can't build as $need mandatory dependency is missing";
796 die "Can't build as $need mandatory dependencies are missing";
799 print "Needed package dependencies are met.\n";
807 my $arg = shift(@ARGV);
809 if ($arg eq "--no-virtualenv") {
811 } elsif ($arg eq "--no-pdf"){
813 } elsif ($arg eq "--version-check"){
816 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
818 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
819 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
820 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
826 # Determine the system type. There's no standard unique way that would
827 # work with all distros with a minimal package install. So, several
828 # methods are used here.
830 # By default, it will use lsb_release function. If not available, it will
831 # fail back to reading the known different places where the distro name
835 $system_release = qx(lsb_release -d) if which("lsb_release");
836 $system_release =~ s/Description:\s*// if ($system_release);
837 $system_release = catcheck("/etc/system-release") if !$system_release;
838 $system_release = catcheck("/etc/redhat-release") if !$system_release;
839 $system_release = catcheck("/etc/lsb-release") if !$system_release;
840 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
842 # This seems more common than LSB these days
843 if (!$system_release) {
845 if (open IN, "cat /etc/os-release|") {
847 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
851 $system_release = $os_var{"NAME"};
852 if (defined($os_var{"VERSION_ID"})) {
853 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
855 $system_release .= " " . $os_var{"VERSION"};
859 $system_release = catcheck("/etc/issue") if !$system_release;
860 $system_release =~ s/\s+$//;