2 # SPDX-License-Identifier: GPL-2.0-or-later
5 # Copyright (c) 2017-2019 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_";
25 my $rec_sphinx_upgrade = 0;
31 # Command line arguments
36 my $version_check = 0;
39 # List of required texlive packages on Fedora and OpenSuse
43 'amsfonts.sty' => 'texlive-amsfonts',
44 'amsmath.sty' => 'texlive-amsmath',
45 'amssymb.sty' => 'texlive-amsfonts',
46 'amsthm.sty' => 'texlive-amscls',
47 'anyfontsize.sty' => 'texlive-anyfontsize',
48 'atbegshi.sty' => 'texlive-oberdiek',
49 'bm.sty' => 'texlive-tools',
50 'capt-of.sty' => 'texlive-capt-of',
51 'cmap.sty' => 'texlive-cmap',
52 'ecrm1000.tfm' => 'texlive-ec',
53 'eqparbox.sty' => 'texlive-eqparbox',
54 'eu1enc.def' => 'texlive-euenc',
55 'fancybox.sty' => 'texlive-fancybox',
56 'fancyvrb.sty' => 'texlive-fancyvrb',
57 'float.sty' => 'texlive-float',
58 'fncychap.sty' => 'texlive-fncychap',
59 'footnote.sty' => 'texlive-mdwtools',
60 'framed.sty' => 'texlive-framed',
61 'luatex85.sty' => 'texlive-luatex85',
62 'multirow.sty' => 'texlive-multirow',
63 'needspace.sty' => 'texlive-needspace',
64 'palatino.sty' => 'texlive-psnfss',
65 'parskip.sty' => 'texlive-parskip',
66 'polyglossia.sty' => 'texlive-polyglossia',
67 'tabulary.sty' => 'texlive-tabulary',
68 'threeparttable.sty' => 'texlive-threeparttable',
69 'titlesec.sty' => 'texlive-titlesec',
70 'ucs.sty' => 'texlive-ucs',
71 'upquote.sty' => 'texlive-upquote',
72 'wrapfig.sty' => 'texlive-wrapfig',
76 # Subroutines that checks if a feature exists
83 foreach my $prog (sort keys %missing) {
84 my $is_optional = $missing{$prog};
87 print "Warning: better to also install \"$prog\".\n";
89 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
91 if (defined($map{$prog})) {
92 $install .= " " . $map{$prog};
94 $install .= " " . $prog;
104 my $is_optional = shift;
106 $missing{$package} = $is_optional;
114 sub check_missing_file
($$$)
118 my $is_optional = shift;
122 add_package
($package, $is_optional);
127 foreach(split(/:/, $ENV{PATH
})) {
128 return "$_/$_[0]" if(-x
"$_/$_[0]");
132 sub check_program
($$)
135 my $is_optional = shift;
137 return if findprog
($prog);
139 add_package
($prog, $is_optional);
142 sub check_perl_module
($$)
145 my $is_optional = shift;
147 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
148 return if ($err == 0);
150 add_package
($prog, $is_optional);
153 sub check_python_module
($$)
156 my $is_optional = shift;
158 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
159 return if ($err == 0);
160 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
161 return if ($err == 0);
163 add_package
($prog, $is_optional);
166 sub check_rpm_missing
($$)
169 my $is_optional = $_[1];
171 foreach my $prog(@pkgs) {
172 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
173 add_package
($prog, $is_optional) if ($err);
177 sub check_pacman_missing
($$)
180 my $is_optional = $_[1];
182 foreach my $prog(@pkgs) {
183 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
184 add_package
($prog, $is_optional) if ($err);
188 sub check_missing_tex
($)
190 my $is_optional = shift;
191 my $kpsewhich = findprog
("kpsewhich");
193 foreach my $prog(keys %texlive) {
194 my $package = $texlive{$prog};
196 add_package
($package, $is_optional);
199 my $file = qx($kpsewhich $prog);
200 add_package
($package, $is_optional) if ($file =~ /^\s*$/);
204 sub get_sphinx_fname
()
206 my $fname = "sphinx-build";
207 return $fname if findprog
($fname);
209 $fname = "sphinx-build-3";
210 if (findprog
($fname)) {
216 my $prog = findprog
("virtualenv-3");
217 $prog = findprog
("virtualenv-3.5") if (!$prog);
219 check_program
("virtualenv", 0) if (!$prog);
222 add_package
("python-sphinx", 0);
233 open IN
, $conf or die "Can't open $conf";
235 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
242 die "Can't get needs_sphinx version from $conf" if (!$min_version);
244 open IN
, $requirement_file or die "Can't open $requirement_file";
246 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
253 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
255 $virtenv_dir = $virtenv_prefix . $rec_version;
257 my $sphinx = get_sphinx_fname
();
258 return if ($sphinx eq "");
260 open IN
, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
262 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
266 # Sphinx 1.2.x uses a different format
267 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
274 die "$sphinx didn't return its version" if (!$cur_version);
276 if ($cur_version lt $min_version) {
277 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
278 $cur_version, $min_version, $rec_version;;
283 if ($cur_version lt $rec_version) {
284 printf "Sphinx version %s\n", $cur_version;
285 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
286 $rec_sphinx_upgrade = 1;
290 # On version check mode, just assume Sphinx has all mandatory deps
291 exit (0) if ($version_check);
295 # Ancillary subroutines
301 $res = qx(cat
$_[0]) if (-r
$_[0]);
308 my @path = split ":", $ENV{PATH
};
310 foreach my $dir(@path) {
311 my $name = $dir.'/'.$file;
312 return $name if (-x
$name );
318 # Subroutines that check distro-specific hints
321 sub give_debian_hints
()
324 "python-sphinx" => "python3-sphinx",
325 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
326 "virtualenv" => "virtualenv",
328 "convert" => "imagemagick",
329 "Pod::Usage" => "perl-modules",
330 "xelatex" => "texlive-xetex",
331 "rsvg-convert" => "librsvg2-bin",
335 check_missing_file
("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
339 check_program
("dvipng", 1) if ($pdf);
340 check_missing
(\
%map);
342 return if (!$need && !$optional);
343 printf("You should run:\n\n\tsudo apt-get install $install\n");
346 sub give_redhat_hints
()
349 "python-sphinx" => "python3-sphinx",
350 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
351 "virtualenv" => "python3-virtualenv",
353 "convert" => "ImageMagick",
354 "Pod::Usage" => "perl-Pod-Usage",
355 "xelatex" => "texlive-xetex-bin",
356 "rsvg-convert" => "librsvg2-tools",
359 my @fedora26_opt_pkgs = (
360 "graphviz-gd", # Fedora 26: needed for PDF support
363 my @fedora_tex_pkgs = (
364 "texlive-collection-fontsrecommended",
365 "texlive-collection-latex",
367 "dejavu-serif-fonts",
368 "dejavu-sans-mono-fonts",
372 # Checks valid for RHEL/CentOS version 7.x.
374 if (! $system_release =~ /Fedora/) {
375 $map{"virtualenv"} = "python-virtualenv";
380 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
382 check_rpm_missing
(\
@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
383 check_rpm_missing
(\
@fedora_tex_pkgs, 1) if ($pdf);
384 check_missing_tex
(1) if ($pdf);
385 check_missing
(\
%map);
387 return if (!$need && !$optional);
389 if ($release >= 18) {
390 # dnf, for Fedora 18+
391 printf("You should run:\n\n\tsudo dnf install -y $install\n");
393 # yum, for RHEL (and clones) or Fedora version < 18
394 printf("You should run:\n\n\tsudo yum install -y $install\n");
398 sub give_opensuse_hints
()
401 "python-sphinx" => "python3-sphinx",
402 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
403 "virtualenv" => "python3-virtualenv",
405 "convert" => "ImageMagick",
406 "Pod::Usage" => "perl-Pod-Usage",
407 "xelatex" => "texlive-xetex-bin",
408 "rsvg-convert" => "rsvg-view",
411 my @suse_tex_pkgs = (
412 "texlive-babel-english",
428 check_rpm_missing
(\
@suse_tex_pkgs, 1) if ($pdf);
429 check_missing_tex
(1) if ($pdf);
430 check_missing
(\
%map);
432 return if (!$need && !$optional);
433 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
436 sub give_mageia_hints
()
439 "python-sphinx" => "python3-sphinx",
440 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
441 "virtualenv" => "python3-virtualenv",
443 "convert" => "ImageMagick",
444 "Pod::Usage" => "perl-Pod-Usage",
445 "xelatex" => "texlive",
446 "rsvg-convert" => "librsvg2-tools",
450 "texlive-fontsextra",
453 check_rpm_missing
(\
@tex_pkgs, 1) if ($pdf);
454 check_missing
(\
%map);
456 return if (!$need && !$optional);
457 printf("You should run:\n\n\tsudo urpmi $install\n");
460 sub give_arch_linux_hints
()
463 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
464 "virtualenv" => "python-virtualenv",
466 "convert" => "imagemagick",
467 "xelatex" => "texlive-bin",
468 "rsvg-convert" => "extra/librsvg",
471 my @archlinux_tex_pkgs = (
473 "texlive-latexextra",
476 check_pacman_missing
(\
@archlinux_tex_pkgs, 1) if ($pdf);
477 check_missing
(\
%map);
479 return if (!$need && !$optional);
480 printf("You should run:\n\n\tsudo pacman -S $install\n");
483 sub give_gentoo_hints
()
486 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
487 "virtualenv" => "dev-python/virtualenv",
488 "dot" => "media-gfx/graphviz",
489 "convert" => "media-gfx/imagemagick",
490 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
491 "rsvg-convert" => "gnome-base/librsvg",
494 check_missing_file
("/usr/share/fonts/dejavu/DejaVuSans.ttf",
495 "media-fonts/dejavu", 1) if ($pdf);
497 check_missing
(\
%map);
499 return if (!$need && !$optional);
501 printf("You should run:\n\n");
502 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
503 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
504 printf("\tsudo emerge --ask $install\n");
510 # Distro-specific hints
511 if ($system_release =~ /Red Hat Enterprise Linux/) {
515 if ($system_release =~ /CentOS/) {
519 if ($system_release =~ /Scientific Linux/) {
523 if ($system_release =~ /Oracle Linux Server/) {
527 if ($system_release =~ /Fedora/) {
531 if ($system_release =~ /Ubuntu/) {
535 if ($system_release =~ /Debian/) {
539 if ($system_release =~ /openSUSE/) {
543 if ($system_release =~ /Mageia/) {
547 if ($system_release =~ /Arch Linux/) {
548 give_arch_linux_hints
;
551 if ($system_release =~ /Gentoo/) {
557 # Fall-back to generic hint code for other distros
558 # That's far from ideal, specially for LaTeX dependencies.
561 "sphinx-build" => "sphinx"
563 check_missing_tex
(1) if ($pdf);
564 check_missing
(\
%map);
565 print "I don't know distro $system_release.\n";
566 print "So, I can't provide you a hint with the install procedure.\n";
567 print "There are likely missing dependencies.\n";
571 # Common dependencies
576 # Check for needed programs/tools
579 if ($system_release) {
580 print "Detected OS: $system_release.\n\n";
582 print "Unknown OS\n\n";
585 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
587 # Check for needed programs/tools
588 check_perl_module
("Pod::Usage", 0);
589 check_program
("make", 0);
590 check_program
("gcc", 0);
591 check_python_module
("sphinx_rtd_theme", 1) if (!$virtualenv);
592 check_program
("xelatex", 1) if ($pdf);
593 check_program
("dot", 1);
594 check_program
("convert", 1);
595 check_program
("rsvg-convert", 1) if ($pdf);
596 check_program
("latexmk", 1) if ($pdf);
601 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
602 which
("sphinx-build-3");
604 if ($need_sphinx || $rec_sphinx_upgrade) {
605 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
606 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
608 @activates = sort {$b cmp $a} @activates;
610 if ($need_sphinx && scalar @activates > 0 && $activates[0] ge $min_activate) {
611 printf "\nNeed to activate a compatible Sphinx version on virtualenv with:\n";
612 printf "\t. $activates[0]\n";
615 my $rec_activate = "$virtenv_dir/bin/activate";
616 my $virtualenv = findprog
("virtualenv-3");
617 $virtualenv = findprog
("virtualenv-3.5") if (!$virtualenv);
618 $virtualenv = findprog
("virtualenv") if (!$virtualenv);
619 $virtualenv = "virtualenv" if (!$virtualenv);
621 printf "\t$virtualenv $virtenv_dir\n";
622 printf "\t. $rec_activate\n";
623 printf "\tpip install -r $requirement_file\n";
625 $need++ if (!$rec_sphinx_upgrade);
630 print "All optional dependencies are met.\n" if (!$optional);
633 die "Can't build as $need mandatory dependency is missing";
635 die "Can't build as $need mandatory dependencies are missing";
638 print "Needed package dependencies are met.\n";
646 my $arg = shift(@ARGV);
648 if ($arg eq "--no-virtualenv") {
650 } elsif ($arg eq "--no-pdf"){
652 } elsif ($arg eq "--version-check"){
655 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
657 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
658 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
659 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
665 # Determine the system type. There's no standard unique way that would
666 # work with all distros with a minimal package install. So, several
667 # methods are used here.
669 # By default, it will use lsb_release function. If not available, it will
670 # fail back to reading the known different places where the distro name
674 $system_release = qx(lsb_release
-d
) if which
("lsb_release");
675 $system_release =~ s/Description:\s*// if ($system_release);
676 $system_release = catcheck
("/etc/system-release") if !$system_release;
677 $system_release = catcheck
("/etc/redhat-release") if !$system_release;
678 $system_release = catcheck
("/etc/lsb-release") if !$system_release;
679 $system_release = catcheck
("/etc/gentoo-release") if !$system_release;
680 $system_release = catcheck
("/etc/issue") if !$system_release;
681 $system_release =~ s/\s+$//;