diff options
author | Justine Tunney <jart@google.com> | 2017-11-13 22:13:54 -0800 |
---|---|---|
committer | Martin Wicke <martin.wicke@gmail.com> | 2017-11-13 22:13:54 -0800 |
commit | a9bd6d67022664db5ccd8128df46651d1d14ee8d (patch) | |
tree | ce21033e69929f5aabd77b2004b4567b57cc790e | |
parent | 577a55777251dfd85bb285fd246a45e913ead6ca (diff) |
Fix Python auto configure build support (#14467)
7ccfbdf caused our Python autoconf to no longer be auto. This was likely due to
the evil helper _get_env_var() which has now been removed.
This change blocks tensorflow/tensorboard#719
-rw-r--r-- | third_party/py/python_configure.bzl | 91 |
1 files changed, 36 insertions, 55 deletions
diff --git a/third_party/py/python_configure.bzl b/third_party/py/python_configure.bzl index bbc07905fc..c16eb3a12a 100644 --- a/third_party/py/python_configure.bzl +++ b/third_party/py/python_configure.bzl @@ -1,11 +1,8 @@ -# -*- Python -*- """Repository rule for Python autoconfiguration. `python_configure` depends on the following environment variables: - * `NUMPY_INCLUDE_PATH`: Location of Numpy libraries. * `PYTHON_BIN_PATH`: location of python binary. - * `PYTHON_INCLUDE_PATH`: Location of python binaries. * `PYTHON_LIB_PATH`: Location of python libraries. """ @@ -23,32 +20,13 @@ def _tpl(repository_ctx, tpl, substitutions={}, out=None): substitutions) -def _python_configure_warning(msg): - """Output warning message during auto configuration.""" - yellow = "\033[1;33m" - no_color = "\033[0m" - print("%sPython Configuration Warning:%s %s" % (yellow, no_color, msg)) - - -def _python_configure_fail(msg): +def _fail(msg): """Output failure message when auto configuration fails.""" red = "\033[0;31m" no_color = "\033[0m" fail("%sPython Configuration Error:%s %s\n" % (red, no_color, msg)) -def _get_env_var(repository_ctx, name, default = None, enable_warning = True): - """Find an environment variable in system path.""" - if name in repository_ctx.os.environ: - return repository_ctx.os.environ[name] - if default != None: - if enable_warning: - _python_configure_warning( - "'%s' environment variable is not set, using '%s' as default" % (name, default)) - return default - _python_configure_fail("'%s' environment variable is not set" % name) - - def _is_windows(repository_ctx): """Returns true if the host operating system is windows.""" os_name = repository_ctx.os.name.lower() @@ -73,11 +51,10 @@ def _execute(repository_ctx, cmdline, error_msg=None, error_details=None, """ result = repository_ctx.execute(cmdline) if result.stderr or not (empty_stdout_fine or result.stdout): - _python_configure_fail( - "\n".join([ - error_msg.strip() if error_msg else "Repository command failed", - result.stderr.strip(), - error_details if error_details else ""])) + _fail("\n".join([ + error_msg.strip() if error_msg else "Repository command failed", + result.stderr.strip(), + error_details if error_details else ""])) return result @@ -163,21 +140,23 @@ def _symlink_genrule_for_dir(repository_ctx, src_dir, dest_dir, genrule_name, def _get_python_bin(repository_ctx): """Gets the python bin path.""" - python_bin = _get_env_var(repository_ctx, _PYTHON_BIN_PATH, - None, False) + python_bin = repository_ctx.os.environ.get(_PYTHON_BIN_PATH) if python_bin != None: return python_bin python_bin_path = repository_ctx.which("python") if python_bin_path != None: return str(python_bin_path) - path = _get_env_var(repository_ctx, "PATH") - _python_configure_fail("Cannot find python in PATH, please make sure " + - "python is installed and add its directory in PATH, or set the " + - "environment variable PYTHON_BIN_PATH.\nPATH=%s" % (path)) + _fail("Cannot find python in PATH, please make sure " + + "python is installed and add its directory in PATH, or --define " + + "%s='/something/else'.\nPATH=%s" % ( + _PYTHON_BIN_PATH, repository_ctx.os.environ.get("PATH", ""))) def _get_python_lib(repository_ctx, python_bin): """Gets the python lib path.""" + python_lib = repository_ctx.os.environ.get(_PYTHON_LIB_PATH) + if python_lib != None: + return python_lib print_lib = ("<<END\n" + "from __future__ import print_function\n" + "import site\n" + @@ -214,7 +193,7 @@ def _check_python_lib(repository_ctx, python_lib): cmd = 'test -d "%s" -a -x "%s"' % (python_lib, python_lib) result = repository_ctx.execute(["bash", "-c", cmd]) if result.return_code == 1: - _python_configure_fail("Invalid python library path: %s" % python_lib) + _fail("Invalid python library path: %s" % python_lib) def _check_python_bin(repository_ctx, python_bin): @@ -222,33 +201,36 @@ def _check_python_bin(repository_ctx, python_bin): cmd = '[[ -x "%s" ]] && [[ ! -d "%s" ]]' % (python_bin, python_bin) result = repository_ctx.execute(["bash", "-c", cmd]) if result.return_code == 1: - _python_configure_fail( - "PYTHON_BIN_PATH is not executable. Is it the python binary?") + _fail("--define %s='%s' is not executable. Is it the python binary?" % ( + _PYTHON_BIN_PATH, python_bin)) def _get_python_include(repository_ctx, python_bin): """Gets the python include path.""" - result = _execute(repository_ctx, - [python_bin, "-c", - 'from __future__ import print_function;' + - 'from distutils import sysconfig;' + - 'print(sysconfig.get_python_inc())'], - error_msg="Problem getting python include path.", - error_details=("Is the Python binary path set up right? " + - "(See ./configure or PYTHON_BIN_PATH.) " + - "Is distutils installed?")) + result = _execute( + repository_ctx, + [python_bin, "-c", + 'from __future__ import print_function;' + + 'from distutils import sysconfig;' + + 'print(sysconfig.get_python_inc())'], + error_msg="Problem getting python include path.", + error_details=("Is the Python binary path set up right? " + + "(See ./configure or " + _PYTHON_BIN_PATH + ".) " + + "Is distutils installed?")) return result.stdout.splitlines()[0] def _get_python_import_lib_name(repository_ctx, python_bin): """Get Python import library name (pythonXY.lib) on Windows.""" - result = _execute(repository_ctx, - [python_bin, "-c", - 'import sys;' + - 'print("python" + str(sys.version_info[0]) + str(sys.version_info[1]) + ".lib")'], - error_msg="Problem getting python import library.", - error_details=("Is the Python binary path set up right? " + - "(See ./configure or PYTHON_BIN_PATH.) ")) + result = _execute( + repository_ctx, + [python_bin, "-c", + 'import sys;' + + 'print("python" + str(sys.version_info[0]) + ' + + ' str(sys.version_info[1]) + ".lib")'], + error_msg="Problem getting python import library.", + error_details=("Is the Python binary path set up right? " + + "(See ./configure or " + _PYTHON_BIN_PATH + ".) ")) return result.stdout.splitlines()[0] @@ -267,8 +249,7 @@ def _create_local_python_repository(repository_ctx): """Creates the repository containing files set up to build with Python.""" python_bin = _get_python_bin(repository_ctx) _check_python_bin(repository_ctx, python_bin) - python_lib = _get_env_var(repository_ctx, _PYTHON_LIB_PATH, - _get_python_lib(repository_ctx, python_bin)) + python_lib = _get_python_lib(repository_ctx, python_bin) _check_python_lib(repository_ctx, python_lib) python_include = _get_python_include(repository_ctx, python_bin) numpy_include = _get_numpy_include(repository_ctx, python_bin) + '/numpy' |