blob: d4dfe1e59989c0d965d568a1b5ba6aaead9d12da [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
234sub check_sphinx()
235{
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300236 my $rec_version;
237 my $cur_version;
238
239 open IN, $conf or die "Can't open $conf";
240 while (<IN>) {
241 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
242 $min_version=$1;
243 last;
244 }
245 }
246 close IN;
247
248 die "Can't get needs_sphinx version from $conf" if (!$min_version);
249
250 open IN, $requirement_file or die "Can't open $requirement_file";
251 while (<IN>) {
252 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
253 $rec_version=$1;
254 last;
255 }
256 }
257 close IN;
258
259 die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
260
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300261 $virtenv_dir = $virtenv_prefix . $rec_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300262
263 my $sphinx = get_sphinx_fname();
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200264 if ($sphinx eq "") {
265 $need_sphinx = 1;
266 return;
267 }
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300268
269 open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
270 while (<IN>) {
Stephen Kittd1c90382020-01-24 19:33:16 +0100271 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300272 $cur_version=$1;
273 last;
274 }
275 # Sphinx 1.2.x uses a different format
276 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
277 $cur_version=$1;
278 last;
279 }
280 }
281 close IN;
282
283 die "$sphinx didn't return its version" if (!$cur_version);
284
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300285 if ($cur_version lt $min_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300286 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
287 $cur_version, $min_version, $rec_version;;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300288 $need_sphinx = 1;
289 return;
290 }
291
292 if ($cur_version lt $rec_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300293 printf "Sphinx version %s\n", $cur_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300294 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300295 $rec_sphinx_upgrade = 1;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300296 return;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300297 }
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300298
299 # On version check mode, just assume Sphinx has all mandatory deps
300 exit (0) if ($version_check);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300301}
302
303#
304# Ancillary subroutines
305#
306
307sub catcheck($)
308{
309 my $res = "";
310 $res = qx(cat $_[0]) if (-r $_[0]);
311 return $res;
312}
313
314sub which($)
315{
316 my $file = shift;
317 my @path = split ":", $ENV{PATH};
318
319 foreach my $dir(@path) {
320 my $name = $dir.'/'.$file;
321 return $name if (-x $name );
322 }
323 return undef;
324}
325
326#
327# Subroutines that check distro-specific hints
328#
329
330sub give_debian_hints()
331{
332 my %map = (
333 "python-sphinx" => "python3-sphinx",
334 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200335 "ensurepip" => "python3-venv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300336 "virtualenv" => "virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300337 "dot" => "graphviz",
338 "convert" => "imagemagick",
339 "Pod::Usage" => "perl-modules",
340 "xelatex" => "texlive-xetex",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300341 "rsvg-convert" => "librsvg2-bin",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300342 );
343
344 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200345 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300346 "fonts-dejavu", 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300347
Jeremy MAURO9692f2f2019-10-02 15:35:42 +0200348 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
Mauro Carvalho Chehabbfc7f422020-04-14 18:56:10 +0200349 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
350 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300351 "fonts-noto-cjk", 2);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300352 }
353
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300354 check_program("dvipng", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300355 check_missing(\%map);
356
357 return if (!$need && !$optional);
358 printf("You should run:\n\n\tsudo apt-get install $install\n");
359}
360
361sub give_redhat_hints()
362{
363 my %map = (
364 "python-sphinx" => "python3-sphinx",
365 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
366 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300367 "dot" => "graphviz",
368 "convert" => "ImageMagick",
369 "Pod::Usage" => "perl-Pod-Usage",
370 "xelatex" => "texlive-xetex-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300371 "rsvg-convert" => "librsvg2-tools",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300372 );
373
Mauro Carvalho Chehab5d889532017-07-17 18:46:39 -0300374 my @fedora26_opt_pkgs = (
375 "graphviz-gd", # Fedora 26: needed for PDF support
376 );
377
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300378 my @fedora_tex_pkgs = (
379 "texlive-collection-fontsrecommended",
380 "texlive-collection-latex",
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300381 "texlive-xecjk",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300382 "dejavu-sans-fonts",
383 "dejavu-serif-fonts",
384 "dejavu-sans-mono-fonts",
385 );
386
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300387 #
388 # Checks valid for RHEL/CentOS version 7.x.
389 #
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300390 my $old = 0;
391 my $rel;
392 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
393
Mauro Carvalho Chehabb3084672019-07-13 08:50:24 -0300394 if (!($system_release =~ /Fedora/)) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300395 $map{"virtualenv"} = "python-virtualenv";
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300396
397 if ($rel && $rel < 8) {
398 $old = 1;
399 $pdf = 0;
400
401 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
402 printf("If you want to build PDF, please read:\n");
403 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
404 }
405 } else {
406 if ($rel && $rel < 26) {
407 $old = 1;
408 }
409 }
410 if (!$rel) {
411 printf("Couldn't identify release number\n");
412 $old = 1;
413 $pdf = 0;
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300414 }
415
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300416 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200417 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300418 "google-noto-sans-cjk-ttc-fonts", 2);
419 }
420
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300421 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
422 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
423 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300424 check_missing(\%map);
425
426 return if (!$need && !$optional);
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300427
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300428 if (!$old) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300429 # dnf, for Fedora 18+
430 printf("You should run:\n\n\tsudo dnf install -y $install\n");
431 } else {
432 # yum, for RHEL (and clones) or Fedora version < 18
433 printf("You should run:\n\n\tsudo yum install -y $install\n");
434 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300435}
436
437sub give_opensuse_hints()
438{
439 my %map = (
440 "python-sphinx" => "python3-sphinx",
441 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
442 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300443 "dot" => "graphviz",
444 "convert" => "ImageMagick",
445 "Pod::Usage" => "perl-Pod-Usage",
446 "xelatex" => "texlive-xetex-bin",
447 );
448
Mauro Carvalho Chehabb3df6222020-04-14 18:56:09 +0200449 # On Tumbleweed, this package is also named rsvg-convert
450 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
451
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300452 my @suse_tex_pkgs = (
453 "texlive-babel-english",
454 "texlive-caption",
455 "texlive-colortbl",
456 "texlive-courier",
457 "texlive-dvips",
458 "texlive-helvetic",
459 "texlive-makeindex",
460 "texlive-metafont",
461 "texlive-metapost",
462 "texlive-palatino",
463 "texlive-preview",
464 "texlive-times",
465 "texlive-zapfchan",
466 "texlive-zapfding",
467 );
468
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300469 $map{"latexmk"} = "texlive-latexmk-bin";
470
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300471 # FIXME: add support for installing CJK fonts
472 #
473 # I tried hard, but was unable to find a way to install
474 # "Noto Sans CJK SC" on openSUSE
475
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300476 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
477 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300478 check_missing(\%map);
479
480 return if (!$need && !$optional);
481 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
482}
483
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300484sub give_mageia_hints()
485{
486 my %map = (
487 "python-sphinx" => "python3-sphinx",
488 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
489 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300490 "dot" => "graphviz",
491 "convert" => "ImageMagick",
492 "Pod::Usage" => "perl-Pod-Usage",
493 "xelatex" => "texlive",
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200494 "rsvg-convert" => "librsvg2",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300495 );
496
497 my @tex_pkgs = (
498 "texlive-fontsextra",
499 );
500
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300501 $map{"latexmk"} = "texlive-collection-basic";
502
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200503 my $packager_cmd;
504 my $noto_sans;
505 if ($system_release =~ /OpenMandriva/) {
506 $packager_cmd = "dnf install";
507 $noto_sans = "noto-sans-cjk-fonts";
508 @tex_pkgs = ( "texlive-collection-fontsextra" );
509 } else {
510 $packager_cmd = "urpmi";
511 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
512 }
513
514
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300515 if ($pdf) {
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200516 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
517 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
518 $noto_sans, 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300519 }
520
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300521 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300522 check_missing(\%map);
523
524 return if (!$need && !$optional);
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200525 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300526}
527
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300528sub give_arch_linux_hints()
529{
530 my %map = (
531 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
532 "virtualenv" => "python-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300533 "dot" => "graphviz",
534 "convert" => "imagemagick",
535 "xelatex" => "texlive-bin",
Louis Taylor0d0da9a2019-11-02 18:45:11 +0000536 "latexmk" => "texlive-core",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300537 "rsvg-convert" => "extra/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300538 );
539
540 my @archlinux_tex_pkgs = (
541 "texlive-core",
542 "texlive-latexextra",
543 "ttf-dejavu",
544 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300545 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
546
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300547 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200548 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300549 "noto-fonts-cjk", 2);
550 }
551
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300552 check_missing(\%map);
553
554 return if (!$need && !$optional);
555 printf("You should run:\n\n\tsudo pacman -S $install\n");
556}
557
558sub give_gentoo_hints()
559{
560 my %map = (
561 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
562 "virtualenv" => "dev-python/virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300563 "dot" => "media-gfx/graphviz",
564 "convert" => "media-gfx/imagemagick",
565 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300566 "rsvg-convert" => "gnome-base/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300567 );
568
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200569 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300570 "media-fonts/dejavu", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300571
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300572 if ($pdf) {
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200573 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
574 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300575 "media-fonts/noto-cjk", 2);
576 }
577
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300578 check_missing(\%map);
579
580 return if (!$need && !$optional);
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300581
582 printf("You should run:\n\n");
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300583
584 my $imagemagick = "media-gfx/imagemagick svg png";
585 my $cairo = "media-gfx/graphviz cairo pdf";
586 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
587 my $portage_cairo = "/etc/portage/package.use/graphviz";
588
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200589 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300590 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
591 }
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200592 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300593 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
594 }
595
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300596 printf("\tsudo emerge --ask $install\n");
597
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300598}
599
600sub check_distros()
601{
602 # Distro-specific hints
603 if ($system_release =~ /Red Hat Enterprise Linux/) {
604 give_redhat_hints;
605 return;
606 }
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300607 if ($system_release =~ /CentOS/) {
608 give_redhat_hints;
609 return;
610 }
611 if ($system_release =~ /Scientific Linux/) {
612 give_redhat_hints;
613 return;
614 }
615 if ($system_release =~ /Oracle Linux Server/) {
616 give_redhat_hints;
617 return;
618 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300619 if ($system_release =~ /Fedora/) {
620 give_redhat_hints;
621 return;
622 }
623 if ($system_release =~ /Ubuntu/) {
624 give_debian_hints;
625 return;
626 }
627 if ($system_release =~ /Debian/) {
628 give_debian_hints;
629 return;
630 }
631 if ($system_release =~ /openSUSE/) {
632 give_opensuse_hints;
633 return;
634 }
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300635 if ($system_release =~ /Mageia/) {
636 give_mageia_hints;
637 return;
638 }
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200639 if ($system_release =~ /OpenMandriva/) {
640 give_mageia_hints;
641 return;
642 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300643 if ($system_release =~ /Arch Linux/) {
644 give_arch_linux_hints;
645 return;
646 }
647 if ($system_release =~ /Gentoo/) {
648 give_gentoo_hints;
649 return;
650 }
651
652 #
653 # Fall-back to generic hint code for other distros
654 # That's far from ideal, specially for LaTeX dependencies.
655 #
656 my %map = (
657 "sphinx-build" => "sphinx"
658 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300659 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300660 check_missing(\%map);
661 print "I don't know distro $system_release.\n";
662 print "So, I can't provide you a hint with the install procedure.\n";
663 print "There are likely missing dependencies.\n";
664}
665
666#
667# Common dependencies
668#
669
Shuah Khan2730ce02019-09-18 18:37:54 -0600670sub deactivate_help()
671{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200672 printf "\nIf you want to exit the virtualenv, you can use:\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600673 printf "\tdeactivate\n";
674}
675
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300676sub check_needs()
677{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200678 # Check if Sphinx is already accessible from current environment
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300679 check_sphinx();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300680
681 if ($system_release) {
682 print "Detected OS: $system_release.\n\n";
683 } else {
684 print "Unknown OS\n\n";
685 }
686
687 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
688
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200689 # Check python command line, trying first python3
690 $python_cmd = findprog("python3");
691 $python_cmd = check_program("python", 0) if (!$python_cmd);
692
693 # Check the type of virtual env, depending on Python version
694 if ($python_cmd) {
695 if ($virtualenv) {
696 my $tmp = qx($python_cmd --version 2>&1);
697 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
698 if ($1 >= 3 && $2 >= 3) {
699 $need_venv = 1; # python 3.3 or upper
700 } else {
701 $need_virtualenv = 1;
702 }
703 if ($1 < 3) {
704 # Complain if it finds python2 (or worse)
705 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
706 }
707 } else {
708 die "Warning: couldn't identify $python_cmd version!";
709 }
710 } else {
711 add_package("python-sphinx", 0);
712 }
713 }
714
715 # Set virtualenv command line, if python < 3.3
716 my $virtualenv_cmd;
717 if ($need_virtualenv) {
718 $virtualenv_cmd = findprog("virtualenv-3");
719 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
720 if (!$virtualenv_cmd) {
721 check_program("virtualenv", 0);
722 $virtualenv_cmd = "virtualenv";
723 }
724 }
725
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300726 # Check for needed programs/tools
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300727 check_perl_module("Pod::Usage", 0);
728 check_program("make", 0);
729 check_program("gcc", 0);
730 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300731 check_program("dot", 1);
732 check_program("convert", 1);
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300733
734 # Extra PDF files - should use 2 for is_optional
735 check_program("xelatex", 2) if ($pdf);
736 check_program("rsvg-convert", 2) if ($pdf);
737 check_program("latexmk", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300738
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200739 if ($need_sphinx || $rec_sphinx_upgrade) {
740 check_python_module("ensurepip", 0) if ($need_venv);
741 }
742
743 # Do distro-specific checks and output distro-install commands
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300744 check_distros();
745
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200746 if (!$python_cmd) {
747 if ($need == 1) {
748 die "Can't build as $need mandatory dependency is missing";
749 } elsif ($need) {
750 die "Can't build as $need mandatory dependencies are missing";
751 }
752 }
753
754 # Check if sphinx-build is called sphinx-build-3
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300755 if ($need_symlink) {
756 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
757 which("sphinx-build-3");
758 }
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200759
760 # NOTE: if the system has a too old Sphinx version installed,
761 # it will recommend installing a newer version using virtualenv
762
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300763 if ($need_sphinx || $rec_sphinx_upgrade) {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300764 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300765 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300766
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300767 @activates = sort {$b cmp $a} @activates;
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300768
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300769 if ($need_sphinx && scalar @activates > 0 && $activates[0] ge $min_activate) {
770 printf "\nNeed to activate a compatible Sphinx version on virtualenv with:\n";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300771 printf "\t. $activates[0]\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600772 deactivate_help();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300773 exit (1);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300774 } else {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300775 my $rec_activate = "$virtenv_dir/bin/activate";
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300776
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200777 if ($need_venv) {
778 printf "\t$python_cmd -m venv $virtenv_dir\n";
779 } else {
780 printf "\t$virtualenv_cmd $virtenv_dir\n";
Tim Birdc428cd52020-02-24 18:34:41 -0700781 }
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300782 printf "\t. $rec_activate\n";
Mauro Carvalho Chehabfb947f32017-07-17 18:46:38 -0300783 printf "\tpip install -r $requirement_file\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600784 deactivate_help();
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300785
786 $need++ if (!$rec_sphinx_upgrade);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300787 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300788 }
789 printf "\n";
790
Bjorn Helgaas54002b52019-05-30 16:59:14 -0500791 print "All optional dependencies are met.\n" if (!$optional);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300792
793 if ($need == 1) {
794 die "Can't build as $need mandatory dependency is missing";
795 } elsif ($need) {
796 die "Can't build as $need mandatory dependencies are missing";
797 }
798
799 print "Needed package dependencies are met.\n";
800}
801
802#
803# Main
804#
805
806while (@ARGV) {
807 my $arg = shift(@ARGV);
808
809 if ($arg eq "--no-virtualenv") {
810 $virtualenv = 0;
811 } elsif ($arg eq "--no-pdf"){
812 $pdf = 0;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300813 } elsif ($arg eq "--version-check"){
814 $version_check = 1;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300815 } else {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300816 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
817 print "Where:\n";
818 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
819 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
820 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 -0300821 exit -1;
822 }
823}
824
825#
826# Determine the system type. There's no standard unique way that would
827# work with all distros with a minimal package install. So, several
828# methods are used here.
829#
830# By default, it will use lsb_release function. If not available, it will
831# fail back to reading the known different places where the distro name
832# is stored
833#
834
835$system_release = qx(lsb_release -d) if which("lsb_release");
836$system_release =~ s/Description:\s*// if ($system_release);
837$system_release = catcheck("/etc/system-release") if !$system_release;
838$system_release = catcheck("/etc/redhat-release") if !$system_release;
839$system_release = catcheck("/etc/lsb-release") if !$system_release;
840$system_release = catcheck("/etc/gentoo-release") if !$system_release;
Mauro Carvalho Chehabd14d0c12020-04-14 18:56:08 +0200841
842# This seems more common than LSB these days
843if (!$system_release) {
844 my %os_var;
845 if (open IN, "cat /etc/os-release|") {
846 while (<IN>) {
847 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
848 $os_var{$1}=$2;
849 }
850 }
851 $system_release = $os_var{"NAME"};
852 if (defined($os_var{"VERSION_ID"})) {
853 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
854 } else {
855 $system_release .= " " . $os_var{"VERSION"};
856 }
857 }
858}
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300859$system_release = catcheck("/etc/issue") if !$system_release;
860$system_release =~ s/\s+$//;
861
862check_needs;