2 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Copyright (c) 2017-2019 Mauro Carvalho Chehab <mchehab@kernel.org>
8 my $conf = "Documentation/conf.py";
9 my $requirement_file = "Documentation/sphinx/requirements.txt";
10 my $virtenv_prefix = "sphinx_";
22 my $rec_sphinx_upgrade = 0;
28 # Command line arguments
33 my $version_check = 0;
36 # List of required texlive packages on Fedora and OpenSuse
40 'amsfonts.sty' => 'texlive-amsfonts',
41 'amsmath.sty' => 'texlive-amsmath',
42 'amssymb.sty' => 'texlive-amsfonts',
43 'amsthm.sty' => 'texlive-amscls',
44 'anyfontsize.sty' => 'texlive-anyfontsize',
45 'atbegshi.sty' => 'texlive-oberdiek',
46 'bm.sty' => 'texlive-tools',
47 'capt-of.sty' => 'texlive-capt-of',
48 'cmap.sty' => 'texlive-cmap',
49 'ecrm1000.tfm' => 'texlive-ec',
50 'eqparbox.sty' => 'texlive-eqparbox',
51 'eu1enc.def' => 'texlive-euenc',
52 'fancybox.sty' => 'texlive-fancybox',
53 'fancyvrb.sty' => 'texlive-fancyvrb',
54 'float.sty' => 'texlive-float',
55 'fncychap.sty' => 'texlive-fncychap',
56 'footnote.sty' => 'texlive-mdwtools',
57 'framed.sty' => 'texlive-framed',
58 'luatex85.sty' => 'texlive-luatex85',
59 'multirow.sty' => 'texlive-multirow',
60 'needspace.sty' => 'texlive-needspace',
61 'palatino.sty' => 'texlive-psnfss',
62 'parskip.sty' => 'texlive-parskip',
63 'polyglossia.sty' => 'texlive-polyglossia',
64 'tabulary.sty' => 'texlive-tabulary',
65 'threeparttable.sty' => 'texlive-threeparttable',
66 'titlesec.sty' => 'texlive-titlesec',
67 'ucs.sty' => 'texlive-ucs',
68 'upquote.sty' => 'texlive-upquote',
69 'wrapfig.sty' => 'texlive-wrapfig',
73 # Subroutines that checks if a feature exists
80 foreach my $prog (sort keys %missing) {
81 my $is_optional = $missing{$prog};
84 print "Warning: better to also install \"$prog\".\n";
86 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
88 if (defined($map{$prog})) {
89 $install .= " " . $map{$prog};
91 $install .= " " . $prog;
101 my $is_optional = shift;
103 $missing{$package} = $is_optional;
111 sub check_missing_file($$$)
115 my $is_optional = shift;
119 add_package($package, $is_optional);
124 foreach(split(/:/, $ENV{PATH})) {
125 return "$_/$_[0]" if(-x "$_/$_[0]");
129 sub check_program($$)
132 my $is_optional = shift;
134 return if findprog($prog);
136 add_package($prog, $is_optional);
139 sub check_perl_module($$)
142 my $is_optional = shift;
144 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
145 return if ($err == 0);
147 add_package($prog, $is_optional);
150 sub check_python_module($$)
153 my $is_optional = shift;
155 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
156 return if ($err == 0);
157 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
158 return if ($err == 0);
160 add_package($prog, $is_optional);
163 sub check_rpm_missing($$)
166 my $is_optional = $_[1];
168 foreach my $prog(@pkgs) {
169 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
170 add_package($prog, $is_optional) if ($err);
174 sub check_pacman_missing($$)
177 my $is_optional = $_[1];
179 foreach my $prog(@pkgs) {
180 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
181 add_package($prog, $is_optional) if ($err);
185 sub check_missing_tex($)
187 my $is_optional = shift;
188 my $kpsewhich = findprog("kpsewhich");
190 foreach my $prog(keys %texlive) {
191 my $package = $texlive{$prog};
193 add_package($package, $is_optional);
196 my $file = qx($kpsewhich $prog);
197 add_package($package, $is_optional) if ($file =~ /^\s*$/);
201 sub get_sphinx_fname()
203 my $fname = "sphinx-build";
204 return $fname if findprog($fname);
206 $fname = "sphinx-build-3";
207 if (findprog($fname)) {
213 my $prog = findprog("virtualenv-3");
214 $prog = findprog("virtualenv-3.5") if (!$prog);
216 check_program("virtualenv", 0) if (!$prog);
219 add_package("python-sphinx", 0);
230 open IN, $conf or die "Can't open $conf";
232 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
239 die "Can't get needs_sphinx version from $conf" if (!$min_version);
241 open IN, $requirement_file or die "Can't open $requirement_file";
243 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
250 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
252 $virtenv_dir = $virtenv_prefix . $rec_version;
254 my $sphinx = get_sphinx_fname();
255 return if ($sphinx eq "");
257 open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
259 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
263 # Sphinx 1.2.x uses a different format
264 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
271 die "$sphinx didn't return its version" if (!$cur_version);
273 if ($cur_version lt $min_version) {
274 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
275 $cur_version, $min_version, $rec_version;;
280 if ($cur_version lt $rec_version) {
281 printf "Sphinx version %s\n", $cur_version;
282 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
283 $rec_sphinx_upgrade = 1;
287 # On version check mode, just assume Sphinx has all mandatory deps
288 exit (0) if ($version_check);
292 # Ancillary subroutines
298 $res = qx(cat $_[0]) if (-r $_[0]);
305 my @path = split ":", $ENV{PATH};
307 foreach my $dir(@path) {
308 my $name = $dir.'/'.$file;
309 return $name if (-x $name );
315 # Subroutines that check distro-specific hints
318 sub give_debian_hints()
321 "python-sphinx" => "python3-sphinx",
322 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
323 "virtualenv" => "virtualenv",
325 "convert" => "imagemagick",
326 "Pod::Usage" => "perl-modules",
327 "xelatex" => "texlive-xetex",
328 "rsvg-convert" => "librsvg2-bin",
332 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
336 check_program("dvipng", 1) if ($pdf);
337 check_missing(\%map);
339 return if (!$need && !$optional);
340 printf("You should run:\n\n\tsudo apt-get install $install\n");
343 sub give_redhat_hints()
346 "python-sphinx" => "python3-sphinx",
347 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
348 "virtualenv" => "python3-virtualenv",
350 "convert" => "ImageMagick",
351 "Pod::Usage" => "perl-Pod-Usage",
352 "xelatex" => "texlive-xetex-bin",
353 "rsvg-convert" => "librsvg2-tools",
356 my @fedora26_opt_pkgs = (
357 "graphviz-gd", # Fedora 26: needed for PDF support
360 my @fedora_tex_pkgs = (
361 "texlive-collection-fontsrecommended",
362 "texlive-collection-latex",
364 "dejavu-serif-fonts",
365 "dejavu-sans-mono-fonts",
369 # Checks valid for RHEL/CentOS version 7.x.
371 if (! $system_release =~ /Fedora/) {
372 $map{"virtualenv"} = "python-virtualenv";
377 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
379 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
380 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
381 check_missing_tex(1) if ($pdf);
382 check_missing(\%map);
384 return if (!$need && !$optional);
386 if ($release >= 18) {
387 # dnf, for Fedora 18+
388 printf("You should run:\n\n\tsudo dnf install -y $install\n");
390 # yum, for RHEL (and clones) or Fedora version < 18
391 printf("You should run:\n\n\tsudo yum install -y $install\n");
395 sub give_opensuse_hints()
398 "python-sphinx" => "python3-sphinx",
399 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
400 "virtualenv" => "python3-virtualenv",
402 "convert" => "ImageMagick",
403 "Pod::Usage" => "perl-Pod-Usage",
404 "xelatex" => "texlive-xetex-bin",
405 "rsvg-convert" => "rsvg-view",
408 my @suse_tex_pkgs = (
409 "texlive-babel-english",
425 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
426 check_missing_tex(1) if ($pdf);
427 check_missing(\%map);
429 return if (!$need && !$optional);
430 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
433 sub give_mageia_hints()
436 "python-sphinx" => "python3-sphinx",
437 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
438 "virtualenv" => "python3-virtualenv",
440 "convert" => "ImageMagick",
441 "Pod::Usage" => "perl-Pod-Usage",
442 "xelatex" => "texlive",
443 "rsvg-convert" => "librsvg2-tools",
447 "texlive-fontsextra",
450 check_rpm_missing(\@tex_pkgs, 1) if ($pdf);
451 check_missing(\%map);
453 return if (!$need && !$optional);
454 printf("You should run:\n\n\tsudo urpmi $install\n");
457 sub give_arch_linux_hints()
460 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
461 "virtualenv" => "python-virtualenv",
463 "convert" => "imagemagick",
464 "xelatex" => "texlive-bin",
465 "rsvg-convert" => "extra/librsvg",
468 my @archlinux_tex_pkgs = (
470 "texlive-latexextra",
473 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
474 check_missing(\%map);
476 return if (!$need && !$optional);
477 printf("You should run:\n\n\tsudo pacman -S $install\n");
480 sub give_gentoo_hints()
483 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
484 "virtualenv" => "dev-python/virtualenv",
485 "dot" => "media-gfx/graphviz",
486 "convert" => "media-gfx/imagemagick",
487 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
488 "rsvg-convert" => "gnome-base/librsvg",
491 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
492 "media-fonts/dejavu", 1) if ($pdf);
494 check_missing(\%map);
496 return if (!$need && !$optional);
498 printf("You should run:\n\n");
499 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
500 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
501 printf("\tsudo emerge --ask $install\n");
507 # Distro-specific hints
508 if ($system_release =~ /Red Hat Enterprise Linux/) {
512 if ($system_release =~ /CentOS/) {
516 if ($system_release =~ /Scientific Linux/) {
520 if ($system_release =~ /Oracle Linux Server/) {
524 if ($system_release =~ /Fedora/) {
528 if ($system_release =~ /Ubuntu/) {
532 if ($system_release =~ /Debian/) {
536 if ($system_release =~ /openSUSE/) {
540 if ($system_release =~ /Mageia/) {
544 if ($system_release =~ /Arch Linux/) {
545 give_arch_linux_hints;
548 if ($system_release =~ /Gentoo/) {
554 # Fall-back to generic hint code for other distros
555 # That's far from ideal, specially for LaTeX dependencies.
558 "sphinx-build" => "sphinx"
560 check_missing_tex(1) if ($pdf);
561 check_missing(\%map);
562 print "I don't know distro $system_release.\n";
563 print "So, I can't provide you a hint with the install procedure.\n";
564 print "There are likely missing dependencies.\n";
568 # Common dependencies
573 # Check for needed programs/tools
576 if ($system_release) {
577 print "Detected OS: $system_release.\n\n";
579 print "Unknown OS\n\n";
582 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
584 # Check for needed programs/tools
585 check_perl_module("Pod::Usage", 0);
586 check_program("make", 0);
587 check_program("gcc", 0);
588 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
589 check_program("xelatex", 1) if ($pdf);
590 check_program("dot", 1);
591 check_program("convert", 1);
592 check_program("rsvg-convert", 1) if ($pdf);
593 check_program("latexmk", 1) if ($pdf);
598 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
599 which("sphinx-build-3");
601 if ($need_sphinx || $rec_sphinx_upgrade) {
602 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
603 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
605 @activates = sort {$b cmp $a} @activates;
607 if ($need_sphinx && scalar @activates > 0 && $activates[0] ge $min_activate) {
608 printf "\nNeed to activate a compatible Sphinx version on virtualenv with:\n";
609 printf "\t. $activates[0]\n";
612 my $rec_activate = "$virtenv_dir/bin/activate";
613 my $virtualenv = findprog("virtualenv-3");
614 $virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
615 $virtualenv = findprog("virtualenv") if (!$virtualenv);
616 $virtualenv = "virtualenv" if (!$virtualenv);
618 printf "\t$virtualenv $virtenv_dir\n";
619 printf "\t. $rec_activate\n";
620 printf "\tpip install -r $requirement_file\n";
622 $need++ if (!$rec_sphinx_upgrade);
627 print "All optional dependencies are met.\n" if (!$optional);
630 die "Can't build as $need mandatory dependency is missing";
632 die "Can't build as $need mandatory dependencies are missing";
635 print "Needed package dependencies are met.\n";
643 my $arg = shift(@ARGV);
645 if ($arg eq "--no-virtualenv") {
647 } elsif ($arg eq "--no-pdf"){
649 } elsif ($arg eq "--version-check"){
652 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
654 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
655 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
656 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
662 # Determine the system type. There's no standard unique way that would
663 # work with all distros with a minimal package install. So, several
664 # methods are used here.
666 # By default, it will use lsb_release function. If not available, it will
667 # fail back to reading the known different places where the distro name
671 $system_release = qx(lsb_release -d) if which("lsb_release");
672 $system_release =~ s/Description:\s*// if ($system_release);
673 $system_release = catcheck("/etc/system-release") if !$system_release;
674 $system_release = catcheck("/etc/redhat-release") if !$system_release;
675 $system_release = catcheck("/etc/lsb-release") if !$system_release;
676 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
677 $system_release = catcheck("/etc/issue") if !$system_release;
678 $system_release =~ s/\s+$//;