blob: 677756ae34c9f230950dea21e46a5bd4b5c403f8 [file] [log] [blame]
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -03001#!/usr/bin/perl
2use strict;
3
4# Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
5#
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.
10#
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.
15
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -030016my $virtenv_dir = "sphinx_1.4";
Mauro Carvalho Chehabfb947f32017-07-17 18:46:38 -030017my $requirement_file = "Documentation/sphinx/requirements.txt";
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -030018
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030019#
20# Static vars
21#
22
23my %missing;
24my $system_release;
25my $need = 0;
26my $optional = 0;
27my $need_symlink = 0;
28my $need_sphinx = 0;
29my $install = "";
30
31#
32# Command line arguments
33#
34
35my $pdf = 1;
36my $virtualenv = 1;
37
38#
39# List of required texlive packages on Fedora and OpenSuse
40#
41
42my %texlive = (
43 'adjustbox.sty' => 'texlive-adjustbox',
44 'amsfonts.sty' => 'texlive-amsfonts',
45 'amsmath.sty' => 'texlive-amsmath',
46 'amssymb.sty' => 'texlive-amsfonts',
47 'amsthm.sty' => 'texlive-amscls',
48 'anyfontsize.sty' => 'texlive-anyfontsize',
49 'atbegshi.sty' => 'texlive-oberdiek',
50 'bm.sty' => 'texlive-tools',
51 'capt-of.sty' => 'texlive-capt-of',
52 'cmap.sty' => 'texlive-cmap',
53 'ecrm1000.tfm' => 'texlive-ec',
54 'eqparbox.sty' => 'texlive-eqparbox',
55 'eu1enc.def' => 'texlive-euenc',
56 'fancybox.sty' => 'texlive-fancybox',
57 'fancyvrb.sty' => 'texlive-fancyvrb',
58 'float.sty' => 'texlive-float',
59 'fncychap.sty' => 'texlive-fncychap',
60 'footnote.sty' => 'texlive-mdwtools',
61 'framed.sty' => 'texlive-framed',
62 'luatex85.sty' => 'texlive-luatex85',
63 'multirow.sty' => 'texlive-multirow',
64 'needspace.sty' => 'texlive-needspace',
65 'palatino.sty' => 'texlive-psnfss',
66 'parskip.sty' => 'texlive-parskip',
67 'polyglossia.sty' => 'texlive-polyglossia',
68 'tabulary.sty' => 'texlive-tabulary',
69 'threeparttable.sty' => 'texlive-threeparttable',
70 'titlesec.sty' => 'texlive-titlesec',
71 'ucs.sty' => 'texlive-ucs',
72 'upquote.sty' => 'texlive-upquote',
73 'wrapfig.sty' => 'texlive-wrapfig',
74);
75
76#
77# Subroutines that checks if a feature exists
78#
79
80sub check_missing(%)
81{
82 my %map = %{$_[0]};
83
84 foreach my $prog (sort keys %missing) {
85 my $is_optional = $missing{$prog};
86
87 if ($is_optional) {
88 print "Warning: better to also install \"$prog\".\n";
89 } else {
90 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
91 }
92 if (defined($map{$prog})) {
93 $install .= " " . $map{$prog};
94 } else {
95 $install .= " " . $prog;
96 }
97 }
98
99 $install =~ s/^\s//;
100}
101
102sub add_package($$)
103{
104 my $package = shift;
105 my $is_optional = shift;
106
107 $missing{$package} = $is_optional;
108 if ($is_optional) {
109 $optional++;
110 } else {
111 $need++;
112 }
113}
114
115sub check_missing_file($$$)
116{
117 my $file = shift;
118 my $package = shift;
119 my $is_optional = shift;
120
121 return if(-e $file);
122
123 add_package($package, $is_optional);
124}
125
126sub findprog($)
127{
128 foreach(split(/:/, $ENV{PATH})) {
129 return "$_/$_[0]" if(-x "$_/$_[0]");
130 }
131}
132
133sub check_program($$)
134{
135 my $prog = shift;
136 my $is_optional = shift;
137
138 return if findprog($prog);
139
140 add_package($prog, $is_optional);
141}
142
143sub check_perl_module($$)
144{
145 my $prog = shift;
146 my $is_optional = shift;
147
148 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
149 return if ($err == 0);
150
151 add_package($prog, $is_optional);
152}
153
154sub check_python_module($$)
155{
156 my $prog = shift;
157 my $is_optional = shift;
158
159 my $err = system("python3 -c 'import $prog' 2>/dev/null /dev/null");
160 return if ($err == 0);
161 my $err = system("python -c 'import $prog' 2>/dev/null /dev/null");
162 return if ($err == 0);
163
164 add_package($prog, $is_optional);
165}
166
167sub check_rpm_missing($$)
168{
169 my @pkgs = @{$_[0]};
170 my $is_optional = $_[1];
171
172 foreach my $prog(@pkgs) {
173 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
174 add_package($prog, $is_optional) if ($err);
175 }
176}
177
178sub check_pacman_missing($$)
179{
180 my @pkgs = @{$_[0]};
181 my $is_optional = $_[1];
182
183 foreach my $prog(@pkgs) {
184 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
185 add_package($prog, $is_optional) if ($err);
186 }
187}
188
189sub check_missing_tex($)
190{
191 my $is_optional = shift;
192 my $kpsewhich = findprog("kpsewhich");
193
194 foreach my $prog(keys %texlive) {
195 my $package = $texlive{$prog};
196 if (!$kpsewhich) {
197 add_package($package, $is_optional);
198 next;
199 }
200 my $file = qx($kpsewhich $prog);
201 add_package($package, $is_optional) if ($file =~ /^\s*$/);
202 }
203}
204
205sub check_sphinx()
206{
207 return if findprog("sphinx-build");
208
209 if (findprog("sphinx-build-3")) {
210 $need_symlink = 1;
211 return;
212 }
213
214 if ($virtualenv) {
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300215 my $prog = findprog("virtualenv-3");
216 $prog = findprog("virtualenv-3.5") if (!$prog);
217
218 check_program("virtualenv", 0) if (!$prog);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300219 $need_sphinx = 1;
220 } else {
221 add_package("python-sphinx", 0);
222 }
223}
224
225#
226# Ancillary subroutines
227#
228
229sub catcheck($)
230{
231 my $res = "";
232 $res = qx(cat $_[0]) if (-r $_[0]);
233 return $res;
234}
235
236sub which($)
237{
238 my $file = shift;
239 my @path = split ":", $ENV{PATH};
240
241 foreach my $dir(@path) {
242 my $name = $dir.'/'.$file;
243 return $name if (-x $name );
244 }
245 return undef;
246}
247
248#
249# Subroutines that check distro-specific hints
250#
251
252sub give_debian_hints()
253{
254 my %map = (
255 "python-sphinx" => "python3-sphinx",
256 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
257 "virtualenv" => "virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300258 "dot" => "graphviz",
259 "convert" => "imagemagick",
260 "Pod::Usage" => "perl-modules",
261 "xelatex" => "texlive-xetex",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300262 "rsvg-convert" => "librsvg2-bin",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300263 );
264
265 if ($pdf) {
266 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
267 "fonts-dejavu", 1);
268 }
269
270 check_program("dvipng", 1) if ($pdf);
271 check_missing(\%map);
272
273 return if (!$need && !$optional);
274 printf("You should run:\n\n\tsudo apt-get install $install\n");
275}
276
277sub give_redhat_hints()
278{
279 my %map = (
280 "python-sphinx" => "python3-sphinx",
281 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
282 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300283 "dot" => "graphviz",
284 "convert" => "ImageMagick",
285 "Pod::Usage" => "perl-Pod-Usage",
286 "xelatex" => "texlive-xetex-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300287 "rsvg-convert" => "librsvg2-tools",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300288 );
289
Mauro Carvalho Chehab5d889532017-07-17 18:46:39 -0300290 my @fedora26_opt_pkgs = (
291 "graphviz-gd", # Fedora 26: needed for PDF support
292 );
293
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300294 my @fedora_tex_pkgs = (
295 "texlive-collection-fontsrecommended",
296 "texlive-collection-latex",
297 "dejavu-sans-fonts",
298 "dejavu-serif-fonts",
299 "dejavu-sans-mono-fonts",
300 );
301
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300302 #
303 # Checks valid for RHEL/CentOS version 7.x.
304 #
305 if (! $system_release =~ /Fedora/) {
306 $map{"virtualenv"} = "python-virtualenv";
307 }
308
Mauro Carvalho Chehab5d889532017-07-17 18:46:39 -0300309 my $release;
310
311 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
312
313 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300314 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
315 check_missing_tex(1) if ($pdf);
316 check_missing(\%map);
317
318 return if (!$need && !$optional);
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300319
320 if ($release >= 18) {
321 # dnf, for Fedora 18+
322 printf("You should run:\n\n\tsudo dnf install -y $install\n");
323 } else {
324 # yum, for RHEL (and clones) or Fedora version < 18
325 printf("You should run:\n\n\tsudo yum install -y $install\n");
326 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300327}
328
329sub give_opensuse_hints()
330{
331 my %map = (
332 "python-sphinx" => "python3-sphinx",
333 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
334 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300335 "dot" => "graphviz",
336 "convert" => "ImageMagick",
337 "Pod::Usage" => "perl-Pod-Usage",
338 "xelatex" => "texlive-xetex-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300339 "rsvg-convert" => "rsvg-view",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300340 );
341
342 my @suse_tex_pkgs = (
343 "texlive-babel-english",
344 "texlive-caption",
345 "texlive-colortbl",
346 "texlive-courier",
347 "texlive-dvips",
348 "texlive-helvetic",
349 "texlive-makeindex",
350 "texlive-metafont",
351 "texlive-metapost",
352 "texlive-palatino",
353 "texlive-preview",
354 "texlive-times",
355 "texlive-zapfchan",
356 "texlive-zapfding",
357 );
358
359 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
360 check_missing_tex(1) if ($pdf);
361 check_missing(\%map);
362
363 return if (!$need && !$optional);
364 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
365}
366
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300367sub give_mageia_hints()
368{
369 my %map = (
370 "python-sphinx" => "python3-sphinx",
371 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
372 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300373 "dot" => "graphviz",
374 "convert" => "ImageMagick",
375 "Pod::Usage" => "perl-Pod-Usage",
376 "xelatex" => "texlive",
377 "rsvg-convert" => "librsvg2-tools",
378 );
379
380 my @tex_pkgs = (
381 "texlive-fontsextra",
382 );
383
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300384 check_rpm_missing(\@tex_pkgs, 1) if ($pdf);
385 check_missing(\%map);
386
387 return if (!$need && !$optional);
388 printf("You should run:\n\n\tsudo urpmi $install\n");
389}
390
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300391sub give_arch_linux_hints()
392{
393 my %map = (
394 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
395 "virtualenv" => "python-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300396 "dot" => "graphviz",
397 "convert" => "imagemagick",
398 "xelatex" => "texlive-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300399 "rsvg-convert" => "extra/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300400 );
401
402 my @archlinux_tex_pkgs = (
403 "texlive-core",
404 "texlive-latexextra",
405 "ttf-dejavu",
406 );
407 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
408 check_missing(\%map);
409
410 return if (!$need && !$optional);
411 printf("You should run:\n\n\tsudo pacman -S $install\n");
412}
413
414sub give_gentoo_hints()
415{
416 my %map = (
417 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
418 "virtualenv" => "dev-python/virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300419 "dot" => "media-gfx/graphviz",
420 "convert" => "media-gfx/imagemagick",
421 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300422 "rsvg-convert" => "gnome-base/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300423 );
424
425 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
426 "media-fonts/dejavu", 1) if ($pdf);
427
428 check_missing(\%map);
429
430 return if (!$need && !$optional);
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300431
432 printf("You should run:\n\n");
433 printf("\tsudo su -c 'echo \"media-gfx/imagemagick svg png\" > /etc/portage/package.use/imagemagick'\n");
434 printf("\tsudo su -c 'echo \"media-gfx/graphviz cairo pdf\" > /etc/portage/package.use/graphviz'\n");
435 printf("\tsudo emerge --ask $install\n");
436
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300437}
438
439sub check_distros()
440{
441 # Distro-specific hints
442 if ($system_release =~ /Red Hat Enterprise Linux/) {
443 give_redhat_hints;
444 return;
445 }
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300446 if ($system_release =~ /CentOS/) {
447 give_redhat_hints;
448 return;
449 }
450 if ($system_release =~ /Scientific Linux/) {
451 give_redhat_hints;
452 return;
453 }
454 if ($system_release =~ /Oracle Linux Server/) {
455 give_redhat_hints;
456 return;
457 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300458 if ($system_release =~ /Fedora/) {
459 give_redhat_hints;
460 return;
461 }
462 if ($system_release =~ /Ubuntu/) {
463 give_debian_hints;
464 return;
465 }
466 if ($system_release =~ /Debian/) {
467 give_debian_hints;
468 return;
469 }
470 if ($system_release =~ /openSUSE/) {
471 give_opensuse_hints;
472 return;
473 }
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300474 if ($system_release =~ /Mageia/) {
475 give_mageia_hints;
476 return;
477 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300478 if ($system_release =~ /Arch Linux/) {
479 give_arch_linux_hints;
480 return;
481 }
482 if ($system_release =~ /Gentoo/) {
483 give_gentoo_hints;
484 return;
485 }
486
487 #
488 # Fall-back to generic hint code for other distros
489 # That's far from ideal, specially for LaTeX dependencies.
490 #
491 my %map = (
492 "sphinx-build" => "sphinx"
493 );
494 check_missing_tex(1) if ($pdf);
495 check_missing(\%map);
496 print "I don't know distro $system_release.\n";
497 print "So, I can't provide you a hint with the install procedure.\n";
498 print "There are likely missing dependencies.\n";
499}
500
501#
502# Common dependencies
503#
504
505sub check_needs()
506{
507 if ($system_release) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300508 print "Detected OS: $system_release.\n";
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300509 } else {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300510 print "Unknown OS\n";
511 }
512
513 # RHEL 7.x and clones have Sphinx version 1.1.x and incomplete texlive
514 if (($system_release =~ /Red Hat Enterprise Linux/) ||
515 ($system_release =~ /CentOS/) ||
516 ($system_release =~ /Scientific Linux/) ||
517 ($system_release =~ /Oracle Linux Server/)) {
518 $virtualenv = 1;
519 $pdf = 0;
520
521 printf("NOTE: On this distro, Sphinx and TexLive shipped versions are incompatible\n");
522 printf("with doc build. So, use Sphinx via a Python virtual environment.\n\n");
523 printf("This script can't install a TexLive version that would provide PDF.\n");
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300524 }
525
526 # Check for needed programs/tools
527 check_sphinx();
528 check_perl_module("Pod::Usage", 0);
529 check_program("make", 0);
530 check_program("gcc", 0);
531 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
532 check_program("xelatex", 1) if ($pdf);
533 check_program("dot", 1);
534 check_program("convert", 1);
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300535 check_program("rsvg-convert", 1) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300536
537 check_distros();
538
539 if ($need_symlink) {
540 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
541 which("sphinx-build-3");
542 }
543 if ($need_sphinx) {
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300544 my $activate = "$virtenv_dir/bin/activate";
545 if (-e "$ENV{'PWD'}/$activate") {
546 printf "\nNeed to activate virtualenv with:\n";
547 printf "\t. $activate\n";
548 } else {
549 my $virtualenv = findprog("virtualenv-3");
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300550 $virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300551 $virtualenv = findprog("virtualenv") if (!$virtualenv);
552 $virtualenv = "virtualenv" if (!$virtualenv);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300553
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300554 printf "\t$virtualenv $virtenv_dir\n";
555 printf "\t. $activate\n";
Mauro Carvalho Chehabfb947f32017-07-17 18:46:38 -0300556 printf "\tpip install -r $requirement_file\n";
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300557 $need++;
558 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300559 }
560 printf "\n";
561
562 print "All optional dependenties are met.\n" if (!$optional);
563
564 if ($need == 1) {
565 die "Can't build as $need mandatory dependency is missing";
566 } elsif ($need) {
567 die "Can't build as $need mandatory dependencies are missing";
568 }
569
570 print "Needed package dependencies are met.\n";
571}
572
573#
574# Main
575#
576
577while (@ARGV) {
578 my $arg = shift(@ARGV);
579
580 if ($arg eq "--no-virtualenv") {
581 $virtualenv = 0;
582 } elsif ($arg eq "--no-pdf"){
583 $pdf = 0;
584 } else {
585 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
586 exit -1;
587 }
588}
589
590#
591# Determine the system type. There's no standard unique way that would
592# work with all distros with a minimal package install. So, several
593# methods are used here.
594#
595# By default, it will use lsb_release function. If not available, it will
596# fail back to reading the known different places where the distro name
597# is stored
598#
599
600$system_release = qx(lsb_release -d) if which("lsb_release");
601$system_release =~ s/Description:\s*// if ($system_release);
602$system_release = catcheck("/etc/system-release") if !$system_release;
603$system_release = catcheck("/etc/redhat-release") if !$system_release;
604$system_release = catcheck("/etc/lsb-release") if !$system_release;
605$system_release = catcheck("/etc/gentoo-release") if !$system_release;
606$system_release = catcheck("/etc/issue") if !$system_release;
607$system_release =~ s/\s+$//;
608
609check_needs;