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