xref: /openbmc/linux/scripts/sphinx-pre-install (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1c25ce589SFinn Behrens#!/usr/bin/env perl
2c942fddfSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
324071ac1SMauro Carvalho Chehabuse strict;
424071ac1SMauro Carvalho Chehab
52f9c5025SMauro Carvalho Chehab# Copyright (c) 2017-2020 Mauro Carvalho Chehab <mchehab@kernel.org>
624071ac1SMauro Carvalho Chehab#
724071ac1SMauro Carvalho Chehab
88c69b77aSMike Rapoportmy $prefix = "./";
98c69b77aSMike Rapoport$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
108c69b77aSMike Rapoport
118c69b77aSMike Rapoportmy $conf = $prefix . "Documentation/conf.py";
128c69b77aSMike Rapoportmy $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
1344f42165SMauro Carvalho Chehabmy $virtenv_prefix = "sphinx_";
145be33182SMauro Carvalho Chehab
1524071ac1SMauro Carvalho Chehab#
1624071ac1SMauro Carvalho Chehab# Static vars
1724071ac1SMauro Carvalho Chehab#
1824071ac1SMauro Carvalho Chehab
1924071ac1SMauro Carvalho Chehabmy %missing;
2024071ac1SMauro Carvalho Chehabmy $system_release;
2124071ac1SMauro Carvalho Chehabmy $need = 0;
2224071ac1SMauro Carvalho Chehabmy $optional = 0;
2324071ac1SMauro Carvalho Chehabmy $need_symlink = 0;
2424071ac1SMauro Carvalho Chehabmy $need_sphinx = 0;
25a5f785f1SMauro Carvalho Chehabmy $need_pip = 0;
262f9c5025SMauro Carvalho Chehabmy $need_virtualenv = 0;
27a5f785f1SMauro Carvalho Chehabmy $rec_sphinx_upgrade = 0;
286a5d7320SMauro Carvalho Chehabmy $verbose_warn_install = 1;
2924071ac1SMauro Carvalho Chehabmy $install = "";
3044f42165SMauro Carvalho Chehabmy $virtenv_dir = "";
312f9c5025SMauro Carvalho Chehabmy $python_cmd = "";
32a5f785f1SMauro Carvalho Chehabmy $activate_cmd;
3344f42165SMauro Carvalho Chehabmy $min_version;
342834a741SMauro Carvalho Chehabmy $cur_version;
351ef70cedSMauro Carvalho Chehabmy $rec_version = "1.7.9";	# PDF won't build here
361ef70cedSMauro Carvalho Chehabmy $min_pdf_version = "2.4.4";	# Min version where pdf builds
37a5f785f1SMauro Carvalho Chehabmy $latest_avail_ver;
38e5089912SMauro Carvalho Chehab
3924071ac1SMauro Carvalho Chehab#
4024071ac1SMauro Carvalho Chehab# Command line arguments
4124071ac1SMauro Carvalho Chehab#
4224071ac1SMauro Carvalho Chehab
4324071ac1SMauro Carvalho Chehabmy $pdf = 1;
4424071ac1SMauro Carvalho Chehabmy $virtualenv = 1;
459b88ad54SMauro Carvalho Chehabmy $version_check = 0;
4624071ac1SMauro Carvalho Chehab
4724071ac1SMauro Carvalho Chehab#
4824071ac1SMauro Carvalho Chehab# List of required texlive packages on Fedora and OpenSuse
4924071ac1SMauro Carvalho Chehab#
5024071ac1SMauro Carvalho Chehab
5124071ac1SMauro Carvalho Chehabmy %texlive = (
5224071ac1SMauro Carvalho Chehab	'amsfonts.sty'       => 'texlive-amsfonts',
5324071ac1SMauro Carvalho Chehab	'amsmath.sty'        => 'texlive-amsmath',
5424071ac1SMauro Carvalho Chehab	'amssymb.sty'        => 'texlive-amsfonts',
5524071ac1SMauro Carvalho Chehab	'amsthm.sty'         => 'texlive-amscls',
5624071ac1SMauro Carvalho Chehab	'anyfontsize.sty'    => 'texlive-anyfontsize',
5724071ac1SMauro Carvalho Chehab	'atbegshi.sty'       => 'texlive-oberdiek',
5824071ac1SMauro Carvalho Chehab	'bm.sty'             => 'texlive-tools',
5924071ac1SMauro Carvalho Chehab	'capt-of.sty'        => 'texlive-capt-of',
6024071ac1SMauro Carvalho Chehab	'cmap.sty'           => 'texlive-cmap',
6124071ac1SMauro Carvalho Chehab	'ecrm1000.tfm'       => 'texlive-ec',
6224071ac1SMauro Carvalho Chehab	'eqparbox.sty'       => 'texlive-eqparbox',
6324071ac1SMauro Carvalho Chehab	'eu1enc.def'         => 'texlive-euenc',
6424071ac1SMauro Carvalho Chehab	'fancybox.sty'       => 'texlive-fancybox',
6524071ac1SMauro Carvalho Chehab	'fancyvrb.sty'       => 'texlive-fancyvrb',
6624071ac1SMauro Carvalho Chehab	'float.sty'          => 'texlive-float',
6724071ac1SMauro Carvalho Chehab	'fncychap.sty'       => 'texlive-fncychap',
6824071ac1SMauro Carvalho Chehab	'footnote.sty'       => 'texlive-mdwtools',
6924071ac1SMauro Carvalho Chehab	'framed.sty'         => 'texlive-framed',
7024071ac1SMauro Carvalho Chehab	'luatex85.sty'       => 'texlive-luatex85',
7124071ac1SMauro Carvalho Chehab	'multirow.sty'       => 'texlive-multirow',
7224071ac1SMauro Carvalho Chehab	'needspace.sty'      => 'texlive-needspace',
7324071ac1SMauro Carvalho Chehab	'palatino.sty'       => 'texlive-psnfss',
7424071ac1SMauro Carvalho Chehab	'parskip.sty'        => 'texlive-parskip',
7524071ac1SMauro Carvalho Chehab	'polyglossia.sty'    => 'texlive-polyglossia',
7624071ac1SMauro Carvalho Chehab	'tabulary.sty'       => 'texlive-tabulary',
7724071ac1SMauro Carvalho Chehab	'threeparttable.sty' => 'texlive-threeparttable',
7824071ac1SMauro Carvalho Chehab	'titlesec.sty'       => 'texlive-titlesec',
7924071ac1SMauro Carvalho Chehab	'ucs.sty'            => 'texlive-ucs',
8024071ac1SMauro Carvalho Chehab	'upquote.sty'        => 'texlive-upquote',
8124071ac1SMauro Carvalho Chehab	'wrapfig.sty'        => 'texlive-wrapfig',
827baab965SMauro Carvalho Chehab	'ctexhook.sty'       => 'texlive-ctex',
8324071ac1SMauro Carvalho Chehab);
8424071ac1SMauro Carvalho Chehab
8524071ac1SMauro Carvalho Chehab#
8624071ac1SMauro Carvalho Chehab# Subroutines that checks if a feature exists
8724071ac1SMauro Carvalho Chehab#
8824071ac1SMauro Carvalho Chehab
8924071ac1SMauro Carvalho Chehabsub check_missing(%)
9024071ac1SMauro Carvalho Chehab{
9124071ac1SMauro Carvalho Chehab	my %map = %{$_[0]};
9224071ac1SMauro Carvalho Chehab
9324071ac1SMauro Carvalho Chehab	foreach my $prog (sort keys %missing) {
9424071ac1SMauro Carvalho Chehab		my $is_optional = $missing{$prog};
9524071ac1SMauro Carvalho Chehab
9656e5a633SMauro Carvalho Chehab		# At least on some LTS distros like CentOS 7, texlive doesn't
9756e5a633SMauro Carvalho Chehab		# provide all packages we need. When such distros are
9856e5a633SMauro Carvalho Chehab		# detected, we have to disable PDF output.
9956e5a633SMauro Carvalho Chehab		#
10056e5a633SMauro Carvalho Chehab		# So, we need to ignore the packages that distros would
10156e5a633SMauro Carvalho Chehab		# need for LaTeX to work
10256e5a633SMauro Carvalho Chehab		if ($is_optional == 2 && !$pdf) {
10356e5a633SMauro Carvalho Chehab			$optional--;
10456e5a633SMauro Carvalho Chehab			next;
10556e5a633SMauro Carvalho Chehab		}
10656e5a633SMauro Carvalho Chehab
1076a5d7320SMauro Carvalho Chehab		if ($verbose_warn_install) {
10824071ac1SMauro Carvalho Chehab			if ($is_optional) {
10924071ac1SMauro Carvalho Chehab				print "Warning: better to also install \"$prog\".\n";
11024071ac1SMauro Carvalho Chehab			} else {
11124071ac1SMauro Carvalho Chehab				print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
11224071ac1SMauro Carvalho Chehab			}
1136a5d7320SMauro Carvalho Chehab		}
11424071ac1SMauro Carvalho Chehab		if (defined($map{$prog})) {
11524071ac1SMauro Carvalho Chehab			$install .= " " . $map{$prog};
11624071ac1SMauro Carvalho Chehab		} else {
11724071ac1SMauro Carvalho Chehab			$install .= " " . $prog;
11824071ac1SMauro Carvalho Chehab		}
11924071ac1SMauro Carvalho Chehab	}
12024071ac1SMauro Carvalho Chehab
12124071ac1SMauro Carvalho Chehab	$install =~ s/^\s//;
12224071ac1SMauro Carvalho Chehab}
12324071ac1SMauro Carvalho Chehab
12424071ac1SMauro Carvalho Chehabsub add_package($$)
12524071ac1SMauro Carvalho Chehab{
12624071ac1SMauro Carvalho Chehab	my $package = shift;
12724071ac1SMauro Carvalho Chehab	my $is_optional = shift;
12824071ac1SMauro Carvalho Chehab
12924071ac1SMauro Carvalho Chehab	$missing{$package} = $is_optional;
13024071ac1SMauro Carvalho Chehab	if ($is_optional) {
13124071ac1SMauro Carvalho Chehab		$optional++;
13224071ac1SMauro Carvalho Chehab	} else {
13324071ac1SMauro Carvalho Chehab		$need++;
13424071ac1SMauro Carvalho Chehab	}
13524071ac1SMauro Carvalho Chehab}
13624071ac1SMauro Carvalho Chehab
13724071ac1SMauro Carvalho Chehabsub check_missing_file($$$)
13824071ac1SMauro Carvalho Chehab{
139ff8fdb36SJeremy MAURO	my $files = shift;
14024071ac1SMauro Carvalho Chehab	my $package = shift;
14124071ac1SMauro Carvalho Chehab	my $is_optional = shift;
14224071ac1SMauro Carvalho Chehab
143ff8fdb36SJeremy MAURO	for (@$files) {
144ff8fdb36SJeremy MAURO		return if(-e $_);
145ff8fdb36SJeremy MAURO	}
14624071ac1SMauro Carvalho Chehab
14724071ac1SMauro Carvalho Chehab	add_package($package, $is_optional);
14824071ac1SMauro Carvalho Chehab}
14924071ac1SMauro Carvalho Chehab
15024071ac1SMauro Carvalho Chehabsub findprog($)
15124071ac1SMauro Carvalho Chehab{
15224071ac1SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
15324071ac1SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
15424071ac1SMauro Carvalho Chehab	}
15524071ac1SMauro Carvalho Chehab}
15624071ac1SMauro Carvalho Chehab
157412b09ddSMauro Carvalho Chehabsub find_python_no_venv()
158412b09ddSMauro Carvalho Chehab{
159412b09ddSMauro Carvalho Chehab	my $prog = shift;
160412b09ddSMauro Carvalho Chehab
161412b09ddSMauro Carvalho Chehab	my $cur_dir = qx(pwd);
162412b09ddSMauro Carvalho Chehab	$cur_dir =~ s/\s+$//;
163412b09ddSMauro Carvalho Chehab
164412b09ddSMauro Carvalho Chehab	foreach my $dir (split(/:/, $ENV{PATH})) {
165412b09ddSMauro Carvalho Chehab		next if ($dir =~ m,($cur_dir)/sphinx,);
166412b09ddSMauro Carvalho Chehab		return "$dir/python3" if(-x "$dir/python3");
167412b09ddSMauro Carvalho Chehab	}
168412b09ddSMauro Carvalho Chehab	foreach my $dir (split(/:/, $ENV{PATH})) {
169412b09ddSMauro Carvalho Chehab		next if ($dir =~ m,($cur_dir)/sphinx,);
170412b09ddSMauro Carvalho Chehab		return "$dir/python" if(-x "$dir/python");
171412b09ddSMauro Carvalho Chehab	}
172412b09ddSMauro Carvalho Chehab	return "python";
173412b09ddSMauro Carvalho Chehab}
174412b09ddSMauro Carvalho Chehab
17524071ac1SMauro Carvalho Chehabsub check_program($$)
17624071ac1SMauro Carvalho Chehab{
17724071ac1SMauro Carvalho Chehab	my $prog = shift;
17824071ac1SMauro Carvalho Chehab	my $is_optional = shift;
17924071ac1SMauro Carvalho Chehab
1802f9c5025SMauro Carvalho Chehab	return $prog if findprog($prog);
18124071ac1SMauro Carvalho Chehab
18224071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
18324071ac1SMauro Carvalho Chehab}
18424071ac1SMauro Carvalho Chehab
18524071ac1SMauro Carvalho Chehabsub check_perl_module($$)
18624071ac1SMauro Carvalho Chehab{
18724071ac1SMauro Carvalho Chehab	my $prog = shift;
18824071ac1SMauro Carvalho Chehab	my $is_optional = shift;
18924071ac1SMauro Carvalho Chehab
19024071ac1SMauro Carvalho Chehab	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
19124071ac1SMauro Carvalho Chehab	return if ($err == 0);
19224071ac1SMauro Carvalho Chehab
19324071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
19424071ac1SMauro Carvalho Chehab}
19524071ac1SMauro Carvalho Chehab
19624071ac1SMauro Carvalho Chehabsub check_python_module($$)
19724071ac1SMauro Carvalho Chehab{
19824071ac1SMauro Carvalho Chehab	my $prog = shift;
19924071ac1SMauro Carvalho Chehab	my $is_optional = shift;
20024071ac1SMauro Carvalho Chehab
2012f9c5025SMauro Carvalho Chehab	return if (!$python_cmd);
2022f9c5025SMauro Carvalho Chehab
2032f9c5025SMauro Carvalho Chehab	my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
20424071ac1SMauro Carvalho Chehab	return if ($err == 0);
20524071ac1SMauro Carvalho Chehab
20624071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
20724071ac1SMauro Carvalho Chehab}
20824071ac1SMauro Carvalho Chehab
20924071ac1SMauro Carvalho Chehabsub check_rpm_missing($$)
21024071ac1SMauro Carvalho Chehab{
21124071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
21224071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
21324071ac1SMauro Carvalho Chehab
21424071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
21524071ac1SMauro Carvalho Chehab		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
21624071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
21724071ac1SMauro Carvalho Chehab	}
21824071ac1SMauro Carvalho Chehab}
21924071ac1SMauro Carvalho Chehab
22024071ac1SMauro Carvalho Chehabsub check_pacman_missing($$)
22124071ac1SMauro Carvalho Chehab{
22224071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
22324071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
22424071ac1SMauro Carvalho Chehab
22524071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
22624071ac1SMauro Carvalho Chehab		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
22724071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
22824071ac1SMauro Carvalho Chehab	}
22924071ac1SMauro Carvalho Chehab}
23024071ac1SMauro Carvalho Chehab
23124071ac1SMauro Carvalho Chehabsub check_missing_tex($)
23224071ac1SMauro Carvalho Chehab{
23324071ac1SMauro Carvalho Chehab	my $is_optional = shift;
23424071ac1SMauro Carvalho Chehab	my $kpsewhich = findprog("kpsewhich");
23524071ac1SMauro Carvalho Chehab
23624071ac1SMauro Carvalho Chehab	foreach my $prog(keys %texlive) {
23724071ac1SMauro Carvalho Chehab		my $package = $texlive{$prog};
23824071ac1SMauro Carvalho Chehab		if (!$kpsewhich) {
23924071ac1SMauro Carvalho Chehab			add_package($package, $is_optional);
24024071ac1SMauro Carvalho Chehab			next;
24124071ac1SMauro Carvalho Chehab		}
24224071ac1SMauro Carvalho Chehab		my $file = qx($kpsewhich $prog);
24324071ac1SMauro Carvalho Chehab		add_package($package, $is_optional) if ($file =~ /^\s*$/);
24424071ac1SMauro Carvalho Chehab	}
24524071ac1SMauro Carvalho Chehab}
24624071ac1SMauro Carvalho Chehab
24777d09ad9SMauro Carvalho Chehabsub get_sphinx_fname()
24824071ac1SMauro Carvalho Chehab{
24977d09ad9SMauro Carvalho Chehab	my $fname = "sphinx-build";
25077d09ad9SMauro Carvalho Chehab	return $fname if findprog($fname);
25124071ac1SMauro Carvalho Chehab
25277d09ad9SMauro Carvalho Chehab	$fname = "sphinx-build-3";
25377d09ad9SMauro Carvalho Chehab	if (findprog($fname)) {
25424071ac1SMauro Carvalho Chehab		$need_symlink = 1;
25577d09ad9SMauro Carvalho Chehab		return $fname;
25624071ac1SMauro Carvalho Chehab	}
25724071ac1SMauro Carvalho Chehab
25877d09ad9SMauro Carvalho Chehab	return "";
25977d09ad9SMauro Carvalho Chehab}
26077d09ad9SMauro Carvalho Chehab
261a8b380c3SMauro Carvalho Chehabsub get_sphinx_version($)
262a8b380c3SMauro Carvalho Chehab{
263a8b380c3SMauro Carvalho Chehab	my $cmd = shift;
264a8b380c3SMauro Carvalho Chehab	my $ver;
265a8b380c3SMauro Carvalho Chehab
266a8b380c3SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
267a8b380c3SMauro Carvalho Chehab	while (<IN>) {
268e9dfeed2SJonathan Corbet		if (m/^\s*sphinx-build\s+([\d\.]+)((\+\/[\da-f]+)|(b\d+))?$/) {
269a8b380c3SMauro Carvalho Chehab			$ver=$1;
270a8b380c3SMauro Carvalho Chehab			last;
271a8b380c3SMauro Carvalho Chehab		}
272a8b380c3SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
273a8b380c3SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
274a8b380c3SMauro Carvalho Chehab			$ver=$1;
275a8b380c3SMauro Carvalho Chehab			last;
276a8b380c3SMauro Carvalho Chehab		}
277a8b380c3SMauro Carvalho Chehab	}
278a8b380c3SMauro Carvalho Chehab	close IN;
279a8b380c3SMauro Carvalho Chehab	return $ver;
280a8b380c3SMauro Carvalho Chehab}
281a8b380c3SMauro Carvalho Chehab
28277d09ad9SMauro Carvalho Chehabsub check_sphinx()
28377d09ad9SMauro Carvalho Chehab{
2841ef70cedSMauro Carvalho Chehab	my $default_version;
28577d09ad9SMauro Carvalho Chehab
28677d09ad9SMauro Carvalho Chehab	open IN, $conf or die "Can't open $conf";
28777d09ad9SMauro Carvalho Chehab	while (<IN>) {
28877d09ad9SMauro Carvalho Chehab		if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
28977d09ad9SMauro Carvalho Chehab			$min_version=$1;
29077d09ad9SMauro Carvalho Chehab			last;
29177d09ad9SMauro Carvalho Chehab		}
29277d09ad9SMauro Carvalho Chehab	}
29377d09ad9SMauro Carvalho Chehab	close IN;
29477d09ad9SMauro Carvalho Chehab
29577d09ad9SMauro Carvalho Chehab	die "Can't get needs_sphinx version from $conf" if (!$min_version);
29677d09ad9SMauro Carvalho Chehab
29777d09ad9SMauro Carvalho Chehab	open IN, $requirement_file or die "Can't open $requirement_file";
29877d09ad9SMauro Carvalho Chehab	while (<IN>) {
29977d09ad9SMauro Carvalho Chehab		if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
3001ef70cedSMauro Carvalho Chehab			$default_version=$1;
30177d09ad9SMauro Carvalho Chehab			last;
30277d09ad9SMauro Carvalho Chehab		}
30377d09ad9SMauro Carvalho Chehab	}
30477d09ad9SMauro Carvalho Chehab	close IN;
30577d09ad9SMauro Carvalho Chehab
3061ef70cedSMauro Carvalho Chehab	die "Can't get default sphinx version from $requirement_file" if (!$default_version);
30777d09ad9SMauro Carvalho Chehab
3081ef70cedSMauro Carvalho Chehab	$virtenv_dir = $virtenv_prefix . $default_version;
30977d09ad9SMauro Carvalho Chehab
31077d09ad9SMauro Carvalho Chehab	my $sphinx = get_sphinx_fname();
3112f9c5025SMauro Carvalho Chehab	if ($sphinx eq "") {
3122f9c5025SMauro Carvalho Chehab		$need_sphinx = 1;
3132f9c5025SMauro Carvalho Chehab		return;
3142f9c5025SMauro Carvalho Chehab	}
31577d09ad9SMauro Carvalho Chehab
316a8b380c3SMauro Carvalho Chehab	$cur_version = get_sphinx_version($sphinx);
317a8b380c3SMauro Carvalho Chehab	die ("$sphinx returned an error") if (!$cur_version);
31877d09ad9SMauro Carvalho Chehab
31977d09ad9SMauro Carvalho Chehab	die "$sphinx didn't return its version" if (!$cur_version);
32077d09ad9SMauro Carvalho Chehab
32177d09ad9SMauro Carvalho Chehab	if ($cur_version lt $min_version) {
3229b88ad54SMauro Carvalho Chehab		printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
3231ef70cedSMauro Carvalho Chehab		       $cur_version, $min_version, $default_version;
32477d09ad9SMauro Carvalho Chehab		$need_sphinx = 1;
32577d09ad9SMauro Carvalho Chehab		return;
32677d09ad9SMauro Carvalho Chehab	}
32777d09ad9SMauro Carvalho Chehab
328e5089912SMauro Carvalho Chehab	return if ($cur_version lt $rec_version);
3299b88ad54SMauro Carvalho Chehab
3309b88ad54SMauro Carvalho Chehab	# On version check mode, just assume Sphinx has all mandatory deps
3319b88ad54SMauro Carvalho Chehab	exit (0) if ($version_check);
33224071ac1SMauro Carvalho Chehab}
33324071ac1SMauro Carvalho Chehab
33424071ac1SMauro Carvalho Chehab#
33524071ac1SMauro Carvalho Chehab# Ancillary subroutines
33624071ac1SMauro Carvalho Chehab#
33724071ac1SMauro Carvalho Chehab
33824071ac1SMauro Carvalho Chehabsub catcheck($)
33924071ac1SMauro Carvalho Chehab{
34024071ac1SMauro Carvalho Chehab  my $res = "";
34124071ac1SMauro Carvalho Chehab  $res = qx(cat $_[0]) if (-r $_[0]);
34224071ac1SMauro Carvalho Chehab  return $res;
34324071ac1SMauro Carvalho Chehab}
34424071ac1SMauro Carvalho Chehab
34524071ac1SMauro Carvalho Chehabsub which($)
34624071ac1SMauro Carvalho Chehab{
34724071ac1SMauro Carvalho Chehab	my $file = shift;
34824071ac1SMauro Carvalho Chehab	my @path = split ":", $ENV{PATH};
34924071ac1SMauro Carvalho Chehab
35024071ac1SMauro Carvalho Chehab	foreach my $dir(@path) {
35124071ac1SMauro Carvalho Chehab		my $name = $dir.'/'.$file;
35224071ac1SMauro Carvalho Chehab		return $name if (-x $name );
35324071ac1SMauro Carvalho Chehab	}
35424071ac1SMauro Carvalho Chehab	return undef;
35524071ac1SMauro Carvalho Chehab}
35624071ac1SMauro Carvalho Chehab
35724071ac1SMauro Carvalho Chehab#
35824071ac1SMauro Carvalho Chehab# Subroutines that check distro-specific hints
35924071ac1SMauro Carvalho Chehab#
36024071ac1SMauro Carvalho Chehab
36124071ac1SMauro Carvalho Chehabsub give_debian_hints()
36224071ac1SMauro Carvalho Chehab{
36324071ac1SMauro Carvalho Chehab	my %map = (
36424071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
3652f9c5025SMauro Carvalho Chehab		"ensurepip"		=> "python3-venv",
36624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "virtualenv",
36724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
36824071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
36924071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-modules",
37024071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex",
3718e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-bin",
37224071ac1SMauro Carvalho Chehab	);
37324071ac1SMauro Carvalho Chehab
37424071ac1SMauro Carvalho Chehab	if ($pdf) {
37587d6576dSMauro Carvalho Chehab		check_missing_file(["/usr/share/texlive/texmf-dist/tex/latex/ctex/ctexhook.sty"],
37687d6576dSMauro Carvalho Chehab				   "texlive-lang-chinese", 2);
37787d6576dSMauro Carvalho Chehab
378ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
37956e5a633SMauro Carvalho Chehab				   "fonts-dejavu", 2);
38027eed923SMauro Carvalho Chehab
3819692f2fdSJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
382bfc7f428SMauro Carvalho Chehab				    "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
3839692f2fdSJeremy MAURO				    "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
38427eed923SMauro Carvalho Chehab				   "fonts-noto-cjk", 2);
38524071ac1SMauro Carvalho Chehab	}
38624071ac1SMauro Carvalho Chehab
38756e5a633SMauro Carvalho Chehab	check_program("dvipng", 2) if ($pdf);
38824071ac1SMauro Carvalho Chehab	check_missing(\%map);
38924071ac1SMauro Carvalho Chehab
39024071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
3916a5d7320SMauro Carvalho Chehab	printf("You should run:\n") if ($verbose_warn_install);
3926a5d7320SMauro Carvalho Chehab	printf("\n\tsudo apt-get install $install\n");
39324071ac1SMauro Carvalho Chehab}
39424071ac1SMauro Carvalho Chehab
39524071ac1SMauro Carvalho Chehabsub give_redhat_hints()
39624071ac1SMauro Carvalho Chehab{
39724071ac1SMauro Carvalho Chehab	my %map = (
39824071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
39924071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
40024071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
40124071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
40224071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
40324071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
4048e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-tools",
40524071ac1SMauro Carvalho Chehab	);
40624071ac1SMauro Carvalho Chehab
4075d88953cSMauro Carvalho Chehab	my @fedora26_opt_pkgs = (
4085d88953cSMauro Carvalho Chehab		"graphviz-gd",		# Fedora 26: needed for PDF support
4095d88953cSMauro Carvalho Chehab	);
4105d88953cSMauro Carvalho Chehab
41124071ac1SMauro Carvalho Chehab	my @fedora_tex_pkgs = (
41224071ac1SMauro Carvalho Chehab		"texlive-collection-fontsrecommended",
41324071ac1SMauro Carvalho Chehab		"texlive-collection-latex",
41427eed923SMauro Carvalho Chehab		"texlive-xecjk",
41524071ac1SMauro Carvalho Chehab		"dejavu-sans-fonts",
41624071ac1SMauro Carvalho Chehab		"dejavu-serif-fonts",
41724071ac1SMauro Carvalho Chehab		"dejavu-sans-mono-fonts",
41824071ac1SMauro Carvalho Chehab	);
41924071ac1SMauro Carvalho Chehab
4209b756a9dSMauro Carvalho Chehab	#
4219b756a9dSMauro Carvalho Chehab	# Checks valid for RHEL/CentOS version 7.x.
4229b756a9dSMauro Carvalho Chehab	#
42356e5a633SMauro Carvalho Chehab	my $old = 0;
42456e5a633SMauro Carvalho Chehab	my $rel;
42556e5a633SMauro Carvalho Chehab	$rel = $1 if ($system_release =~ /release\s+(\d+)/);
42656e5a633SMauro Carvalho Chehab
427b308467cSMauro Carvalho Chehab	if (!($system_release =~ /Fedora/)) {
4289b756a9dSMauro Carvalho Chehab		$map{"virtualenv"} = "python-virtualenv";
42956e5a633SMauro Carvalho Chehab
43056e5a633SMauro Carvalho Chehab		if ($rel && $rel < 8) {
43156e5a633SMauro Carvalho Chehab			$old = 1;
43256e5a633SMauro Carvalho Chehab			$pdf = 0;
43356e5a633SMauro Carvalho Chehab
43456e5a633SMauro Carvalho Chehab			printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
43556e5a633SMauro Carvalho Chehab			printf("If you want to build PDF, please read:\n");
43656e5a633SMauro Carvalho Chehab			printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
43756e5a633SMauro Carvalho Chehab		}
43856e5a633SMauro Carvalho Chehab	} else {
43956e5a633SMauro Carvalho Chehab		if ($rel && $rel < 26) {
44056e5a633SMauro Carvalho Chehab			$old = 1;
44156e5a633SMauro Carvalho Chehab		}
44256e5a633SMauro Carvalho Chehab	}
44356e5a633SMauro Carvalho Chehab	if (!$rel) {
44456e5a633SMauro Carvalho Chehab		printf("Couldn't identify release number\n");
44556e5a633SMauro Carvalho Chehab		$old = 1;
44656e5a633SMauro Carvalho Chehab		$pdf = 0;
4479b756a9dSMauro Carvalho Chehab	}
4489b756a9dSMauro Carvalho Chehab
44927eed923SMauro Carvalho Chehab	if ($pdf) {
450ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
45127eed923SMauro Carvalho Chehab				   "google-noto-sans-cjk-ttc-fonts", 2);
45227eed923SMauro Carvalho Chehab	}
45327eed923SMauro Carvalho Chehab
45456e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
45556e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
45656e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
45724071ac1SMauro Carvalho Chehab	check_missing(\%map);
45824071ac1SMauro Carvalho Chehab
45924071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
4609b756a9dSMauro Carvalho Chehab
46156e5a633SMauro Carvalho Chehab	if (!$old) {
4629b756a9dSMauro Carvalho Chehab		# dnf, for Fedora 18+
4636a5d7320SMauro Carvalho Chehab		printf("You should run:\n") if ($verbose_warn_install);
4646a5d7320SMauro Carvalho Chehab		printf("\n\tsudo dnf install -y $install\n");
4659b756a9dSMauro Carvalho Chehab	} else {
4669b756a9dSMauro Carvalho Chehab		# yum, for RHEL (and clones) or Fedora version < 18
4676a5d7320SMauro Carvalho Chehab		printf("You should run:\n") if ($verbose_warn_install);
4686a5d7320SMauro Carvalho Chehab		printf("\n\tsudo yum install -y $install\n");
4699b756a9dSMauro Carvalho Chehab	}
47024071ac1SMauro Carvalho Chehab}
47124071ac1SMauro Carvalho Chehab
47224071ac1SMauro Carvalho Chehabsub give_opensuse_hints()
47324071ac1SMauro Carvalho Chehab{
47424071ac1SMauro Carvalho Chehab	my %map = (
47524071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
47624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
47724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
47824071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
47924071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
48024071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
48124071ac1SMauro Carvalho Chehab	);
48224071ac1SMauro Carvalho Chehab
483b3df6223SMauro Carvalho Chehab	# On Tumbleweed, this package is also named rsvg-convert
484b3df6223SMauro Carvalho Chehab	$map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
485b3df6223SMauro Carvalho Chehab
48624071ac1SMauro Carvalho Chehab	my @suse_tex_pkgs = (
48724071ac1SMauro Carvalho Chehab		"texlive-babel-english",
48824071ac1SMauro Carvalho Chehab		"texlive-caption",
48924071ac1SMauro Carvalho Chehab		"texlive-colortbl",
49024071ac1SMauro Carvalho Chehab		"texlive-courier",
49124071ac1SMauro Carvalho Chehab		"texlive-dvips",
49224071ac1SMauro Carvalho Chehab		"texlive-helvetic",
49324071ac1SMauro Carvalho Chehab		"texlive-makeindex",
49424071ac1SMauro Carvalho Chehab		"texlive-metafont",
49524071ac1SMauro Carvalho Chehab		"texlive-metapost",
49624071ac1SMauro Carvalho Chehab		"texlive-palatino",
49724071ac1SMauro Carvalho Chehab		"texlive-preview",
49824071ac1SMauro Carvalho Chehab		"texlive-times",
49924071ac1SMauro Carvalho Chehab		"texlive-zapfchan",
50024071ac1SMauro Carvalho Chehab		"texlive-zapfding",
50124071ac1SMauro Carvalho Chehab	);
50224071ac1SMauro Carvalho Chehab
503353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-latexmk-bin";
504353290a9SMauro Carvalho Chehab
50527eed923SMauro Carvalho Chehab	# FIXME: add support for installing CJK fonts
50627eed923SMauro Carvalho Chehab	#
50727eed923SMauro Carvalho Chehab	# I tried hard, but was unable to find a way to install
50827eed923SMauro Carvalho Chehab	# "Noto Sans CJK SC" on openSUSE
50927eed923SMauro Carvalho Chehab
51056e5a633SMauro Carvalho Chehab	check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
51156e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
51224071ac1SMauro Carvalho Chehab	check_missing(\%map);
51324071ac1SMauro Carvalho Chehab
51424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
5156a5d7320SMauro Carvalho Chehab	printf("You should run:\n") if ($verbose_warn_install);
5166a5d7320SMauro Carvalho Chehab	printf("\n\tsudo zypper install --no-recommends $install\n");
51724071ac1SMauro Carvalho Chehab}
51824071ac1SMauro Carvalho Chehab
519800d408aSMauro Carvalho Chehabsub give_mageia_hints()
520800d408aSMauro Carvalho Chehab{
521800d408aSMauro Carvalho Chehab	my %map = (
522800d408aSMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
523800d408aSMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
524800d408aSMauro Carvalho Chehab		"dot"			=> "graphviz",
525800d408aSMauro Carvalho Chehab		"convert"		=> "ImageMagick",
526800d408aSMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
527800d408aSMauro Carvalho Chehab		"xelatex"		=> "texlive",
528d6ebf189SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2",
529800d408aSMauro Carvalho Chehab	);
530800d408aSMauro Carvalho Chehab
531800d408aSMauro Carvalho Chehab	my @tex_pkgs = (
532800d408aSMauro Carvalho Chehab		"texlive-fontsextra",
533800d408aSMauro Carvalho Chehab	);
534800d408aSMauro Carvalho Chehab
535353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-collection-basic";
536353290a9SMauro Carvalho Chehab
537d6ebf189SMauro Carvalho Chehab	my $packager_cmd;
538d6ebf189SMauro Carvalho Chehab	my $noto_sans;
539d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
540d6ebf189SMauro Carvalho Chehab		$packager_cmd = "dnf install";
541d6ebf189SMauro Carvalho Chehab		$noto_sans = "noto-sans-cjk-fonts";
542d6ebf189SMauro Carvalho Chehab		@tex_pkgs = ( "texlive-collection-fontsextra" );
543d6ebf189SMauro Carvalho Chehab	} else {
544d6ebf189SMauro Carvalho Chehab		$packager_cmd = "urpmi";
545d6ebf189SMauro Carvalho Chehab		$noto_sans = "google-noto-sans-cjk-ttc-fonts";
546d6ebf189SMauro Carvalho Chehab	}
547d6ebf189SMauro Carvalho Chehab
548d6ebf189SMauro Carvalho Chehab
54927eed923SMauro Carvalho Chehab	if ($pdf) {
550d6ebf189SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
551d6ebf189SMauro Carvalho Chehab				    "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
552d6ebf189SMauro Carvalho Chehab				   $noto_sans, 2);
55327eed923SMauro Carvalho Chehab	}
55427eed923SMauro Carvalho Chehab
55556e5a633SMauro Carvalho Chehab	check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
556800d408aSMauro Carvalho Chehab	check_missing(\%map);
557800d408aSMauro Carvalho Chehab
558800d408aSMauro Carvalho Chehab	return if (!$need && !$optional);
5596a5d7320SMauro Carvalho Chehab	printf("You should run:\n") if ($verbose_warn_install);
5606a5d7320SMauro Carvalho Chehab	printf("\n\tsudo $packager_cmd $install\n");
561800d408aSMauro Carvalho Chehab}
562800d408aSMauro Carvalho Chehab
56324071ac1SMauro Carvalho Chehabsub give_arch_linux_hints()
56424071ac1SMauro Carvalho Chehab{
56524071ac1SMauro Carvalho Chehab	my %map = (
56624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python-virtualenv",
56724071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
56824071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
56924071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-bin",
5700d0da9aaSLouis Taylor		"latexmk"		=> "texlive-core",
5718e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "extra/librsvg",
57224071ac1SMauro Carvalho Chehab	);
57324071ac1SMauro Carvalho Chehab
57424071ac1SMauro Carvalho Chehab	my @archlinux_tex_pkgs = (
57524071ac1SMauro Carvalho Chehab		"texlive-core",
57624071ac1SMauro Carvalho Chehab		"texlive-latexextra",
57724071ac1SMauro Carvalho Chehab		"ttf-dejavu",
57824071ac1SMauro Carvalho Chehab	);
57956e5a633SMauro Carvalho Chehab	check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
58056e5a633SMauro Carvalho Chehab
58127eed923SMauro Carvalho Chehab	if ($pdf) {
582ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
58327eed923SMauro Carvalho Chehab				   "noto-fonts-cjk", 2);
58427eed923SMauro Carvalho Chehab	}
58527eed923SMauro Carvalho Chehab
58624071ac1SMauro Carvalho Chehab	check_missing(\%map);
58724071ac1SMauro Carvalho Chehab
58824071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
5896a5d7320SMauro Carvalho Chehab	printf("You should run:\n") if ($verbose_warn_install);
5906a5d7320SMauro Carvalho Chehab	printf("\n\tsudo pacman -S $install\n");
59124071ac1SMauro Carvalho Chehab}
59224071ac1SMauro Carvalho Chehab
59324071ac1SMauro Carvalho Chehabsub give_gentoo_hints()
59424071ac1SMauro Carvalho Chehab{
59524071ac1SMauro Carvalho Chehab	my %map = (
59624071ac1SMauro Carvalho Chehab		"virtualenv"		=> "dev-python/virtualenv",
59724071ac1SMauro Carvalho Chehab		"dot"			=> "media-gfx/graphviz",
59824071ac1SMauro Carvalho Chehab		"convert"		=> "media-gfx/imagemagick",
59924071ac1SMauro Carvalho Chehab		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
6008e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "gnome-base/librsvg",
60124071ac1SMauro Carvalho Chehab	);
60224071ac1SMauro Carvalho Chehab
603ff8fdb36SJeremy MAURO	check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
60456e5a633SMauro Carvalho Chehab			   "media-fonts/dejavu", 2) if ($pdf);
60524071ac1SMauro Carvalho Chehab
60627eed923SMauro Carvalho Chehab	if ($pdf) {
607e45a6317SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
608e45a6317SMauro Carvalho Chehab				    "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
60927eed923SMauro Carvalho Chehab				   "media-fonts/noto-cjk", 2);
61027eed923SMauro Carvalho Chehab	}
61127eed923SMauro Carvalho Chehab
61224071ac1SMauro Carvalho Chehab	check_missing(\%map);
61324071ac1SMauro Carvalho Chehab
61424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
615bba1e4cbSMauro Carvalho Chehab
6166a5d7320SMauro Carvalho Chehab	printf("You should run:\n") if ($verbose_warn_install);
6176a5d7320SMauro Carvalho Chehab	printf("\n");
6184ea96d57SMauro Carvalho Chehab
6194ea96d57SMauro Carvalho Chehab	my $imagemagick = "media-gfx/imagemagick svg png";
6204ea96d57SMauro Carvalho Chehab	my $cairo = "media-gfx/graphviz cairo pdf";
6214ea96d57SMauro Carvalho Chehab	my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
6224ea96d57SMauro Carvalho Chehab	my $portage_cairo = "/etc/portage/package.use/graphviz";
6234ea96d57SMauro Carvalho Chehab
624e45a6317SMauro Carvalho Chehab	if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
6254ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
6264ea96d57SMauro Carvalho Chehab	}
627e45a6317SMauro Carvalho Chehab	if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
6284ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
6294ea96d57SMauro Carvalho Chehab	}
6304ea96d57SMauro Carvalho Chehab
631bba1e4cbSMauro Carvalho Chehab	printf("\tsudo emerge --ask $install\n");
632bba1e4cbSMauro Carvalho Chehab
63324071ac1SMauro Carvalho Chehab}
63424071ac1SMauro Carvalho Chehab
63524071ac1SMauro Carvalho Chehabsub check_distros()
63624071ac1SMauro Carvalho Chehab{
63724071ac1SMauro Carvalho Chehab	# Distro-specific hints
63824071ac1SMauro Carvalho Chehab	if ($system_release =~ /Red Hat Enterprise Linux/) {
63924071ac1SMauro Carvalho Chehab		give_redhat_hints;
64024071ac1SMauro Carvalho Chehab		return;
64124071ac1SMauro Carvalho Chehab	}
6429b756a9dSMauro Carvalho Chehab	if ($system_release =~ /CentOS/) {
6439b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6449b756a9dSMauro Carvalho Chehab		return;
6459b756a9dSMauro Carvalho Chehab	}
6469b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Scientific Linux/) {
6479b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6489b756a9dSMauro Carvalho Chehab		return;
6499b756a9dSMauro Carvalho Chehab	}
6509b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Oracle Linux Server/) {
6519b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6529b756a9dSMauro Carvalho Chehab		return;
6539b756a9dSMauro Carvalho Chehab	}
65424071ac1SMauro Carvalho Chehab	if ($system_release =~ /Fedora/) {
65524071ac1SMauro Carvalho Chehab		give_redhat_hints;
65624071ac1SMauro Carvalho Chehab		return;
65724071ac1SMauro Carvalho Chehab	}
65824071ac1SMauro Carvalho Chehab	if ($system_release =~ /Ubuntu/) {
65924071ac1SMauro Carvalho Chehab		give_debian_hints;
66024071ac1SMauro Carvalho Chehab		return;
66124071ac1SMauro Carvalho Chehab	}
66224071ac1SMauro Carvalho Chehab	if ($system_release =~ /Debian/) {
66324071ac1SMauro Carvalho Chehab		give_debian_hints;
66424071ac1SMauro Carvalho Chehab		return;
66524071ac1SMauro Carvalho Chehab	}
66624071ac1SMauro Carvalho Chehab	if ($system_release =~ /openSUSE/) {
66724071ac1SMauro Carvalho Chehab		give_opensuse_hints;
66824071ac1SMauro Carvalho Chehab		return;
66924071ac1SMauro Carvalho Chehab	}
670800d408aSMauro Carvalho Chehab	if ($system_release =~ /Mageia/) {
671800d408aSMauro Carvalho Chehab		give_mageia_hints;
672800d408aSMauro Carvalho Chehab		return;
673800d408aSMauro Carvalho Chehab	}
674d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
675d6ebf189SMauro Carvalho Chehab		give_mageia_hints;
676d6ebf189SMauro Carvalho Chehab		return;
677d6ebf189SMauro Carvalho Chehab	}
67824071ac1SMauro Carvalho Chehab	if ($system_release =~ /Arch Linux/) {
67924071ac1SMauro Carvalho Chehab		give_arch_linux_hints;
68024071ac1SMauro Carvalho Chehab		return;
68124071ac1SMauro Carvalho Chehab	}
68224071ac1SMauro Carvalho Chehab	if ($system_release =~ /Gentoo/) {
68324071ac1SMauro Carvalho Chehab		give_gentoo_hints;
68424071ac1SMauro Carvalho Chehab		return;
68524071ac1SMauro Carvalho Chehab	}
68624071ac1SMauro Carvalho Chehab
68724071ac1SMauro Carvalho Chehab	#
68824071ac1SMauro Carvalho Chehab	# Fall-back to generic hint code for other distros
68924071ac1SMauro Carvalho Chehab	# That's far from ideal, specially for LaTeX dependencies.
69024071ac1SMauro Carvalho Chehab	#
69124071ac1SMauro Carvalho Chehab	my %map = (
69224071ac1SMauro Carvalho Chehab		"sphinx-build" => "sphinx"
69324071ac1SMauro Carvalho Chehab	);
69456e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
69524071ac1SMauro Carvalho Chehab	check_missing(\%map);
69624071ac1SMauro Carvalho Chehab	print "I don't know distro $system_release.\n";
69724071ac1SMauro Carvalho Chehab	print "So, I can't provide you a hint with the install procedure.\n";
69824071ac1SMauro Carvalho Chehab	print "There are likely missing dependencies.\n";
69924071ac1SMauro Carvalho Chehab}
70024071ac1SMauro Carvalho Chehab
70124071ac1SMauro Carvalho Chehab#
70224071ac1SMauro Carvalho Chehab# Common dependencies
70324071ac1SMauro Carvalho Chehab#
70424071ac1SMauro Carvalho Chehab
7052730ce01SShuah Khansub deactivate_help()
7062730ce01SShuah Khan{
7072f9c5025SMauro Carvalho Chehab	printf "\n    If you want to exit the virtualenv, you can use:\n";
7082730ce01SShuah Khan	printf "\tdeactivate\n";
7092730ce01SShuah Khan}
7102730ce01SShuah Khan
711e5089912SMauro Carvalho Chehabsub get_virtenv()
712e5089912SMauro Carvalho Chehab{
713a5f785f1SMauro Carvalho Chehab	my $ver;
714e5089912SMauro Carvalho Chehab	my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
715e5089912SMauro Carvalho Chehab	my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
716e5089912SMauro Carvalho Chehab
717e5089912SMauro Carvalho Chehab	@activates = sort {$b cmp $a} @activates;
718a5f785f1SMauro Carvalho Chehab
719e5089912SMauro Carvalho Chehab	foreach my $f (@activates) {
720e5089912SMauro Carvalho Chehab		next if ($f lt $min_activate);
721e5089912SMauro Carvalho Chehab
722e5089912SMauro Carvalho Chehab		my $sphinx_cmd = $f;
723e5089912SMauro Carvalho Chehab		$sphinx_cmd =~ s/activate/sphinx-build/;
724e5089912SMauro Carvalho Chehab		next if (! -f $sphinx_cmd);
725e5089912SMauro Carvalho Chehab
726e5089912SMauro Carvalho Chehab		my $ver = get_sphinx_version($sphinx_cmd);
727ed2133b7SMauro Carvalho Chehab
728ed2133b7SMauro Carvalho Chehab		if (!$ver) {
729ed2133b7SMauro Carvalho Chehab			$f =~ s#/bin/activate##;
730ed2133b7SMauro Carvalho Chehab			print("Warning: virtual environment $f is not working.\nPython version upgrade? Remove it with:\n\n\trm -rf $f\n\n");
731ed2133b7SMauro Carvalho Chehab		}
732ed2133b7SMauro Carvalho Chehab
733e5089912SMauro Carvalho Chehab		if ($need_sphinx && ($ver ge $min_version)) {
734e5089912SMauro Carvalho Chehab			return ($f, $ver);
735e5089912SMauro Carvalho Chehab		} elsif ($ver gt $cur_version) {
736e5089912SMauro Carvalho Chehab			return ($f, $ver);
737e5089912SMauro Carvalho Chehab		}
738e5089912SMauro Carvalho Chehab	}
739e5089912SMauro Carvalho Chehab	return ("", "");
740e5089912SMauro Carvalho Chehab}
741e5089912SMauro Carvalho Chehab
742a5f785f1SMauro Carvalho Chehabsub recommend_sphinx_upgrade()
743a5f785f1SMauro Carvalho Chehab{
744a5f785f1SMauro Carvalho Chehab	my $venv_ver;
745a5f785f1SMauro Carvalho Chehab
746a5f785f1SMauro Carvalho Chehab	# Avoid running sphinx-builds from venv if $cur_version is good
747a5f785f1SMauro Carvalho Chehab	if ($cur_version && ($cur_version ge $rec_version)) {
748a5f785f1SMauro Carvalho Chehab		$latest_avail_ver = $cur_version;
749a5f785f1SMauro Carvalho Chehab		return;
750a5f785f1SMauro Carvalho Chehab	}
751a5f785f1SMauro Carvalho Chehab
752a5f785f1SMauro Carvalho Chehab	# Get the highest version from sphinx_*/bin/sphinx-build and the
753a5f785f1SMauro Carvalho Chehab	# corresponding command to activate the venv/virtenv
7547c2d45a3SMauro Carvalho Chehab	($activate_cmd, $venv_ver) = get_virtenv();
755a5f785f1SMauro Carvalho Chehab
756a5f785f1SMauro Carvalho Chehab	# Store the highest version from Sphinx existing virtualenvs
757a5f785f1SMauro Carvalho Chehab	if (($activate_cmd ne "") && ($venv_ver gt $cur_version)) {
758a5f785f1SMauro Carvalho Chehab		$latest_avail_ver = $venv_ver;
759a5f785f1SMauro Carvalho Chehab	} else {
760a5f785f1SMauro Carvalho Chehab		$latest_avail_ver = $cur_version if ($cur_version);
761a5f785f1SMauro Carvalho Chehab	}
762a5f785f1SMauro Carvalho Chehab
763a5f785f1SMauro Carvalho Chehab	# As we don't know package version of Sphinx, and there's no
764a5f785f1SMauro Carvalho Chehab	# virtual environments, don't check if upgrades are needed
765a5f785f1SMauro Carvalho Chehab	if (!$virtualenv) {
766a5f785f1SMauro Carvalho Chehab		return if (!$latest_avail_ver);
767a5f785f1SMauro Carvalho Chehab	}
768a5f785f1SMauro Carvalho Chehab
769a5f785f1SMauro Carvalho Chehab	# Either there are already a virtual env or a new one should be created
770a5f785f1SMauro Carvalho Chehab	$need_pip = 1;
771a5f785f1SMauro Carvalho Chehab
7727c2d45a3SMauro Carvalho Chehab	return if (!$latest_avail_ver);
7737c2d45a3SMauro Carvalho Chehab
774a5f785f1SMauro Carvalho Chehab	# Return if the reason is due to an upgrade or not
775a5f785f1SMauro Carvalho Chehab	if ($latest_avail_ver lt $rec_version) {
776a5f785f1SMauro Carvalho Chehab		$rec_sphinx_upgrade = 1;
777a5f785f1SMauro Carvalho Chehab	}
7787c2d45a3SMauro Carvalho Chehab
7797c2d45a3SMauro Carvalho Chehab	return $latest_avail_ver;
780a5f785f1SMauro Carvalho Chehab}
781a5f785f1SMauro Carvalho Chehab
782e5089912SMauro Carvalho Chehab#
783e5089912SMauro Carvalho Chehab# The logic here is complex, as it have to deal with different versions:
784e5089912SMauro Carvalho Chehab#	- minimal supported version;
785e5089912SMauro Carvalho Chehab#	- minimal PDF version;
786e5089912SMauro Carvalho Chehab#	- recommended version.
787e5089912SMauro Carvalho Chehab# It also needs to work fine with both distro's package and venv/virtualenv
788e5089912SMauro Carvalho Chehabsub recommend_sphinx_version($)
789e5089912SMauro Carvalho Chehab{
790e5089912SMauro Carvalho Chehab	my $virtualenv_cmd = shift;
791e5089912SMauro Carvalho Chehab
7925b27d255SMauro Carvalho Chehab	# Version is OK. Nothing to do.
7935b27d255SMauro Carvalho Chehab	if ($cur_version && ($cur_version ge $rec_version)) {
7945b27d255SMauro Carvalho Chehab		if ($cur_version lt $min_pdf_version) {
795a5f785f1SMauro Carvalho Chehab			print "note: If you want pdf, you need at least Sphinx $min_pdf_version.\n";
796e5089912SMauro Carvalho Chehab		}
7975b27d255SMauro Carvalho Chehab		return;
7985b27d255SMauro Carvalho Chehab	};
799a5f785f1SMauro Carvalho Chehab
800e5089912SMauro Carvalho Chehab	if (!$need_sphinx) {
801e5089912SMauro Carvalho Chehab		# sphinx-build is present and its version is >= $min_version
802e5089912SMauro Carvalho Chehab
803e5089912SMauro Carvalho Chehab		#only recommend enabling a newer virtenv version if makes sense.
804e5089912SMauro Carvalho Chehab		if ($latest_avail_ver gt $cur_version) {
805a5f785f1SMauro Carvalho Chehab			printf "\nYou may also use the newer Sphinx version $latest_avail_ver with:\n";
806e5089912SMauro Carvalho Chehab			printf "\tdeactivate\n"  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
807a5f785f1SMauro Carvalho Chehab			printf "\t. $activate_cmd\n";
808e5089912SMauro Carvalho Chehab			deactivate_help();
809e5089912SMauro Carvalho Chehab
810e5089912SMauro Carvalho Chehab			return;
811e5089912SMauro Carvalho Chehab		}
812e5089912SMauro Carvalho Chehab		return if ($latest_avail_ver ge $rec_version);
813e5089912SMauro Carvalho Chehab	}
814e5089912SMauro Carvalho Chehab
815e5089912SMauro Carvalho Chehab	if (!$virtualenv) {
816e5089912SMauro Carvalho Chehab		# No sphinx either via package or via virtenv. As we can't
817e5089912SMauro Carvalho Chehab		# Compare the versions here, just return, recommending the
818e5089912SMauro Carvalho Chehab		# user to install it from the package distro.
819e5089912SMauro Carvalho Chehab		return if (!$latest_avail_ver);
820e5089912SMauro Carvalho Chehab
821e5089912SMauro Carvalho Chehab		# User doesn't want a virtenv recommendation, but he already
822e5089912SMauro Carvalho Chehab		# installed one via virtenv with a newer version.
823e5089912SMauro Carvalho Chehab		# So, print commands to enable it
824e5089912SMauro Carvalho Chehab		if ($latest_avail_ver gt $cur_version) {
825a5f785f1SMauro Carvalho Chehab			printf "\nYou may also use the Sphinx virtualenv version $latest_avail_ver with:\n";
826e5089912SMauro Carvalho Chehab			printf "\tdeactivate\n"  if ($ENV{'PWD'} =~ /${virtenv_prefix}/);
827a5f785f1SMauro Carvalho Chehab			printf "\t. $activate_cmd\n";
828e5089912SMauro Carvalho Chehab			deactivate_help();
829e5089912SMauro Carvalho Chehab
830e5089912SMauro Carvalho Chehab			return;
831e5089912SMauro Carvalho Chehab		}
832e5089912SMauro Carvalho Chehab		print "\n";
833e5089912SMauro Carvalho Chehab	} else {
834e5089912SMauro Carvalho Chehab		$need++ if ($need_sphinx);
835e5089912SMauro Carvalho Chehab	}
836e5089912SMauro Carvalho Chehab
837e5089912SMauro Carvalho Chehab	# Suggest newer versions if current ones are too old
8387c2d45a3SMauro Carvalho Chehab	if ($latest_avail_ver && $latest_avail_ver ge $min_version) {
839e5089912SMauro Carvalho Chehab		# If there's a good enough version, ask the user to enable it
840e5089912SMauro Carvalho Chehab		if ($latest_avail_ver ge $rec_version) {
841a5f785f1SMauro Carvalho Chehab			printf "\nNeed to activate Sphinx (version $latest_avail_ver) on virtualenv with:\n";
842a5f785f1SMauro Carvalho Chehab			printf "\t. $activate_cmd\n";
843e5089912SMauro Carvalho Chehab			deactivate_help();
844e5089912SMauro Carvalho Chehab
8455b27d255SMauro Carvalho Chehab			if ($latest_avail_ver lt $min_pdf_version) {
8465b27d255SMauro Carvalho Chehab				print "note: If you want pdf, you need at least Sphinx $min_pdf_version.\n";
8475b27d255SMauro Carvalho Chehab			}
8485b27d255SMauro Carvalho Chehab
849e5089912SMauro Carvalho Chehab			return;
850e5089912SMauro Carvalho Chehab		}
851e5089912SMauro Carvalho Chehab
852e5089912SMauro Carvalho Chehab		# Version is above the minimal required one, but may be
853e5089912SMauro Carvalho Chehab		# below the recommended one. So, print warnings/notes
854e5089912SMauro Carvalho Chehab
855e5089912SMauro Carvalho Chehab		if ($latest_avail_ver lt $rec_version) {
856e5089912SMauro Carvalho Chehab			print "Warning: It is recommended at least Sphinx version $rec_version.\n";
857e5089912SMauro Carvalho Chehab		}
858e5089912SMauro Carvalho Chehab	}
859e5089912SMauro Carvalho Chehab
860e5089912SMauro Carvalho Chehab	# At this point, either it needs Sphinx or upgrade is recommended,
861e5089912SMauro Carvalho Chehab	# both via pip
862e5089912SMauro Carvalho Chehab
863e5089912SMauro Carvalho Chehab	if ($rec_sphinx_upgrade) {
864e5089912SMauro Carvalho Chehab		if (!$virtualenv) {
865e5089912SMauro Carvalho Chehab			print "Instead of install/upgrade Python Sphinx pkg, you could use pip/pypi with:\n\n";
866e5089912SMauro Carvalho Chehab		} else {
867e5089912SMauro Carvalho Chehab			print "To upgrade Sphinx, use:\n\n";
868e5089912SMauro Carvalho Chehab		}
869e5089912SMauro Carvalho Chehab	} else {
8706a5d7320SMauro Carvalho Chehab		print "\nSphinx needs to be installed either:\n1) via pip/pypi with:\n\n";
871e5089912SMauro Carvalho Chehab	}
872e5089912SMauro Carvalho Chehab
873e5089912SMauro Carvalho Chehab	$python_cmd = find_python_no_venv();
874e5089912SMauro Carvalho Chehab
875e5089912SMauro Carvalho Chehab	printf "\t$virtualenv_cmd $virtenv_dir\n";
876a5f785f1SMauro Carvalho Chehab
877e5089912SMauro Carvalho Chehab	printf "\t. $virtenv_dir/bin/activate\n";
878e5089912SMauro Carvalho Chehab	printf "\tpip install -r $requirement_file\n";
879e5089912SMauro Carvalho Chehab	deactivate_help();
8806a5d7320SMauro Carvalho Chehab
8816a5d7320SMauro Carvalho Chehab	printf "\n2) As a package with:\n";
8826a5d7320SMauro Carvalho Chehab
8836a5d7320SMauro Carvalho Chehab	my $old_need = $need;
8846a5d7320SMauro Carvalho Chehab	my $old_optional = $optional;
8856a5d7320SMauro Carvalho Chehab	%missing = ();
8866a5d7320SMauro Carvalho Chehab	$pdf = 0;
8876a5d7320SMauro Carvalho Chehab	$optional = 0;
8886a5d7320SMauro Carvalho Chehab	$install = "";
8896a5d7320SMauro Carvalho Chehab	$verbose_warn_install = 0;
8906a5d7320SMauro Carvalho Chehab
8916a5d7320SMauro Carvalho Chehab	add_package("python-sphinx", 0);
8926a5d7320SMauro Carvalho Chehab
8936a5d7320SMauro Carvalho Chehab	check_distros();
8946a5d7320SMauro Carvalho Chehab
8956a5d7320SMauro Carvalho Chehab	$need = $old_need;
8966a5d7320SMauro Carvalho Chehab	$optional = $old_optional;
8976a5d7320SMauro Carvalho Chehab
898*02bba8caSMauro Carvalho Chehab	printf "\n    Please note that Sphinx >= 3.0 will currently produce false-positive\n";
899*02bba8caSMauro Carvalho Chehab	printf "   warning when the same name is used for more than one type (functions,\n";
900*02bba8caSMauro Carvalho Chehab	printf "   structs, enums,...). This is known Sphinx bug. For more details, see:\n";
901*02bba8caSMauro Carvalho Chehab	printf "\thttps://github.com/sphinx-doc/sphinx/pull/8313\n";
902e5089912SMauro Carvalho Chehab}
903e5089912SMauro Carvalho Chehab
90424071ac1SMauro Carvalho Chehabsub check_needs()
90524071ac1SMauro Carvalho Chehab{
9062f9c5025SMauro Carvalho Chehab	# Check if Sphinx is already accessible from current environment
90724071ac1SMauro Carvalho Chehab	check_sphinx();
9089b88ad54SMauro Carvalho Chehab
9099b88ad54SMauro Carvalho Chehab	if ($system_release) {
910ec43a27fSMauro Carvalho Chehab		print "Detected OS: $system_release.\n";
9119b88ad54SMauro Carvalho Chehab	} else {
912ec43a27fSMauro Carvalho Chehab		print "Unknown OS\n";
9139b88ad54SMauro Carvalho Chehab	}
914ec43a27fSMauro Carvalho Chehab	printf "Sphinx version: %s\n\n", $cur_version if ($cur_version);
9159b88ad54SMauro Carvalho Chehab
9162f9c5025SMauro Carvalho Chehab	# Check python command line, trying first python3
9172f9c5025SMauro Carvalho Chehab	$python_cmd = findprog("python3");
9182f9c5025SMauro Carvalho Chehab	$python_cmd = check_program("python", 0) if (!$python_cmd);
9192f9c5025SMauro Carvalho Chehab
9202f9c5025SMauro Carvalho Chehab	# Check the type of virtual env, depending on Python version
9212f9c5025SMauro Carvalho Chehab	if ($python_cmd) {
9222f9c5025SMauro Carvalho Chehab		if ($virtualenv) {
9232f9c5025SMauro Carvalho Chehab			my $tmp = qx($python_cmd --version 2>&1);
9242f9c5025SMauro Carvalho Chehab			if ($tmp =~ m/(\d+\.)(\d+\.)/) {
9252f9c5025SMauro Carvalho Chehab				if ($1 < 3) {
9264217e507SJonathan Corbet					# Fail if it finds python2 (or worse)
9274217e507SJonathan Corbet					die "Python 3 is required to build the kernel docs\n";
9282f9c5025SMauro Carvalho Chehab				}
929a5f785f1SMauro Carvalho Chehab				if ($1 == 3 && $2 < 3) {
930a5f785f1SMauro Carvalho Chehab					# Need Python 3.3 or upper for venv
931a5f785f1SMauro Carvalho Chehab					$need_virtualenv = 1;
932a5f785f1SMauro Carvalho Chehab				}
9332f9c5025SMauro Carvalho Chehab			} else {
9342f9c5025SMauro Carvalho Chehab				die "Warning: couldn't identify $python_cmd version!";
9352f9c5025SMauro Carvalho Chehab			}
9362f9c5025SMauro Carvalho Chehab		} else {
9372f9c5025SMauro Carvalho Chehab			add_package("python-sphinx", 0);
9382f9c5025SMauro Carvalho Chehab		}
9392f9c5025SMauro Carvalho Chehab	}
9402f9c5025SMauro Carvalho Chehab
9417c2d45a3SMauro Carvalho Chehab	my $venv_ver = recommend_sphinx_upgrade();
942a5f785f1SMauro Carvalho Chehab
9432f9c5025SMauro Carvalho Chehab	my $virtualenv_cmd;
944a5f785f1SMauro Carvalho Chehab
945a5f785f1SMauro Carvalho Chehab	if ($need_pip) {
946a5f785f1SMauro Carvalho Chehab		# Set virtualenv command line, if python < 3.3
9472f9c5025SMauro Carvalho Chehab		if ($need_virtualenv) {
9482f9c5025SMauro Carvalho Chehab			$virtualenv_cmd = findprog("virtualenv-3");
9492f9c5025SMauro Carvalho Chehab			$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
9502f9c5025SMauro Carvalho Chehab			if (!$virtualenv_cmd) {
9512f9c5025SMauro Carvalho Chehab				check_program("virtualenv", 0);
9522f9c5025SMauro Carvalho Chehab				$virtualenv_cmd = "virtualenv";
9532f9c5025SMauro Carvalho Chehab			}
954a5f785f1SMauro Carvalho Chehab		} else {
955a5f785f1SMauro Carvalho Chehab			$virtualenv_cmd = "$python_cmd -m venv";
956a5f785f1SMauro Carvalho Chehab			check_python_module("ensurepip", 0);
957a5f785f1SMauro Carvalho Chehab		}
9582f9c5025SMauro Carvalho Chehab	}
9592f9c5025SMauro Carvalho Chehab
9609b88ad54SMauro Carvalho Chehab	# Check for needed programs/tools
96124071ac1SMauro Carvalho Chehab	check_perl_module("Pod::Usage", 0);
96224071ac1SMauro Carvalho Chehab	check_program("make", 0);
96324071ac1SMauro Carvalho Chehab	check_program("gcc", 0);
96424071ac1SMauro Carvalho Chehab	check_program("dot", 1);
96524071ac1SMauro Carvalho Chehab	check_program("convert", 1);
96656e5a633SMauro Carvalho Chehab
96756e5a633SMauro Carvalho Chehab	# Extra PDF files - should use 2 for is_optional
96856e5a633SMauro Carvalho Chehab	check_program("xelatex", 2) if ($pdf);
96956e5a633SMauro Carvalho Chehab	check_program("rsvg-convert", 2) if ($pdf);
97056e5a633SMauro Carvalho Chehab	check_program("latexmk", 2) if ($pdf);
97124071ac1SMauro Carvalho Chehab
9722f9c5025SMauro Carvalho Chehab	# Do distro-specific checks and output distro-install commands
97324071ac1SMauro Carvalho Chehab	check_distros();
97424071ac1SMauro Carvalho Chehab
9752f9c5025SMauro Carvalho Chehab	if (!$python_cmd) {
9762f9c5025SMauro Carvalho Chehab		if ($need == 1) {
9772f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependency is missing";
9782f9c5025SMauro Carvalho Chehab		} elsif ($need) {
9792f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependencies are missing";
9802f9c5025SMauro Carvalho Chehab		}
9812f9c5025SMauro Carvalho Chehab	}
9822f9c5025SMauro Carvalho Chehab
9832f9c5025SMauro Carvalho Chehab	# Check if sphinx-build is called sphinx-build-3
98424071ac1SMauro Carvalho Chehab	if ($need_symlink) {
98524071ac1SMauro Carvalho Chehab		printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
98624071ac1SMauro Carvalho Chehab		       which("sphinx-build-3");
98724071ac1SMauro Carvalho Chehab	}
9882f9c5025SMauro Carvalho Chehab
989e5089912SMauro Carvalho Chehab	recommend_sphinx_version($virtualenv_cmd);
99024071ac1SMauro Carvalho Chehab	printf "\n";
99124071ac1SMauro Carvalho Chehab
99254002b56SBjorn Helgaas	print "All optional dependencies are met.\n" if (!$optional);
99324071ac1SMauro Carvalho Chehab
99424071ac1SMauro Carvalho Chehab	if ($need == 1) {
99524071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependency is missing";
99624071ac1SMauro Carvalho Chehab	} elsif ($need) {
99724071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependencies are missing";
99824071ac1SMauro Carvalho Chehab	}
99924071ac1SMauro Carvalho Chehab
100024071ac1SMauro Carvalho Chehab	print "Needed package dependencies are met.\n";
100124071ac1SMauro Carvalho Chehab}
100224071ac1SMauro Carvalho Chehab
100324071ac1SMauro Carvalho Chehab#
100424071ac1SMauro Carvalho Chehab# Main
100524071ac1SMauro Carvalho Chehab#
100624071ac1SMauro Carvalho Chehab
100724071ac1SMauro Carvalho Chehabwhile (@ARGV) {
100824071ac1SMauro Carvalho Chehab	my $arg = shift(@ARGV);
100924071ac1SMauro Carvalho Chehab
101024071ac1SMauro Carvalho Chehab	if ($arg eq "--no-virtualenv") {
101124071ac1SMauro Carvalho Chehab		$virtualenv = 0;
101224071ac1SMauro Carvalho Chehab	} elsif ($arg eq "--no-pdf"){
101324071ac1SMauro Carvalho Chehab		$pdf = 0;
10149b88ad54SMauro Carvalho Chehab	} elsif ($arg eq "--version-check"){
10159b88ad54SMauro Carvalho Chehab		$version_check = 1;
101624071ac1SMauro Carvalho Chehab	} else {
10179b88ad54SMauro Carvalho Chehab		print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
10189b88ad54SMauro Carvalho Chehab		print "Where:\n";
10199b88ad54SMauro Carvalho Chehab		print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
10209b88ad54SMauro Carvalho Chehab		print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
10219b88ad54SMauro Carvalho Chehab		print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
102224071ac1SMauro Carvalho Chehab		exit -1;
102324071ac1SMauro Carvalho Chehab	}
102424071ac1SMauro Carvalho Chehab}
102524071ac1SMauro Carvalho Chehab
102624071ac1SMauro Carvalho Chehab#
102724071ac1SMauro Carvalho Chehab# Determine the system type. There's no standard unique way that would
102824071ac1SMauro Carvalho Chehab# work with all distros with a minimal package install. So, several
102924071ac1SMauro Carvalho Chehab# methods are used here.
103024071ac1SMauro Carvalho Chehab#
103124071ac1SMauro Carvalho Chehab# By default, it will use lsb_release function. If not available, it will
103224071ac1SMauro Carvalho Chehab# fail back to reading the known different places where the distro name
103324071ac1SMauro Carvalho Chehab# is stored
103424071ac1SMauro Carvalho Chehab#
103524071ac1SMauro Carvalho Chehab
103624071ac1SMauro Carvalho Chehab$system_release = qx(lsb_release -d) if which("lsb_release");
103724071ac1SMauro Carvalho Chehab$system_release =~ s/Description:\s*// if ($system_release);
103824071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/system-release") if !$system_release;
103924071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/redhat-release") if !$system_release;
104024071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/lsb-release") if !$system_release;
104124071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/gentoo-release") if !$system_release;
1042d14d0c1aSMauro Carvalho Chehab
1043d14d0c1aSMauro Carvalho Chehab# This seems more common than LSB these days
1044d14d0c1aSMauro Carvalho Chehabif (!$system_release) {
1045d14d0c1aSMauro Carvalho Chehab	my %os_var;
1046d14d0c1aSMauro Carvalho Chehab	if (open IN, "cat /etc/os-release|") {
1047d14d0c1aSMauro Carvalho Chehab		while (<IN>) {
1048d14d0c1aSMauro Carvalho Chehab			if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
1049d14d0c1aSMauro Carvalho Chehab				$os_var{$1}=$2;
1050d14d0c1aSMauro Carvalho Chehab			}
1051d14d0c1aSMauro Carvalho Chehab		}
1052d14d0c1aSMauro Carvalho Chehab		$system_release = $os_var{"NAME"};
1053d14d0c1aSMauro Carvalho Chehab		if (defined($os_var{"VERSION_ID"})) {
1054d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
1055d14d0c1aSMauro Carvalho Chehab		} else {
1056d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION"};
1057d14d0c1aSMauro Carvalho Chehab		}
1058d14d0c1aSMauro Carvalho Chehab	}
1059d14d0c1aSMauro Carvalho Chehab}
106024071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/issue") if !$system_release;
106124071ac1SMauro Carvalho Chehab$system_release =~ s/\s+$//;
106224071ac1SMauro Carvalho Chehab
106324071ac1SMauro Carvalho Chehabcheck_needs;
1064