sphinx-pre-install: add dependencies for ImageMagick to work with svg
[linux-block.git] / scripts / sphinx-pre-install
CommitLineData
24071ac1
MCC
1#!/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
5be33182 16my $virtenv_dir = "sphinx_1.4";
fb947f3f 17my $requirement_file = "Documentation/sphinx/requirements.txt";
5be33182 18
24071ac1
MCC
19#
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) {
215 check_program("virtualenv", 0) if (!findprog("virtualenv-3"));
216 check_program("pip", 0) if (!findprog("pip3"));
217 $need_sphinx = 1;
218 } else {
219 add_package("python-sphinx", 0);
220 }
221}
222
223#
224# Ancillary subroutines
225#
226
227sub catcheck($)
228{
229 my $res = "";
230 $res = qx(cat $_[0]) if (-r $_[0]);
231 return $res;
232}
233
234sub which($)
235{
236 my $file = shift;
237 my @path = split ":", $ENV{PATH};
238
239 foreach my $dir(@path) {
240 my $name = $dir.'/'.$file;
241 return $name if (-x $name );
242 }
243 return undef;
244}
245
246#
247# Subroutines that check distro-specific hints
248#
249
250sub give_debian_hints()
251{
252 my %map = (
253 "python-sphinx" => "python3-sphinx",
254 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
255 "virtualenv" => "virtualenv",
256 "pip" => "python3-pip",
257 "dot" => "graphviz",
258 "convert" => "imagemagick",
259 "Pod::Usage" => "perl-modules",
260 "xelatex" => "texlive-xetex",
8e7d5d15 261 "rsvg-convert" => "librsvg2-bin",
24071ac1
MCC
262 );
263
264 if ($pdf) {
265 check_missing_file("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
266 "fonts-dejavu", 1);
267 }
268
269 check_program("dvipng", 1) if ($pdf);
270 check_missing(\%map);
271
272 return if (!$need && !$optional);
273 printf("You should run:\n\n\tsudo apt-get install $install\n");
274}
275
276sub give_redhat_hints()
277{
278 my %map = (
279 "python-sphinx" => "python3-sphinx",
280 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
281 "virtualenv" => "python3-virtualenv",
282 "pip" => "python3-pip",
283 "dot" => "graphviz",
284 "convert" => "ImageMagick",
285 "Pod::Usage" => "perl-Pod-Usage",
286 "xelatex" => "texlive-xetex-bin",
8e7d5d15 287 "rsvg-convert" => "librsvg2-tools",
24071ac1
MCC
288 );
289
5d88953c
MCC
290 my @fedora26_opt_pkgs = (
291 "graphviz-gd", # Fedora 26: needed for PDF support
292 );
293
24071ac1
MCC
294 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
5d88953c
MCC
302 my $release;
303
304 $release = $1 if ($system_release =~ /Fedora\s+release\s+(\d+)/);
305
306 check_rpm_missing(\@fedora26_opt_pkgs, 1) if ($pdf && $release >= 26);
24071ac1
MCC
307 check_rpm_missing(\@fedora_tex_pkgs, 1) if ($pdf);
308 check_missing_tex(1) if ($pdf);
309 check_missing(\%map);
310
311 return if (!$need && !$optional);
312 printf("You should run:\n\n\tsudo dnf install -y $install\n");
313}
314
315sub give_opensuse_hints()
316{
317 my %map = (
318 "python-sphinx" => "python3-sphinx",
319 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
320 "virtualenv" => "python3-virtualenv",
321 "pip" => "python3-pip",
322 "dot" => "graphviz",
323 "convert" => "ImageMagick",
324 "Pod::Usage" => "perl-Pod-Usage",
325 "xelatex" => "texlive-xetex-bin",
8e7d5d15 326 "rsvg-convert" => "rsvg-view",
24071ac1
MCC
327 );
328
329 my @suse_tex_pkgs = (
330 "texlive-babel-english",
331 "texlive-caption",
332 "texlive-colortbl",
333 "texlive-courier",
334 "texlive-dvips",
335 "texlive-helvetic",
336 "texlive-makeindex",
337 "texlive-metafont",
338 "texlive-metapost",
339 "texlive-palatino",
340 "texlive-preview",
341 "texlive-times",
342 "texlive-zapfchan",
343 "texlive-zapfding",
344 );
345
346 check_rpm_missing(\@suse_tex_pkgs, 1) if ($pdf);
347 check_missing_tex(1) if ($pdf);
348 check_missing(\%map);
349
350 return if (!$need && !$optional);
351 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
352}
353
354sub give_arch_linux_hints()
355{
356 my %map = (
357 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
358 "virtualenv" => "python-virtualenv",
359 "pip" => "python-pip",
360 "dot" => "graphviz",
361 "convert" => "imagemagick",
362 "xelatex" => "texlive-bin",
8e7d5d15 363 "rsvg-convert" => "extra/librsvg",
24071ac1
MCC
364 );
365
366 my @archlinux_tex_pkgs = (
367 "texlive-core",
368 "texlive-latexextra",
369 "ttf-dejavu",
370 );
371 check_pacman_missing(\@archlinux_tex_pkgs, 1) if ($pdf);
372 check_missing(\%map);
373
374 return if (!$need && !$optional);
375 printf("You should run:\n\n\tsudo pacman -S $install\n");
376}
377
378sub give_gentoo_hints()
379{
380 my %map = (
381 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
382 "virtualenv" => "dev-python/virtualenv",
383 "pip" => "dev-python/pip",
384 "dot" => "media-gfx/graphviz",
385 "convert" => "media-gfx/imagemagick",
386 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
8e7d5d15 387 "rsvg-convert" => "gnome-base/librsvg",
24071ac1
MCC
388 );
389
390 check_missing_file("/usr/share/fonts/dejavu/DejaVuSans.ttf",
391 "media-fonts/dejavu", 1) if ($pdf);
392
393 check_missing(\%map);
394
395 return if (!$need && !$optional);
396 printf("You should run:\n\n\tsudo emerge --ask $install\n");
397}
398
399sub check_distros()
400{
401 # Distro-specific hints
402 if ($system_release =~ /Red Hat Enterprise Linux/) {
403 give_redhat_hints;
404 return;
405 }
406 if ($system_release =~ /Fedora/) {
407 give_redhat_hints;
408 return;
409 }
410 if ($system_release =~ /Ubuntu/) {
411 give_debian_hints;
412 return;
413 }
414 if ($system_release =~ /Debian/) {
415 give_debian_hints;
416 return;
417 }
418 if ($system_release =~ /openSUSE/) {
419 give_opensuse_hints;
420 return;
421 }
422 if ($system_release =~ /Arch Linux/) {
423 give_arch_linux_hints;
424 return;
425 }
426 if ($system_release =~ /Gentoo/) {
427 give_gentoo_hints;
428 return;
429 }
430
431 #
432 # Fall-back to generic hint code for other distros
433 # That's far from ideal, specially for LaTeX dependencies.
434 #
435 my %map = (
436 "sphinx-build" => "sphinx"
437 );
438 check_missing_tex(1) if ($pdf);
439 check_missing(\%map);
440 print "I don't know distro $system_release.\n";
441 print "So, I can't provide you a hint with the install procedure.\n";
442 print "There are likely missing dependencies.\n";
443}
444
445#
446# Common dependencies
447#
448
449sub check_needs()
450{
451 if ($system_release) {
452 print "Checking if the needed tools for $system_release are available\n";
453 } else {
454 print "Checking if the needed tools are present\n";
455 }
456
457 # Check for needed programs/tools
458 check_sphinx();
459 check_perl_module("Pod::Usage", 0);
460 check_program("make", 0);
461 check_program("gcc", 0);
462 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
463 check_program("xelatex", 1) if ($pdf);
464 check_program("dot", 1);
465 check_program("convert", 1);
8e7d5d15 466 check_program("rsvg-convert", 1) if ($pdf);
24071ac1
MCC
467
468 check_distros();
469
470 if ($need_symlink) {
471 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
472 which("sphinx-build-3");
473 }
474 if ($need_sphinx) {
5be33182
MCC
475 my $activate = "$virtenv_dir/bin/activate";
476 if (-e "$ENV{'PWD'}/$activate") {
477 printf "\nNeed to activate virtualenv with:\n";
478 printf "\t. $activate\n";
479 } else {
480 my $virtualenv = findprog("virtualenv-3");
481 $virtualenv = findprog("virtualenv") if (!$virtualenv);
482 $virtualenv = "virtualenv" if (!$virtualenv);
483
484 printf "\t$virtualenv $virtenv_dir\n";
485 printf "\t. $activate\n";
fb947f3f 486 printf "\tpip install -r $requirement_file\n";
5be33182
MCC
487 $need++;
488 }
24071ac1
MCC
489 }
490 printf "\n";
491
492 print "All optional dependenties are met.\n" if (!$optional);
493
494 if ($need == 1) {
495 die "Can't build as $need mandatory dependency is missing";
496 } elsif ($need) {
497 die "Can't build as $need mandatory dependencies are missing";
498 }
499
500 print "Needed package dependencies are met.\n";
501}
502
503#
504# Main
505#
506
507while (@ARGV) {
508 my $arg = shift(@ARGV);
509
510 if ($arg eq "--no-virtualenv") {
511 $virtualenv = 0;
512 } elsif ($arg eq "--no-pdf"){
513 $pdf = 0;
514 } else {
515 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
516 exit -1;
517 }
518}
519
520#
521# Determine the system type. There's no standard unique way that would
522# work with all distros with a minimal package install. So, several
523# methods are used here.
524#
525# By default, it will use lsb_release function. If not available, it will
526# fail back to reading the known different places where the distro name
527# is stored
528#
529
530$system_release = qx(lsb_release -d) if which("lsb_release");
531$system_release =~ s/Description:\s*// if ($system_release);
532$system_release = catcheck("/etc/system-release") if !$system_release;
533$system_release = catcheck("/etc/redhat-release") if !$system_release;
534$system_release = catcheck("/etc/lsb-release") if !$system_release;
535$system_release = catcheck("/etc/gentoo-release") if !$system_release;
536$system_release = catcheck("/etc/issue") if !$system_release;
537$system_release =~ s/\s+$//;
538
539check_needs;