xref: /openbmc/linux/scripts/sphinx-pre-install (revision 4217e5074f33d855873370378d427e329b60a7b4)
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;
252f9c5025SMauro Carvalho Chehabmy $need_venv = 0;
262f9c5025SMauro Carvalho Chehabmy $need_virtualenv = 0;
2777d09ad9SMauro Carvalho Chehabmy $rec_sphinx_upgrade = 0;
2824071ac1SMauro Carvalho Chehabmy $install = "";
2944f42165SMauro Carvalho Chehabmy $virtenv_dir = "";
302f9c5025SMauro Carvalho Chehabmy $python_cmd = "";
3144f42165SMauro Carvalho Chehabmy $min_version;
322834a741SMauro Carvalho Chehabmy $cur_version;
331ef70cedSMauro Carvalho Chehabmy $rec_version = "1.7.9";	# PDF won't build here
341ef70cedSMauro Carvalho Chehabmy $min_pdf_version = "2.4.4";	# Min version where pdf builds
3524071ac1SMauro Carvalho Chehab
3624071ac1SMauro Carvalho Chehab#
3724071ac1SMauro Carvalho Chehab# Command line arguments
3824071ac1SMauro Carvalho Chehab#
3924071ac1SMauro Carvalho Chehab
4024071ac1SMauro Carvalho Chehabmy $pdf = 1;
4124071ac1SMauro Carvalho Chehabmy $virtualenv = 1;
429b88ad54SMauro Carvalho Chehabmy $version_check = 0;
4324071ac1SMauro Carvalho Chehab
4424071ac1SMauro Carvalho Chehab#
4524071ac1SMauro Carvalho Chehab# List of required texlive packages on Fedora and OpenSuse
4624071ac1SMauro Carvalho Chehab#
4724071ac1SMauro Carvalho Chehab
4824071ac1SMauro Carvalho Chehabmy %texlive = (
4924071ac1SMauro Carvalho Chehab	'amsfonts.sty'       => 'texlive-amsfonts',
5024071ac1SMauro Carvalho Chehab	'amsmath.sty'        => 'texlive-amsmath',
5124071ac1SMauro Carvalho Chehab	'amssymb.sty'        => 'texlive-amsfonts',
5224071ac1SMauro Carvalho Chehab	'amsthm.sty'         => 'texlive-amscls',
5324071ac1SMauro Carvalho Chehab	'anyfontsize.sty'    => 'texlive-anyfontsize',
5424071ac1SMauro Carvalho Chehab	'atbegshi.sty'       => 'texlive-oberdiek',
5524071ac1SMauro Carvalho Chehab	'bm.sty'             => 'texlive-tools',
5624071ac1SMauro Carvalho Chehab	'capt-of.sty'        => 'texlive-capt-of',
5724071ac1SMauro Carvalho Chehab	'cmap.sty'           => 'texlive-cmap',
5824071ac1SMauro Carvalho Chehab	'ecrm1000.tfm'       => 'texlive-ec',
5924071ac1SMauro Carvalho Chehab	'eqparbox.sty'       => 'texlive-eqparbox',
6024071ac1SMauro Carvalho Chehab	'eu1enc.def'         => 'texlive-euenc',
6124071ac1SMauro Carvalho Chehab	'fancybox.sty'       => 'texlive-fancybox',
6224071ac1SMauro Carvalho Chehab	'fancyvrb.sty'       => 'texlive-fancyvrb',
6324071ac1SMauro Carvalho Chehab	'float.sty'          => 'texlive-float',
6424071ac1SMauro Carvalho Chehab	'fncychap.sty'       => 'texlive-fncychap',
6524071ac1SMauro Carvalho Chehab	'footnote.sty'       => 'texlive-mdwtools',
6624071ac1SMauro Carvalho Chehab	'framed.sty'         => 'texlive-framed',
6724071ac1SMauro Carvalho Chehab	'luatex85.sty'       => 'texlive-luatex85',
6824071ac1SMauro Carvalho Chehab	'multirow.sty'       => 'texlive-multirow',
6924071ac1SMauro Carvalho Chehab	'needspace.sty'      => 'texlive-needspace',
7024071ac1SMauro Carvalho Chehab	'palatino.sty'       => 'texlive-psnfss',
7124071ac1SMauro Carvalho Chehab	'parskip.sty'        => 'texlive-parskip',
7224071ac1SMauro Carvalho Chehab	'polyglossia.sty'    => 'texlive-polyglossia',
7324071ac1SMauro Carvalho Chehab	'tabulary.sty'       => 'texlive-tabulary',
7424071ac1SMauro Carvalho Chehab	'threeparttable.sty' => 'texlive-threeparttable',
7524071ac1SMauro Carvalho Chehab	'titlesec.sty'       => 'texlive-titlesec',
7624071ac1SMauro Carvalho Chehab	'ucs.sty'            => 'texlive-ucs',
7724071ac1SMauro Carvalho Chehab	'upquote.sty'        => 'texlive-upquote',
7824071ac1SMauro Carvalho Chehab	'wrapfig.sty'        => 'texlive-wrapfig',
7924071ac1SMauro Carvalho Chehab);
8024071ac1SMauro Carvalho Chehab
8124071ac1SMauro Carvalho Chehab#
8224071ac1SMauro Carvalho Chehab# Subroutines that checks if a feature exists
8324071ac1SMauro Carvalho Chehab#
8424071ac1SMauro Carvalho Chehab
8524071ac1SMauro Carvalho Chehabsub check_missing(%)
8624071ac1SMauro Carvalho Chehab{
8724071ac1SMauro Carvalho Chehab	my %map = %{$_[0]};
8824071ac1SMauro Carvalho Chehab
8924071ac1SMauro Carvalho Chehab	foreach my $prog (sort keys %missing) {
9024071ac1SMauro Carvalho Chehab		my $is_optional = $missing{$prog};
9124071ac1SMauro Carvalho Chehab
9256e5a633SMauro Carvalho Chehab		# At least on some LTS distros like CentOS 7, texlive doesn't
9356e5a633SMauro Carvalho Chehab		# provide all packages we need. When such distros are
9456e5a633SMauro Carvalho Chehab		# detected, we have to disable PDF output.
9556e5a633SMauro Carvalho Chehab		#
9656e5a633SMauro Carvalho Chehab		# So, we need to ignore the packages that distros would
9756e5a633SMauro Carvalho Chehab		# need for LaTeX to work
9856e5a633SMauro Carvalho Chehab		if ($is_optional == 2 && !$pdf) {
9956e5a633SMauro Carvalho Chehab			$optional--;
10056e5a633SMauro Carvalho Chehab			next;
10156e5a633SMauro Carvalho Chehab		}
10256e5a633SMauro Carvalho Chehab
10324071ac1SMauro Carvalho Chehab		if ($is_optional) {
10424071ac1SMauro Carvalho Chehab			print "Warning: better to also install \"$prog\".\n";
10524071ac1SMauro Carvalho Chehab		} else {
10624071ac1SMauro Carvalho Chehab			print "ERROR: please install \"$prog\", otherwise, build won't work.\n";
10724071ac1SMauro Carvalho Chehab		}
10824071ac1SMauro Carvalho Chehab		if (defined($map{$prog})) {
10924071ac1SMauro Carvalho Chehab			$install .= " " . $map{$prog};
11024071ac1SMauro Carvalho Chehab		} else {
11124071ac1SMauro Carvalho Chehab			$install .= " " . $prog;
11224071ac1SMauro Carvalho Chehab		}
11324071ac1SMauro Carvalho Chehab	}
11424071ac1SMauro Carvalho Chehab
11524071ac1SMauro Carvalho Chehab	$install =~ s/^\s//;
11624071ac1SMauro Carvalho Chehab}
11724071ac1SMauro Carvalho Chehab
11824071ac1SMauro Carvalho Chehabsub add_package($$)
11924071ac1SMauro Carvalho Chehab{
12024071ac1SMauro Carvalho Chehab	my $package = shift;
12124071ac1SMauro Carvalho Chehab	my $is_optional = shift;
12224071ac1SMauro Carvalho Chehab
12324071ac1SMauro Carvalho Chehab	$missing{$package} = $is_optional;
12424071ac1SMauro Carvalho Chehab	if ($is_optional) {
12524071ac1SMauro Carvalho Chehab		$optional++;
12624071ac1SMauro Carvalho Chehab	} else {
12724071ac1SMauro Carvalho Chehab		$need++;
12824071ac1SMauro Carvalho Chehab	}
12924071ac1SMauro Carvalho Chehab}
13024071ac1SMauro Carvalho Chehab
13124071ac1SMauro Carvalho Chehabsub check_missing_file($$$)
13224071ac1SMauro Carvalho Chehab{
133ff8fdb36SJeremy MAURO	my $files = shift;
13424071ac1SMauro Carvalho Chehab	my $package = shift;
13524071ac1SMauro Carvalho Chehab	my $is_optional = shift;
13624071ac1SMauro Carvalho Chehab
137ff8fdb36SJeremy MAURO	for (@$files) {
138ff8fdb36SJeremy MAURO		return if(-e $_);
139ff8fdb36SJeremy MAURO	}
14024071ac1SMauro Carvalho Chehab
14124071ac1SMauro Carvalho Chehab	add_package($package, $is_optional);
14224071ac1SMauro Carvalho Chehab}
14324071ac1SMauro Carvalho Chehab
14424071ac1SMauro Carvalho Chehabsub findprog($)
14524071ac1SMauro Carvalho Chehab{
14624071ac1SMauro Carvalho Chehab	foreach(split(/:/, $ENV{PATH})) {
14724071ac1SMauro Carvalho Chehab		return "$_/$_[0]" if(-x "$_/$_[0]");
14824071ac1SMauro Carvalho Chehab	}
14924071ac1SMauro Carvalho Chehab}
15024071ac1SMauro Carvalho Chehab
151412b09ddSMauro Carvalho Chehabsub find_python_no_venv()
152412b09ddSMauro Carvalho Chehab{
153412b09ddSMauro Carvalho Chehab	my $prog = shift;
154412b09ddSMauro Carvalho Chehab
155412b09ddSMauro Carvalho Chehab	my $cur_dir = qx(pwd);
156412b09ddSMauro Carvalho Chehab	$cur_dir =~ s/\s+$//;
157412b09ddSMauro Carvalho Chehab
158412b09ddSMauro Carvalho Chehab	foreach my $dir (split(/:/, $ENV{PATH})) {
159412b09ddSMauro Carvalho Chehab		next if ($dir =~ m,($cur_dir)/sphinx,);
160412b09ddSMauro Carvalho Chehab		return "$dir/python3" if(-x "$dir/python3");
161412b09ddSMauro Carvalho Chehab	}
162412b09ddSMauro Carvalho Chehab	foreach my $dir (split(/:/, $ENV{PATH})) {
163412b09ddSMauro Carvalho Chehab		next if ($dir =~ m,($cur_dir)/sphinx,);
164412b09ddSMauro Carvalho Chehab		return "$dir/python" if(-x "$dir/python");
165412b09ddSMauro Carvalho Chehab	}
166412b09ddSMauro Carvalho Chehab	return "python";
167412b09ddSMauro Carvalho Chehab}
168412b09ddSMauro Carvalho Chehab
16924071ac1SMauro Carvalho Chehabsub check_program($$)
17024071ac1SMauro Carvalho Chehab{
17124071ac1SMauro Carvalho Chehab	my $prog = shift;
17224071ac1SMauro Carvalho Chehab	my $is_optional = shift;
17324071ac1SMauro Carvalho Chehab
1742f9c5025SMauro Carvalho Chehab	return $prog if findprog($prog);
17524071ac1SMauro Carvalho Chehab
17624071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
17724071ac1SMauro Carvalho Chehab}
17824071ac1SMauro Carvalho Chehab
17924071ac1SMauro Carvalho Chehabsub check_perl_module($$)
18024071ac1SMauro Carvalho Chehab{
18124071ac1SMauro Carvalho Chehab	my $prog = shift;
18224071ac1SMauro Carvalho Chehab	my $is_optional = shift;
18324071ac1SMauro Carvalho Chehab
18424071ac1SMauro Carvalho Chehab	my $err = system("perl -M$prog -e 1 2>/dev/null /dev/null");
18524071ac1SMauro Carvalho Chehab	return if ($err == 0);
18624071ac1SMauro Carvalho Chehab
18724071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
18824071ac1SMauro Carvalho Chehab}
18924071ac1SMauro Carvalho Chehab
19024071ac1SMauro Carvalho Chehabsub check_python_module($$)
19124071ac1SMauro Carvalho Chehab{
19224071ac1SMauro Carvalho Chehab	my $prog = shift;
19324071ac1SMauro Carvalho Chehab	my $is_optional = shift;
19424071ac1SMauro Carvalho Chehab
1952f9c5025SMauro Carvalho Chehab	return if (!$python_cmd);
1962f9c5025SMauro Carvalho Chehab
1972f9c5025SMauro Carvalho Chehab	my $err = system("$python_cmd -c 'import $prog' 2>/dev/null /dev/null");
19824071ac1SMauro Carvalho Chehab	return if ($err == 0);
19924071ac1SMauro Carvalho Chehab
20024071ac1SMauro Carvalho Chehab	add_package($prog, $is_optional);
20124071ac1SMauro Carvalho Chehab}
20224071ac1SMauro Carvalho Chehab
20324071ac1SMauro Carvalho Chehabsub check_rpm_missing($$)
20424071ac1SMauro Carvalho Chehab{
20524071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
20624071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
20724071ac1SMauro Carvalho Chehab
20824071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
20924071ac1SMauro Carvalho Chehab		my $err = system("rpm -q '$prog' 2>/dev/null >/dev/null");
21024071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
21124071ac1SMauro Carvalho Chehab	}
21224071ac1SMauro Carvalho Chehab}
21324071ac1SMauro Carvalho Chehab
21424071ac1SMauro Carvalho Chehabsub check_pacman_missing($$)
21524071ac1SMauro Carvalho Chehab{
21624071ac1SMauro Carvalho Chehab	my @pkgs = @{$_[0]};
21724071ac1SMauro Carvalho Chehab	my $is_optional = $_[1];
21824071ac1SMauro Carvalho Chehab
21924071ac1SMauro Carvalho Chehab	foreach my $prog(@pkgs) {
22024071ac1SMauro Carvalho Chehab		my $err = system("pacman -Q '$prog' 2>/dev/null >/dev/null");
22124071ac1SMauro Carvalho Chehab		add_package($prog, $is_optional) if ($err);
22224071ac1SMauro Carvalho Chehab	}
22324071ac1SMauro Carvalho Chehab}
22424071ac1SMauro Carvalho Chehab
22524071ac1SMauro Carvalho Chehabsub check_missing_tex($)
22624071ac1SMauro Carvalho Chehab{
22724071ac1SMauro Carvalho Chehab	my $is_optional = shift;
22824071ac1SMauro Carvalho Chehab	my $kpsewhich = findprog("kpsewhich");
22924071ac1SMauro Carvalho Chehab
23024071ac1SMauro Carvalho Chehab	foreach my $prog(keys %texlive) {
23124071ac1SMauro Carvalho Chehab		my $package = $texlive{$prog};
23224071ac1SMauro Carvalho Chehab		if (!$kpsewhich) {
23324071ac1SMauro Carvalho Chehab			add_package($package, $is_optional);
23424071ac1SMauro Carvalho Chehab			next;
23524071ac1SMauro Carvalho Chehab		}
23624071ac1SMauro Carvalho Chehab		my $file = qx($kpsewhich $prog);
23724071ac1SMauro Carvalho Chehab		add_package($package, $is_optional) if ($file =~ /^\s*$/);
23824071ac1SMauro Carvalho Chehab	}
23924071ac1SMauro Carvalho Chehab}
24024071ac1SMauro Carvalho Chehab
24177d09ad9SMauro Carvalho Chehabsub get_sphinx_fname()
24224071ac1SMauro Carvalho Chehab{
24377d09ad9SMauro Carvalho Chehab	my $fname = "sphinx-build";
24477d09ad9SMauro Carvalho Chehab	return $fname if findprog($fname);
24524071ac1SMauro Carvalho Chehab
24677d09ad9SMauro Carvalho Chehab	$fname = "sphinx-build-3";
24777d09ad9SMauro Carvalho Chehab	if (findprog($fname)) {
24824071ac1SMauro Carvalho Chehab		$need_symlink = 1;
24977d09ad9SMauro Carvalho Chehab		return $fname;
25024071ac1SMauro Carvalho Chehab	}
25124071ac1SMauro Carvalho Chehab
25277d09ad9SMauro Carvalho Chehab	return "";
25377d09ad9SMauro Carvalho Chehab}
25477d09ad9SMauro Carvalho Chehab
255a8b380c3SMauro Carvalho Chehabsub get_sphinx_version($)
256a8b380c3SMauro Carvalho Chehab{
257a8b380c3SMauro Carvalho Chehab	my $cmd = shift;
258a8b380c3SMauro Carvalho Chehab	my $ver;
259a8b380c3SMauro Carvalho Chehab
260a8b380c3SMauro Carvalho Chehab	open IN, "$cmd --version 2>&1 |";
261a8b380c3SMauro Carvalho Chehab	while (<IN>) {
262a8b380c3SMauro Carvalho Chehab		if (m/^\s*sphinx-build\s+([\d\.]+)(\+\/[\da-f]+)?$/) {
263a8b380c3SMauro Carvalho Chehab			$ver=$1;
264a8b380c3SMauro Carvalho Chehab			last;
265a8b380c3SMauro Carvalho Chehab		}
266a8b380c3SMauro Carvalho Chehab		# Sphinx 1.2.x uses a different format
267a8b380c3SMauro Carvalho Chehab		if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
268a8b380c3SMauro Carvalho Chehab			$ver=$1;
269a8b380c3SMauro Carvalho Chehab			last;
270a8b380c3SMauro Carvalho Chehab		}
271a8b380c3SMauro Carvalho Chehab	}
272a8b380c3SMauro Carvalho Chehab	close IN;
273a8b380c3SMauro Carvalho Chehab	return $ver;
274a8b380c3SMauro Carvalho Chehab}
275a8b380c3SMauro Carvalho Chehab
27677d09ad9SMauro Carvalho Chehabsub check_sphinx()
27777d09ad9SMauro Carvalho Chehab{
2781ef70cedSMauro Carvalho Chehab	my $default_version;
27977d09ad9SMauro Carvalho Chehab
28077d09ad9SMauro Carvalho Chehab	open IN, $conf or die "Can't open $conf";
28177d09ad9SMauro Carvalho Chehab	while (<IN>) {
28277d09ad9SMauro Carvalho Chehab		if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
28377d09ad9SMauro Carvalho Chehab			$min_version=$1;
28477d09ad9SMauro Carvalho Chehab			last;
28577d09ad9SMauro Carvalho Chehab		}
28677d09ad9SMauro Carvalho Chehab	}
28777d09ad9SMauro Carvalho Chehab	close IN;
28877d09ad9SMauro Carvalho Chehab
28977d09ad9SMauro Carvalho Chehab	die "Can't get needs_sphinx version from $conf" if (!$min_version);
29077d09ad9SMauro Carvalho Chehab
29177d09ad9SMauro Carvalho Chehab	open IN, $requirement_file or die "Can't open $requirement_file";
29277d09ad9SMauro Carvalho Chehab	while (<IN>) {
29377d09ad9SMauro Carvalho Chehab		if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
2941ef70cedSMauro Carvalho Chehab			$default_version=$1;
29577d09ad9SMauro Carvalho Chehab			last;
29677d09ad9SMauro Carvalho Chehab		}
29777d09ad9SMauro Carvalho Chehab	}
29877d09ad9SMauro Carvalho Chehab	close IN;
29977d09ad9SMauro Carvalho Chehab
3001ef70cedSMauro Carvalho Chehab	die "Can't get default sphinx version from $requirement_file" if (!$default_version);
30177d09ad9SMauro Carvalho Chehab
3021ef70cedSMauro Carvalho Chehab	$virtenv_dir = $virtenv_prefix . $default_version;
30377d09ad9SMauro Carvalho Chehab
30477d09ad9SMauro Carvalho Chehab	my $sphinx = get_sphinx_fname();
3052f9c5025SMauro Carvalho Chehab	if ($sphinx eq "") {
3062f9c5025SMauro Carvalho Chehab		$need_sphinx = 1;
3072f9c5025SMauro Carvalho Chehab		return;
3082f9c5025SMauro Carvalho Chehab	}
30977d09ad9SMauro Carvalho Chehab
310a8b380c3SMauro Carvalho Chehab	$cur_version = get_sphinx_version($sphinx);
311a8b380c3SMauro Carvalho Chehab	die ("$sphinx returned an error") if (!$cur_version);
31277d09ad9SMauro Carvalho Chehab
31377d09ad9SMauro Carvalho Chehab	die "$sphinx didn't return its version" if (!$cur_version);
31477d09ad9SMauro Carvalho Chehab
31577d09ad9SMauro Carvalho Chehab	if ($cur_version lt $min_version) {
3169b88ad54SMauro Carvalho Chehab		printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
3171ef70cedSMauro Carvalho Chehab		       $cur_version, $min_version, $default_version;
31877d09ad9SMauro Carvalho Chehab		$need_sphinx = 1;
31977d09ad9SMauro Carvalho Chehab		return;
32077d09ad9SMauro Carvalho Chehab	}
32177d09ad9SMauro Carvalho Chehab
32277d09ad9SMauro Carvalho Chehab	if ($cur_version lt $rec_version) {
3231ef70cedSMauro Carvalho Chehab		$rec_sphinx_upgrade = 1;
3241ef70cedSMauro Carvalho Chehab		return;
3251ef70cedSMauro Carvalho Chehab	}
3269b88ad54SMauro Carvalho Chehab
3279b88ad54SMauro Carvalho Chehab	# On version check mode, just assume Sphinx has all mandatory deps
3289b88ad54SMauro Carvalho Chehab	exit (0) if ($version_check);
32924071ac1SMauro Carvalho Chehab}
33024071ac1SMauro Carvalho Chehab
33124071ac1SMauro Carvalho Chehab#
33224071ac1SMauro Carvalho Chehab# Ancillary subroutines
33324071ac1SMauro Carvalho Chehab#
33424071ac1SMauro Carvalho Chehab
33524071ac1SMauro Carvalho Chehabsub catcheck($)
33624071ac1SMauro Carvalho Chehab{
33724071ac1SMauro Carvalho Chehab  my $res = "";
33824071ac1SMauro Carvalho Chehab  $res = qx(cat $_[0]) if (-r $_[0]);
33924071ac1SMauro Carvalho Chehab  return $res;
34024071ac1SMauro Carvalho Chehab}
34124071ac1SMauro Carvalho Chehab
34224071ac1SMauro Carvalho Chehabsub which($)
34324071ac1SMauro Carvalho Chehab{
34424071ac1SMauro Carvalho Chehab	my $file = shift;
34524071ac1SMauro Carvalho Chehab	my @path = split ":", $ENV{PATH};
34624071ac1SMauro Carvalho Chehab
34724071ac1SMauro Carvalho Chehab	foreach my $dir(@path) {
34824071ac1SMauro Carvalho Chehab		my $name = $dir.'/'.$file;
34924071ac1SMauro Carvalho Chehab		return $name if (-x $name );
35024071ac1SMauro Carvalho Chehab	}
35124071ac1SMauro Carvalho Chehab	return undef;
35224071ac1SMauro Carvalho Chehab}
35324071ac1SMauro Carvalho Chehab
35424071ac1SMauro Carvalho Chehab#
35524071ac1SMauro Carvalho Chehab# Subroutines that check distro-specific hints
35624071ac1SMauro Carvalho Chehab#
35724071ac1SMauro Carvalho Chehab
35824071ac1SMauro Carvalho Chehabsub give_debian_hints()
35924071ac1SMauro Carvalho Chehab{
36024071ac1SMauro Carvalho Chehab	my %map = (
36124071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
36224071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx-rtd-theme",
3632f9c5025SMauro Carvalho Chehab		"ensurepip"		=> "python3-venv",
36424071ac1SMauro Carvalho Chehab		"virtualenv"		=> "virtualenv",
36524071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
36624071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
36724071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-modules",
36824071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex",
3698e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-bin",
37024071ac1SMauro Carvalho Chehab	);
37124071ac1SMauro Carvalho Chehab
37224071ac1SMauro Carvalho Chehab	if ($pdf) {
373ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"],
37456e5a633SMauro Carvalho Chehab				   "fonts-dejavu", 2);
37527eed923SMauro Carvalho Chehab
3769692f2fdSJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
377bfc7f428SMauro Carvalho Chehab				    "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
3789692f2fdSJeremy MAURO				    "/usr/share/fonts/opentype/noto/NotoSerifCJK-Regular.ttc"],
37927eed923SMauro Carvalho Chehab				   "fonts-noto-cjk", 2);
38024071ac1SMauro Carvalho Chehab	}
38124071ac1SMauro Carvalho Chehab
38256e5a633SMauro Carvalho Chehab	check_program("dvipng", 2) if ($pdf);
38324071ac1SMauro Carvalho Chehab	check_missing(\%map);
38424071ac1SMauro Carvalho Chehab
38524071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
38624071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo apt-get install $install\n");
38724071ac1SMauro Carvalho Chehab}
38824071ac1SMauro Carvalho Chehab
38924071ac1SMauro Carvalho Chehabsub give_redhat_hints()
39024071ac1SMauro Carvalho Chehab{
39124071ac1SMauro Carvalho Chehab	my %map = (
39224071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
39324071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
39424071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
39524071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
39624071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
39724071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
39824071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
3998e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2-tools",
40024071ac1SMauro Carvalho Chehab	);
40124071ac1SMauro Carvalho Chehab
4025d88953cSMauro Carvalho Chehab	my @fedora26_opt_pkgs = (
4035d88953cSMauro Carvalho Chehab		"graphviz-gd",		# Fedora 26: needed for PDF support
4045d88953cSMauro Carvalho Chehab	);
4055d88953cSMauro Carvalho Chehab
40624071ac1SMauro Carvalho Chehab	my @fedora_tex_pkgs = (
40724071ac1SMauro Carvalho Chehab		"texlive-collection-fontsrecommended",
40824071ac1SMauro Carvalho Chehab		"texlive-collection-latex",
40927eed923SMauro Carvalho Chehab		"texlive-xecjk",
41024071ac1SMauro Carvalho Chehab		"dejavu-sans-fonts",
41124071ac1SMauro Carvalho Chehab		"dejavu-serif-fonts",
41224071ac1SMauro Carvalho Chehab		"dejavu-sans-mono-fonts",
41324071ac1SMauro Carvalho Chehab	);
41424071ac1SMauro Carvalho Chehab
4159b756a9dSMauro Carvalho Chehab	#
4169b756a9dSMauro Carvalho Chehab	# Checks valid for RHEL/CentOS version 7.x.
4179b756a9dSMauro Carvalho Chehab	#
41856e5a633SMauro Carvalho Chehab	my $old = 0;
41956e5a633SMauro Carvalho Chehab	my $rel;
42056e5a633SMauro Carvalho Chehab	$rel = $1 if ($system_release =~ /release\s+(\d+)/);
42156e5a633SMauro Carvalho Chehab
422b308467cSMauro Carvalho Chehab	if (!($system_release =~ /Fedora/)) {
4239b756a9dSMauro Carvalho Chehab		$map{"virtualenv"} = "python-virtualenv";
42456e5a633SMauro Carvalho Chehab
42556e5a633SMauro Carvalho Chehab		if ($rel && $rel < 8) {
42656e5a633SMauro Carvalho Chehab			$old = 1;
42756e5a633SMauro Carvalho Chehab			$pdf = 0;
42856e5a633SMauro Carvalho Chehab
42956e5a633SMauro Carvalho Chehab			printf("Note: texlive packages on RHEL/CENTOS <= 7 are incomplete. Can't support PDF output\n");
43056e5a633SMauro Carvalho Chehab			printf("If you want to build PDF, please read:\n");
43156e5a633SMauro Carvalho Chehab			printf("\thttps://www.systutorials.com/241660/how-to-install-tex-live-on-centos-7-linux/\n");
43256e5a633SMauro Carvalho Chehab		}
43356e5a633SMauro Carvalho Chehab	} else {
43456e5a633SMauro Carvalho Chehab		if ($rel && $rel < 26) {
43556e5a633SMauro Carvalho Chehab			$old = 1;
43656e5a633SMauro Carvalho Chehab		}
43756e5a633SMauro Carvalho Chehab	}
43856e5a633SMauro Carvalho Chehab	if (!$rel) {
43956e5a633SMauro Carvalho Chehab		printf("Couldn't identify release number\n");
44056e5a633SMauro Carvalho Chehab		$old = 1;
44156e5a633SMauro Carvalho Chehab		$pdf = 0;
4429b756a9dSMauro Carvalho Chehab	}
4439b756a9dSMauro Carvalho Chehab
44427eed923SMauro Carvalho Chehab	if ($pdf) {
445ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc"],
44627eed923SMauro Carvalho Chehab				   "google-noto-sans-cjk-ttc-fonts", 2);
44727eed923SMauro Carvalho Chehab	}
44827eed923SMauro Carvalho Chehab
44956e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora26_opt_pkgs, 2) if ($pdf && !$old);
45056e5a633SMauro Carvalho Chehab	check_rpm_missing(\@fedora_tex_pkgs, 2) if ($pdf);
45156e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
45224071ac1SMauro Carvalho Chehab	check_missing(\%map);
45324071ac1SMauro Carvalho Chehab
45424071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
4559b756a9dSMauro Carvalho Chehab
45656e5a633SMauro Carvalho Chehab	if (!$old) {
4579b756a9dSMauro Carvalho Chehab		# dnf, for Fedora 18+
45824071ac1SMauro Carvalho Chehab		printf("You should run:\n\n\tsudo dnf install -y $install\n");
4599b756a9dSMauro Carvalho Chehab	} else {
4609b756a9dSMauro Carvalho Chehab		# yum, for RHEL (and clones) or Fedora version < 18
4619b756a9dSMauro Carvalho Chehab		printf("You should run:\n\n\tsudo yum install -y $install\n");
4629b756a9dSMauro Carvalho Chehab	}
46324071ac1SMauro Carvalho Chehab}
46424071ac1SMauro Carvalho Chehab
46524071ac1SMauro Carvalho Chehabsub give_opensuse_hints()
46624071ac1SMauro Carvalho Chehab{
46724071ac1SMauro Carvalho Chehab	my %map = (
46824071ac1SMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
46924071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
47024071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
47124071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
47224071ac1SMauro Carvalho Chehab		"convert"		=> "ImageMagick",
47324071ac1SMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
47424071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-xetex-bin",
47524071ac1SMauro Carvalho Chehab	);
47624071ac1SMauro Carvalho Chehab
477b3df6223SMauro Carvalho Chehab	# On Tumbleweed, this package is also named rsvg-convert
478b3df6223SMauro Carvalho Chehab	$map{"rsvg-convert"} = "rsvg-view" if (!($system_release =~ /Tumbleweed/));
479b3df6223SMauro Carvalho Chehab
48024071ac1SMauro Carvalho Chehab	my @suse_tex_pkgs = (
48124071ac1SMauro Carvalho Chehab		"texlive-babel-english",
48224071ac1SMauro Carvalho Chehab		"texlive-caption",
48324071ac1SMauro Carvalho Chehab		"texlive-colortbl",
48424071ac1SMauro Carvalho Chehab		"texlive-courier",
48524071ac1SMauro Carvalho Chehab		"texlive-dvips",
48624071ac1SMauro Carvalho Chehab		"texlive-helvetic",
48724071ac1SMauro Carvalho Chehab		"texlive-makeindex",
48824071ac1SMauro Carvalho Chehab		"texlive-metafont",
48924071ac1SMauro Carvalho Chehab		"texlive-metapost",
49024071ac1SMauro Carvalho Chehab		"texlive-palatino",
49124071ac1SMauro Carvalho Chehab		"texlive-preview",
49224071ac1SMauro Carvalho Chehab		"texlive-times",
49324071ac1SMauro Carvalho Chehab		"texlive-zapfchan",
49424071ac1SMauro Carvalho Chehab		"texlive-zapfding",
49524071ac1SMauro Carvalho Chehab	);
49624071ac1SMauro Carvalho Chehab
497353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-latexmk-bin";
498353290a9SMauro Carvalho Chehab
49927eed923SMauro Carvalho Chehab	# FIXME: add support for installing CJK fonts
50027eed923SMauro Carvalho Chehab	#
50127eed923SMauro Carvalho Chehab	# I tried hard, but was unable to find a way to install
50227eed923SMauro Carvalho Chehab	# "Noto Sans CJK SC" on openSUSE
50327eed923SMauro Carvalho Chehab
50456e5a633SMauro Carvalho Chehab	check_rpm_missing(\@suse_tex_pkgs, 2) if ($pdf);
50556e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
50624071ac1SMauro Carvalho Chehab	check_missing(\%map);
50724071ac1SMauro Carvalho Chehab
50824071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
50924071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo zypper install --no-recommends $install\n");
51024071ac1SMauro Carvalho Chehab}
51124071ac1SMauro Carvalho Chehab
512800d408aSMauro Carvalho Chehabsub give_mageia_hints()
513800d408aSMauro Carvalho Chehab{
514800d408aSMauro Carvalho Chehab	my %map = (
515800d408aSMauro Carvalho Chehab		"python-sphinx"		=> "python3-sphinx",
516800d408aSMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python3-sphinx_rtd_theme",
517800d408aSMauro Carvalho Chehab		"virtualenv"		=> "python3-virtualenv",
518800d408aSMauro Carvalho Chehab		"dot"			=> "graphviz",
519800d408aSMauro Carvalho Chehab		"convert"		=> "ImageMagick",
520800d408aSMauro Carvalho Chehab		"Pod::Usage"		=> "perl-Pod-Usage",
521800d408aSMauro Carvalho Chehab		"xelatex"		=> "texlive",
522d6ebf189SMauro Carvalho Chehab		"rsvg-convert"		=> "librsvg2",
523800d408aSMauro Carvalho Chehab	);
524800d408aSMauro Carvalho Chehab
525800d408aSMauro Carvalho Chehab	my @tex_pkgs = (
526800d408aSMauro Carvalho Chehab		"texlive-fontsextra",
527800d408aSMauro Carvalho Chehab	);
528800d408aSMauro Carvalho Chehab
529353290a9SMauro Carvalho Chehab	$map{"latexmk"} = "texlive-collection-basic";
530353290a9SMauro Carvalho Chehab
531d6ebf189SMauro Carvalho Chehab	my $packager_cmd;
532d6ebf189SMauro Carvalho Chehab	my $noto_sans;
533d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
534d6ebf189SMauro Carvalho Chehab		$packager_cmd = "dnf install";
535d6ebf189SMauro Carvalho Chehab		$noto_sans = "noto-sans-cjk-fonts";
536d6ebf189SMauro Carvalho Chehab		@tex_pkgs = ( "texlive-collection-fontsextra" );
537d6ebf189SMauro Carvalho Chehab	} else {
538d6ebf189SMauro Carvalho Chehab		$packager_cmd = "urpmi";
539d6ebf189SMauro Carvalho Chehab		$noto_sans = "google-noto-sans-cjk-ttc-fonts";
540d6ebf189SMauro Carvalho Chehab	}
541d6ebf189SMauro Carvalho Chehab
542d6ebf189SMauro Carvalho Chehab
54327eed923SMauro Carvalho Chehab	if ($pdf) {
544d6ebf189SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc",
545d6ebf189SMauro Carvalho Chehab				    "/usr/share/fonts/TTF/NotoSans-Regular.ttf"],
546d6ebf189SMauro Carvalho Chehab				   $noto_sans, 2);
54727eed923SMauro Carvalho Chehab	}
54827eed923SMauro Carvalho Chehab
54956e5a633SMauro Carvalho Chehab	check_rpm_missing(\@tex_pkgs, 2) if ($pdf);
550800d408aSMauro Carvalho Chehab	check_missing(\%map);
551800d408aSMauro Carvalho Chehab
552800d408aSMauro Carvalho Chehab	return if (!$need && !$optional);
553d6ebf189SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo $packager_cmd $install\n");
554800d408aSMauro Carvalho Chehab}
555800d408aSMauro Carvalho Chehab
55624071ac1SMauro Carvalho Chehabsub give_arch_linux_hints()
55724071ac1SMauro Carvalho Chehab{
55824071ac1SMauro Carvalho Chehab	my %map = (
55924071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "python-sphinx_rtd_theme",
56024071ac1SMauro Carvalho Chehab		"virtualenv"		=> "python-virtualenv",
56124071ac1SMauro Carvalho Chehab		"dot"			=> "graphviz",
56224071ac1SMauro Carvalho Chehab		"convert"		=> "imagemagick",
56324071ac1SMauro Carvalho Chehab		"xelatex"		=> "texlive-bin",
5640d0da9aaSLouis Taylor		"latexmk"		=> "texlive-core",
5658e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "extra/librsvg",
56624071ac1SMauro Carvalho Chehab	);
56724071ac1SMauro Carvalho Chehab
56824071ac1SMauro Carvalho Chehab	my @archlinux_tex_pkgs = (
56924071ac1SMauro Carvalho Chehab		"texlive-core",
57024071ac1SMauro Carvalho Chehab		"texlive-latexextra",
57124071ac1SMauro Carvalho Chehab		"ttf-dejavu",
57224071ac1SMauro Carvalho Chehab	);
57356e5a633SMauro Carvalho Chehab	check_pacman_missing(\@archlinux_tex_pkgs, 2) if ($pdf);
57456e5a633SMauro Carvalho Chehab
57527eed923SMauro Carvalho Chehab	if ($pdf) {
576ff8fdb36SJeremy MAURO		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc"],
57727eed923SMauro Carvalho Chehab				   "noto-fonts-cjk", 2);
57827eed923SMauro Carvalho Chehab	}
57927eed923SMauro Carvalho Chehab
58024071ac1SMauro Carvalho Chehab	check_missing(\%map);
58124071ac1SMauro Carvalho Chehab
58224071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
58324071ac1SMauro Carvalho Chehab	printf("You should run:\n\n\tsudo pacman -S $install\n");
58424071ac1SMauro Carvalho Chehab}
58524071ac1SMauro Carvalho Chehab
58624071ac1SMauro Carvalho Chehabsub give_gentoo_hints()
58724071ac1SMauro Carvalho Chehab{
58824071ac1SMauro Carvalho Chehab	my %map = (
58924071ac1SMauro Carvalho Chehab		"sphinx_rtd_theme"	=> "dev-python/sphinx_rtd_theme",
59024071ac1SMauro Carvalho Chehab		"virtualenv"		=> "dev-python/virtualenv",
59124071ac1SMauro Carvalho Chehab		"dot"			=> "media-gfx/graphviz",
59224071ac1SMauro Carvalho Chehab		"convert"		=> "media-gfx/imagemagick",
59324071ac1SMauro Carvalho Chehab		"xelatex"		=> "dev-texlive/texlive-xetex media-fonts/dejavu",
5948e7d5d15SMauro Carvalho Chehab		"rsvg-convert"		=> "gnome-base/librsvg",
59524071ac1SMauro Carvalho Chehab	);
59624071ac1SMauro Carvalho Chehab
597ff8fdb36SJeremy MAURO	check_missing_file(["/usr/share/fonts/dejavu/DejaVuSans.ttf"],
59856e5a633SMauro Carvalho Chehab			   "media-fonts/dejavu", 2) if ($pdf);
59924071ac1SMauro Carvalho Chehab
60027eed923SMauro Carvalho Chehab	if ($pdf) {
601e45a6317SMauro Carvalho Chehab		check_missing_file(["/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf",
602e45a6317SMauro Carvalho Chehab				    "/usr/share/fonts/noto-cjk/NotoSerifCJK-Regular.ttc"],
60327eed923SMauro Carvalho Chehab				   "media-fonts/noto-cjk", 2);
60427eed923SMauro Carvalho Chehab	}
60527eed923SMauro Carvalho Chehab
60624071ac1SMauro Carvalho Chehab	check_missing(\%map);
60724071ac1SMauro Carvalho Chehab
60824071ac1SMauro Carvalho Chehab	return if (!$need && !$optional);
609bba1e4cbSMauro Carvalho Chehab
610bba1e4cbSMauro Carvalho Chehab	printf("You should run:\n\n");
6114ea96d57SMauro Carvalho Chehab
6124ea96d57SMauro Carvalho Chehab	my $imagemagick = "media-gfx/imagemagick svg png";
6134ea96d57SMauro Carvalho Chehab	my $cairo = "media-gfx/graphviz cairo pdf";
6144ea96d57SMauro Carvalho Chehab	my $portage_imagemagick = "/etc/portage/package.use/imagemagick";
6154ea96d57SMauro Carvalho Chehab	my $portage_cairo = "/etc/portage/package.use/graphviz";
6164ea96d57SMauro Carvalho Chehab
617e45a6317SMauro Carvalho Chehab	if (qx(grep imagemagick $portage_imagemagick 2>/dev/null) eq "") {
6184ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$imagemagick\" > $portage_imagemagick'\n")
6194ea96d57SMauro Carvalho Chehab	}
620e45a6317SMauro Carvalho Chehab	if (qx(grep graphviz $portage_cairo 2>/dev/null) eq  "") {
6214ea96d57SMauro Carvalho Chehab		printf("\tsudo su -c 'echo \"$cairo\" > $portage_cairo'\n");
6224ea96d57SMauro Carvalho Chehab	}
6234ea96d57SMauro Carvalho Chehab
624bba1e4cbSMauro Carvalho Chehab	printf("\tsudo emerge --ask $install\n");
625bba1e4cbSMauro Carvalho Chehab
62624071ac1SMauro Carvalho Chehab}
62724071ac1SMauro Carvalho Chehab
62824071ac1SMauro Carvalho Chehabsub check_distros()
62924071ac1SMauro Carvalho Chehab{
63024071ac1SMauro Carvalho Chehab	# Distro-specific hints
63124071ac1SMauro Carvalho Chehab	if ($system_release =~ /Red Hat Enterprise Linux/) {
63224071ac1SMauro Carvalho Chehab		give_redhat_hints;
63324071ac1SMauro Carvalho Chehab		return;
63424071ac1SMauro Carvalho Chehab	}
6359b756a9dSMauro Carvalho Chehab	if ($system_release =~ /CentOS/) {
6369b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6379b756a9dSMauro Carvalho Chehab		return;
6389b756a9dSMauro Carvalho Chehab	}
6399b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Scientific Linux/) {
6409b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6419b756a9dSMauro Carvalho Chehab		return;
6429b756a9dSMauro Carvalho Chehab	}
6439b756a9dSMauro Carvalho Chehab	if ($system_release =~ /Oracle Linux Server/) {
6449b756a9dSMauro Carvalho Chehab		give_redhat_hints;
6459b756a9dSMauro Carvalho Chehab		return;
6469b756a9dSMauro Carvalho Chehab	}
64724071ac1SMauro Carvalho Chehab	if ($system_release =~ /Fedora/) {
64824071ac1SMauro Carvalho Chehab		give_redhat_hints;
64924071ac1SMauro Carvalho Chehab		return;
65024071ac1SMauro Carvalho Chehab	}
65124071ac1SMauro Carvalho Chehab	if ($system_release =~ /Ubuntu/) {
65224071ac1SMauro Carvalho Chehab		give_debian_hints;
65324071ac1SMauro Carvalho Chehab		return;
65424071ac1SMauro Carvalho Chehab	}
65524071ac1SMauro Carvalho Chehab	if ($system_release =~ /Debian/) {
65624071ac1SMauro Carvalho Chehab		give_debian_hints;
65724071ac1SMauro Carvalho Chehab		return;
65824071ac1SMauro Carvalho Chehab	}
65924071ac1SMauro Carvalho Chehab	if ($system_release =~ /openSUSE/) {
66024071ac1SMauro Carvalho Chehab		give_opensuse_hints;
66124071ac1SMauro Carvalho Chehab		return;
66224071ac1SMauro Carvalho Chehab	}
663800d408aSMauro Carvalho Chehab	if ($system_release =~ /Mageia/) {
664800d408aSMauro Carvalho Chehab		give_mageia_hints;
665800d408aSMauro Carvalho Chehab		return;
666800d408aSMauro Carvalho Chehab	}
667d6ebf189SMauro Carvalho Chehab	if ($system_release =~ /OpenMandriva/) {
668d6ebf189SMauro Carvalho Chehab		give_mageia_hints;
669d6ebf189SMauro Carvalho Chehab		return;
670d6ebf189SMauro Carvalho Chehab	}
67124071ac1SMauro Carvalho Chehab	if ($system_release =~ /Arch Linux/) {
67224071ac1SMauro Carvalho Chehab		give_arch_linux_hints;
67324071ac1SMauro Carvalho Chehab		return;
67424071ac1SMauro Carvalho Chehab	}
67524071ac1SMauro Carvalho Chehab	if ($system_release =~ /Gentoo/) {
67624071ac1SMauro Carvalho Chehab		give_gentoo_hints;
67724071ac1SMauro Carvalho Chehab		return;
67824071ac1SMauro Carvalho Chehab	}
67924071ac1SMauro Carvalho Chehab
68024071ac1SMauro Carvalho Chehab	#
68124071ac1SMauro Carvalho Chehab	# Fall-back to generic hint code for other distros
68224071ac1SMauro Carvalho Chehab	# That's far from ideal, specially for LaTeX dependencies.
68324071ac1SMauro Carvalho Chehab	#
68424071ac1SMauro Carvalho Chehab	my %map = (
68524071ac1SMauro Carvalho Chehab		"sphinx-build" => "sphinx"
68624071ac1SMauro Carvalho Chehab	);
68756e5a633SMauro Carvalho Chehab	check_missing_tex(2) if ($pdf);
68824071ac1SMauro Carvalho Chehab	check_missing(\%map);
68924071ac1SMauro Carvalho Chehab	print "I don't know distro $system_release.\n";
69024071ac1SMauro Carvalho Chehab	print "So, I can't provide you a hint with the install procedure.\n";
69124071ac1SMauro Carvalho Chehab	print "There are likely missing dependencies.\n";
69224071ac1SMauro Carvalho Chehab}
69324071ac1SMauro Carvalho Chehab
69424071ac1SMauro Carvalho Chehab#
69524071ac1SMauro Carvalho Chehab# Common dependencies
69624071ac1SMauro Carvalho Chehab#
69724071ac1SMauro Carvalho Chehab
6982730ce01SShuah Khansub deactivate_help()
6992730ce01SShuah Khan{
7002f9c5025SMauro Carvalho Chehab	printf "\nIf you want to exit the virtualenv, you can use:\n";
7012730ce01SShuah Khan	printf "\tdeactivate\n";
7022730ce01SShuah Khan}
7032730ce01SShuah Khan
70424071ac1SMauro Carvalho Chehabsub check_needs()
70524071ac1SMauro Carvalho Chehab{
7062f9c5025SMauro Carvalho Chehab	# Check if Sphinx is already accessible from current environment
70724071ac1SMauro Carvalho Chehab	check_sphinx();
7089b88ad54SMauro Carvalho Chehab
7099b88ad54SMauro Carvalho Chehab	if ($system_release) {
710ec43a27fSMauro Carvalho Chehab		print "Detected OS: $system_release.\n";
7119b88ad54SMauro Carvalho Chehab	} else {
712ec43a27fSMauro Carvalho Chehab		print "Unknown OS\n";
7139b88ad54SMauro Carvalho Chehab	}
714ec43a27fSMauro Carvalho Chehab	printf "Sphinx version: %s\n\n", $cur_version if ($cur_version);
7159b88ad54SMauro Carvalho Chehab
7162f9c5025SMauro Carvalho Chehab	# Check python command line, trying first python3
7172f9c5025SMauro Carvalho Chehab	$python_cmd = findprog("python3");
7182f9c5025SMauro Carvalho Chehab	$python_cmd = check_program("python", 0) if (!$python_cmd);
7192f9c5025SMauro Carvalho Chehab
7202f9c5025SMauro Carvalho Chehab	# Check the type of virtual env, depending on Python version
7212f9c5025SMauro Carvalho Chehab	if ($python_cmd) {
7222f9c5025SMauro Carvalho Chehab		if ($virtualenv) {
7232f9c5025SMauro Carvalho Chehab			my $tmp = qx($python_cmd --version 2>&1);
7242f9c5025SMauro Carvalho Chehab			if ($tmp =~ m/(\d+\.)(\d+\.)/) {
7252f9c5025SMauro Carvalho Chehab				if ($1 >= 3 && $2 >= 3) {
7262f9c5025SMauro Carvalho Chehab					$need_venv = 1;		# python 3.3 or upper
7272f9c5025SMauro Carvalho Chehab				} else {
7282f9c5025SMauro Carvalho Chehab					$need_virtualenv = 1;
7292f9c5025SMauro Carvalho Chehab				}
7302f9c5025SMauro Carvalho Chehab				if ($1 < 3) {
731*4217e507SJonathan Corbet					# Fail if it finds python2 (or worse)
732*4217e507SJonathan Corbet					die "Python 3 is required to build the kernel docs\n";
7332f9c5025SMauro Carvalho Chehab				}
7342f9c5025SMauro Carvalho Chehab			} else {
7352f9c5025SMauro Carvalho Chehab				die "Warning: couldn't identify $python_cmd version!";
7362f9c5025SMauro Carvalho Chehab			}
7372f9c5025SMauro Carvalho Chehab		} else {
7382f9c5025SMauro Carvalho Chehab			add_package("python-sphinx", 0);
7392f9c5025SMauro Carvalho Chehab		}
7402f9c5025SMauro Carvalho Chehab	}
7412f9c5025SMauro Carvalho Chehab
7422f9c5025SMauro Carvalho Chehab	# Set virtualenv command line, if python < 3.3
7432f9c5025SMauro Carvalho Chehab	my $virtualenv_cmd;
7442f9c5025SMauro Carvalho Chehab	if ($need_virtualenv) {
7452f9c5025SMauro Carvalho Chehab		$virtualenv_cmd = findprog("virtualenv-3");
7462f9c5025SMauro Carvalho Chehab		$virtualenv_cmd = findprog("virtualenv-3.5") if (!$virtualenv_cmd);
7472f9c5025SMauro Carvalho Chehab		if (!$virtualenv_cmd) {
7482f9c5025SMauro Carvalho Chehab			check_program("virtualenv", 0);
7492f9c5025SMauro Carvalho Chehab			$virtualenv_cmd = "virtualenv";
7502f9c5025SMauro Carvalho Chehab		}
7512f9c5025SMauro Carvalho Chehab	}
7522f9c5025SMauro Carvalho Chehab
7539b88ad54SMauro Carvalho Chehab	# Check for needed programs/tools
75424071ac1SMauro Carvalho Chehab	check_perl_module("Pod::Usage", 0);
75524071ac1SMauro Carvalho Chehab	check_program("make", 0);
75624071ac1SMauro Carvalho Chehab	check_program("gcc", 0);
75724071ac1SMauro Carvalho Chehab	check_python_module("sphinx_rtd_theme", 1) if (!$virtualenv);
75824071ac1SMauro Carvalho Chehab	check_program("dot", 1);
75924071ac1SMauro Carvalho Chehab	check_program("convert", 1);
76056e5a633SMauro Carvalho Chehab
76156e5a633SMauro Carvalho Chehab	# Extra PDF files - should use 2 for is_optional
76256e5a633SMauro Carvalho Chehab	check_program("xelatex", 2) if ($pdf);
76356e5a633SMauro Carvalho Chehab	check_program("rsvg-convert", 2) if ($pdf);
76456e5a633SMauro Carvalho Chehab	check_program("latexmk", 2) if ($pdf);
76524071ac1SMauro Carvalho Chehab
7662f9c5025SMauro Carvalho Chehab	if ($need_sphinx || $rec_sphinx_upgrade) {
7672f9c5025SMauro Carvalho Chehab		check_python_module("ensurepip", 0) if ($need_venv);
7682f9c5025SMauro Carvalho Chehab	}
7692f9c5025SMauro Carvalho Chehab
7702f9c5025SMauro Carvalho Chehab	# Do distro-specific checks and output distro-install commands
77124071ac1SMauro Carvalho Chehab	check_distros();
77224071ac1SMauro Carvalho Chehab
7732f9c5025SMauro Carvalho Chehab	if (!$python_cmd) {
7742f9c5025SMauro Carvalho Chehab		if ($need == 1) {
7752f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependency is missing";
7762f9c5025SMauro Carvalho Chehab		} elsif ($need) {
7772f9c5025SMauro Carvalho Chehab			die "Can't build as $need mandatory dependencies are missing";
7782f9c5025SMauro Carvalho Chehab		}
7792f9c5025SMauro Carvalho Chehab	}
7802f9c5025SMauro Carvalho Chehab
7812f9c5025SMauro Carvalho Chehab	# Check if sphinx-build is called sphinx-build-3
78224071ac1SMauro Carvalho Chehab	if ($need_symlink) {
78324071ac1SMauro Carvalho Chehab		printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
78424071ac1SMauro Carvalho Chehab		       which("sphinx-build-3");
78524071ac1SMauro Carvalho Chehab	}
7862f9c5025SMauro Carvalho Chehab
7872f9c5025SMauro Carvalho Chehab	# NOTE: if the system has a too old Sphinx version installed,
7882f9c5025SMauro Carvalho Chehab	# it will recommend installing a newer version using virtualenv
7892f9c5025SMauro Carvalho Chehab
79077d09ad9SMauro Carvalho Chehab	if ($need_sphinx || $rec_sphinx_upgrade) {
79144f42165SMauro Carvalho Chehab		my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
79244f42165SMauro Carvalho Chehab		my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
79344f42165SMauro Carvalho Chehab
794ec43a27fSMauro Carvalho Chehab		if ($cur_version lt $rec_version) {
795ec43a27fSMauro Carvalho Chehab			print "Warning: It is recommended at least Sphinx version $rec_version.\n";
796ec43a27fSMauro Carvalho Chehab			print "         If you want pdf, you need at least $min_pdf_version.\n";
797ec43a27fSMauro Carvalho Chehab		}
798ec43a27fSMauro Carvalho Chehab		if ($cur_version lt $min_pdf_version) {
799ec43a27fSMauro Carvalho Chehab			print "Note: It is recommended at least Sphinx version $min_pdf_version if you need PDF support.\n";
800ec43a27fSMauro Carvalho Chehab		}
80144f42165SMauro Carvalho Chehab		@activates = sort {$b cmp $a} @activates;
802a8b380c3SMauro Carvalho Chehab		my ($activate, $ver);
803a8b380c3SMauro Carvalho Chehab		foreach my $f (@activates) {
8042834a741SMauro Carvalho Chehab			next if ($f lt $min_activate);
80544f42165SMauro Carvalho Chehab
8062834a741SMauro Carvalho Chehab			my $sphinx_cmd = $f;
807a8b380c3SMauro Carvalho Chehab			$sphinx_cmd =~ s/activate/sphinx-build/;
808a8b380c3SMauro Carvalho Chehab			next if (! -f $sphinx_cmd);
809a8b380c3SMauro Carvalho Chehab
810a8b380c3SMauro Carvalho Chehab			$ver = get_sphinx_version($sphinx_cmd);
8112834a741SMauro Carvalho Chehab			if ($need_sphinx && ($ver ge $min_version)) {
8122834a741SMauro Carvalho Chehab				$activate = $f;
8132834a741SMauro Carvalho Chehab				last;
8142834a741SMauro Carvalho Chehab			} elsif ($ver gt $cur_version) {
8152834a741SMauro Carvalho Chehab				$activate = $f;
8162834a741SMauro Carvalho Chehab				last;
817a8b380c3SMauro Carvalho Chehab			}
8182834a741SMauro Carvalho Chehab		}
8192834a741SMauro Carvalho Chehab		if ($activate ne "") {
8202834a741SMauro Carvalho Chehab			if ($need_sphinx) {
821a8b380c3SMauro Carvalho Chehab				printf "\nNeed to activate Sphinx (version $ver) on virtualenv with:\n";
822a8b380c3SMauro Carvalho Chehab				printf "\t. $activate\n";
8232730ce01SShuah Khan				deactivate_help();
8249b88ad54SMauro Carvalho Chehab				exit (1);
8255be33182SMauro Carvalho Chehab			} else {
8262834a741SMauro Carvalho Chehab				printf "\nYou may also use a newer Sphinx (version $ver) with:\n";
8272834a741SMauro Carvalho Chehab				printf "\tdeactivate && . $activate\n";
8282834a741SMauro Carvalho Chehab			}
8292834a741SMauro Carvalho Chehab		} else {
83044f42165SMauro Carvalho Chehab			my $rec_activate = "$virtenv_dir/bin/activate";
83124071ac1SMauro Carvalho Chehab
8322834a741SMauro Carvalho Chehab			print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
8332834a741SMauro Carvalho Chehab
834412b09ddSMauro Carvalho Chehab			$python_cmd = find_python_no_venv();
835412b09ddSMauro Carvalho Chehab
8362f9c5025SMauro Carvalho Chehab			if ($need_venv) {
8372f9c5025SMauro Carvalho Chehab				printf "\t$python_cmd -m venv $virtenv_dir\n";
8382f9c5025SMauro Carvalho Chehab			} else {
8392f9c5025SMauro Carvalho Chehab				printf "\t$virtualenv_cmd $virtenv_dir\n";
840c428cd52STim Bird			}
84144f42165SMauro Carvalho Chehab			printf "\t. $rec_activate\n";
842fb947f3fSMauro Carvalho Chehab			printf "\tpip install -r $requirement_file\n";
8432730ce01SShuah Khan			deactivate_help();
84477d09ad9SMauro Carvalho Chehab
84577d09ad9SMauro Carvalho Chehab			$need++ if (!$rec_sphinx_upgrade);
84624071ac1SMauro Carvalho Chehab		}
8475be33182SMauro Carvalho Chehab	}
84824071ac1SMauro Carvalho Chehab	printf "\n";
84924071ac1SMauro Carvalho Chehab
85054002b56SBjorn Helgaas	print "All optional dependencies are met.\n" if (!$optional);
85124071ac1SMauro Carvalho Chehab
85224071ac1SMauro Carvalho Chehab	if ($need == 1) {
85324071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependency is missing";
85424071ac1SMauro Carvalho Chehab	} elsif ($need) {
85524071ac1SMauro Carvalho Chehab		die "Can't build as $need mandatory dependencies are missing";
85624071ac1SMauro Carvalho Chehab	}
85724071ac1SMauro Carvalho Chehab
85824071ac1SMauro Carvalho Chehab	print "Needed package dependencies are met.\n";
85924071ac1SMauro Carvalho Chehab}
86024071ac1SMauro Carvalho Chehab
86124071ac1SMauro Carvalho Chehab#
86224071ac1SMauro Carvalho Chehab# Main
86324071ac1SMauro Carvalho Chehab#
86424071ac1SMauro Carvalho Chehab
86524071ac1SMauro Carvalho Chehabwhile (@ARGV) {
86624071ac1SMauro Carvalho Chehab	my $arg = shift(@ARGV);
86724071ac1SMauro Carvalho Chehab
86824071ac1SMauro Carvalho Chehab	if ($arg eq "--no-virtualenv") {
86924071ac1SMauro Carvalho Chehab		$virtualenv = 0;
87024071ac1SMauro Carvalho Chehab	} elsif ($arg eq "--no-pdf"){
87124071ac1SMauro Carvalho Chehab		$pdf = 0;
8729b88ad54SMauro Carvalho Chehab	} elsif ($arg eq "--version-check"){
8739b88ad54SMauro Carvalho Chehab		$version_check = 1;
87424071ac1SMauro Carvalho Chehab	} else {
8759b88ad54SMauro Carvalho Chehab		print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
8769b88ad54SMauro Carvalho Chehab		print "Where:\n";
8779b88ad54SMauro Carvalho Chehab		print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
8789b88ad54SMauro Carvalho Chehab		print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
8799b88ad54SMauro Carvalho Chehab		print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
88024071ac1SMauro Carvalho Chehab		exit -1;
88124071ac1SMauro Carvalho Chehab	}
88224071ac1SMauro Carvalho Chehab}
88324071ac1SMauro Carvalho Chehab
88424071ac1SMauro Carvalho Chehab#
88524071ac1SMauro Carvalho Chehab# Determine the system type. There's no standard unique way that would
88624071ac1SMauro Carvalho Chehab# work with all distros with a minimal package install. So, several
88724071ac1SMauro Carvalho Chehab# methods are used here.
88824071ac1SMauro Carvalho Chehab#
88924071ac1SMauro Carvalho Chehab# By default, it will use lsb_release function. If not available, it will
89024071ac1SMauro Carvalho Chehab# fail back to reading the known different places where the distro name
89124071ac1SMauro Carvalho Chehab# is stored
89224071ac1SMauro Carvalho Chehab#
89324071ac1SMauro Carvalho Chehab
89424071ac1SMauro Carvalho Chehab$system_release = qx(lsb_release -d) if which("lsb_release");
89524071ac1SMauro Carvalho Chehab$system_release =~ s/Description:\s*// if ($system_release);
89624071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/system-release") if !$system_release;
89724071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/redhat-release") if !$system_release;
89824071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/lsb-release") if !$system_release;
89924071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/gentoo-release") if !$system_release;
900d14d0c1aSMauro Carvalho Chehab
901d14d0c1aSMauro Carvalho Chehab# This seems more common than LSB these days
902d14d0c1aSMauro Carvalho Chehabif (!$system_release) {
903d14d0c1aSMauro Carvalho Chehab	my %os_var;
904d14d0c1aSMauro Carvalho Chehab	if (open IN, "cat /etc/os-release|") {
905d14d0c1aSMauro Carvalho Chehab		while (<IN>) {
906d14d0c1aSMauro Carvalho Chehab			if (m/^([\w\d\_]+)=\"?([^\"]*)\"?\n/) {
907d14d0c1aSMauro Carvalho Chehab				$os_var{$1}=$2;
908d14d0c1aSMauro Carvalho Chehab			}
909d14d0c1aSMauro Carvalho Chehab		}
910d14d0c1aSMauro Carvalho Chehab		$system_release = $os_var{"NAME"};
911d14d0c1aSMauro Carvalho Chehab		if (defined($os_var{"VERSION_ID"})) {
912d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION_ID"} if (defined($os_var{"VERSION_ID"}));
913d14d0c1aSMauro Carvalho Chehab		} else {
914d14d0c1aSMauro Carvalho Chehab			$system_release .= " " . $os_var{"VERSION"};
915d14d0c1aSMauro Carvalho Chehab		}
916d14d0c1aSMauro Carvalho Chehab	}
917d14d0c1aSMauro Carvalho Chehab}
91824071ac1SMauro Carvalho Chehab$system_release = catcheck("/etc/issue") if !$system_release;
91924071ac1SMauro Carvalho Chehab$system_release =~ s/\s+$//;
92024071ac1SMauro Carvalho Chehab
92124071ac1SMauro Carvalho Chehabcheck_needs;
922