Merge tag 'mtd/fixes-for-5.2-final' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-2.6-arm.git] / scripts / sphinx-pre-install
blob9be208db88d3a8647fb7c01b8ae785f33e673510
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 use strict;
5 # Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
8 my $conf = "Documentation/conf.py";
9 my $requirement_file = "Documentation/sphinx/requirements.txt";
12 # Static vars
15 my %missing;
16 my $system_release;
17 my $need = 0;
18 my $optional = 0;
19 my $need_symlink = 0;
20 my $need_sphinx = 0;
21 my $rec_sphinx_upgrade = 0;
22 my $install = "";
23 my $virtenv_dir = "sphinx_";
26 # Command line arguments
29 my $pdf = 1;
30 my $virtualenv = 1;
33 # List of required texlive packages on Fedora and OpenSuse
36 my %texlive = (
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
73 sub check_missing(%)
75 my %map = %{$_[0]};
77 foreach my $prog (sort keys %missing) {
78 my $is_optional = $missing{$prog};
80 if ($is_optional) {
81 print "Warning: better to also install \"$prog\".\n";
82 } else {
83 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
85 if (defined($map{$prog})) {
86 $install .= " " . $map{$prog};
87 } else {
88 $install .= " " . $prog;
92 $install =~ s/^\s//;
95 sub add_package($$)
97 my $package = shift;
98 my $is_optional = shift;
100 $missing{$package} = $is_optional;
101 if ($is_optional) {
102 $optional++;
103 } else {
104 $need++;
108 sub check_missing_file($$$)
110 my $file = shift;
111 my $package = shift;
112 my $is_optional = shift;
114 return if(-e $file);
116 add_package($package, $is_optional);
119 sub findprog($)
121 foreach(split(/:/, $ENV{PATH})) {
122 return "$_/$_[0]" if(-x "$_/$_[0]");
126 sub check_program($$)
128 my $prog = shift;
129 my $is_optional = shift;
131 return if findprog($prog);
133 add_package($prog, $is_optional);
136 sub check_perl_module($$)
138 my $prog = shift;
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($$)
149 my $prog = shift;
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($$)
162 my @pkgs = @{$_[0]};
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($$)
173 my @pkgs = @{$_[0]};
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};
189 if (!$kpsewhich) {
190 add_package($package, $is_optional);
191 next;
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)) {
205 $need_symlink = 1;
206 return $fname;
209 if ($virtualenv) {
210 my $prog = findprog("virtualenv-3");
211 $prog = findprog("virtualenv-3.5") if (!$prog);
213 check_program("virtualenv", 0) if (!$prog);
214 $need_sphinx = 1;
215 } else {
216 add_package("python-sphinx", 0);
219 return "";
222 sub check_sphinx()
224 my $min_version;
225 my $rec_version;
226 my $cur_version;
228 open IN, $conf or die "Can't open $conf";
229 while (<IN>) {
230 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
231 $min_version=$1;
232 last;
235 close IN;
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";
240 while (<IN>) {
241 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
242 $rec_version=$1;
243 last;
246 close IN;
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";
256 while (<IN>) {
257 if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
258 $cur_version=$1;
259 last;
261 # Sphinx 1.2.x uses a different format
262 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
263 $cur_version=$1;
264 last;
267 close IN;
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";
276 $need_sphinx = 1;
277 return;
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
291 sub catcheck($)
293 my $res = "";
294 $res = qx(cat $_[0]) if (-r $_[0]);
295 return $res;
298 sub which($)
300 my $file = shift;
301 my @path = split ":", $ENV{PATH};
303 foreach my $dir(@path) {
304 my $name = $dir.'/'.$file;
305 return $name if (-x $name );
307 return undef;
311 # Subroutines that check distro-specific hints
314 sub give_debian_hints()
316 my %map = (
317 "python-sphinx" => "python3-sphinx",
318 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
319 "virtualenv" => "virtualenv",
320 "dot" => "graphviz",
321 "convert" => "imagemagick",
322 "Pod::Usage" => "perl-modules",
323 "xelatex" => "texlive-xetex",
324 "rsvg-convert" => "librsvg2-bin",
327 if ($pdf) {
328 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
329 "fonts-dejavu", 1);
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()
341 my %map = (
342 "python-sphinx" => "python3-sphinx",
343 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
344 "virtualenv" => "python3-virtualenv",
345 "dot" => "graphviz",
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",
359 "dejavu-sans-fonts",
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";
371 my $release;
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");
385 } else {
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()
393 my %map = (
394 "python-sphinx" => "python3-sphinx",
395 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
396 "virtualenv" => "python3-virtualenv",
397 "dot" => "graphviz",
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",
406 "texlive-caption",
407 "texlive-colortbl",
408 "texlive-courier",
409 "texlive-dvips",
410 "texlive-helvetic",
411 "texlive-makeindex",
412 "texlive-metafont",
413 "texlive-metapost",
414 "texlive-palatino",
415 "texlive-preview",
416 "texlive-times",
417 "texlive-zapfchan",
418 "texlive-zapfding",
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()
431 my %map = (
432 "python-sphinx" => "python3-sphinx",
433 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
434 "virtualenv" => "python3-virtualenv",
435 "dot" => "graphviz",
436 "convert" => "ImageMagick",
437 "Pod::Usage" => "perl-Pod-Usage",
438 "xelatex" => "texlive",
439 "rsvg-convert" => "librsvg2-tools",
442 my @tex_pkgs = (
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()
455 my %map = (
456 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
457 "virtualenv" => "python-virtualenv",
458 "dot" => "graphviz",
459 "convert" => "imagemagick",
460 "xelatex" => "texlive-bin",
461 "rsvg-convert" => "extra/librsvg",
464 my @archlinux_tex_pkgs = (
465 "texlive-core",
466 "texlive-latexextra",
467 "ttf-dejavu",
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()
478 my %map = (
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");
501 sub check_distros()
503 # Distro-specific hints
504 if ($system_release =~ /Red Hat Enterprise Linux/) {
505 give_redhat_hints;
506 return;
508 if ($system_release =~ /CentOS/) {
509 give_redhat_hints;
510 return;
512 if ($system_release =~ /Scientific Linux/) {
513 give_redhat_hints;
514 return;
516 if ($system_release =~ /Oracle Linux Server/) {
517 give_redhat_hints;
518 return;
520 if ($system_release =~ /Fedora/) {
521 give_redhat_hints;
522 return;
524 if ($system_release =~ /Ubuntu/) {
525 give_debian_hints;
526 return;
528 if ($system_release =~ /Debian/) {
529 give_debian_hints;
530 return;
532 if ($system_release =~ /openSUSE/) {
533 give_opensuse_hints;
534 return;
536 if ($system_release =~ /Mageia/) {
537 give_mageia_hints;
538 return;
540 if ($system_release =~ /Arch Linux/) {
541 give_arch_linux_hints;
542 return;
544 if ($system_release =~ /Gentoo/) {
545 give_gentoo_hints;
546 return;
550 # Fall-back to generic hint code for other distros
551 # That's far from ideal, specially for LaTeX dependencies.
553 my %map = (
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
567 sub check_needs()
569 if ($system_release) {
570 print "Detected OS: $system_release.\n";
571 } else {
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/)) {
580 $virtualenv = 1;
581 $pdf = 0;
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
589 check_sphinx();
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);
600 check_distros();
602 if ($need_symlink) {
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";
611 } else {
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);
624 printf "\n";
626 print "All optional dependenties are met.\n" if (!$optional);
628 if ($need == 1) {
629 die "Can't build as $need mandatory dependency is missing";
630 } elsif ($need) {
631 die "Can't build as $need mandatory dependencies are missing";
634 print "Needed package dependencies are met.\n";
638 # Main
641 while (@ARGV) {
642 my $arg = shift(@ARGV);
644 if ($arg eq "--no-virtualenv") {
645 $virtualenv = 0;
646 } elsif ($arg eq "--no-pdf"){
647 $pdf = 0;
648 } else {
649 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
650 exit -1;
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
661 # is stored
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+$//;
673 check_needs;