]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - scripts/sphinx-pre-install
Merge tag 'dma-mapping-5.3' of git://git.infradead.org/users/hch/dma-mapping
[linux.git] / scripts / sphinx-pre-install
index f6a5c0bae31ec5aca92fa3da34aa0408265dad28..f230e65329a293a6cec6f32fc6aad412e728fdf9 100755 (executable)
@@ -1,20 +1,16 @@
 #!/usr/bin/perl
+# SPDX-License-Identifier: GPL-2.0-or-later
 use strict;
 
-# Copyright (c) 2017 Mauro Carvalho Chehab <mchehab@kernel.org>
+# Copyright (c) 2017-2019 Mauro Carvalho Chehab <mchehab@kernel.org>
 #
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
 
-my $virtenv_dir = "sphinx_1.4";
-my $requirement_file = "Documentation/sphinx/requirements.txt";
+my $prefix = "./";
+$prefix = "$ENV{'srctree'}/" if ($ENV{'srctree'});
+
+my $conf = $prefix . "Documentation/conf.py";
+my $requirement_file = $prefix . "Documentation/sphinx/requirements.txt";
+my $virtenv_prefix = "sphinx_";
 
 #
 # Static vars
@@ -26,7 +22,10 @@ my $need = 0;
 my $optional = 0;
 my $need_symlink = 0;
 my $need_sphinx = 0;
+my $rec_sphinx_upgrade = 0;
 my $install = "";
+my $virtenv_dir = "";
+my $min_version;
 
 #
 # Command line arguments
@@ -34,6 +33,7 @@ my $install = "";
 
 my $pdf = 1;
 my $virtualenv = 1;
+my $version_check = 0;
 
 #
 # List of required texlive packages on Fedora and OpenSuse
@@ -201,13 +201,15 @@ sub check_missing_tex($)
        }
 }
 
