4 # Copyright (c) 2017-2019 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";
18 my $virtenv_prefix = "sphinx_";
30 my $rec_sphinx_upgrade = 0;
36 # Command line arguments
43 # List of required texlive packages on Fedora and OpenSuse
47 'amsfonts.sty' => 'texlive-amsfonts',
48 'amsmath.sty' => 'texlive-amsmath',
49 'amssymb.sty' => 'texlive-amsfonts',
50 'amsthm.sty' => 'texlive-amscls',
51 'anyfontsize.sty' => 'texlive-anyfontsize',
52 'atbegshi.sty' => 'texlive-oberdiek',
53 'bm.sty' => 'texlive-tools',
54 'capt-of.sty' => 'texlive-capt-of',
55 'cmap.sty' => 'texlive-cmap',
56 'ecrm1000.tfm' => 'texlive-ec',
57 'eqparbox.sty' => 'texlive-eqparbox',
58 'eu1enc.def' => 'texlive-euenc',
59 'fancybox.sty' => 'texlive-fancybox',
60 'fancyvrb.sty' => 'texlive-fancyvrb',
61 'float.sty' => 'texlive-float',
62 'fncychap.sty' => 'texlive-fncychap',
63 'footnote.sty' => 'texlive-mdwtools',
64 'framed.sty' => 'texlive-framed',
65 'luatex85.sty' => 'texlive-luatex85',
66 'multirow.sty' => 'texlive-multirow',
67 'needspace.sty' => 'texlive-needspace',
68 'palatino.sty' => 'texlive-psnfss',
69 'parskip.sty' => 'texlive-parskip',
70 'polyglossia.sty' => 'texlive-polyglossia',
71 'tabulary.sty' => 'texlive-tabulary',
72 'threeparttable.sty' => 'texlive-threeparttable',
73 'titlesec.sty' => 'texlive-titlesec',
74 'ucs.sty' => 'texlive-ucs',
75 'upquote.sty' => 'texlive-upquote',
76 'wrapfig.sty' => 'texlive-wrapfig',
80 # Subroutines that checks if a feature exists
87 foreach my $prog (sort keys %missing) {
88 my $is_optional = $missing{$prog};
91 print "Warning: better to also install \"$prog\".\n";
93 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
95 if (defined($map{$prog})) {
96 $install .= " " . $map{$prog};
98 $install .= " " . $prog;
108 my $is_optional = shift;
110 $missing{$package} = $is_optional;
118 sub check_missing_file($$$)
122 my $is_optional = shift;
126 add_package($package, $is_optional);
131 foreach(split(/:/, $ENV{PATH})) {
132 return "$_/$_[0]" if(-x "$_/$_[0]");
136 sub check_program($$)
139 my $is_optional = shift;
141 return if findprog($prog);
143 add_package($prog, $is_optional);
146 sub check_perl_module($$)
149 my $is_optional = shift;
151 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
152 return if ($err == 0);
154 add_package($prog, $is_optional);
157 sub check_python_module($$)
160 my $is_optional = shift;
162 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
163 return if ($err == 0);
164 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
165 return if ($err == 0);
167 add_package($prog, $is_optional);
170 sub check_rpm_missing($$)
173 my $is_optional = $_[1];
175 foreach my $prog(@pkgs) {
176 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
177 add_package($prog, $is_optional) if ($err);
181 sub check_pacman_missing($$)
184 my $is_optional = $_[1];
186 foreach my $prog(@pkgs) {
187 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
188 add_package($prog, $is_optional) if ($err);
192 sub check_missing_tex($)
194 my $is_optional = shift;
195 my $kpsewhich = findprog("kpsewhich");
197 foreach my $prog(keys %texlive) {
198 my $package = $texlive{$prog};
200 add_package($package, $is_optional);
203 my $file = qx($kpsewhich $prog);
204 add_package($package, $is_optional) if ($file =~ /^\s*$/);
208 sub get_sphinx_fname()
210 my $fname = "sphinx-build";
211 return $fname if findprog($fname);
213 $fname = "sphinx-build-3";
214 if (findprog($fname)) {
220 my $prog = findprog("virtualenv-3");
221 $prog = findprog("virtualenv-3.5") if (!$prog);
223 check_program("virtualenv", 0) if (!$prog);
226 add_package("python-sphinx", 0);
237 open IN, $conf or die "Can't open $conf";
239 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
246 die "Can't get needs_sphinx version from $conf" if (!$min_version);
248 open IN, $requirement_file or die "Can't open $requirement_file";
250 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
257 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
259 $virtenv_dir = $virtenv_prefix . $rec_version;
261 my $sphinx = get_sphinx_fname();
262 return if ($sphinx eq "");
264 open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
266 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
270 # Sphinx 1.2.x uses a different format
271 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
278 die "$sphinx didn't return its version" if (!$cur_version);
280 printf "Sphinx version %s (minimal: %s, recommended >= %s)\n",
281 $cur_version, $min_version, $rec_version;
283 if ($cur_version lt $min_version) {
284 print "Warning: Sphinx version should be >= $min_version\n\n";
289 if ($cur_version lt $rec_version) {
290 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
291 print " To upgrade, use:\n\n";
292 $rec_sphinx_upgrade = 1;
297 # Ancillary subroutines
303 $res = qx(cat $_[0]) if (-r $_[0]);
310 my @path = split ":", $ENV{PATH};
312 foreach my $dir(@path) {
313 my $name = $dir.'/'.$file;
314 return $name if (-x $name );
320 # Subroutines that check distro-specific hints
323 sub give_debian_hints()
326 "python-sphinx" => "python3-sphinx",
327 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
328 "virtualenv" => "virtualenv",
330 "convert" => "imagemagick",
331 "Pod::Usage" => "perl-modules",
332 "xelatex" => "texlive-xetex",
333 "rsvg-convert" => "librsvg2-bin",
337 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
341 check_program("dvipng", 1) if ($pdf);
342 check_missing(\%map);
344 return if (!$need && !$optional);
345 printf("You should run:\n\n\tsudo apt-get install $install\n");
348 sub give_redhat_hints()
351 "python-sphinx" => "python3-sphinx",
352 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
353 "virtualenv" => "python3-virtualenv",
355 "convert" => "ImageMagick",
356 "Pod::Usage" => "perl-Pod-Usage",
357 "xelatex" => "texlive-xetex-bin",
358 "rsvg-convert" => "librsvg2-tools",
361 my @fedora26_opt_pkgs = (
362 "graphviz-gd", # Fedora 26: needed for PDF support
365 my @fedora_tex_pkgs = (
366 "texlive-collection-fontsrecommended",
367 "texlive-collection-latex",
369 "dejavu-serif-fonts",
370 "dejavu-sans-mono-fonts",
374 # Checks valid for RHEL/CentOS version 7.x.
376 if (! $system_release =~ /Fedora/) {
377 $map{"virtualenv"} = "python-virtualenv";
382 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
384 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
385 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
386 check_missing_tex(1) if ($pdf);
387 check_missing(\%map);
389 return if (!$need && !$optional);
391 if ($release >= 18) {
392 # dnf, for Fedora 18+
393 printf("You should run:\n\n\tsudo dnf install -y $install\n");
395 # yum, for RHEL (and clones) or Fedora version < 18
396 printf("You should run:\n\n\tsudo yum install -y $install\n");
400 sub give_opensuse_hints()
403 "python-sphinx" => "python3-sphinx",
404 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
405 "virtualenv" => "python3-virtualenv",
407 "convert" => "ImageMagick",
408 "Pod::Usage" => "perl-Pod-Usage",
409 "xelatex" => "texlive-xetex-bin",
410 "rsvg-convert" => "rsvg-view",
413 my @suse_tex_pkgs = (
414 "texlive-babel-english",
430 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
431 check_missing_tex(1) if ($pdf);
432 check_missing(\%map);
434 return if (!$need && !$optional);
435 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
438 sub give_mageia_hints()
441 "python-sphinx" => "python3-sphinx",
442 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
443 "virtualenv" => "python3-virtualenv",
445 "convert" => "ImageMagick",
446 "Pod::Usage" => "perl-Pod-Usage",
447 "xelatex" => "texlive",
448 "rsvg-convert" => "librsvg2-tools",
452 "texlive-fontsextra",
455 check_rpm_missing(\@tex_pkgs, 1) if ($pdf);
456 check_missing(\%map);
458 return if (!$need && !$optional);
459 printf("You should run:\n\n\tsudo urpmi $install\n");
462 sub give_arch_linux_hints()
465 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
466 "virtualenv" => "python-virtualenv",
468 "convert" => "imagemagick",
469 "xelatex" => "texlive-bin",
470 "rsvg-convert" => "extra/librsvg",
473 my @archlinux_tex_pkgs = (
475 "texlive-latexextra",
478 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
479 check_missing(\%map);
481 return if (!$need && !$optional);
482 printf("You should run:\n\n\tsudo pacman -S $install\n");
485 sub give_gentoo_hints()
488 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
489 "virtualenv" => "dev-python/virtualenv",
490 "dot" => "media-gfx/graphviz",
491 "convert" => "media-gfx/imagemagick",
492 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
493 "rsvg-convert" => "gnome-base/librsvg",
496 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
497 "media-fonts/dejavu", 1) if ($pdf);
499 check_missing(\%map);
501 return if (!$need && !$optional);
503 printf("You should run:\n\n");
504 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
505 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
506 printf("\tsudo emerge --ask $install\n");
512 # Distro-specific hints
513 if ($system_release =~ /Red Hat Enterprise Linux/) {
517 if ($system_release =~ /CentOS/) {
521 if ($system_release =~ /Scientific Linux/) {
525 if ($system_release =~ /Oracle Linux Server/) {
529 if ($system_release =~ /Fedora/) {
533 if ($system_release =~ /Ubuntu/) {
537 if ($system_release =~ /Debian/) {
541 if ($system_release =~ /openSUSE/) {
545 if ($system_release =~ /Mageia/) {
549 if ($system_release =~ /Arch Linux/) {
550 give_arch_linux_hints;
553 if ($system_release =~ /Gentoo/) {
559 # Fall-back to generic hint code for other distros
560 # That's far from ideal, specially for LaTeX dependencies.
563 "sphinx-build" => "sphinx"
565 check_missing_tex(1) if ($pdf);
566 check_missing(\%map);
567 print "I don't know distro $system_release.\n";
568 print "So, I can't provide you a hint with the install procedure.\n";
569 print "There are likely missing dependencies.\n";
573 # Common dependencies
578 if ($system_release) {
579 print "Detected OS: $system_release.\n";
581 print "Unknown OS\n";
584 # Check for needed programs/tools
586 check_perl_module("Pod::Usage", 0);
587 check_program("make", 0);
588 check_program("gcc", 0);
589 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
590 check_program("xelatex", 1) if ($pdf);
591 check_program("dot", 1);
592 check_program("convert", 1);
593 check_program("rsvg-convert", 1) if ($pdf);
594 check_program("latexmk", 1) if ($pdf);
599 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
600 which("sphinx-build-3");
602 if ($need_sphinx || $rec_sphinx_upgrade) {
603 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
604 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
606 @activates = sort {$b cmp $a} @activates;
608 if (scalar @activates > 0 && $activates[0] ge $min_activate) {
609 printf "\nNeed to activate virtualenv with:\n";
610 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 dependenties 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"){
650 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
656 # Determine the system type. There's no standard unique way that would
657 # work with all distros with a minimal package install. So, several
658 # methods are used here.
660 # By default, it will use lsb_release function. If not available, it will
661 # fail back to reading the known different places where the distro name
665 $system_release = qx(lsb_release -d) if which("lsb_release");
666 $system_release =~ s/Description:\s*// if ($system_release);
667 $system_release = catcheck("/etc/system-release") if !$system_release;
668 $system_release = catcheck("/etc/redhat-release") if !$system_release;
669 $system_release = catcheck("/etc/lsb-release") if !$system_release;
670 $system_release = catcheck("/etc/gentoo-release") if !$system_release;
671 $system_release = catcheck("/etc/issue") if !$system_release;
672 $system_release =~ s/\s+$//;