4 # Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 my $conf = "Documentation/conf.py";
17 my $requirement_file = "Documentation/sphinx/requirements.txt";
29 my $rec_sphinx_upgrade = 0;
31 my $virtenv_dir = "sphinx_";
34 # Command line arguments
41 # List of required texlive packages on Fedora and OpenSuse
45 'amsfonts.sty' => 'texlive-amsfonts',
46 'amsmath.sty' => 'texlive-amsmath',
47 'amssymb.sty' => 'texlive-amsfonts',
48 'amsthm.sty' => 'texlive-amscls',
49 'anyfontsize.sty' => 'texlive-anyfontsize',
50 'atbegshi.sty' => 'texlive-oberdiek',
51 'bm.sty' => 'texlive-tools',
52 'capt-of.sty' => 'texlive-capt-of',
53 'cmap.sty' => 'texlive-cmap',
54 'ecrm1000.tfm' => 'texlive-ec',
55 'eqparbox.sty' => 'texlive-eqparbox',
56 'eu1enc.def' => 'texlive-euenc',
57 'fancybox.sty' => 'texlive-fancybox',
58 'fancyvrb.sty' => 'texlive-fancyvrb',
59 'float.sty' => 'texlive-float',
60 'fncychap.sty' => 'texlive-fncychap',
61 'footnote.sty' => 'texlive-mdwtools',
62 'framed.sty' => 'texlive-framed',
63 'luatex85.sty' => 'texlive-luatex85',
64 'multirow.sty' => 'texlive-multirow',
65 'needspace.sty' => 'texlive-needspace',
66 'palatino.sty' => 'texlive-psnfss',
67 'parskip.sty' => 'texlive-parskip',
68 'polyglossia.sty' => 'texlive-polyglossia',
69 'tabulary.sty' => 'texlive-tabulary',
70 'threeparttable.sty' => 'texlive-threeparttable',
71 'titlesec.sty' => 'texlive-titlesec',
72 'ucs.sty' => 'texlive-ucs',
73 'upquote.sty' => 'texlive-upquote',
74 'wrapfig.sty' => 'texlive-wrapfig',
78 # Subroutines that checks if a feature exists
85 foreach my $prog (sort keys %missing) {
86 my $is_optional = $missing{$prog};
89 print "Warning: better to also install \"$prog\".\n";
91 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
93 if (defined($map{$prog})) {
94 $install .= " " . $map{$prog};
96 $install .= " " . $prog;
106 my $is_optional = shift;
108 $missing{$package} = $is_optional;
116 sub check_missing_file($$$)
120 my $is_optional = shift;
124 add_package($package, $is_optional);
129 foreach(split(/:/, $ENV{PATH})) {
130 return "$_/$_[0]" if(-x "$_/$_[0]");
134 sub check_program($$)
137 my $is_optional = shift;
139 return if findprog($prog);
141 add_package($prog, $is_optional);
144 sub check_perl_module($$)
147 my $is_optional = shift;
149 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
150 return if ($err == 0);
152 add_package($prog, $is_optional);
155 sub check_python_module($$)
158 my $is_optional = shift;
160 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
161 return if ($err == 0);
162 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
163 return if ($err == 0);
165 add_package($prog, $is_optional);
168 sub check_rpm_missing($$)
171 my $is_optional = $_[1];
173 foreach my $prog(@pkgs) {
174 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
175 add_package($prog, $is_optional) if ($err);
179 sub check_pacman_missing($$)
182 my $is_optional = $_[1];
184 foreach my $prog(@pkgs) {
185 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
186 add_package($prog, $is_optional) if ($err);
190 sub check_missing_tex($)
192 my $is_optional = shift;
193 my $kpsewhich = findprog("kpsewhich");
195 foreach my $prog(keys %texlive) {
196 my $package = $texlive{$prog};
198 add_package($package, $is_optional);
201 my $file = qx($kpsewhich $prog);
202 add_package($package, $is_optional) if ($file =~ /^\s*$/);
206 sub get_sphinx_fname()
208 my $fname = "sphinx-build";
209 return $fname if findprog($fname);
211 $fname = "sphinx-build-3";
212 if (findprog($fname)) {
218 my $prog = findprog("virtualenv-3");
219 $prog = findprog("virtualenv-3.5") if (!$prog);
221 check_program("virtualenv", 0) if (!$prog);
224 add_package("python-sphinx", 0);
236 open IN, $conf or die "Can't open $conf";
238 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
245 die "Can't get needs_sphinx version from $conf" if (!$min_version);
247 open IN, $requirement_file or die "Can't open $requirement_file";
249 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
256 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
258 $virtenv_dir .= $rec_version;
260 my $sphinx = get_sphinx_fname();
261 return if ($sphinx eq "");
263 open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
265 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
269 # Sphinx 1.2.x uses a different format
270 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
277 die "$sphinx didn't return its version" if (!$cur_version);
279 printf "Sphinx version %s (minimal: %s, recommended >= %s)\n",
280 $cur_version, $min_version, $rec_version;
282 if ($cur_version lt $min_version) {
283 print "Warning: Sphinx version should be >= $min_version\n\n";
288 if ($cur_version lt $rec_version) {
289 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
290 print " To upgrade, use:\n\n";
291 $rec_sphinx_upgrade = 1;
296 # Ancillary subroutines
302 $res = qx(cat $_[0]) if (-r $_[0]);
309 my @path = split ":", $ENV{PATH};
311 foreach my $dir(@path) {
312 my $name = $dir.'/'.$file;
313 return $name if (-x $name );
319 # Subroutines that check distro-specific hints
322 sub give_debian_hints()
325 "python-sphinx" => "python3-sphinx",
326 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
327 "virtualenv" => "virtualenv",
329 "convert" => "imagemagick",
330 "Pod::Usage" => "perl-modules",
331 "xelatex" => "texlive-xetex",
332 "rsvg-convert" => "librsvg2-bin",
336 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
340 check_program("dvipng", 1) if ($pdf);
341 check_missing(\%map);
343 return if (!$need && !$optional);
344 printf("You should run:\n\n\tsudo apt-get install $install\n");
347 sub give_redhat_hints()
350 "python-sphinx" => "python3-sphinx",
351 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
352 "virtualenv" => "python3-virtualenv",
354 "convert" => "ImageMagick",
355 "Pod::Usage" => "perl-Pod-Usage",
356 "xelatex" => "texlive-xetex-bin",
357 "rsvg-convert" => "librsvg2-tools",
360 my @fedora26_opt_pkgs = (
361 "graphviz-gd", # Fedora 26: needed for PDF support
364 my @fedora_tex_pkgs = (
365 "texlive-collection-fontsrecommended",
366 "texlive-collection-latex",
368 "dejavu-serif-fonts",
369 "dejavu-sans-mono-fonts",
373 # Checks valid for RHEL/CentOS version 7.x.
375 if (! $system_release =~ /Fedora/) {
376 $map{"virtualenv"} = "python-virtualenv";
381 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
383 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
384 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
385 check_missing_tex(1) if ($pdf);
386 check_missing(\%map);
388 return if (!$need && !$optional);
390 if ($release >= 18) {
391 # dnf, for Fedora 18+
392 printf("You should run:\n\n\tsudo dnf install -y $install\n");
394 # yum, for RHEL (and clones) or Fedora version < 18
395 printf("You should run:\n\n\tsudo yum install -y $install\n");
399 sub give_opensuse_hints()
402 "python-sphinx" => "python3-sphinx",
403 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
404 "virtualenv" => "python3-virtualenv",
406 "convert" => "ImageMagick",
407 "Pod::Usage" => "perl-Pod-Usage",
408 "xelatex" => "texlive-xetex-bin",
409 "rsvg-convert" => "rsvg-view",
412 my @suse_tex_pkgs = (
413 "texlive-babel-english",
429 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
430 check_missing_tex(1) if ($pdf);
431 check_missing(\%map);
433 return if (!$need && !$optional);
434 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
437 sub give_mageia_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",
447 "rsvg-convert" => "librsvg2-tools",
451 "texlive-fontsextra",
454 check_rpm_missing(\@tex_pkgs, 1) if ($pdf);
455 check_missing(\%map);
457 return if (!$need && !$optional);
458 printf("You should run:\n\n\tsudo urpmi $install\n");
461 sub give_arch_linux_hints()
464 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
465 "virtualenv" => "python-virtualenv",
467 "convert" => "imagemagick",
468 "xelatex" => "texlive-bin",
469 "rsvg-convert" => "extra/librsvg",
472 my @archlinux_tex_pkgs = (
474 "texlive-latexextra",
477 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
478 check_missing(\%map);
480 return if (!$need && !$optional);
481 printf("You should run:\n\n\tsudo pacman -S $install\n");
484 sub give_gentoo_hints()
487 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
488 "virtualenv" => "dev-python/virtualenv",
489 "dot" => "media-gfx/graphviz",
490 "convert" => "media-gfx/imagemagick",
491 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
492 "rsvg-convert" => "gnome-base/librsvg",
495 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
496 "media-fonts/dejavu", 1) if ($pdf);
498 check_missing(\%map);
500 return if (!$need && !$optional);
502 printf("You should run:\n\n");
503 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
504 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
505 printf("\tsudo emerge --ask $install\n");
511 # Distro-specific hints
512 if ($system_release =~ /Red Hat Enterprise Linux/) {
516 if ($system_release =~ /CentOS/) {
520 if ($system_release =~ /Scientific Linux/) {
524 if ($system_release =~ /Oracle Linux Server/) {
528 if ($system_release =~ /Fedora/) {
532 if ($system_release =~ /Ubuntu/) {
536 if ($system_release =~ /Debian/) {
540 if ($system_release =~ /openSUSE/) {
544 if ($system_release =~ /Mageia/) {
548 if ($system_release =~ /Arch Linux/) {
549 give_arch_linux_hints;
552 if ($system_release =~ /Gentoo/) {
558 # Fall-back to generic hint code for other distros
559 # That's far from ideal, specially for LaTeX dependencies.
562 "sphinx-build" => "sphinx"
564 check_missing_tex(1) if ($pdf);
565 check_missing(\%map);
566 print "I don't know distro $system_release.\n";
567 print "So, I can't provide you a hint with the install procedure.\n";
568 print "There are likely missing dependencies.\n";
572 # Common dependencies
577 if ($system_release) {
578 print "Detected OS: $system_release.\n";
580 print "Unknown OS\n";
583 # RHEL 7.x and clones have Sphinx version 1.1.x and incomplete texlive
584 if (($system_release =~ /Red Hat Enterprise Linux/) ||
585 ($system_release =~ /CentOS/) ||
586 ($system_release =~ /Scientific Linux/) ||
587 ($system_release =~ /Oracle Linux Server/)) {
591 printf("NOTE: On this distro, Sphinx and TexLive shipped versions are incompatible\n");
592 printf("with doc build. So, use Sphinx via a Python virtual environment.\n\n");
593 printf("This script can't install a TexLive version that would provide PDF.\n");
596 # Check for needed programs/tools
598 check_perl_module("Pod::Usage", 0);
599 check_program("make", 0);
600 check_program("gcc", 0);
601 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
602 check_program("xelatex", 1) if ($pdf);
603 check_program("dot", 1);
604 check_program("convert", 1);
605 check_program("rsvg-convert", 1) if ($pdf);
606 check_program("latexmk", 1) if ($pdf);
611 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
612 which("sphinx-build-3");
614 if ($need_sphinx || $rec_sphinx_upgrade) {
615 my $activate = "$virtenv_dir/bin/activate";
616 if (-e "$ENV{'PWD'}/$activate") {
617 printf "\nNeed to activate virtualenv with:\n";
618 printf "\t. $activate\n";
620 my $virtualenv = findprog("virtualenv-3");
621 $virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
622 $virtualenv = findprog("virtualenv") if (!$virtualenv);
623 $virtualenv = "virtualenv" if (!$virtualenv);
625 printf "\t$virtualenv $virtenv_dir\n";
626 printf "\t. $activate\n";
627 printf "\tpip install -r $requirement_file\n";
629 $need++ if (!$rec_sphinx_upgrade);
634 print "All optional dependenties are met.\n" if (!$optional);
637 die "Can't build as $need mandatory dependency is missing";
639 die "Can't build as $need mandatory dependencies are missing";
642 print "Needed package dependencies are met.\n";
650 my $arg = shift(@ARGV);
652 if ($arg eq "--no-virtualenv") {
654 } elsif ($arg eq "--no-pdf"){
657 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
663 # Determine the system type. There's no standard unique way that would
664 # work with all distros with a minimal package install. So, several
665 # methods are used here.
667 # By default, it will use lsb_release function. If not available, it will
668 # fail back to reading the known different places where the distro name
672 $system_release = qx(lsb_release -d) if which("lsb_release");
673 $system_release =~ s/Description:\s*// if ($system_release);
674 $system_release = catcheck("/etc/system-release") if !$system_release;
675 $system_release = catcheck("/etc/redhat-release") if !$system_release;
676 $system_release = catcheck("/etc/lsb-release") if !$system_release;
677 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
678 $system_release = catcheck("/etc/issue") if !$system_release;
679 $system_release =~ s/\s+$//;