-sub check_sphinx()
+sub get_sphinx_fname()
 {
-       return if findprog("sphinx-build");
+       my $fname = "sphinx-build";
+       return $fname if findprog($fname);
 
-       if (findprog("sphinx-build-3")) {
+       $fname = "sphinx-build-3";
+       if (findprog($fname)) {
                $need_symlink = 1;
-               return;
+               return $fname;
        }
 
        if ($virtualenv) {
@@ -219,6 +221,74 @@ sub check_sphinx()
        } else {
                add_package("python-sphinx", 0);
        }
+
+       return "";
+}
+
+sub check_sphinx()
+{
+       my $rec_version;
+       my $cur_version;
+
+       open IN, $conf or die "Can't open $conf";
+       while (<IN>) {
+               if (m/^\s*needs_sphinx\s*=\s*[\'\"]([\d\.]+)[\'\"]/) {
+                       $min_version=$1;
+                       last;
+               }
+       }
+       close IN;
+
+       die "Can't get needs_sphinx version from $conf" if (!$min_version);
+
+       open IN, $requirement_file or die "Can't open $requirement_file";
+       while (<IN>) {
+               if (m/^\s*Sphinx\s*==\s*([\d\.]+)$/) {
+                       $rec_version=$1;
+                       last;
+               }
+       }
+       close IN;
+
+       die "Can't get recommended sphinx version from $requirement_file" if (!$min_version);
+
+       $virtenv_dir = $virtenv_prefix . $rec_version;
+
+       my $sphinx = get_sphinx_fname();
+       return if ($sphinx eq "");
+
+       open IN, "$sphinx --version 2>&1 |" or die "$sphinx returned an error";
+       while (<IN>) {
+               if (m/^\s*sphinx-build\s+([\d\.]+)$/) {
+                       $cur_version=$1;
+                       last;
+               }
+               # Sphinx 1.2.x uses a different format
+               if (m/^\s*Sphinx.*\s+([\d\.]+)$/) {
+                       $cur_version=$1;
+                       last;
+               }
+       }
+       close IN;
+
+       die "$sphinx didn't return its version" if (!$cur_version);
+
+       if ($cur_version lt $min_version) {
+               printf "ERROR: Sphinx version is %s. It should be >= %s (recommended >= %s)\n",
+                      $cur_version, $min_version, $rec_version;;
+               $need_sphinx = 1;
+               return;
+       }
+
+       if ($cur_version lt $rec_version) {
+               printf "Sphinx version %s\n", $cur_version;
+               print "Warning: It is recommended at least Sphinx version $rec_version.\n";
+               $rec_sphinx_upgrade = 1;
+               return;
+       }
+
+       # On version check mode, just assume Sphinx has all mandatory deps
+       exit (0) if ($version_check);
 }
 
 #
@@ -503,27 +573,18 @@ sub check_distros()
 
 sub check_needs()
 {
+       # Check for needed programs/tools
+       check_sphinx();
+
        if ($system_release) {
-               print "Detected OS: $system_release.\n";
+               print "Detected OS: $system_release.\n\n";
        } else {
-               print "Unknown OS\n";
+               print "Unknown OS\n\n";
        }
 
-       # RHEL 7.x and clones have Sphinx version 1.1.x and incomplete texlive
-       if (($system_release =~ /Red Hat Enterprise Linux/) ||
-           ($system_release =~ /CentOS/) ||
-           ($system_release =~ /Scientific Linux/) ||
-           ($system_release =~ /Oracle Linux Server/)) {
-               $virtualenv = 1;
-               $pdf = 0;
-
-               printf("NOTE: On this distro, Sphinx and TexLive shipped versions are incompatible\n");
-               printf("with doc build. So, use Sphinx via a Python virtual environment.\n\n");
-               printf("This script can't install a TexLive version that would provide PDF.\n");
-       }
+       print "To upgrade Sphinx, use:\n\n" if ($rec_sphinx_upgrade);
 
        # Check for needed programs/tools
-       check_sphinx();
        check_perl_module("Pod::Usage", 0);
        check_program("make", 0);
        check_program("gcc", 0);
@@ -540,26 +601,33 @@ sub check_needs()
                printf "\tsudo ln -sf %s /usr/bin/sphinx-build\n\n",
                       which("sphinx-build-3");
        }
-       if ($need_sphinx) {
-               my $activate = "$virtenv_dir/bin/activate";
-               if (-e "$ENV{'PWD'}/$activate") {
-                       printf "\nNeed to activate virtualenv with:\n";
-                       printf "\t. $activate\n";
+       if ($need_sphinx || $rec_sphinx_upgrade) {
+               my $min_activate = "$ENV{'PWD'}/${virtenv_prefix}${min_version}/bin/activate";
+               my @activates = glob "$ENV{'PWD'}/${virtenv_prefix}*/bin/activate";
+
+               @activates = sort {$b cmp $a} @activates;
+
+               if ($need_sphinx && scalar @activates > 0 && $activates[0] ge $min_activate) {
+                       printf "\nNeed to activate a compatible Sphinx version on virtualenv with:\n";
+                       printf "\t. $activates[0]\n";
+                       exit (1);
                } else {
+                       my $rec_activate = "$virtenv_dir/bin/activate";
                        my $virtualenv = findprog("virtualenv-3");
                        $virtualenv = findprog("virtualenv-3.5") if (!$virtualenv);
                        $virtualenv = findprog("virtualenv") if (!$virtualenv);
                        $virtualenv = "virtualenv" if (!$virtualenv);
 
                        printf "\t$virtualenv $virtenv_dir\n";
-                       printf "\t. $activate\n";
+                       printf "\t. $rec_activate\n";
                        printf "\tpip install -r $requirement_file\n";
-                       $need++;
+
+                       $need++ if (!$rec_sphinx_upgrade);
                }
        }
        printf "\n";
 
-       print "All optional dependenties are met.\n" if (!$optional);
+       print "All optional dependencies are met.\n" if (!$optional);
 
        if ($need == 1) {
                die "Can't build as $need mandatory dependency is missing";
@@ -581,8 +649,14 @@ while (@ARGV) {
                $virtualenv = 0;
        } elsif ($arg eq "--no-pdf"){
                $pdf = 0;
+       } elsif ($arg eq "--version-check"){
+               $version_check = 1;
        } else {
-               print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf>\n\n";
+               print "Usage:\n\t$0 <--no-virtualenv> <--no-pdf> <--version-check>\n\n";
+               print "Where:\n";
+               print "\t--no-virtualenv\t- Recommend installing Sphinx instead of using a virtualenv\n";
+               print "\t--version-check\t- if version is compatible, don't check for missing dependencies\n";
+               print "\t--no-pdf\t- don't check for dependencies required to build PDF docs\n\n";
                exit -1;
        }
 }