blob: 0d5684c08bbc67c2f83886ea0577e9c63a63a795 [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 Chehab1ef70ce2020-04-21 16:31:06 +020032my $rec_version = "1.7.9"; # PDF won't build here
33my $min_pdf_version = "2.4.4"; # Min version where pdf builds
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030034
35#
36# Command line arguments
37#
38
39my $pdf = 1;
40my $virtualenv = 1;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -030041my $version_check = 0;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030042
43#
44# List of required texlive packages on Fedora and OpenSuse
45#
46
47my %texlive = (
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -030048 'amsfonts.sty' => 'texlive-amsfonts',
49 'amsmath.sty' => 'texlive-amsmath',
50 'amssymb.sty' => 'texlive-amsfonts',
51 'amsthm.sty' => 'texlive-amscls',
52 'anyfontsize.sty' => 'texlive-anyfontsize',
53 'atbegshi.sty' => 'texlive-oberdiek',
54 'bm.sty' => 'texlive-tools',
55 'capt-of.sty' => 'texlive-capt-of',
56 'cmap.sty' => 'texlive-cmap',
57 'ecrm1000.tfm' => 'texlive-ec',
58 'eqparbox.sty' => 'texlive-eqparbox',
59 'eu1enc.def' => 'texlive-euenc',
60 'fancybox.sty' => 'texlive-fancybox',
61 'fancyvrb.sty' => 'texlive-fancyvrb',
62 'float.sty' => 'texlive-float',
63 'fncychap.sty' => 'texlive-fncychap',
64 'footnote.sty' => 'texlive-mdwtools',
65 'framed.sty' => 'texlive-framed',
66 'luatex85.sty' => 'texlive-luatex85',
67 'multirow.sty' => 'texlive-multirow',
68 'needspace.sty' => 'texlive-needspace',
69 'palatino.sty' => 'texlive-psnfss',
70 'parskip.sty' => 'texlive-parskip',
71 'polyglossia.sty' => 'texlive-polyglossia',
72 'tabulary.sty' => 'texlive-tabulary',
73 'threeparttable.sty' => 'texlive-threeparttable',
74 'titlesec.sty' => 'texlive-titlesec',
75 'ucs.sty' => 'texlive-ucs',
76 'upquote.sty' => 'texlive-upquote',
77 'wrapfig.sty' => 'texlive-wrapfig',
78);
79
80#
81# Subroutines that checks if a feature exists
82#
83
84sub check_missing(%)
85{
86 my %map = %{$_[0]};
87
88 foreach my $prog (sort keys %missing) {
89 my $is_optional = $missing{$prog};
90
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -030091 # At least on some LTS distros like CentOS 7, texlive doesn't
92 # provide all packages we need. When such distros are
93 # detected, we have to disable PDF output.
94 #
95 # So, we need to ignore the packages that distros would
96 # need for LaTeX to work
97 if ($is_optional == 2 && !$pdf) {
98 $optional--;
99 next;
100 }
101
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300102 if ($is_optional) {
103 print "Warning: better to also install \"$prog\".\n";
104 } else {
105 print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
106 }
107 if (defined($map{$prog})) {
108 $install .= " " . $map{$prog};
109 } else {
110 $install .= " " . $prog;
111 }
112 }
113
114 $install =~ s/^\s//;
115}
116
117sub add_package($$)
118{
119 my $package = shift;
120 my $is_optional = shift;
121
122 $missing{$package} = $is_optional;
123 if ($is_optional) {
124 $optional++;
125 } else {
126 $need++;
127 }
128}
129
130sub check_missing_file($$$)
131{
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200132 my $files = shift;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300133 my $package = shift;
134 my $is_optional = shift;
135
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200136 for (@$files) {
137 return if(-e $_);
138 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300139
140 add_package($package, $is_optional);
141}
142
143sub findprog($)
144{
145 foreach(split(/:/, $ENV{PATH})) {
146 return "$_/$_[0]" if(-x "$_/$_[0]");
147 }
148}
149
150sub check_program($$)
151{
152 my $prog = shift;
153 my $is_optional = shift;
154
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200155 return $prog if findprog($prog);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300156
157 add_package($prog, $is_optional);
158}
159
160sub check_perl_module($$)
161{
162 my $prog = shift;
163 my $is_optional = shift;
164
165 my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
166 return if ($err == 0);
167
168 add_package($prog, $is_optional);
169}
170
171sub check_python_module($$)
172{
173 my $prog = shift;
174 my $is_optional = shift;
175
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200176 return if (!$python_cmd);
177
178 my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300179 return if ($err == 0);
180
181 add_package($prog, $is_optional);
182}
183
184sub check_rpm_missing($$)
185{
186 my @pkgs = @{$_[0]};
187 my $is_optional = $_[1];
188
189 foreach my $prog(@pkgs) {
190 my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
191 add_package($prog, $is_optional) if ($err);
192 }
193}
194
195sub check_pacman_missing($$)
196{
197 my @pkgs = @{$_[0]};
198 my $is_optional = $_[1];
199
200 foreach my $prog(@pkgs) {
201 my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
202 add_package($prog, $is_optional) if ($err);
203 }
204}
205
206sub check_missing_tex($)
207{
208 my $is_optional = shift;
209 my $kpsewhich = findprog("kpsewhich");
210
211 foreach my $prog(keys %texlive) {
212 my $package = $texlive{$prog};
213 if (!$kpsewhich) {
214 add_package($package, $is_optional);
215 next;
216 }
217 my $file = qx($kpsewhich $prog);
218 add_package($package, $is_optional) if ($file =~ /^\s*$/);
219 }
220}
221
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300222sub get_sphinx_fname()
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300223{
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300224 my $fname = "sphinx-build";
225 return $fname if findprog($fname);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300226
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300227 $fname = "sphinx-build-3";
228 if (findprog($fname)) {
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300229 $need_symlink = 1;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300230 return $fname;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300231 }
232
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300233 return "";
234}
235
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200236sub get_sphinx_version($)
237{
238 my $cmd = shift;
239 my $ver;
240
241 open IN, "$cmd --version 2>&1 |";
242 while (<IN>) {
243 if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
244 $ver=$1;
245 last;
246 }
247 # Sphinx 1.2.x uses a different format
248 if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
249 $ver=$1;
250 last;
251 }
252 }
253 close IN;
254 return $ver;
255}
256
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300257sub check_sphinx()
258{
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200259 my $default_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300260 my $cur_version;
261
262 open IN, $conf or die "Can't open $conf";
263 while (<IN>) {
264 if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
265 $min_version=$1;
266 last;
267 }
268 }
269 close IN;
270
271 die "Can't get needs_sphinx version from $conf" if (!$min_version);
272
273 open IN, $requirement_file or die "Can't open $requirement_file";
274 while (<IN>) {
275 if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200276 $default_version=$1;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300277 last;
278 }
279 }
280 close IN;
281
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200282 die "Can't get default sphinx version from $requirement_file" if (!$default_version);
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300283
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200284 $virtenv_dir = $virtenv_prefix . $default_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300285
286 my $sphinx = get_sphinx_fname();
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200287 if ($sphinx eq "") {
288 $need_sphinx = 1;
289 return;
290 }
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300291
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200292 $cur_version = get_sphinx_version($sphinx);
293 die ("$sphinx returned an error") if (!$cur_version);
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300294
295 die "$sphinx didn't return its version" if (!$cur_version);
296
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300297 if ($cur_version lt $min_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300298 printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200299 $cur_version, $min_version, $default_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300300 $need_sphinx = 1;
301 return;
302 }
303
304 if ($cur_version lt $rec_version) {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300305 printf "Sphinx version %s\n", $cur_version;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300306 print "Warning: It is recommended at least Sphinx version $rec_version.\n";
Mauro Carvalho Chehab1ef70ce2020-04-21 16:31:06 +0200307 print " If you want pdf, you need at least $min_pdf_version.\n";
308 $rec_sphinx_upgrade = 1;
309 return;
310 }
311 if ($cur_version lt $min_pdf_version) {
312 printf "Sphinx version %s\n", $cur_version;
313 print "Note: It is recommended at least Sphinx version $min_pdf_version if you need PDF support.\n";
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300314 $rec_sphinx_upgrade = 1;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300315 return;
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300316 }
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300317
318 # On version check mode, just assume Sphinx has all mandatory deps
319 exit (0) if ($version_check);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300320}
321
322#
323# Ancillary subroutines
324#
325
326sub catcheck($)
327{
328 my $res = "";
329 $res = qx(cat $_[0]) if (-r $_[0]);
330 return $res;
331}
332
333sub which($)
334{
335 my $file = shift;
336 my @path = split ":", $ENV{PATH};
337
338 foreach my $dir(@path) {
339 my $name = $dir.'/'.$file;
340 return $name if (-x $name );
341 }
342 return undef;
343}
344
345#
346# Subroutines that check distro-specific hints
347#
348
349sub give_debian_hints()
350{
351 my %map = (
352 "python-sphinx" => "python3-sphinx",
353 "sphinx_rtd_theme" => "python3-sphinx-rtd-theme",
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200354 "ensurepip" => "python3-venv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300355 "virtualenv" => "virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300356 "dot" => "graphviz",
357 "convert" => "imagemagick",
358 "Pod::Usage" => "perl-modules",
359 "xelatex" => "texlive-xetex",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300360 "rsvg-convert" => "librsvg2-bin",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300361 );
362
363 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200364 check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300365 "fonts-dejavu", 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300366
Jeremy MAURO9692f2f2019-10-02 15:35:42 +0200367 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
Mauro Carvalho Chehabbfc7f422020-04-14 18:56:10 +0200368 "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
369 "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300370 "fonts-noto-cjk", 2);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300371 }
372
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300373 check_program("dvipng", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300374 check_missing(\%map);
375
376 return if (!$need && !$optional);
377 printf("You should run:\n\n\tsudo apt-get install $install\n");
378}
379
380sub give_redhat_hints()
381{
382 my %map = (
383 "python-sphinx" => "python3-sphinx",
384 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
385 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300386 "dot" => "graphviz",
387 "convert" => "ImageMagick",
388 "Pod::Usage" => "perl-Pod-Usage",
389 "xelatex" => "texlive-xetex-bin",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300390 "rsvg-convert" => "librsvg2-tools",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300391 );
392
Mauro Carvalho Chehab5d889532017-07-17 18:46:39 -0300393 my @fedora26_opt_pkgs = (
394 "graphviz-gd", # Fedora 26: needed for PDF support
395 );
396
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300397 my @fedora_tex_pkgs = (
398 "texlive-collection-fontsrecommended",
399 "texlive-collection-latex",
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300400 "texlive-xecjk",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300401 "dejavu-sans-fonts",
402 "dejavu-serif-fonts",
403 "dejavu-sans-mono-fonts",
404 );
405
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300406 #
407 # Checks valid for RHEL/CentOS version 7.x.
408 #
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300409 my $old = 0;
410 my $rel;
411 $rel = $1 if ($system_release =~ /release\s+(\d+)/);
412
Mauro Carvalho Chehabb3084672019-07-13 08:50:24 -0300413 if (!($system_release =~ /Fedora/)) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300414 $map{"virtualenv"} = "python-virtualenv";
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300415
416 if ($rel && $rel < 8) {
417 $old = 1;
418 $pdf = 0;
419
420 printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
421 printf("If you want to build PDF, please read:\n");
422 printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
423 }
424 } else {
425 if ($rel && $rel < 26) {
426 $old = 1;
427 }
428 }
429 if (!$rel) {
430 printf("Couldn't identify release number\n");
431 $old = 1;
432 $pdf = 0;
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300433 }
434
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300435 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200436 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300437 "google-noto-sans-cjk-ttc-fonts", 2);
438 }
439
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300440 check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
441 check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
442 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300443 check_missing(\%map);
444
445 return if (!$need && !$optional);
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300446
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300447 if (!$old) {
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300448 # dnf, for Fedora 18+
449 printf("You should run:\n\n\tsudo dnf install -y $install\n");
450 } else {
451 # yum, for RHEL (and clones) or Fedora version < 18
452 printf("You should run:\n\n\tsudo yum install -y $install\n");
453 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300454}
455
456sub give_opensuse_hints()
457{
458 my %map = (
459 "python-sphinx" => "python3-sphinx",
460 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
461 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300462 "dot" => "graphviz",
463 "convert" => "ImageMagick",
464 "Pod::Usage" => "perl-Pod-Usage",
465 "xelatex" => "texlive-xetex-bin",
466 );
467
Mauro Carvalho Chehabb3df6222020-04-14 18:56:09 +0200468 # On Tumbleweed, this package is also named rsvg-convert
469 $map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
470
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300471 my @suse_tex_pkgs = (
472 "texlive-babel-english",
473 "texlive-caption",
474 "texlive-colortbl",
475 "texlive-courier",
476 "texlive-dvips",
477 "texlive-helvetic",
478 "texlive-makeindex",
479 "texlive-metafont",
480 "texlive-metapost",
481 "texlive-palatino",
482 "texlive-preview",
483 "texlive-times",
484 "texlive-zapfchan",
485 "texlive-zapfding",
486 );
487
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300488 $map{"latexmk"} = "texlive-latexmk-bin";
489
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300490 # FIXME: add support for installing CJK fonts
491 #
492 # I tried hard, but was unable to find a way to install
493 # "Noto Sans CJK SC" on openSUSE
494
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300495 check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
496 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300497 check_missing(\%map);
498
499 return if (!$need && !$optional);
500 printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
501}
502
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300503sub give_mageia_hints()
504{
505 my %map = (
506 "python-sphinx" => "python3-sphinx",
507 "sphinx_rtd_theme" => "python3-sphinx_rtd_theme",
508 "virtualenv" => "python3-virtualenv",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300509 "dot" => "graphviz",
510 "convert" => "ImageMagick",
511 "Pod::Usage" => "perl-Pod-Usage",
512 "xelatex" => "texlive",
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200513 "rsvg-convert" => "librsvg2",
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300514 );
515
516 my @tex_pkgs = (
517 "texlive-fontsextra",
518 );
519
Mauro Carvalho Chehab353290a2019-07-13 08:19:44 -0300520 $map{"latexmk"} = "texlive-collection-basic";
521
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200522 my $packager_cmd;
523 my $noto_sans;
524 if ($system_release =~ /OpenMandriva/) {
525 $packager_cmd = "dnf install";
526 $noto_sans = "noto-sans-cjk-fonts";
527 @tex_pkgs = ( "texlive-collection-fontsextra" );
528 } else {
529 $packager_cmd = "urpmi";
530 $noto_sans = "google-noto-sans-cjk-ttc-fonts";
531 }
532
533
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300534 if ($pdf) {
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200535 check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
536 "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
537 $noto_sans, 2);
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300538 }
539
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300540 check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300541 check_missing(\%map);
542
543 return if (!$need && !$optional);
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200544 printf("You should run:\n\n\tsudo $packager_cmd $install\n");
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300545}
546
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300547sub give_arch_linux_hints()
548{
549 my %map = (
550 "sphinx_rtd_theme" => "python-sphinx_rtd_theme",
551 "virtualenv" => "python-virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300552 "dot" => "graphviz",
553 "convert" => "imagemagick",
554 "xelatex" => "texlive-bin",
Louis Taylor0d0da9a2019-11-02 18:45:11 +0000555 "latexmk" => "texlive-core",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300556 "rsvg-convert" => "extra/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300557 );
558
559 my @archlinux_tex_pkgs = (
560 "texlive-core",
561 "texlive-latexextra",
562 "ttf-dejavu",
563 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300564 check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
565
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300566 if ($pdf) {
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200567 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300568 "noto-fonts-cjk", 2);
569 }
570
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300571 check_missing(\%map);
572
573 return if (!$need && !$optional);
574 printf("You should run:\n\n\tsudo pacman -S $install\n");
575}
576
577sub give_gentoo_hints()
578{
579 my %map = (
580 "sphinx_rtd_theme" => "dev-python/sphinx_rtd_theme",
581 "virtualenv" => "dev-python/virtualenv",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300582 "dot" => "media-gfx/graphviz",
583 "convert" => "media-gfx/imagemagick",
584 "xelatex" => "dev-texlive/texlive-xetex media-fonts/dejavu",
Mauro Carvalho Chehab8e7d5d12017-07-17 18:46:40 -0300585 "rsvg-convert" => "gnome-base/librsvg",
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300586 );
587
Jeremy MAUROff8fdb32019-10-02 15:33:39 +0200588 check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300589 "media-fonts/dejavu", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300590
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300591 if ($pdf) {
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200592 check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
593 "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
Mauro Carvalho Chehab27eed922019-07-13 08:19:44 -0300594 "media-fonts/noto-cjk", 2);
595 }
596
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300597 check_missing(\%map);
598
599 return if (!$need && !$optional);
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300600
601 printf("You should run:\n\n");
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300602
603 my $imagemagick = "media-gfx/imagemagick svg png";
604 my $cairo = "media-gfx/graphviz cairo pdf";
605 my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
606 my $portage_cairo = "/etc/portage/package.use/graphviz";
607
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200608 if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300609 printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
610 }
Mauro Carvalho Chehabe45a6312020-04-14 18:56:11 +0200611 if (qx(grep graphviz $portage_cairo 2>/dev/null) eq "") {
Mauro Carvalho Chehab4ea96d52019-07-13 08:19:44 -0300612 printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
613 }
614
Mauro Carvalho Chehabbba1e4c2017-07-17 18:46:41 -0300615 printf("\tsudo emerge --ask $install\n");
616
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300617}
618
619sub check_distros()
620{
621 # Distro-specific hints
622 if ($system_release =~ /Red Hat Enterprise Linux/) {
623 give_redhat_hints;
624 return;
625 }
Mauro Carvalho Chehab9b756a92017-07-24 09:09:24 -0300626 if ($system_release =~ /CentOS/) {
627 give_redhat_hints;
628 return;
629 }
630 if ($system_release =~ /Scientific Linux/) {
631 give_redhat_hints;
632 return;
633 }
634 if ($system_release =~ /Oracle Linux Server/) {
635 give_redhat_hints;
636 return;
637 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300638 if ($system_release =~ /Fedora/) {
639 give_redhat_hints;
640 return;
641 }
642 if ($system_release =~ /Ubuntu/) {
643 give_debian_hints;
644 return;
645 }
646 if ($system_release =~ /Debian/) {
647 give_debian_hints;
648 return;
649 }
650 if ($system_release =~ /openSUSE/) {
651 give_opensuse_hints;
652 return;
653 }
Mauro Carvalho Chehab800d4082017-07-21 13:20:41 -0300654 if ($system_release =~ /Mageia/) {
655 give_mageia_hints;
656 return;
657 }
Mauro Carvalho Chehabd6ebf182020-04-14 18:56:12 +0200658 if ($system_release =~ /OpenMandriva/) {
659 give_mageia_hints;
660 return;
661 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300662 if ($system_release =~ /Arch Linux/) {
663 give_arch_linux_hints;
664 return;
665 }
666 if ($system_release =~ /Gentoo/) {
667 give_gentoo_hints;
668 return;
669 }
670
671 #
672 # Fall-back to generic hint code for other distros
673 # That's far from ideal, specially for LaTeX dependencies.
674 #
675 my %map = (
676 "sphinx-build" => "sphinx"
677 );
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300678 check_missing_tex(2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300679 check_missing(\%map);
680 print "I don't know distro $system_release.\n";
681 print "So, I can't provide you a hint with the install procedure.\n";
682 print "There are likely missing dependencies.\n";
683}
684
685#
686# Common dependencies
687#
688
Shuah Khan2730ce02019-09-18 18:37:54 -0600689sub deactivate_help()
690{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200691 printf "\nIf you want to exit the virtualenv, you can use:\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600692 printf "\tdeactivate\n";
693}
694
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300695sub check_needs()
696{
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200697 # Check if Sphinx is already accessible from current environment
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300698 check_sphinx();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300699
700 if ($system_release) {
701 print "Detected OS: $system_release.\n\n";
702 } else {
703 print "Unknown OS\n\n";
704 }
705
706 print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
707
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200708 # Check python command line, trying first python3
709 $python_cmd = findprog("python3");
710 $python_cmd = check_program("python", 0) if (!$python_cmd);
711
712 # Check the type of virtual env, depending on Python version
713 if ($python_cmd) {
714 if ($virtualenv) {
715 my $tmp = qx($python_cmd --version 2>&1);
716 if ($tmp =~ m/(\d+\.)(\d+\.)/) {
717 if ($1 >= 3 && $2 >= 3) {
718 $need_venv = 1; # python 3.3 or upper
719 } else {
720 $need_virtualenv = 1;
721 }
722 if ($1 < 3) {
723 # Complain if it finds python2 (or worse)
724 printf "Warning: python$1 support is deprecated. Use it with caution!\n";
725 }
726 } else {
727 die "Warning: couldn't identify $python_cmd version!";
728 }
729 } else {
730 add_package("python-sphinx", 0);
731 }
732 }
733
734 # Set virtualenv command line, if python < 3.3
735 my $virtualenv_cmd;
736 if ($need_virtualenv) {
737 $virtualenv_cmd = findprog("virtualenv-3");
738 $virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
739 if (!$virtualenv_cmd) {
740 check_program("virtualenv", 0);
741 $virtualenv_cmd = "virtualenv";
742 }
743 }
744
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300745 # Check for needed programs/tools
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300746 check_perl_module("Pod::Usage", 0);
747 check_program("make", 0);
748 check_program("gcc", 0);
749 check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300750 check_program("dot", 1);
751 check_program("convert", 1);
Mauro Carvalho Chehab56e5a632019-07-13 09:37:16 -0300752
753 # Extra PDF files - should use 2 for is_optional
754 check_program("xelatex", 2) if ($pdf);
755 check_program("rsvg-convert", 2) if ($pdf);
756 check_program("latexmk", 2) if ($pdf);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300757
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200758 if ($need_sphinx || $rec_sphinx_upgrade) {
759 check_python_module("ensurepip", 0) if ($need_venv);
760 }
761
762 # Do distro-specific checks and output distro-install commands
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300763 check_distros();
764
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200765 if (!$python_cmd) {
766 if ($need == 1) {
767 die "Can't build as $need mandatory dependency is missing";
768 } elsif ($need) {
769 die "Can't build as $need mandatory dependencies are missing";
770 }
771 }
772
773 # Check if sphinx-build is called sphinx-build-3
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300774 if ($need_symlink) {
775 printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
776 which("sphinx-build-3");
777 }
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200778
779 # NOTE: if the system has a too old Sphinx version installed,
780 # it will recommend installing a newer version using virtualenv
781
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300782 if ($need_sphinx || $rec_sphinx_upgrade) {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300783 my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300784 my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300785
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300786 @activates = sort {$b cmp $a} @activates;
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200787 my ($activate, $ver);
788 foreach my $f (@activates) {
789 $activate = $f;
790 next if ($activate lt $min_activate);
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300791
Mauro Carvalho Chehaba8b380c2020-04-21 16:31:05 +0200792 my $sphinx_cmd = $activate;
793 $sphinx_cmd =~ s/activate/sphinx-build/;
794 next if (! -f $sphinx_cmd);
795
796 $ver = get_sphinx_version($sphinx_cmd);
797 last if ($ver ge $min_version);
798 }
799 if ($need_sphinx && ($activate ne "")) {
800 printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
801 printf "\t. $activate\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600802 deactivate_help();
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300803 exit (1);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300804 } else {
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300805 my $rec_activate = "$virtenv_dir/bin/activate";
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300806
Mauro Carvalho Chehab2f9c5022020-04-14 18:56:13 +0200807 if ($need_venv) {
808 printf "\t$python_cmd -m venv $virtenv_dir\n";
809 } else {
810 printf "\t$virtualenv_cmd $virtenv_dir\n";
Tim Birdc428cd52020-02-24 18:34:41 -0700811 }
Mauro Carvalho Chehab44f42162019-05-29 20:09:24 -0300812 printf "\t. $rec_activate\n";
Mauro Carvalho Chehabfb947f32017-07-17 18:46:38 -0300813 printf "\tpip install -r $requirement_file\n";
Shuah Khan2730ce02019-09-18 18:37:54 -0600814 deactivate_help();
Mauro Carvalho Chehab77d09ad2019-05-22 18:43:46 -0300815
816 $need++ if (!$rec_sphinx_upgrade);
Mauro Carvalho Chehab5be33182017-07-17 18:46:37 -0300817 }
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300818 }
819 printf "\n";
820
Bjorn Helgaas54002b52019-05-30 16:59:14 -0500821 print "All optional dependencies are met.\n" if (!$optional);
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300822
823 if ($need == 1) {
824 die "Can't build as $need mandatory dependency is missing";
825 } elsif ($need) {
826 die "Can't build as $need mandatory dependencies are missing";
827 }
828
829 print "Needed package dependencies are met.\n";
830}
831
832#
833# Main
834#
835
836while (@ARGV) {
837 my $arg = shift(@ARGV);
838
839 if ($arg eq "--no-virtualenv") {
840 $virtualenv = 0;
841 } elsif ($arg eq "--no-pdf"){
842 $pdf = 0;
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300843 } elsif ($arg eq "--version-check"){
844 $version_check = 1;
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300845 } else {
Mauro Carvalho Chehab9b88ad52019-05-29 20:09:26 -0300846 print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
847 print "Where:\n";
848 print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
849 print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
850 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 -0300851 exit -1;
852 }
853}
854
855#
856# Determine the system type. There's no standard unique way that would
857# work with all distros with a minimal package install. So, several
858# methods are used here.
859#
860# By default, it will use lsb_release function. If not available, it will
861# fail back to reading the known different places where the distro name
862# is stored
863#
864
865$system_release = qx(lsb_release -d) if which("lsb_release");
866$system_release =~ s/Description:\s*// if ($system_release);
867$system_release = catcheck("/etc/system-release") if !$system_release;
868$system_release = catcheck("/etc/redhat-release") if !$system_release;
869$system_release = catcheck("/etc/lsb-release") if !$system_release;
870$system_release = catcheck("/etc/gentoo-release") if !$system_release;
Mauro Carvalho Chehabd14d0c12020-04-14 18:56:08 +0200871
872# This seems more common than LSB these days
873if (!$system_release) {
874 my %os_var;
875 if (open IN, "cat /etc/os-release|") {
876 while (<IN>) {
877 if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
878 $os_var{$1}=$2;
879 }
880 }
881 $system_release = $os_var{"NAME"};
882 if (defined($os_var{"VERSION_ID"})) {
883 $system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
884 } else {
885 $system_release .= " " . $os_var{"VERSION"};
886 }
887 }
888}
Mauro Carvalho Chehab24071ac2017-07-17 18:46:36 -0300889$system_release = catcheck("/etc/issue") if !$system_release;
890$system_release =~ s/\s+$//;
891
892check_needs;