sphinx-pre-install 26 KB

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