xref: /linux-6.15/scripts/sphinx-pre-install (revision 412b09dd)
1#!/usr/bin/perl
2# SPDX-License-Identifier: GPL-2.0-or-later
3use strict;
4
5# Copyright (c) 2017-2020 Mauro Carvalho Chehab <[email protected]>
6#
7
8my $prefix = "./";
9$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
10
11my $conf = $prefix . "Documentation/conf.py";
12my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
13my $virtenv_prefix = "sphinx_";
14
15#
16# Static vars
17#
18
19my %missing;
20my $system_release;
21my $need = 0;
22my $optional = 0;
23my $need_symlink = 0;
24my $need_sphinx = 0;
25my $need_venv = 0;
26my $need_virtualenv = 0;
27my $rec_sphinx_upgrade = 0;
28my $install = "";
29my $virtenv_dir = "";
30my $python_cmd = "";
31my $min_version;
32my $cur_version;
33my $rec_version = "1.7.9";	# PDF won't build here
34my $min_pdf_version = "2.4.4";	# Min version where pdf builds
35
36#
37# Command line arguments
38#
39
40my $pdf = 1;
41my $virtualenv = 1;
42my $version_check = 0;
43
44#
45# List of required texlive packages on Fedora and OpenSuse
46#
47
48my %texlive = (
49	'amsfonts.sty'       => 'texlive-amsfonts',
50	'amsmath.sty'        => 'texlive-amsmath',
51	'amssymb.sty'        => 'texlive-amsfonts',
52	'amsthm.sty'         => 'texlive-amscls',
53	'anyfontsize.sty'    => 'texlive-anyfontsize',
54	'atbegshi.sty'       => 'texlive-oberdiek',
55	'bm.sty'             => 'texlive-tools',
56	'capt-of.sty'        => 'texlive-capt-of',
57	'cmap.sty'           => 'texlive-cmap',
58	'ecrm1000.tfm'       => 'texlive-ec',
59	'eqparbox.sty'       => 'texlive-eqparbox',
60	'eu1enc.def'         => 'texlive-euenc',
61	'fancybox.sty'       => 'texlive-fancybox',
62	'fancyvrb.sty'       => 'texlive-fancyvrb',
63	'float.sty'          => 'texlive-float',
64	'fncychap.sty'       => 'texlive-fncychap',
65	'footnote.sty'       => 'texlive-mdwtools',
66	'framed.sty'         => 'texlive-framed',
67	'luatex85.sty'       => 'texlive-luatex85',
68	'multirow.sty'       => 'texlive-multirow',
69	'needspace.sty'      => 'texlive-needspace',
70	'palatino.sty'       => 'texlive-psnfss',
71	'parskip.sty'        => 'texlive-parskip',
72	'polyglossia.sty'    => 'texlive-polyglossia',
73	'tabulary.sty'       => 'texlive-tabulary',
74	'threeparttable.sty' => 'texlive-threeparttable',
75	'titlesec.sty'       => 'texlive-titlesec',
76	'ucs.sty'            => 'texlive-ucs',
77	'upquote.sty'        => 'texlive-upquote',
78	'wrapfig.sty'        => 'texlive-wrapfig',
79);
80
81#
82# Subroutines that checks if a feature exists
83#
84
85sub check_missing(%)
86{
87	my %map = %{$_[0]};
88
89	foreach my $prog (sort keys %missing) {
90		my $is_optional = $missing{$prog};
91
92		# At least on some LTS distros like CentOS 7, texlive doesn't
93		# provide all packages we need. When such distros are
94		# detected, we have to disable PDF output.
95		#
96		# So, we need to ignore the packages that distros would
97		# need for LaTeX to work
98		if ($is_optional == 2 && !$pdf) {
99			$optional--;
100			next;
101		}
102
103		if ($is_optional) {
104			print "Warning: better to also install \"$prog\".\n";
105		} else {
106			print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
107		}
108		if (defined($map{$prog})) {
109			$install .= " " . $map{$prog};
110		} else {
111			$install .= " " . $prog;
112		}
113	}
114
115	$install =~ s/^\s//;
116}
117
118sub add_package($$)
119{
120	my $package = shift;
121	my $is_optional = shift;
122
123	$missing{$package} = $is_optional;
124	if ($is_optional) {
125		$optional++;
126	} else {
127		$need++;
128	}
129}
130
131sub check_missing_file($$$)
132{
133	my $files = shift;
134	my $package = shift;
135	my $is_optional = shift;
136
137	for (@$files) {
138		return if(-e $_);
139	}
140
141	add_package($package, $is_optional);
142}
143
144sub findprog($)
145{
146	foreach(split(/:/, $ENV{PATH})) {
147		return "$_/$_[0]" if(-x "$_/$_[0]");
148	}
149}
150
151sub find_python_no_venv()
152{
153	my $prog = shift;
154
155	my $cur_dir = qx(pwd);
156	$cur_dir =~ s/\s+$//;
157
158	foreach my $dir (split(/:/, $ENV{PATH})) {
159		next if ($dir =~ m,($cur_dir)/sphinx,);
160		return "$dir/python3" if(-x "$dir/python3");
161	}
162	foreach my $dir (split(/:/, $ENV{PATH})) {
163		next if ($dir =~ m,($cur_dir)/sphinx,);
164		return "$dir/python" if(-x "$dir/python");
165	}
166	return "python";
167}
168
169sub check_program($$)
170{
171	my $prog = shift;
172	my $is_optional = shift;
173
174	return $prog if findprog($prog);
175
176	add_package($prog, $is_optional);
177}
178
179sub check_perl_module($$)
180{
181	my $prog = shift;
182	my $is_optional = shift;
183
184	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
185	return if ($err == 0);
186
187	add_package($prog, $is_optional);
188}
189
190sub check_python_module($$)
191{
192	my $prog = shift;
193	my $is_optional = shift;
194
195	return if (!$python_cmd);
196
197	my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
198	return if ($err == 0);
199
200	add_package($prog, $is_optional);
201}
202
203sub check_rpm_missing($$)
204{
205	my @pkgs = @{$_[0]};
206	my $is_optional = $_[1];
207
208	foreach my $prog(@pkgs) {
209		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
210		add_package($prog, $is_optional) if ($err);
211	}
212}
213
214sub check_pacman_missing($$)
215{
216	my @pkgs = @{$_[0]};
217	my $is_optional = $_[1];
218
219	foreach my $prog(@pkgs) {
220		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
221		add_package($prog, $is_optional) if ($err);
222	}
223}
224
225sub check_missing_tex($)
226{
227	my $is_optional = shift;
228	my $kpsewhich = findprog("kpsewhich");
229
230	foreach my $prog(keys %texlive) {
231		my $package = $texlive{$prog};
232		if (!$kpsewhich) {
233			add_package($package, $is_optional);
234			next;
235		}
236		my $file = qx($kpsewhich $prog);
237		add_package($package, $is_optional) if ($file =~ /^\s*$/);
238	}
239}
240
241sub get_sphinx_fname()
242{
243	my $fname = "sphinx-build";
244	return $fname if findprog($fname);
245
246	$fname = "sphinx-build-3";
247	if (findprog($fname)) {
248		$need_symlink = 1;
249		return $fname;
250	}
251
252	return "";
253}
254
255sub get_sphinx_version($)
256{
257	my $cmd = shift;
258	my $ver;
259
260	open IN, "$cmd --version 2>&1 |";
261	while (<IN>) {
262		if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
263			$ver=$1;
264			last;
265		}
266		# Sphinx 1.2.x uses a different format
267		if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
268			$ver=$1;
269			last;
270		}
271	}
272	close IN;
273	return $ver;
274}
275
276sub check_sphinx()
277{
278	my $default_version;
279
280	open IN, $conf or die "Can't open $conf";
281	while (<IN>) {
282		if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
283			$min_version=$1;
284			last;
285		}
286	}
287	close IN;
288
289	die "Can't get needs_sphinx version from $conf" if (!$min_version);
290
291	open IN, $requirement_file or die "Can't open $requirement_file";
292	while (<IN>) {
293		if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
294			$default_version=$1;
295			last;
296		}
297	}
298	close IN;
299
300	die "Can't get default sphinx version from $requirement_file" if (!$default_version);
301
302	$virtenv_dir = $virtenv_prefix . $default_version;
303
304	my $sphinx = get_sphinx_fname();
305	if ($sphinx eq "") {
306		$need_sphinx = 1;
307		return;
308	}
309
310	$cur_version = get_sphinx_version($sphinx);
311	die ("$sphinx returned an error") if (!$cur_version);
312
313	die "$sphinx didn't return its version" if (!$cur_version);
314
315	if ($cur_version lt $min_version) {
316		printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
317		       $cur_version, $min_version, $default_version;
318		$need_sphinx = 1;
319		return;
320	}
321
322	if ($cur_version lt $rec_version) {
323		printf "Sphinx version %s\n", $cur_version;
324		print "Warning: It is recommended at least Sphinx version $rec_version.\n";
325		print "         If you want pdf, you need at least $min_pdf_version.\n";
326		$rec_sphinx_upgrade = 1;
327		return;
328	}
329	if ($cur_version lt $min_pdf_version) {
330		printf "Sphinx version %s\n", $cur_version;
331		print "Note: It is recommended at least Sphinx version $min_pdf_version if you need PDF support.\n";
332		$rec_sphinx_upgrade = 1;
333		return;
334	}
335
336	# On version check mode, just assume Sphinx has all mandatory deps
337	exit (0) if ($version_check);
338}
339
340#
341# Ancillary subroutines
342#
343
344sub catcheck($)
345{
346  my $res = "";
347  $res = qx(cat $_[0]) if (-r $_[0]);
348  return $res;
349}
350
351sub which($)
352{
353	my $file = shift;
354	my @path = split ":", $ENV{PATH};
355
356	foreach my $dir(@path) {
357		my $name = $dir.'/'.$file;
358		return $name if (-x $name );
359	}
360	return undef;
361}
362
363#
364# Subroutines that check distro-specific hints
365#
366
367sub give_debian_hints()
368{
369	my %map = (
370		"python-sphinx"		=> "python3-sphinx",
371		"sphinx_rtd_theme"	=> "python3-sphinx-rtd-theme",
372		"ensurepip"		=> "python3-venv",
373		"virtualenv"		=> "virtualenv",
374		"dot"			=> "graphviz",
375		"convert"		=> "imagemagick",
376		"Pod::Usage"		=> "perl-modules",
377		"xelatex"		=> "texlive-xetex",
378		"rsvg-convert"		=> "librsvg2-bin",
379	);
380
381	if ($pdf) {
382		check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
383				   "fonts-dejavu", 2);
384
385		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
386				    "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
387				    "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
388				   "fonts-noto-cjk", 2);
389	}
390
391	check_program("dvipng", 2) if ($pdf);
392	check_missing(\%map);
393
394	return if (!$need && !$optional);
395	printf("You should run:\n\n\tsudo apt-get install $install\n");
396}
397
398sub give_redhat_hints()
399{
400	my %map = (
401		"python-sphinx"		=> "python3-sphinx",
402		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
403		"virtualenv"		=> "python3-virtualenv",
404		"dot"			=> "graphviz",
405		"convert"		=> "ImageMagick",
406		"Pod::Usage"		=> "perl-Pod-Usage",
407		"xelatex"		=> "texlive-xetex-bin",
408		"rsvg-convert"		=> "librsvg2-tools",
409	);
410
411	my @fedora26_opt_pkgs = (
412		"graphviz-gd",		# Fedora 26: needed for PDF support
413	);
414
415	my @fedora_tex_pkgs = (
416		"texlive-collection-fontsrecommended",
417		"texlive-collection-latex",
418		"texlive-xecjk",
419		"dejavu-sans-fonts",
420		"dejavu-serif-fonts",
421		"dejavu-sans-mono-fonts",
422	);
423
424	#
425	# Checks valid for RHEL/CentOS version 7.x.
426	#
427	my $old = 0;
428	my $rel;
429	$rel = $1 if ($system_release =~ /release\s+(\d+)/);
430
431	if (!($system_release =~ /Fedora/)) {
432		$map{"virtualenv"} = "python-virtualenv";
433
434		if ($rel && $rel < 8) {
435			$old = 1;
436			$pdf = 0;
437
438			printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
439			printf("If you want to build PDF, please read:\n");
440			printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
441		}
442	} else {
443		if ($rel && $rel < 26) {
444			$old = 1;
445		}
446	}
447	if (!$rel) {
448		printf("Couldn't identify release number\n");
449		$old = 1;
450		$pdf = 0;
451	}
452
453	if ($pdf) {
454		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
455				   "google-noto-sans-cjk-ttc-fonts", 2);
456	}
457
458	check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
459	check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
460	check_missing_tex(2) if ($pdf);
461	check_missing(\%map);
462
463	return if (!$need && !$optional);
464
465	if (!$old) {
466		# dnf, for Fedora 18+
467		printf("You should run:\n\n\tsudo dnf install -y $install\n");
468	} else {
469		# yum, for RHEL (and clones) or Fedora version < 18
470		printf("You should run:\n\n\tsudo yum install -y $install\n");
471	}
472}
473
474sub give_opensuse_hints()
475{
476	my %map = (
477		"python-sphinx"		=> "python3-sphinx",
478		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
479		"virtualenv"		=> "python3-virtualenv",
480		"dot"			=> "graphviz",
481		"convert"		=> "ImageMagick",
482		"Pod::Usage"		=> "perl-Pod-Usage",
483		"xelatex"		=> "texlive-xetex-bin",
484	);
485
486	# On Tumbleweed, this package is also named rsvg-convert
487	$map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
488
489	my @suse_tex_pkgs = (
490		"texlive-babel-english",
491		"texlive-caption",
492		"texlive-colortbl",
493		"texlive-courier",
494		"texlive-dvips",
495		"texlive-helvetic",
496		"texlive-makeindex",
497		"texlive-metafont",
498		"texlive-metapost",
499		"texlive-palatino",
500		"texlive-preview",
501		"texlive-times",
502		"texlive-zapfchan",
503		"texlive-zapfding",
504	);
505
506	$map{"latexmk"} = "texlive-latexmk-bin";
507
508	# FIXME: add support for installing CJK fonts
509	#
510	# I tried hard, but was unable to find a way to install
511	# "Noto Sans CJK SC" on openSUSE
512
513	check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
514	check_missing_tex(2) if ($pdf);
515	check_missing(\%map);
516
517	return if (!$need && !$optional);
518	printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
519}
520
521sub give_mageia_hints()
522{
523	my %map = (
524		"python-sphinx"		=> "python3-sphinx",
525		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
526		"virtualenv"		=> "python3-virtualenv",
527		"dot"			=> "graphviz",
528		"convert"		=> "ImageMagick",
529		"Pod::Usage"		=> "perl-Pod-Usage",
530		"xelatex"		=> "texlive",
531		"rsvg-convert"		=> "librsvg2",
532	);
533
534	my @tex_pkgs = (
535		"texlive-fontsextra",
536	);
537
538	$map{"latexmk"} = "texlive-collection-basic";
539
540	my $packager_cmd;
541	my $noto_sans;
542	if ($system_release =~ /OpenMandriva/) {
543		$packager_cmd = "dnf install";
544		$noto_sans = "noto-sans-cjk-fonts";
545		@tex_pkgs = ( "texlive-collection-fontsextra" );
546	} else {
547		$packager_cmd = "urpmi";
548		$noto_sans = "google-noto-sans-cjk-ttc-fonts";
549	}
550
551
552	if ($pdf) {
553		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
554				    "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
555				   $noto_sans, 2);
556	}
557
558	check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
559	check_missing(\%map);
560
561	return if (!$need && !$optional);
562	printf("You should run:\n\n\tsudo $packager_cmd $install\n");
563}
564
565sub give_arch_linux_hints()
566{
567	my %map = (
568		"sphinx_rtd_theme"	=> "python-sphinx_rtd_theme",
569		"virtualenv"		=> "python-virtualenv",
570		"dot"			=> "graphviz",
571		"convert"		=> "imagemagick",
572		"xelatex"		=> "texlive-bin",
573		"latexmk"		=> "texlive-core",
574		"rsvg-convert"		=> "extra/librsvg",
575	);
576
577	my @archlinux_tex_pkgs = (
578		"texlive-core",
579		"texlive-latexextra",
580		"ttf-dejavu",
581	);
582	check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
583
584	if ($pdf) {
585		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
586				   "noto-fonts-cjk", 2);
587	}
588
589	check_missing(\%map);
590
591	return if (!$need && !$optional);
592	printf("You should run:\n\n\tsudo pacman -S $install\n");
593}
594
595sub give_gentoo_hints()
596{
597	my %map = (
598		"sphinx_rtd_theme"	=> "dev-python/sphinx_rtd_theme",
599		"virtualenv"		=> "dev-python/virtualenv",
600		"dot"			=> "media-gfx/graphviz",
601		"convert"		=> "media-gfx/imagemagick",
602		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
603		"rsvg-convert"		=> "gnome-base/librsvg",
604	);
605
606	check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
607			   "media-fonts/dejavu", 2) if ($pdf);
608
609	if ($pdf) {
610		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
611				    "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
612				   "media-fonts/noto-cjk", 2);
613	}
614
615	check_missing(\%map);
616
617	return if (!$need && !$optional);
618
619	printf("You should run:\n\n");
620
621	my $imagemagick = "media-gfx/imagemagick svg png";
622	my $cairo = "media-gfx/graphviz cairo pdf";
623	my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
624	my $portage_cairo = "/etc/portage/package.use/graphviz";
625
626	if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
627		printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
628	}
629	if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
630		printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
631	}
632
633	printf("\tsudo emerge --ask $install\n");
634
635}
636
637sub check_distros()
638{
639	# Distro-specific hints
640	if ($system_release =~ /Red Hat Enterprise Linux/) {
641		give_redhat_hints;
642		return;
643	}
644	if ($system_release =~ /CentOS/) {
645		give_redhat_hints;
646		return;
647	}
648	if ($system_release =~ /Scientific Linux/) {
649		give_redhat_hints;
650		return;
651	}
652	if ($system_release =~ /Oracle Linux Server/) {
653		give_redhat_hints;
654		return;
655	}
656	if ($system_release =~ /Fedora/) {
657		give_redhat_hints;
658		return;
659	}
660	if ($system_release =~ /Ubuntu/) {
661		give_debian_hints;
662		return;
663	}
664	if ($system_release =~ /Debian/) {
665		give_debian_hints;
666		return;
667	}
668	if ($system_release =~ /openSUSE/) {
669		give_opensuse_hints;
670		return;
671	}
672	if ($system_release =~ /Mageia/) {
673		give_mageia_hints;
674		return;
675	}
676	if ($system_release =~ /OpenMandriva/) {
677		give_mageia_hints;
678		return;
679	}
680	if ($system_release =~ /Arch Linux/) {
681		give_arch_linux_hints;
682		return;
683	}
684	if ($system_release =~ /Gentoo/) {
685		give_gentoo_hints;
686		return;
687	}
688
689	#
690	# Fall-back to generic hint code for other distros
691	# That's far from ideal, specially for LaTeX dependencies.
692	#
693	my %map = (
694		"sphinx-build" => "sphinx"
695	);
696	check_missing_tex(2) if ($pdf);
697	check_missing(\%map);
698	print "I don't know distro $system_release.\n";
699	print "So, I can't provide you a hint with the install procedure.\n";
700	print "There are likely missing dependencies.\n";
701}
702
703#
704# Common dependencies
705#
706
707sub deactivate_help()
708{
709	printf "\nIf you want to exit the virtualenv, you can use:\n";
710	printf "\tdeactivate\n";
711}
712
713sub check_needs()
714{
715	# Check if Sphinx is already accessible from current environment
716	check_sphinx();
717
718	if ($system_release) {
719		print "Detected OS: $system_release.\n\n";
720	} else {
721		print "Unknown OS\n\n";
722	}
723
724	# Check python command line, trying first python3
725	$python_cmd = findprog("python3");
726	$python_cmd = check_program("python", 0) if (!$python_cmd);
727
728	# Check the type of virtual env, depending on Python version
729	if ($python_cmd) {
730		if ($virtualenv) {
731			my $tmp = qx($python_cmd --version 2>&1);
732			if ($tmp =~ m/(\d+\.)(\d+\.)/) {
733				if ($1 >= 3 && $2 >= 3) {
734					$need_venv = 1;		# python 3.3 or upper
735				} else {
736					$need_virtualenv = 1;
737				}
738				if ($1 < 3) {
739					# Complain if it finds python2 (or worse)
740					printf "Warning: python$1 support is deprecated. Use it with caution!\n";
741				}
742			} else {
743				die "Warning: couldn't identify $python_cmd version!";
744			}
745		} else {
746			add_package("python-sphinx", 0);
747		}
748	}
749
750	# Set virtualenv command line, if python < 3.3
751	my $virtualenv_cmd;
752	if ($need_virtualenv) {
753		$virtualenv_cmd = findprog("virtualenv-3");
754		$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
755		if (!$virtualenv_cmd) {
756			check_program("virtualenv", 0);
757			$virtualenv_cmd = "virtualenv";
758		}
759	}
760
761	# Check for needed programs/tools
762	check_perl_module("Pod::Usage", 0);
763	check_program("make", 0);
764	check_program("gcc", 0);
765	check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
766	check_program("dot", 1);
767	check_program("convert", 1);
768
769	# Extra PDF files - should use 2 for is_optional
770	check_program("xelatex", 2) if ($pdf);
771	check_program("rsvg-convert", 2) if ($pdf);
772	check_program("latexmk", 2) if ($pdf);
773
774	if ($need_sphinx || $rec_sphinx_upgrade) {
775		check_python_module("ensurepip", 0) if ($need_venv);
776	}
777
778	# Do distro-specific checks and output distro-install commands
779	check_distros();
780
781	if (!$python_cmd) {
782		if ($need == 1) {
783			die "Can't build as $need mandatory dependency is missing";
784		} elsif ($need) {
785			die "Can't build as $need mandatory dependencies are missing";
786		}
787	}
788
789	# Check if sphinx-build is called sphinx-build-3
790	if ($need_symlink) {
791		printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
792		       which("sphinx-build-3");
793	}
794
795	# NOTE: if the system has a too old Sphinx version installed,
796	# it will recommend installing a newer version using virtualenv
797
798	if ($need_sphinx || $rec_sphinx_upgrade) {
799		my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
800		my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
801
802		@activates = sort {$b cmp $a} @activates;
803		my ($activate, $ver);
804		foreach my $f (@activates) {
805			next if ($f lt $min_activate);
806
807			my $sphinx_cmd = $f;
808			$sphinx_cmd =~ s/activate/sphinx-build/;
809			next if (! -f $sphinx_cmd);
810
811			$ver = get_sphinx_version($sphinx_cmd);
812			if ($need_sphinx && ($ver ge $min_version)) {
813				$activate = $f;
814				last;
815			} elsif ($ver gt $cur_version) {
816				$activate = $f;
817				last;
818			}
819		}
820		if ($activate ne "") {
821			if ($need_sphinx) {
822				printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
823				printf "\t. $activate\n";
824				deactivate_help();
825				exit (1);
826			} else {
827				printf "\nYou may also use a newer Sphinx (version $ver) with:\n";
828				printf "\tdeactivate && . $activate\n";
829			}
830		} else {
831			my $rec_activate = "$virtenv_dir/bin/activate";
832
833			print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
834
835			$python_cmd = find_python_no_venv();
836
837			if ($need_venv) {
838				printf "\t$python_cmd -m venv $virtenv_dir\n";
839			} else {
840				printf "\t$virtualenv_cmd $virtenv_dir\n";
841			}
842			printf "\t. $rec_activate\n";
843			printf "\tpip install -r $requirement_file\n";
844			deactivate_help();
845
846			$need++ if (!$rec_sphinx_upgrade);
847		}
848	}
849	printf "\n";
850
851	print "All optional dependencies are met.\n" if (!$optional);
852
853	if ($need == 1) {
854		die "Can't build as $need mandatory dependency is missing";
855	} elsif ($need) {
856		die "Can't build as $need mandatory dependencies are missing";
857	}
858
859	print "Needed package dependencies are met.\n";
860}
861
862#
863# Main
864#
865
866while (@ARGV) {
867	my $arg = shift(@ARGV);
868
869	if ($arg eq "--no-virtualenv") {
870		$virtualenv = 0;
871	} elsif ($arg eq "--no-pdf"){
872		$pdf = 0;
873	} elsif ($arg eq "--version-check"){
874		$version_check = 1;
875	} else {
876		print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
877		print "Where:\n";
878		print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
879		print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
880		print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
881		exit -1;
882	}
883}
884
885#
886# Determine the system type. There's no standard unique way that would
887# work with all distros with a minimal package install. So, several
888# methods are used here.
889#
890# By default, it will use lsb_release function. If not available, it will
891# fail back to reading the known different places where the distro name
892# is stored
893#
894
895$system_release = qx(lsb_release -d) if which("lsb_release");
896$system_release =~ s/Description:\s*// if ($system_release);
897$system_release = catcheck("/etc/system-release") if !$system_release;
898$system_release = catcheck("/etc/redhat-release") if !$system_release;
899$system_release = catcheck("/etc/lsb-release") if !$system_release;
900$system_release = catcheck("/etc/gentoo-release") if !$system_release;
901
902# This seems more common than LSB these days
903if (!$system_release) {
904	my %os_var;
905	if (open IN, "cat /etc/os-release|") {
906		while (<IN>) {
907			if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
908				$os_var{$1}=$2;
909			}
910		}
911		$system_release = $os_var{"NAME"};
912		if (defined($os_var{"VERSION_ID"})) {
913			$system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
914		} else {
915			$system_release .= " " . $os_var{"VERSION"};
916		}
917	}
918}
919$system_release = catcheck("/etc/issue") if !$system_release;
920$system_release =~ s/\s+$//;
921
922check_needs;
923