blob: 249edb3932af0afeb3590208a764e473e912287d [file] [log] [blame]
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -03001#!/usr/bin/perl
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02002# SPDX-License-Identifier: GPL-2.0-or-later
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -03003use strict;
4
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +02005# Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -03006#
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -03007
Mike Rapoport8c69b772019-06-24 08:25:07 +03008my $prefix = "./";
9$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
10
11my $conf = $prefix . "Documentation/conf.py";
12my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -030013my $virtenv_prefix = "sphinx_";
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -030014
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030015#
16# Static vars
17#
18
19my %missing;
20my $system_release;
21my $need = 0;
22my $optional = 0;
23my $need_symlink = 0;
24my $need_sphinx = 0;
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +020025my $need_venv = 0;
26my $need_virtualenv = 0;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -030027my $rec_sphinx_upgrade = 0;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030028my $install = "";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -030029my $virtenv_dir = "";
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +020030my $python_cmd = "";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -030031my $min_version;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030032
33#
34# Command line arguments
35#
36
37my $pdf = 1;
38my $virtualenv = 1;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -030039my $version_check = 0;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030040
41#
42# List of required texlive packages on Fedora and OpenSuse
43#
44
45my %texlive = (
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030046 'amsfonts.sty' => 'texlive-amsfonts',
47 'amsmath.sty' => 'texlive-amsmath',
48 'amssymb.sty' => 'texlive-amsfonts',
49 'amsthm.sty' => 'texlive-amscls',
50 'anyfontsize.sty' => 'texlive-anyfontsize',
51 'atbegshi.sty' => 'texlive-oberdiek',
52 'bm.sty' => 'texlive-tools',
53 'capt-of.sty' => 'texlive-capt-of',
54 'cmap.sty' => 'texlive-cmap',
55 'ecrm1000.tfm' => 'texlive-ec',
56 'eqparbox.sty' => 'texlive-eqparbox',
57 'eu1enc.def' => 'texlive-euenc',
58 'fancybox.sty' => 'texlive-fancybox',
59 'fancyvrb.sty' => 'texlive-fancyvrb',
60 'float.sty' => 'texlive-float',
61 'fncychap.sty' => 'texlive-fncychap',
62 'footnote.sty' => 'texlive-mdwtools',
63 'framed.sty' => 'texlive-framed',
64 'luatex85.sty' => 'texlive-luatex85',
65 'multirow.sty' => 'texlive-multirow',
66 'needspace.sty' => 'texlive-needspace',
67 'palatino.sty' => 'texlive-psnfss',
68 'parskip.sty' => 'texlive-parskip',
69 'polyglossia.sty' => 'texlive-polyglossia',
70 'tabulary.sty' => 'texlive-tabulary',
71 'threeparttable.sty' => 'texlive-threeparttable',
72 'titlesec.sty' => 'texlive-titlesec',
73 'ucs.sty' => 'texlive-ucs',
74 'upquote.sty' => 'texlive-upquote',
75 'wrapfig.sty' => 'texlive-wrapfig',
76);
77
78#
79# Subroutines that checks if a feature exists
80#
81
82sub check_missing(%)
83{
84 my %map = %{$_[0]};
85
86 foreach my $prog (sort keys %missing) {
87 my $is_optional = $missing{$prog};
88
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -030089 # At least on some LTS distros like CentOS 7, texlive doesn't
90 # provide all packages we need. When such distros are
91 # detected, we have to disable PDF output.
92 #
93 # So, we need to ignore the packages that distros would
94 # need for LaTeX to work
95 if ($is_optional == 2 && !$pdf) {
96 $optional--;
97 next;
98 }
99
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300100 if ($is_optional) {
101 print "Warning: better to also install \"$prog\".\n";
102 } else {
103 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
104 }
105 if (defined($map{$prog})) {
106 $install .= " " . $map{$prog};
107 } else {
108 $install .= " " . $prog;
109 }
110 }
111
112 $install =~ s/^\s//;
113}
114
115sub add_package($$)
116{
117 my $package = shift;
118 my $is_optional = shift;
119
120 $missing{$package} = $is_optional;
121 if ($is_optional) {
122 $optional++;
123 } else {
124 $need++;
125 }
126}
127
128sub check_missing_file($$$)
129{
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200130 my $files = shift;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300131 my $package = shift;
132 my $is_optional = shift;
133
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200134 for (@$files) {
135 return if(-e $_);
136 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300137
138 add_package($package, $is_optional);
139}
140
141sub findprog($)
142{
143 foreach(split(/:/, $ENV{PATH})) {
144 return "$_/$_[0]" if(-x "$_/$_[0]");
145 }
146}
147
148sub check_program($$)
149{
150 my $prog = shift;
151 my $is_optional = shift;
152
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200153 return $prog if findprog($prog);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300154
155 add_package($prog, $is_optional);
156}
157
158sub check_perl_module($$)
159{
160 my $prog = shift;
161 my $is_optional = shift;
162
163 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
164 return if ($err == 0);
165
166 add_package($prog, $is_optional);
167}
168
169sub check_python_module($$)
170{
171 my $prog = shift;
172 my $is_optional = shift;
173
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200174 return if (!$python_cmd);
175
176 my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300177 return if ($err == 0);
178
179 add_package($prog, $is_optional);
180}
181
182sub check_rpm_missing($$)
183{
184 my @pkgs = @{$_[0]};
185 my $is_optional = $_[1];
186
187 foreach my $prog(@pkgs) {
188 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
189 add_package($prog, $is_optional) if ($err);
190 }
191}
192
193sub check_pacman_missing($$)
194{
195 my @pkgs = @{$_[0]};
196 my $is_optional = $_[1];
197
198 foreach my $prog(@pkgs) {
199 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
200 add_package($prog, $is_optional) if ($err);
201 }
202}
203
204sub check_missing_tex($)
205{
206 my $is_optional = shift;
207 my $kpsewhich = findprog("kpsewhich");
208
209 foreach my $prog(keys %texlive) {
210 my $package = $texlive{$prog};
211 if (!$kpsewhich) {
212 add_package($package, $is_optional);
213 next;
214 }
215 my $file = qx($kpsewhich $prog);
216 add_package($package, $is_optional) if ($file =~ /^\s*$/);
217 }
218}
219
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300220sub get_sphinx_fname()
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300221{
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300222 my $fname = "sphinx-build";
223 return $fname if findprog($fname);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300224
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300225 $fname = "sphinx-build-3";
226 if (findprog($fname)) {
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300227 $need_symlink = 1;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300228 return $fname;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300229 }
230
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300231 return "";
232}
233
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200234sub get_sphinx_version($)
235{
236 my $cmd = shift;
237 my $ver;
238
239 open IN, "$cmd --version 2>&1 |";
240 while (<IN>) {
241 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
242 $ver=$1;
243 last;
244 }
245 # Sphinx 1.2.x uses a different format
246 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
247 $ver=$1;
248 last;
249 }
250 }
251 close IN;
252 return $ver;
253}
254
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300255sub check_sphinx()
256{
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300257 my $rec_version;
258 my $cur_version;
259
260 open IN, $conf or die "Can't open $conf";
261 while (<IN>) {
262 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
263 $min_version=$1;
264 last;
265 }
266 }
267 close IN;
268
269 die "Can't get needs_sphinx version from $conf" if (!$min_version);
270
271 open IN, $requirement_file or die "Can't open $requirement_file";
272 while (<IN>) {
273 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
274 $rec_version=$1;
275 last;
276 }
277 }
278 close IN;
279
280 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
281
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300282 $virtenv_dir = $virtenv_prefix . $rec_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300283
284 my $sphinx = get_sphinx_fname();
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200285 if ($sphinx eq "") {
286 $need_sphinx = 1;
287 return;
288 }
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300289
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200290 $cur_version = get_sphinx_version($sphinx);
291 die ("$sphinx returned an error") if (!$cur_version);
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300292
293 die "$sphinx didn't return its version" if (!$cur_version);
294
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300295 if ($cur_version lt $min_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300296 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
297 $cur_version, $min_version, $rec_version;;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300298 $need_sphinx = 1;
299 return;
300 }
301
302 if ($cur_version lt $rec_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300303 printf "Sphinx version %s\n", $cur_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300304 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300305 $rec_sphinx_upgrade = 1;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300306 return;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300307 }
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300308
309 # On version check mode, just assume Sphinx has all mandatory deps
310 exit (0) if ($version_check);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300311}
312
313#
314# Ancillary subroutines
315#
316
317sub catcheck($)
318{
319 my $res = "";
320 $res = qx(cat $_[0]) if (-r $_[0]);
321 return $res;
322}
323
324sub which($)
325{
326 my $file = shift;
327 my @path = split ":", $ENV{PATH};
328
329 foreach my $dir(@path) {
330 my $name = $dir.'/'.$file;
331 return $name if (-x $name );
332 }
333 return undef;
334}
335
336#
337# Subroutines that check distro-specific hints
338#
339
340sub give_debian_hints()
341{
342 my %map = (
343 "python-sphinx" => "python3-sphinx",
344 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200345 "ensurepip" => "python3-venv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300346 "virtualenv" => "virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300347 "dot" => "graphviz",
348 "convert" => "imagemagick",
349 "Pod::Usage" => "perl-modules",
350 "xelatex" => "texlive-xetex",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300351 "rsvg-convert" => "librsvg2-bin",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300352 );
353
354 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200355 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300356 "fonts-dejavu", 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300357
Jeremy MAURO9692f2f2019-10-02 15:35:42 +0200358 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
Mauro Carvalho Chehabbfc7f422020-04-14 18:56:10 +0200359 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
360 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300361 "fonts-noto-cjk", 2);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300362 }
363
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300364 check_program("dvipng", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300365 check_missing(\%map);
366
367 return if (!$need && !$optional);
368 printf("You should run:\n\n\tsudo apt-get install $install\n");
369}
370
371sub give_redhat_hints()
372{
373 my %map = (
374 "python-sphinx" => "python3-sphinx",
375 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
376 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300377 "dot" => "graphviz",
378 "convert" => "ImageMagick",
379 "Pod::Usage" => "perl-Pod-Usage",
380 "xelatex" => "texlive-xetex-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300381 "rsvg-convert" => "librsvg2-tools",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300382 );
383
Mauro Carvalho Chehab5d889532017-07-17 18:46:39 -0300384 my @fedora26_opt_pkgs = (
385 "graphviz-gd", # Fedora 26: needed for PDF support
386 );
387
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300388 my @fedora_tex_pkgs = (
389 "texlive-collection-fontsrecommended",
390 "texlive-collection-latex",
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300391 "texlive-xecjk",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300392 "dejavu-sans-fonts",
393 "dejavu-serif-fonts",
394 "dejavu-sans-mono-fonts",
395 );
396
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300397 #
398 # Checks valid for RHEL/CentOS version 7.x.
399 #
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300400 my $old = 0;
401 my $rel;
402 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
403
Mauro Carvalho Chehabb3084672019-07-13 08:50:24 -0300404 if (!($system_release =~ /Fedora/)) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300405 $map{"virtualenv"} = "python-virtualenv";
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300406
407 if ($rel && $rel < 8) {
408 $old = 1;
409 $pdf = 0;
410
411 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
412 printf("If you want to build PDF, please read:\n");
413 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
414 }
415 } else {
416 if ($rel && $rel < 26) {
417 $old = 1;
418 }
419 }
420 if (!$rel) {
421 printf("Couldn't identify release number\n");
422 $old = 1;
423 $pdf = 0;
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300424 }
425
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300426 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200427 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300428 "google-noto-sans-cjk-ttc-fonts", 2);
429 }
430
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300431 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
432 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
433 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300434 check_missing(\%map);
435
436 return if (!$need && !$optional);
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300437
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300438 if (!$old) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300439 # dnf, for Fedora 18+
440 printf("You should run:\n\n\tsudo dnf install -y $install\n");
441 } else {
442 # yum, for RHEL (and clones) or Fedora version < 18
443 printf("You should run:\n\n\tsudo yum install -y $install\n");
444 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300445}
446
447sub give_opensuse_hints()
448{
449 my %map = (
450 "python-sphinx" => "python3-sphinx",
451 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
452 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300453 "dot" => "graphviz",
454 "convert" => "ImageMagick",
455 "Pod::Usage" => "perl-Pod-Usage",
456 "xelatex" => "texlive-xetex-bin",
457 );
458
Mauro Carvalho Chehabb3df6222020-04-14 18:56:09 +0200459 # On Tumbleweed, this package is also named rsvg-convert
460 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
461
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300462 my @suse_tex_pkgs = (
463 "texlive-babel-english",
464 "texlive-caption",
465 "texlive-colortbl",
466 "texlive-courier",
467 "texlive-dvips",
468 "texlive-helvetic",
469 "texlive-makeindex",
470 "texlive-metafont",
471 "texlive-metapost",
472 "texlive-palatino",
473 "texlive-preview",
474 "texlive-times",
475 "texlive-zapfchan",
476 "texlive-zapfding",
477 );
478
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300479 $map{"latexmk"} = "texlive-latexmk-bin";
480
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300481 # FIXME: add support for installing CJK fonts
482 #
483 # I tried hard, but was unable to find a way to install
484 # "Noto Sans CJK SC" on openSUSE
485
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300486 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
487 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300488 check_missing(\%map);
489
490 return if (!$need && !$optional);
491 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
492}
493
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300494sub give_mageia_hints()
495{
496 my %map = (
497 "python-sphinx" => "python3-sphinx",
498 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
499 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300500 "dot" => "graphviz",
501 "convert" => "ImageMagick",
502 "Pod::Usage" => "perl-Pod-Usage",
503 "xelatex" => "texlive",
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200504 "rsvg-convert" => "librsvg2",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300505 );
506
507 my @tex_pkgs = (
508 "texlive-fontsextra",
509 );
510
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300511 $map{"latexmk"} = "texlive-collection-basic";
512
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200513 my $packager_cmd;
514 my $noto_sans;
515 if ($system_release =~ /OpenMandriva/) {
516 $packager_cmd = "dnf install";
517 $noto_sans = "noto-sans-cjk-fonts";
518 @tex_pkgs = ( "texlive-collection-fontsextra" );
519 } else {
520 $packager_cmd = "urpmi";
521 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
522 }
523
524
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300525 if ($pdf) {
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200526 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
527 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
528 $noto_sans, 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300529 }
530
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300531 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300532 check_missing(\%map);
533
534 return if (!$need && !$optional);
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200535 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300536}
537
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300538sub give_arch_linux_hints()
539{
540 my %map = (
541 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
542 "virtualenv" => "python-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300543 "dot" => "graphviz",
544 "convert" => "imagemagick",
545 "xelatex" => "texlive-bin",
Louis Taylor0d0da9a2019-11-02 18:45:11 +0000546 "latexmk" => "texlive-core",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300547 "rsvg-convert" => "extra/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300548 );
549
550 my @archlinux_tex_pkgs = (
551 "texlive-core",
552 "texlive-latexextra",
553 "ttf-dejavu",
554 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300555 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
556
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300557 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200558 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300559 "noto-fonts-cjk", 2);
560 }
561
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300562 check_missing(\%map);
563
564 return if (!$need && !$optional);
565 printf("You should run:\n\n\tsudo pacman -S $install\n");
566}
567
568sub give_gentoo_hints()
569{
570 my %map = (
571 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
572 "virtualenv" => "dev-python/virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300573 "dot" => "media-gfx/graphviz",
574 "convert" => "media-gfx/imagemagick",
575 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300576 "rsvg-convert" => "gnome-base/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300577 );
578
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200579 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300580 "media-fonts/dejavu", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300581
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300582 if ($pdf) {
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200583 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
584 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300585 "media-fonts/noto-cjk", 2);
586 }
587
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300588 check_missing(\%map);
589
590 return if (!$need && !$optional);
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300591
592 printf("You should run:\n\n");
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300593
594 my $imagemagick = "media-gfx/imagemagick svg png";
595 my $cairo = "media-gfx/graphviz cairo pdf";
596 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
597 my $portage_cairo = "/etc/portage/package.use/graphviz";
598
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200599 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300600 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
601 }
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200602 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300603 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
604 }
605
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300606 printf("\tsudo emerge --ask $install\n");
607
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300608}
609
610sub check_distros()
611{
612 # Distro-specific hints
613 if ($system_release =~ /Red Hat Enterprise Linux/) {
614 give_redhat_hints;
615 return;
616 }
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300617 if ($system_release =~ /CentOS/) {
618 give_redhat_hints;
619 return;
620 }
621 if ($system_release =~ /Scientific Linux/) {
622 give_redhat_hints;
623 return;
624 }
625 if ($system_release =~ /Oracle Linux Server/) {
626 give_redhat_hints;
627 return;
628 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300629 if ($system_release =~ /Fedora/) {
630 give_redhat_hints;
631 return;
632 }
633 if ($system_release =~ /Ubuntu/) {
634 give_debian_hints;
635 return;
636 }
637 if ($system_release =~ /Debian/) {
638 give_debian_hints;
639 return;
640 }
641 if ($system_release =~ /openSUSE/) {
642 give_opensuse_hints;
643 return;
644 }
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300645 if ($system_release =~ /Mageia/) {
646 give_mageia_hints;
647 return;
648 }
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200649 if ($system_release =~ /OpenMandriva/) {
650 give_mageia_hints;
651 return;
652 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300653 if ($system_release =~ /Arch Linux/) {
654 give_arch_linux_hints;
655 return;
656 }
657 if ($system_release =~ /Gentoo/) {
658 give_gentoo_hints;
659 return;
660 }
661
662 #
663 # Fall-back to generic hint code for other distros
664 # That's far from ideal, specially for LaTeX dependencies.
665 #
666 my %map = (
667 "sphinx-build" => "sphinx"
668 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300669 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300670 check_missing(\%map);
671 print "I don't know distro $system_release.\n";
672 print "So, I can't provide you a hint with the install procedure.\n";
673 print "There are likely missing dependencies.\n";
674}
675
676#
677# Common dependencies
678#
679
Shuah Khan2730ce02019-09-18 18:37:54 -0600680sub deactivate_help()
681{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200682 printf "\nIf you want to exit the virtualenv, you can use:\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600683 printf "\tdeactivate\n";
684}
685
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300686sub check_needs()
687{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200688 # Check if Sphinx is already accessible from current environment
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300689 check_sphinx();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300690
691 if ($system_release) {
692 print "Detected OS: $system_release.\n\n";
693 } else {
694 print "Unknown OS\n\n";
695 }
696
697 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
698
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200699 # Check python command line, trying first python3
700 $python_cmd = findprog("python3");
701 $python_cmd = check_program("python", 0) if (!$python_cmd);
702
703 # Check the type of virtual env, depending on Python version
704 if ($python_cmd) {
705 if ($virtualenv) {
706 my $tmp = qx($python_cmd --version 2>&1);
707 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
708 if ($1 >= 3 && $2 >= 3) {
709 $need_venv = 1; # python 3.3 or upper
710 } else {
711 $need_virtualenv = 1;
712 }
713 if ($1 < 3) {
714 # Complain if it finds python2 (or worse)
715 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
716 }
717 } else {
718 die "Warning: couldn't identify $python_cmd version!";
719 }
720 } else {
721 add_package("python-sphinx", 0);
722 }
723 }
724
725 # Set virtualenv command line, if python < 3.3
726 my $virtualenv_cmd;
727 if ($need_virtualenv) {
728 $virtualenv_cmd = findprog("virtualenv-3");
729 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
730 if (!$virtualenv_cmd) {
731 check_program("virtualenv", 0);
732 $virtualenv_cmd = "virtualenv";
733 }
734 }
735
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300736 # Check for needed programs/tools
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300737 check_perl_module("Pod::Usage", 0);
738 check_program("make", 0);
739 check_program("gcc", 0);
740 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300741 check_program("dot", 1);
742 check_program("convert", 1);
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300743
744 # Extra PDF files - should use 2 for is_optional
745 check_program("xelatex", 2) if ($pdf);
746 check_program("rsvg-convert", 2) if ($pdf);
747 check_program("latexmk", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300748
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200749 if ($need_sphinx || $rec_sphinx_upgrade) {
750 check_python_module("ensurepip", 0) if ($need_venv);
751 }
752
753 # Do distro-specific checks and output distro-install commands
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300754 check_distros();
755
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200756 if (!$python_cmd) {
757 if ($need == 1) {
758 die "Can't build as $need mandatory dependency is missing";
759 } elsif ($need) {
760 die "Can't build as $need mandatory dependencies are missing";
761 }
762 }
763
764 # Check if sphinx-build is called sphinx-build-3
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300765 if ($need_symlink) {
766 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
767 which("sphinx-build-3");
768 }
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200769
770 # NOTE: if the system has a too old Sphinx version installed,
771 # it will recommend installing a newer version using virtualenv
772
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300773 if ($need_sphinx || $rec_sphinx_upgrade) {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300774 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300775 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300776
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300777 @activates = sort {$b cmp $a} @activates;
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200778 my ($activate, $ver);
779 foreach my $f (@activates) {
780 $activate = $f;
781 next if ($activate lt $min_activate);
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300782
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200783 my $sphinx_cmd = $activate;
784 $sphinx_cmd =~ s/activate/sphinx-build/;
785 next if (! -f $sphinx_cmd);
786
787 $ver = get_sphinx_version($sphinx_cmd);
788 last if ($ver ge $min_version);
789 }
790 if ($need_sphinx && ($activate ne "")) {
791 printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
792 printf "\t. $activate\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600793 deactivate_help();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300794 exit (1);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300795 } else {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300796 my $rec_activate = "$virtenv_dir/bin/activate";
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300797
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200798 if ($need_venv) {
799 printf "\t$python_cmd -m venv $virtenv_dir\n";
800 } else {
801 printf "\t$virtualenv_cmd $virtenv_dir\n";
Tim Birdc428cd52020-02-24 18:34:41 -0700802 }
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300803 printf "\t. $rec_activate\n";
Mauro Carvalho Chehabfb947f32017-07-17 18:46:38 -0300804 printf "\tpip install -r $requirement_file\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600805 deactivate_help();
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300806
807 $need++ if (!$rec_sphinx_upgrade);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300808 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300809 }
810 printf "\n";
811
Bjorn Helgaas54002b52019-05-30 16:59:14 -0500812 print "All optional dependencies are met.\n" if (!$optional);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300813
814 if ($need == 1) {
815 die "Can't build as $need mandatory dependency is missing";
816 } elsif ($need) {
817 die "Can't build as $need mandatory dependencies are missing";
818 }
819
820 print "Needed package dependencies are met.\n";
821}
822
823#
824# Main
825#
826
827while (@ARGV) {
828 my $arg = shift(@ARGV);
829
830 if ($arg eq "--no-virtualenv") {
831 $virtualenv = 0;
832 } elsif ($arg eq "--no-pdf"){
833 $pdf = 0;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300834 } elsif ($arg eq "--version-check"){
835 $version_check = 1;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300836 } else {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300837 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
838 print "Where:\n";
839 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
840 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
841 print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300842 exit -1;
843 }
844}
845
846#
847# Determine the system type. There's no standard unique way that would
848# work with all distros with a minimal package install. So, several
849# methods are used here.
850#
851# By default, it will use lsb_release function. If not available, it will
852# fail back to reading the known different places where the distro name
853# is stored
854#
855
856$system_release = qx(lsb_release -d) if which("lsb_release");
857$system_release =~ s/Description:\s*// if ($system_release);
858$system_release = catcheck("/etc/system-release") if !$system_release;
859$system_release = catcheck("/etc/redhat-release") if !$system_release;
860$system_release = catcheck("/etc/lsb-release") if !$system_release;
861$system_release = catcheck("/etc/gentoo-release") if !$system_release;
Mauro Carvalho Chehabd14d0c12020-04-14 18:56:08 +0200862
863# This seems more common than LSB these days
864if (!$system_release) {
865 my %os_var;
866 if (open IN, "cat /etc/os-release|") {
867 while (<IN>) {
868 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
869 $os_var{$1}=$2;
870 }
871 }
872 $system_release = $os_var{"NAME"};
873 if (defined($os_var{"VERSION_ID"})) {
874 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
875 } else {
876 $system_release .= " " . $os_var{"VERSION"};
877 }
878 }
879}
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300880$system_release = catcheck("/etc/issue") if !$system_release;
881$system_release =~ s/\s+$//;
882
883check_needs;