Skip to content

Commit 4161552

Browse files
committed
lib: create a Python symlink and add it to PATH
Helps to ensure a version of Python validated by lib/find-python.js is used to run various Python scripts generated by gyp. Known to affect gyp-mac-tool, probably affects gyp-flock-tool as well. These Python scripts (such as `gyp-mac-tool`) are invoked directly, via the generated Makefile, so their shebang lines determine which Python binary is used to run them. The shebang lines of these scripts are all `#!/usr/bin/env python3`, so the first `python3` on the user's PATH will be used. By adding a symlink to the Python binary validated by find-python.js, and putting this symlink first on the PATH, we can ensure we use a compatible version of Python to run these scripts. (Only on Unix/Unix-like OSes. Symlinks are tricky on Windows, and Python isn't used at build-time anyhow on Windows, so this intervention isn't useful or necessary on Windows. A similar technique for Windows, no symlinks required, would be to make batch scripts which execute the target binary, much like what Node does for its bundled copy of npm on Windows.)
1 parent 6f74c76 commit 4161552

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/build.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ function build (gyp, argv, callback) {
185185
}
186186
}
187187

188+
if (!win) {
189+
// Add build-time dependency symlinks (such as Python) to PATH
190+
const buildBinsDir = path.resolve('build', 'node_gyp_bins')
191+
process.env.PATH = `${buildBinsDir}:${process.env.PATH}`
192+
log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`)
193+
}
194+
188195
var proc = gyp.spawn(command, argv)
189196
proc.on('exit', onExit)
190197
}

lib/configure.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (win) {
1717
function configure (gyp, argv, callback) {
1818
var python
1919
var buildDir = path.resolve('build')
20+
var buildBinsDir = path.join(buildDir, 'node_gyp_bins')
2021
var configNames = ['config.gypi', 'common.gypi']
2122
var configs = []
2223
var nodeDir
@@ -73,7 +74,9 @@ function configure (gyp, argv, callback) {
7374

7475
function createBuildDir () {
7576
log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
76-
fs.mkdir(buildDir, { recursive: true }, function (err, isNew) {
77+
78+
const deepestBuildDirSubdirectory = win ? buildDir : buildBinsDir
79+
fs.mkdir(deepestBuildDirSubdirectory, { recursive: true }, function (err, isNew) {
7780
if (err) {
7881
return callback(err)
7982
}
@@ -84,11 +87,22 @@ function configure (gyp, argv, callback) {
8487
findVisualStudio(release.semver, gyp.opts.msvs_version,
8588
createConfigFile)
8689
} else {
90+
createPythonSymlink()
8791
createConfigFile()
8892
}
8993
})
9094
}
9195

96+
function createPythonSymlink () {
97+
const symlinkDestination = path.join(buildBinsDir, 'python3')
98+
99+
log.verbose('python symlink', `creating symlink to "${python}" at "${symlinkDestination}"`)
100+
101+
fs.unlink(symlinkDestination, function () {
102+
fs.symlink(python, symlinkDestination, function () {})
103+
})
104+
}
105+
92106
function createConfigFile (err, vsInfo) {
93107
if (err) {
94108
return callback(err)

0 commit comments

Comments
 (0)