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;
32 my $rec_version = "1.7.9"; # PDF won't build here
33 my $min_pdf_version = "2.4.4"; # Min version where pdf builds
36 # Command line arguments
41 my $version_check = 0;
44 # List of required texlive packages on Fedora and OpenSuse
48 'amsfonts.sty' => 'texlive-amsfonts',
49 'amsmath.sty' => 'texlive-amsmath',
50 'amssymb.sty' => 'texlive-amsfonts',
51 'amsthm.sty' => 'texlive-amscls',
52 'anyfontsize.sty' => 'texlive-anyfontsize',
53 'atbegshi.sty' => 'texlive-oberdiek',
54 'bm.sty' => 'texlive-tools',
55 'capt-of.sty' => 'texlive-capt-of',
56 'cmap.sty' => 'texlive-cmap',
57 'ecrm1000.tfm' => 'texlive-ec',
58 'eqparbox.sty' => 'texlive-eqparbox',
59 'eu1enc.def' => 'texlive-euenc',
60 'fancybox.sty' => 'texlive-fancybox',
61 'fancyvrb.sty' => 'texlive-fancyvrb',
62 'float.sty' => 'texlive-float',
63 'fncychap.sty' => 'texlive-fncychap',
64 'footnote.sty' => 'texlive-mdwtools',
65 'framed.sty' => 'texlive-framed',
66 'luatex85.sty' => 'texlive-luatex85',
67 'multirow.sty' => 'texlive-multirow',
68 'needspace.sty' => 'texlive-needspace',
69 'palatino.sty' => 'texlive-psnfss',
70 'parskip.sty' => 'texlive-parskip',
71 'polyglossia.sty' => 'texlive-polyglossia',
72 'tabulary.sty' => 'texlive-tabulary',
73 'threeparttable.sty' => 'texlive-threeparttable',
74 'titlesec.sty' => 'texlive-titlesec',
75 'ucs.sty' => 'texlive-ucs',
76 'upquote.sty' => 'texlive-upquote',
77 'wrapfig.sty' => 'texlive-wrapfig',
81 # Subroutines that checks if a feature exists
88 foreach my $prog (sort keys %missing) {
89 my $is_optional = $missing{$prog};
91 # At least on some LTS distros like CentOS 7, texlive doesn't
92 # provide all packages we need. When such distros are
93 # detected, we have to disable PDF output.
95 # So, we need to ignore the packages that distros would
96 # need for LaTeX to work
97 if ($is_optional == 2 && !$pdf) {
103 print "Warning: better to also install \"$prog\".\n";
105 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
107 if (defined($map{$prog})) {
108 $install .= " " . $map{$prog};
110 $install .= " " . $prog;
120 my $is_optional = shift;
122 $missing{$package} = $is_optional;
130 sub check_missing_file($$$)
134 my $is_optional = shift;
140 add_package($package, $is_optional);
145 foreach(split(/:/, $ENV{PATH})) {
146 return "$_/$_[0]" if(-x "$_/$_[0]");
150 sub check_program($$)
153 my $is_optional = shift;
155 return $prog if findprog($prog);
157 add_package($prog, $is_optional);
160 sub check_perl_module($$)
163 my $is_optional = shift;
165 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
166 return if ($err == 0);
168 add_package($prog, $is_optional);
171 sub check_python_module($$)
174 my $is_optional = shift;
176 return if (!$python_cmd);
178 my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
179 return if ($err == 0);
181 add_package($prog, $is_optional);
184 sub check_rpm_missing($$)
187 my $is_optional = $_[1];
189 foreach my $prog(@pkgs) {
190 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
191 add_package($prog, $is_optional) if ($err);
195 sub check_pacman_missing($$)
198 my $is_optional = $_[1];
200 foreach my $prog(@pkgs) {
201 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
202 add_package($prog, $is_optional) if ($err);
206 sub check_missing_tex($)
208 my $is_optional = shift;
209 my $kpsewhich = findprog("kpsewhich");
211 foreach my $prog(keys %texlive) {
212 my $package = $texlive{$prog};
214 add_package($package, $is_optional);
217 my $file = qx($kpsewhich $prog);
218 add_package($package, $is_optional) if ($file =~ /^\s*$/);
222 sub get_sphinx_fname()
224 my $fname = "sphinx-build";
225 return $fname if findprog($fname);
227 $fname = "sphinx-build-3";
228 if (findprog($fname)) {
236 sub get_sphinx_version($)
241 open IN, "$cmd --version 2>&1 |";
243 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
247 # Sphinx 1.2.x uses a different format
248 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
262 open IN, $conf or die "Can't open $conf";
264 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
271 die "Can't get needs_sphinx version from $conf" if (!$min_version);
273 open IN, $requirement_file or die "Can't open $requirement_file";
275 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
282 die "Can't get default sphinx version from $requirement_file" if (!$default_version);
284 $virtenv_dir = $virtenv_prefix . $default_version;
286 my $sphinx = get_sphinx_fname();
292 $cur_version = get_sphinx_version($sphinx);
293 die ("$sphinx returned an error") if (!$cur_version);
295 die "$sphinx didn't return its version" if (!$cur_version);
297 if ($cur_version lt $min_version) {
298 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
299 $cur_version, $min_version, $default_version;
304 if ($cur_version lt $rec_version) {
305 printf "Sphinx version %s\n", $cur_version;
306 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
307 print " If you want pdf, you need at least $min_pdf_version.\n";
308 $rec_sphinx_upgrade = 1;
311 if ($cur_version lt $min_pdf_version) {
312 printf "Sphinx version %s\n", $cur_version;
313 print "Note: It is recommended at least Sphinx version $min_pdf_version if you need PDF support.\n";
314 $rec_sphinx_upgrade = 1;
318 # On version check mode, just assume Sphinx has all mandatory deps
319 exit (0) if ($version_check);
323 # Ancillary subroutines
329 $res = qx(cat $_[0]) if (-r $_[0]);
336 my @path = split ":", $ENV{PATH};
338 foreach my $dir(@path) {
339 my $name = $dir.'/'.$file;
340 return $name if (-x $name );
346 # Subroutines that check distro-specific hints
349 sub give_debian_hints()
352 "python-sphinx" => "python3-sphinx",
353 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
354 "ensurepip" => "python3-venv",
355 "virtualenv" => "virtualenv",
357 "convert" => "imagemagick",
358 "Pod::Usage" => "perl-modules",
359 "xelatex" => "texlive-xetex",
360 "rsvg-convert" => "librsvg2-bin",
364 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
367 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
368 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
369 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
370 "fonts-noto-cjk", 2);
373 check_program("dvipng", 2) if ($pdf);
374 check_missing(\%map);
376 return if (!$need && !$optional);
377 printf("You should run:\n\n\tsudo apt-get install $install\n");
380 sub give_redhat_hints()
383 "python-sphinx" => "python3-sphinx",
384 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
385 "virtualenv" => "python3-virtualenv",
387 "convert" => "ImageMagick",
388 "Pod::Usage" => "perl-Pod-Usage",
389 "xelatex" => "texlive-xetex-bin",
390 "rsvg-convert" => "librsvg2-tools",
393 my @fedora26_opt_pkgs = (
394 "graphviz-gd", # Fedora 26: needed for PDF support
397 my @fedora_tex_pkgs = (
398 "texlive-collection-fontsrecommended",
399 "texlive-collection-latex",
402 "dejavu-serif-fonts",
403 "dejavu-sans-mono-fonts",
407 # Checks valid for RHEL/CentOS version 7.x.
411 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
413 if (!($system_release =~ /Fedora/)) {
414 $map{"virtualenv"} = "python-virtualenv";
416 if ($rel && $rel < 8) {
420 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
421 printf("If you want to build PDF, please read:\n");
422 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
425 if ($rel && $rel < 26) {
430 printf("Couldn't identify release number\n");
436 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
437 "google-noto-sans-cjk-ttc-fonts", 2);
440 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
441 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
442 check_missing_tex(2) if ($pdf);
443 check_missing(\%map);
445 return if (!$need && !$optional);
448 # dnf, for Fedora 18+
449 printf("You should run:\n\n\tsudo dnf install -y $install\n");
451 # yum, for RHEL (and clones) or Fedora version < 18
452 printf("You should run:\n\n\tsudo yum install -y $install\n");
456 sub give_opensuse_hints()
459 "python-sphinx" => "python3-sphinx",
460 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
461 "virtualenv" => "python3-virtualenv",
463 "convert" => "ImageMagick",
464 "Pod::Usage" => "perl-Pod-Usage",
465 "xelatex" => "texlive-xetex-bin",
468 # On Tumbleweed, this package is also named rsvg-convert
469 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
471 my @suse_tex_pkgs = (
472 "texlive-babel-english",
488 $map{"latexmk"} = "texlive-latexmk-bin";
490 # FIXME: add support for installing CJK fonts
492 # I tried hard, but was unable to find a way to install
493 # "Noto Sans CJK SC" on openSUSE
495 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
496 check_missing_tex(2) if ($pdf);
497 check_missing(\%map);
499 return if (!$need && !$optional);
500 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
503 sub give_mageia_hints()
506 "python-sphinx" => "python3-sphinx",
507 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
508 "virtualenv" => "python3-virtualenv",
510 "convert" => "ImageMagick",
511 "Pod::Usage" => "perl-Pod-Usage",
512 "xelatex" => "texlive",
513 "rsvg-convert" => "librsvg2",
517 "texlive-fontsextra",
520 $map{"latexmk"} = "texlive-collection-basic";
524 if ($system_release =~ /OpenMandriva/) {
525 $packager_cmd = "dnf install";
526 $noto_sans = "noto-sans-cjk-fonts";
527 @tex_pkgs = ( "texlive-collection-fontsextra" );
529 $packager_cmd = "urpmi";
530 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
535 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
536 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
540 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
541 check_missing(\%map);
543 return if (!$need && !$optional);
544 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
547 sub give_arch_linux_hints()
550 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
551 "virtualenv" => "python-virtualenv",
553 "convert" => "imagemagick",
554 "xelatex" => "texlive-bin",
555 "latexmk" => "texlive-core",
556 "rsvg-convert" => "extra/librsvg",
559 my @archlinux_tex_pkgs = (
561 "texlive-latexextra",
564 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
567 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
568 "noto-fonts-cjk", 2);
571 check_missing(\%map);
573 return if (!$need && !$optional);
574 printf("You should run:\n\n\tsudo pacman -S $install\n");
577 sub give_gentoo_hints()
580 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
581 "virtualenv" => "dev-python/virtualenv",
582 "dot" => "media-gfx/graphviz",
583 "convert" => "media-gfx/imagemagick",
584 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
585 "rsvg-convert" => "gnome-base/librsvg",
588 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
589 "media-fonts/dejavu", 2) if ($pdf);
592 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
593 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
594 "media-fonts/noto-cjk", 2);
597 check_missing(\%map);
599 return if (!$need && !$optional);
601 printf("You should run:\n\n");
603 my $imagemagick = "media-gfx/imagemagick svg png";
604 my $cairo = "media-gfx/graphviz cairo pdf";
605 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
606 my $portage_cairo = "/etc/portage/package.use/graphviz";
608 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
609 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
611 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
612 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
615 printf("\tsudo emerge --ask $install\n");
621 # Distro-specific hints
622 if ($system_release =~ /Red Hat Enterprise Linux/) {
626 if ($system_release =~ /CentOS/) {
630 if ($system_release =~ /Scientific Linux/) {
634 if ($system_release =~ /Oracle Linux Server/) {
638 if ($system_release =~ /Fedora/) {
642 if ($system_release =~ /Ubuntu/) {
646 if ($system_release =~ /Debian/) {
650 if ($system_release =~ /openSUSE/) {
654 if ($system_release =~ /Mageia/) {
658 if ($system_release =~ /OpenMandriva/) {
662 if ($system_release =~ /Arch Linux/) {
663 give_arch_linux_hints;
666 if ($system_release =~ /Gentoo/) {
672 # Fall-back to generic hint code for other distros
673 # That's far from ideal, specially for LaTeX dependencies.
676 "sphinx-build" => "sphinx"
678 check_missing_tex(2) if ($pdf);
679 check_missing(\%map);
680 print "I don't know distro $system_release.\n";
681 print "So, I can't provide you a hint with the install procedure.\n";
682 print "There are likely missing dependencies.\n";
686 # Common dependencies
689 sub deactivate_help()
691 printf "\nIf you want to exit the virtualenv, you can use:\n";
692 printf "\tdeactivate\n";
697 # Check if Sphinx is already accessible from current environment
700 if ($system_release) {
701 print "Detected OS: $system_release.\n\n";
703 print "Unknown OS\n\n";
706 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
708 # Check python command line, trying first python3
709 $python_cmd = findprog("python3");
710 $python_cmd = check_program("python", 0) if (!$python_cmd);
712 # Check the type of virtual env, depending on Python version
715 my $tmp = qx($python_cmd --version 2>&1);
716 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
717 if ($1 >= 3 && $2 >= 3) {
718 $need_venv = 1; # python 3.3 or upper
720 $need_virtualenv = 1;
723 # Complain if it finds python2 (or worse)
724 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
727 die "Warning: couldn't identify $python_cmd version!";
730 add_package("python-sphinx", 0);
734 # Set virtualenv command line, if python < 3.3
736 if ($need_virtualenv) {
737 $virtualenv_cmd = findprog("virtualenv-3");
738 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
739 if (!$virtualenv_cmd) {
740 check_program("virtualenv", 0);
741 $virtualenv_cmd = "virtualenv";
745 # Check for needed programs/tools
746 check_perl_module("Pod::Usage", 0);
747 check_program("make", 0);
748 check_program("gcc", 0);
749 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
750 check_program("dot", 1);
751 check_program("convert", 1);
753 # Extra PDF files - should use 2 for is_optional
754 check_program("xelatex", 2) if ($pdf);
755 check_program("rsvg-convert", 2) if ($pdf);
756 check_program("latexmk", 2) if ($pdf);
758 if ($need_sphinx || $rec_sphinx_upgrade) {
759 check_python_module("ensurepip", 0) if ($need_venv);
762 # Do distro-specific checks and output distro-install commands
767 die "Can't build as $need mandatory dependency is missing";
769 die "Can't build as $need mandatory dependencies are missing";
773 # Check if sphinx-build is called sphinx-build-3
775 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
776 which("sphinx-build-3");
779 # NOTE: if the system has a too old Sphinx version installed,
780 # it will recommend installing a newer version using virtualenv
782 if ($need_sphinx || $rec_sphinx_upgrade) {
783 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
784 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
786 @activates = sort {$b cmp $a} @activates;
787 my ($activate, $ver);
788 foreach my $f (@activates) {
790 next if ($activate lt $min_activate);
792 my $sphinx_cmd = $activate;
793 $sphinx_cmd =~ s/activate/sphinx-build/;
794 next if (! -f $sphinx_cmd);
796 $ver = get_sphinx_version($sphinx_cmd);
797 last if ($ver ge $min_version);
799 if ($need_sphinx && ($activate ne "")) {
800 printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
801 printf "\t. $activate\n";
805 my $rec_activate = "$virtenv_dir/bin/activate";
808 printf "\t$python_cmd -m venv $virtenv_dir\n";
810 printf "\t$virtualenv_cmd $virtenv_dir\n";
812 printf "\t. $rec_activate\n";
813 printf "\tpip install -r $requirement_file\n";
816 $need++ if (!$rec_sphinx_upgrade);
821 print "All optional dependencies are met.\n" if (!$optional);
824 die "Can't build as $need mandatory dependency is missing";
826 die "Can't build as $need mandatory dependencies are missing";
829 print "Needed package dependencies are met.\n";
837 my $arg = shift(@ARGV);
839 if ($arg eq "--no-virtualenv") {
841 } elsif ($arg eq "--no-pdf"){
843 } elsif ($arg eq "--version-check"){
846 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
848 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
849 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
850 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
856 # Determine the system type. There's no standard unique way that would
857 # work with all distros with a minimal package install. So, several
858 # methods are used here.
860 # By default, it will use lsb_release function. If not available, it will
861 # fail back to reading the known different places where the distro name
865 $system_release = qx(lsb_release -d) if which("lsb_release");
866 $system_release =~ s/Description:\s*// if ($system_release);
867 $system_release = catcheck("/etc/system-release") if !$system_release;
868 $system_release = catcheck("/etc/redhat-release") if !$system_release;
869 $system_release = catcheck("/etc/lsb-release") if !$system_release;
870 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
872 # This seems more common than LSB these days
873 if (!$system_release) {
875 if (open IN, "cat /etc/os-release|") {
877 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
881 $system_release = $os_var{"NAME"};
882 if (defined($os_var{"VERSION_ID"})) {
883 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
885 $system_release .= " " . $os_var{"VERSION"};
889 $system_release = catcheck("/etc/issue") if !$system_release;
890 $system_release =~ s/\s+$//;