From d4b3b633d8c8f72836ef44431ff1e75dd0b5d981 Mon Sep 17 00:00:00 2001 From: Emily Marigold Klassen Date: Fri, 6 Oct 2017 16:49:45 -0700 Subject: [PATCH] build: allow build with system python 3 When the system python is python 3, configure now creates a directory with a symlink called 'python' to python2, uses it when it calls run_gyp, and puts it in config.mk so that it propagates to everything that make launches PR-URL: https://github.com/nodejs/node/pull/16058 Reviewed-By: Gibson Fahnestock Reviewed-By: Benjamin Gruenbaum Reviewed-By: Anna Henningsen Reviewed-By: Bryan English Reviewed-By: Ben Noordhuis --- configure | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/configure b/configure index 1eaf26c0955575..2b130743790139 100755 --- a/configure +++ b/configure @@ -12,10 +12,10 @@ exec python "$0" "$@" del _ import sys +from distutils.spawn import find_executable as which if sys.version_info[0] != 2 or sys.version_info[1] not in (6, 7): sys.stderr.write('Please use either Python 2.6 or 2.7') - from distutils.spawn import find_executable as which python2 = which('python2') or which('python2.6') or which('python2.7') if python2: @@ -1318,6 +1318,36 @@ def configure_inspector(o): options.without_ssl) o['variables']['v8_inspector'] = b(not disable_inspector) + +def get_bin_override(): + # If the system python is not the python we are running (which should be + # python 2), then create a directory with a symlink called `python` to our + # sys.executable. This directory will be prefixed to the PATH, so that + # other tools that shell out to `python` will use the appropriate python + + if os.path.realpath(which('python')) == os.path.realpath(sys.executable): + return + + bin_override = os.path.abspath('out/tools/bin') + try: + os.makedirs(bin_override) + except OSError as e: + if e.errno != errno.EEXIST: raise e + + python_link = os.path.join(bin_override, 'python') + try: + os.unlink(python_link) + except OSError as e: + if e.errno != errno.ENOENT: raise e + os.symlink(sys.executable, python_link) + + # We need to set the environment right now so that when gyp (in run_gyp) + # shells out, it finds the right python (specifically at + # https://github.com/nodejs/node/blob/d82e107/deps/v8/gypfiles/toolchain.gypi#L43) + os.environ['PATH'] = bin_override + ':' + os.environ['PATH'] + + return bin_override + output = { 'variables': {}, 'include_dirs': [], @@ -1394,6 +1424,10 @@ if options.prefix: config = '\n'.join(map('='.join, config.iteritems())) + '\n' +bin_override = get_bin_override() +if bin_override: + config = 'export PATH:=' + bin_override + ':$(PATH)\n' + config + write('config.mk', do_not_edit + config) gyp_args = ['--no-parallel']