diff --git a/lib/build.js b/lib/build.js index c2388fb348..3baba4140c 100644 --- a/lib/build.js +++ b/lib/build.js @@ -185,6 +185,13 @@ function build (gyp, argv, callback) { } } + if (!win) { + // Add build-time dependency symlinks (such as Python) to PATH + const buildBinsDir = path.resolve('build', 'node_gyp_bins') + process.env.PATH = `${buildBinsDir}:${process.env.PATH}` + log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`) + } + var proc = gyp.spawn(command, argv) proc.on('exit', onExit) } diff --git a/lib/configure.js b/lib/configure.js index 17a6487fa9..c7010385b5 100644 --- a/lib/configure.js +++ b/lib/configure.js @@ -17,6 +17,7 @@ if (win) { function configure (gyp, argv, callback) { var python var buildDir = path.resolve('build') + var buildBinsDir = path.join(buildDir, 'node_gyp_bins') var configNames = ['config.gypi', 'common.gypi'] var configs = [] var nodeDir @@ -73,7 +74,9 @@ function configure (gyp, argv, callback) { function createBuildDir () { log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir) - fs.mkdir(buildDir, { recursive: true }, function (err, isNew) { + + const deepestBuildDirSubdirectory = win ? buildDir : buildBinsDir + fs.mkdir(deepestBuildDirSubdirectory, { recursive: true }, function (err, isNew) { if (err) { return callback(err) } @@ -84,11 +87,31 @@ function configure (gyp, argv, callback) { findVisualStudio(release.semver, gyp.opts.msvs_version, createConfigFile) } else { + createPythonSymlink() createConfigFile() } }) } + function createPythonSymlink () { + const symlinkDestination = path.join(buildBinsDir, 'python3') + + log.verbose('python symlink', `creating symlink to "${python}" at "${symlinkDestination}"`) + + fs.unlink(symlinkDestination, function (err) { + if (err && err.code !== 'ENOENT') { + log.verbose('python symlink', 'error when attempting to remove existing symlink') + log.verbose('python symlink', err.stack, 'errno: ' + err.errno) + } + fs.symlink(python, symlinkDestination, function (err) { + if (err) { + log.verbose('python symlink', 'error when attempting to create Python symlink') + log.verbose('python symlink', err.stack, 'errno: ' + err.errno) + } + }) + }) + } + function createConfigFile (err, vsInfo) { if (err) { return callback(err) diff --git a/test/test-configure-python.js b/test/test-configure-python.js index 4290e7af1b..aacd75f7c7 100644 --- a/test/test-configure-python.js +++ b/test/test-configure-python.js @@ -14,7 +14,9 @@ const configure = requireInject('../lib/configure', { mkdir: function (dir, options, cb) { cb() }, promises: { writeFile: function (file, data) { return Promise.resolve(null) } - } + }, + unlink: function (path, cb) { cb() }, + symlink: function (target, path, cb) { cb() } } })