2 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
8 my $conf = "Documentation/conf.py";
9 my $requirement_file = "Documentation/sphinx/requirements.txt";
21 my $rec_sphinx_upgrade = 0;
23 my $virtenv_dir = "sphinx_";
26 # Command line arguments
33 # List of required texlive packages on Fedora and OpenSuse
37 'amsfonts.sty' => 'texlive-amsfonts',
38 'amsmath.sty' => 'texlive-amsmath',
39 'amssymb.sty' => 'texlive-amsfonts',
40 'amsthm.sty' => 'texlive-amscls',
41 'anyfontsize.sty' => 'texlive-anyfontsize',
42 'atbegshi.sty' => 'texlive-oberdiek',
43 'bm.sty' => 'texlive-tools',
44 'capt-of.sty' => 'texlive-capt-of',
45 'cmap.sty' => 'texlive-cmap',
46 'ecrm1000.tfm' => 'texlive-ec',
47 'eqparbox.sty' => 'texlive-eqparbox',
48 'eu1enc.def' => 'texlive-euenc',
49 'fancybox.sty' => 'texlive-fancybox',
50 'fancyvrb.sty' => 'texlive-fancyvrb',
51 'float.sty' => 'texlive-float',
52 'fncychap.sty' => 'texlive-fncychap',
53 'footnote.sty' => 'texlive-mdwtools',
54 'framed.sty' => 'texlive-framed',
55 'luatex85.sty' => 'texlive-luatex85',
56 'multirow.sty' => 'texlive-multirow',
57 'needspace.sty' => 'texlive-needspace',
58 'palatino.sty' => 'texlive-psnfss',
59 'parskip.sty' => 'texlive-parskip',
60 'polyglossia.sty' => 'texlive-polyglossia',
61 'tabulary.sty' => 'texlive-tabulary',
62 'threeparttable.sty' => 'texlive-threeparttable',
63 'titlesec.sty' => 'texlive-titlesec',
64 'ucs.sty' => 'texlive-ucs',
65 'upquote.sty' => 'texlive-upquote',
66 'wrapfig.sty' => 'texlive-wrapfig',
70 # Subroutines that checks if a feature exists
77 foreach my $prog (sort keys %missing) {
78 my $is_optional = $missing{$prog};
81 print "Warning: better to also install \"$prog\".\n";
83 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
85 if (defined($map{$prog})) {
86 $install .= " " . $map{$prog};
88 $install .= " " . $prog;
98 my $is_optional = shift;
100 $missing{$package} = $is_optional;
108 sub check_missing_file
($$$)
112 my $is_optional = shift;
116 add_package
($package, $is_optional);
121 foreach(split(/:/, $ENV{PATH
})) {
122 return "$_/$_[0]" if(-x
"$_/$_[0]");
126 sub check_program
($$)
129 my $is_optional = shift;
131 return if findprog
($prog);
133 add_package
($prog, $is_optional);
136 sub check_perl_module
($$)
139 my $is_optional = shift;
141 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
142 return if ($err == 0);
144 add_package
($prog, $is_optional);
147 sub check_python_module
($$)
150 my $is_optional = shift;
152 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
153 return if ($err == 0);
154 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
155 return if ($err == 0);
157 add_package
($prog, $is_optional);
160 sub check_rpm_missing
($$)
163 my $is_optional = $_[1];
165 foreach my $prog(@pkgs) {
166 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
167 add_package
($prog, $is_optional) if ($err);
171 sub check_pacman_missing
($$)
174 my $is_optional = $_[1];
176 foreach my $prog(@pkgs) {
177 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
178 add_package
($prog, $is_optional) if ($err);
182 sub check_missing_tex
($)
184 my $is_optional = shift;
185 my $kpsewhich = findprog
("kpsewhich");
187 foreach my $prog(keys %texlive) {
188 my $package = $texlive{$prog};
190 add_package
($package, $is_optional);
193 my $file = qx($kpsewhich $prog);
194 add_package
($package, $is_optional) if ($file =~ /^\s*$/);
198 sub get_sphinx_fname
()
200 my $fname = "sphinx-build";
201 return $fname if findprog
($fname);
203 $fname = "sphinx-build-3";
204 if (findprog
($fname)) {
210 my $prog = findprog
("virtualenv-3");
211 $prog = findprog
("virtualenv-3.5") if (!$prog);
213 check_program
("virtualenv", 0) if (!$prog);
216 add_package
("python-sphinx", 0);
228 open IN
, $conf or die "Can't open $conf";
230 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
237 die "Can't get needs_sphinx version from $conf" if (!$min_version);
239 open IN
, $requirement_file or die "Can't open $requirement_file";
241 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
248 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
250 $virtenv_dir .= $rec_version;
252 my $sphinx = get_sphinx_fname
();
253 return if ($sphinx eq "");
255 open IN
, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
257 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
261 # Sphinx 1.2.x uses a different format
262 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
269 die "$sphinx didn't return its version" if (!$cur_version);
271 printf "Sphinx version %s (minimal: %s, recommended >= %s)\n",
272 $cur_version, $min_version, $rec_version;
274 if ($cur_version lt $min_version) {
275 print "Warning: Sphinx version should be >= $min_version\n\n";
280 if ($cur_version lt $rec_version) {
281 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
282 print " To upgrade, use:\n\n";
283 $rec_sphinx_upgrade = 1;
288 # Ancillary subroutines
294 $res = qx(cat
$_[0]) if (-r
$_[0]);
301 my @path = split ":", $ENV{PATH
};
303 foreach my $dir(@path) {
304 my $name = $dir.'/'.$file;
305 return $name if (-x
$name );
311 # Subroutines that check distro-specific hints
314 sub give_debian_hints
()
317 "python-sphinx" => "python3-sphinx",
318 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
319 "virtualenv" => "virtualenv",
321 "convert" => "imagemagick",
322 "Pod::Usage" => "perl-modules",
323 "xelatex" => "texlive-xetex",
324 "rsvg-convert" => "librsvg2-bin",
328 check_missing_file
("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
332 check_program
("dvipng", 1) if ($pdf);
333 check_missing
(\
%map);
335 return if (!$need && !$optional);
336 printf("You should run:\n\n\tsudo apt-get install $install\n");
339 sub give_redhat_hints
()
342 "python-sphinx" => "python3-sphinx",
343 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
344 "virtualenv" => "python3-virtualenv",
346 "convert" => "ImageMagick",
347 "Pod::Usage" => "perl-Pod-Usage",
348 "xelatex" => "texlive-xetex-bin",
349 "rsvg-convert" => "librsvg2-tools",
352 my @fedora26_opt_pkgs = (
353 "graphviz-gd", # Fedora 26: needed for PDF support
356 my @fedora_tex_pkgs = (
357 "texlive-collection-fontsrecommended",
358 "texlive-collection-latex",
360 "dejavu-serif-fonts",
361 "dejavu-sans-mono-fonts",
365 # Checks valid for RHEL/CentOS version 7.x.
367 if (! $system_release =~ /Fedora/) {
368 $map{"virtualenv"} = "python-virtualenv";
373 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
375 check_rpm_missing
(\
@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
376 check_rpm_missing
(\
@fedora_tex_pkgs, 1) if ($pdf);
377 check_missing_tex
(1) if ($pdf);
378 check_missing
(\
%map);
380 return if (!$need && !$optional);
382 if ($release >= 18) {
383 # dnf, for Fedora 18+
384 printf("You should run:\n\n\tsudo dnf install -y $install\n");
386 # yum, for RHEL (and clones) or Fedora version < 18
387 printf("You should run:\n\n\tsudo yum install -y $install\n");
391 sub give_opensuse_hints
()
394 "python-sphinx" => "python3-sphinx",
395 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
396 "virtualenv" => "python3-virtualenv",
398 "convert" => "ImageMagick",
399 "Pod::Usage" => "perl-Pod-Usage",
400 "xelatex" => "texlive-xetex-bin",
401 "rsvg-convert" => "rsvg-view",
404 my @suse_tex_pkgs = (
405 "texlive-babel-english",
421 check_rpm_missing
(\
@suse_tex_pkgs, 1) if ($pdf);
422 check_missing_tex
(1) if ($pdf);
423 check_missing
(\
%map);
425 return if (!$need && !$optional);
426 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
429 sub give_mageia_hints
()
432 "python-sphinx" => "python3-sphinx",
433 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
434 "virtualenv" => "python3-virtualenv",
436 "convert" => "ImageMagick",
437 "Pod::Usage" => "perl-Pod-Usage",
438 "xelatex" => "texlive",
439 "rsvg-convert" => "librsvg2-tools",
443 "texlive-fontsextra",
446 check_rpm_missing
(\
@tex_pkgs, 1) if ($pdf);
447 check_missing
(\
%map);
449 return if (!$need && !$optional);
450 printf("You should run:\n\n\tsudo urpmi $install\n");
453 sub give_arch_linux_hints
()
456 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
457 "virtualenv" => "python-virtualenv",
459 "convert" => "imagemagick",
460 "xelatex" => "texlive-bin",
461 "rsvg-convert" => "extra/librsvg",
464 my @archlinux_tex_pkgs = (
466 "texlive-latexextra",
469 check_pacman_missing
(\
@archlinux_tex_pkgs, 1) if ($pdf);
470 check_missing
(\
%map);
472 return if (!$need && !$optional);
473 printf("You should run:\n\n\tsudo pacman -S $install\n");
476 sub give_gentoo_hints
()
479 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
480 "virtualenv" => "dev-python/virtualenv",
481 "dot" => "media-gfx/graphviz",
482 "convert" => "media-gfx/imagemagick",
483 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
484 "rsvg-convert" => "gnome-base/librsvg",
487 check_missing_file
("/usr/share/fonts/dejavu/DejaVuSans.ttf",
488 "media-fonts/dejavu", 1) if ($pdf);
490 check_missing
(\
%map);
492 return if (!$need && !$optional);
494 printf("You should run:\n\n");
495 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
496 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
497 printf("\tsudo emerge --ask $install\n");
503 # Distro-specific hints
504 if ($system_release =~ /Red Hat Enterprise Linux/) {
508 if ($system_release =~ /CentOS/) {
512 if ($system_release =~ /Scientific Linux/) {
516 if ($system_release =~ /Oracle Linux Server/) {
520 if ($system_release =~ /Fedora/) {
524 if ($system_release =~ /Ubuntu/) {
528 if ($system_release =~ /Debian/) {
532 if ($system_release =~ /openSUSE/) {
536 if ($system_release =~ /Mageia/) {
540 if ($system_release =~ /Arch Linux/) {
541 give_arch_linux_hints
;
544 if ($system_release =~ /Gentoo/) {
550 # Fall-back to generic hint code for other distros
551 # That's far from ideal, specially for LaTeX dependencies.
554 "sphinx-build" => "sphinx"
556 check_missing_tex
(1) if ($pdf);
557 check_missing
(\
%map);
558 print "I don't know distro $system_release.\n";
559 print "So, I can't provide you a hint with the install procedure.\n";
560 print "There are likely missing dependencies.\n";
564 # Common dependencies
569 if ($system_release) {
570 print "Detected OS: $system_release.\n";
572 print "Unknown OS\n";
575 # RHEL 7.x and clones have Sphinx version 1.1.x and incomplete texlive
576 if (($system_release =~ /Red Hat Enterprise Linux/) ||
577 ($system_release =~ /CentOS/) ||
578 ($system_release =~ /Scientific Linux/) ||
579 ($system_release =~ /Oracle Linux Server/)) {
583 printf("NOTE: On this distro, Sphinx and TexLive shipped versions are incompatible\n");
584 printf("with doc build. So, use Sphinx via a Python virtual environment.\n\n");
585 printf("This script can't install a TexLive version that would provide PDF.\n");
588 # Check for needed programs/tools
590 check_perl_module
("Pod::Usage", 0);
591 check_program
("make", 0);
592 check_program
("gcc", 0);
593 check_python_module
("sphinx_rtd_theme", 1) if (!$virtualenv);
594 check_program
("xelatex", 1) if ($pdf);
595 check_program
("dot", 1);
596 check_program
("convert", 1);
597 check_program
("rsvg-convert", 1) if ($pdf);
598 check_program
("latexmk", 1) if ($pdf);
603 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
604 which
("sphinx-build-3");
606 if ($need_sphinx || $rec_sphinx_upgrade) {
607 my $activate = "$virtenv_dir/bin/activate";
608 if (-e
"$ENV{'PWD'}/$activate") {
609 printf "\nNeed to activate virtualenv with:\n";
610 printf "\t. $activate\n";
612 my $virtualenv = findprog
("virtualenv-3");
613 $virtualenv = findprog
("virtualenv-3.5") if (!$virtualenv);
614 $virtualenv = findprog
("virtualenv") if (!$virtualenv);
615 $virtualenv = "virtualenv" if (!$virtualenv);
617 printf "\t$virtualenv $virtenv_dir\n";
618 printf "\t. $activate\n";
619 printf "\tpip install -r $requirement_file\n";
621 $need++ if (!$rec_sphinx_upgrade);
626 print "All optional dependenties are met.\n" if (!$optional);
629 die "Can't build as $need mandatory dependency is missing";
631 die "Can't build as $need mandatory dependencies are missing";
634 print "Needed package dependencies are met.\n";
642 my $arg = shift(@ARGV);
644 if ($arg eq "--no-virtualenv") {
646 } elsif ($arg eq "--no-pdf"){
649 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
655 # Determine the system type. There's no standard unique way that would
656 # work with all distros with a minimal package install. So, several
657 # methods are used here.
659 # By default, it will use lsb_release function. If not available, it will
660 # fail back to reading the known different places where the distro name
664 $system_release = qx(lsb_release
-d
) if which
("lsb_release");
665 $system_release =~ s/Description:\s*// if ($system_release);
666 $system_release = catcheck
("/etc/system-release") if !$system_release;
667 $system_release = catcheck
("/etc/redhat-release") if !$system_release;
668 $system_release = catcheck
("/etc/lsb-release") if !$system_release;
669 $system_release = catcheck
("/etc/gentoo-release") if !$system_release;
670 $system_release = catcheck
("/etc/issue") if !$system_release;
671 $system_release =~ s/\s+$//;