configure.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. module.exports = exports = configure
  2. module.exports.test = {
  3. PythonFinder: PythonFinder,
  4. findAccessibleSync: findAccessibleSync,
  5. findPython: findPython,
  6. }
  7. /**
  8. * Module dependencies.
  9. */
  10. var fs = require('graceful-fs')
  11. , path = require('path')
  12. , log = require('npmlog')
  13. , osenv = require('osenv')
  14. , which = require('which')
  15. , semver = require('semver')
  16. , mkdirp = require('mkdirp')
  17. , cp = require('child_process')
  18. , extend = require('util')._extend
  19. , processRelease = require('./process-release')
  20. , win = process.platform === 'win32'
  21. , findNodeDirectory = require('./find-node-directory')
  22. , msgFormat = require('util').format
  23. if (win)
  24. var findVS2017 = require('./find-vs2017')
  25. exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
  26. function configure (gyp, argv, callback) {
  27. var python = gyp.opts.python || process.env.PYTHON || 'python2'
  28. , buildDir = path.resolve('build')
  29. , configNames = [ 'config.gypi', 'common.gypi' ]
  30. , configs = []
  31. , nodeDir
  32. , release = processRelease(argv, gyp, process.version, process.release)
  33. findPython(python, function (err, found) {
  34. if (err) {
  35. callback(err)
  36. } else {
  37. python = found
  38. getNodeDir()
  39. }
  40. })
  41. function getNodeDir () {
  42. // 'python' should be set by now
  43. process.env.PYTHON = python
  44. if (gyp.opts.nodedir) {
  45. // --nodedir was specified. use that for the dev files
  46. nodeDir = gyp.opts.nodedir.replace(/^~/, osenv.home())
  47. log.verbose('get node dir', 'compiling against specified --nodedir dev files: %s', nodeDir)
  48. createBuildDir()
  49. } else {
  50. // if no --nodedir specified, ensure node dependencies are installed
  51. if ('v' + release.version !== process.version) {
  52. // if --target was given, then determine a target version to compile for
  53. log.verbose('get node dir', 'compiling against --target node version: %s', release.version)
  54. } else {
  55. // if no --target was specified then use the current host node version
  56. log.verbose('get node dir', 'no --target version specified, falling back to host node version: %s', release.version)
  57. }
  58. if (!release.semver) {
  59. // could not parse the version string with semver
  60. return callback(new Error('Invalid version number: ' + release.version))
  61. }
  62. // ensure that the target node version's dev files are installed
  63. gyp.opts.ensure = true
  64. gyp.commands.install([ release.version ], function (err, version) {
  65. if (err) return callback(err)
  66. log.verbose('get node dir', 'target node version installed:', release.versionDir)
  67. nodeDir = path.resolve(gyp.devDir, release.versionDir)
  68. createBuildDir()
  69. })
  70. }
  71. }
  72. function createBuildDir () {
  73. log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
  74. mkdirp(buildDir, function (err, isNew) {
  75. if (err) return callback(err)
  76. log.verbose('build dir', '"build" dir needed to be created?', isNew)
  77. if (win && (!gyp.opts.msvs_version || gyp.opts.msvs_version === '2017')) {
  78. findVS2017(function (err, vsSetup) {
  79. if (err) {
  80. log.verbose('Not using VS2017:', err.message)
  81. createConfigFile()
  82. } else {
  83. createConfigFile(null, vsSetup)
  84. }
  85. })
  86. } else {
  87. createConfigFile()
  88. }
  89. })
  90. }
  91. function createConfigFile (err, vsSetup) {
  92. if (err) return callback(err)
  93. var configFilename = 'config.gypi'
  94. var configPath = path.resolve(buildDir, configFilename)
  95. log.verbose('build/' + configFilename, 'creating config file')
  96. var config = process.config || {}
  97. , defaults = config.target_defaults
  98. , variables = config.variables
  99. // default "config.variables"
  100. if (!variables) variables = config.variables = {}
  101. // default "config.defaults"
  102. if (!defaults) defaults = config.target_defaults = {}
  103. // don't inherit the "defaults" from node's `process.config` object.
  104. // doing so could cause problems in cases where the `node` executable was
  105. // compiled on a different machine (with different lib/include paths) than
  106. // the machine where the addon is being built to
  107. defaults.cflags = []
  108. defaults.defines = []
  109. defaults.include_dirs = []
  110. defaults.libraries = []
  111. // set the default_configuration prop
  112. if ('debug' in gyp.opts) {
  113. defaults.default_configuration = gyp.opts.debug ? 'Debug' : 'Release'
  114. }
  115. if (!defaults.default_configuration) {
  116. defaults.default_configuration = 'Release'
  117. }
  118. // set the target_arch variable
  119. variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
  120. // set the node development directory
  121. variables.nodedir = nodeDir
  122. // disable -T "thin" static archives by default
  123. variables.standalone_static_library = gyp.opts.thin ? 0 : 1
  124. if (vsSetup) {
  125. // GYP doesn't (yet) have support for VS2017, so we force it to VS2015
  126. // to avoid pulling a floating patch that has not landed upstream.
  127. // Ref: https://chromium-review.googlesource.com/#/c/433540/
  128. gyp.opts.msvs_version = '2015'
  129. process.env['GYP_MSVS_VERSION'] = 2015
  130. process.env['GYP_MSVS_OVERRIDE_PATH'] = vsSetup.path
  131. defaults['msbuild_toolset'] = 'v141'
  132. defaults['msvs_windows_target_platform_version'] = vsSetup.sdk
  133. variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', '15.0',
  134. 'Bin', 'MSBuild.exe')
  135. }
  136. // loop through the rest of the opts and add the unknown ones as variables.
  137. // this allows for module-specific configure flags like:
  138. //
  139. // $ node-gyp configure --shared-libxml2
  140. Object.keys(gyp.opts).forEach(function (opt) {
  141. if (opt === 'argv') return
  142. if (opt in gyp.configDefs) return
  143. variables[opt.replace(/-/g, '_')] = gyp.opts[opt]
  144. })
  145. // ensures that any boolean values from `process.config` get stringified
  146. function boolsToString (k, v) {
  147. if (typeof v === 'boolean')
  148. return String(v)
  149. return v
  150. }
  151. log.silly('build/' + configFilename, config)
  152. // now write out the config.gypi file to the build/ dir
  153. var prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
  154. , json = JSON.stringify(config, boolsToString, 2)
  155. log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
  156. configs.push(configPath)
  157. fs.writeFile(configPath, [prefix, json, ''].join('\n'), findConfigs)
  158. }
  159. function findConfigs (err) {
  160. if (err) return callback(err)
  161. var name = configNames.shift()
  162. if (!name) return runGyp()
  163. var fullPath = path.resolve(name)
  164. log.verbose(name, 'checking for gypi file: %s', fullPath)
  165. fs.stat(fullPath, function (err, stat) {
  166. if (err) {
  167. if (err.code == 'ENOENT') {
  168. findConfigs() // check next gypi filename
  169. } else {
  170. callback(err)
  171. }
  172. } else {
  173. log.verbose(name, 'found gypi file')
  174. configs.push(fullPath)
  175. findConfigs()
  176. }
  177. })
  178. }
  179. function runGyp (err) {
  180. if (err) return callback(err)
  181. if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
  182. if (win) {
  183. log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
  184. // force the 'make' target for non-Windows
  185. argv.push('-f', 'msvs')
  186. } else {
  187. log.verbose('gyp', 'gyp format was not specified; forcing "make"')
  188. // force the 'make' target for non-Windows
  189. argv.push('-f', 'make')
  190. }
  191. }
  192. function hasMsvsVersion () {
  193. return argv.some(function (arg) {
  194. return arg.indexOf('msvs_version') === 0
  195. })
  196. }
  197. if (win && !hasMsvsVersion()) {
  198. if ('msvs_version' in gyp.opts) {
  199. argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
  200. } else {
  201. argv.push('-G', 'msvs_version=auto')
  202. }
  203. }
  204. // include all the ".gypi" files that were found
  205. configs.forEach(function (config) {
  206. argv.push('-I', config)
  207. })
  208. // for AIX we need to set up the path to the exp file
  209. // which contains the symbols needed for linking.
  210. // The file will either be in one of the following
  211. // depending on whether it is an installed or
  212. // development environment:
  213. // - the include/node directory
  214. // - the out/Release directory
  215. // - the out/Debug directory
  216. // - the root directory
  217. var node_exp_file = undefined
  218. if (process.platform === 'aix') {
  219. var node_root_dir = findNodeDirectory()
  220. var candidates = ['include/node/node.exp',
  221. 'out/Release/node.exp',
  222. 'out/Debug/node.exp',
  223. 'node.exp']
  224. var logprefix = 'find exports file'
  225. node_exp_file = findAccessibleSync(logprefix, node_root_dir, candidates)
  226. if (node_exp_file !== undefined) {
  227. log.verbose(logprefix, 'Found exports file: %s', node_exp_file)
  228. } else {
  229. var msg = msgFormat('Could not find node.exp file in %s', node_root_dir)
  230. log.error(logprefix, 'Could not find exports file')
  231. return callback(new Error(msg))
  232. }
  233. }
  234. // this logic ported from the old `gyp_addon` python file
  235. var gyp_script = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py')
  236. var addon_gypi = path.resolve(__dirname, '..', 'addon.gypi')
  237. var common_gypi = path.resolve(nodeDir, 'include/node/common.gypi')
  238. fs.stat(common_gypi, function (err, stat) {
  239. if (err)
  240. common_gypi = path.resolve(nodeDir, 'common.gypi')
  241. var output_dir = 'build'
  242. if (win) {
  243. // Windows expects an absolute path
  244. output_dir = buildDir
  245. }
  246. var nodeGypDir = path.resolve(__dirname, '..')
  247. var nodeLibFile = path.join(nodeDir,
  248. !gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)',
  249. release.name + '.lib')
  250. argv.push('-I', addon_gypi)
  251. argv.push('-I', common_gypi)
  252. argv.push('-Dlibrary=shared_library')
  253. argv.push('-Dvisibility=default')
  254. argv.push('-Dnode_root_dir=' + nodeDir)
  255. if (process.platform === 'aix') {
  256. argv.push('-Dnode_exp_file=' + node_exp_file)
  257. }
  258. argv.push('-Dnode_gyp_dir=' + nodeGypDir)
  259. argv.push('-Dnode_lib_file=' + nodeLibFile)
  260. argv.push('-Dmodule_root_dir=' + process.cwd())
  261. argv.push('-Dnode_engine=' +
  262. (gyp.opts.node_engine || process.jsEngine || 'v8'))
  263. argv.push('--depth=.')
  264. argv.push('--no-parallel')
  265. // tell gyp to write the Makefile/Solution files into output_dir
  266. argv.push('--generator-output', output_dir)
  267. // tell make to write its output into the same dir
  268. argv.push('-Goutput_dir=.')
  269. // enforce use of the "binding.gyp" file
  270. argv.unshift('binding.gyp')
  271. // execute `gyp` from the current target nodedir
  272. argv.unshift(gyp_script)
  273. // make sure python uses files that came with this particular node package
  274. var pypath = [path.join(__dirname, '..', 'gyp', 'pylib')]
  275. if (process.env.PYTHONPATH) {
  276. pypath.push(process.env.PYTHONPATH)
  277. }
  278. process.env.PYTHONPATH = pypath.join(win ? ';' : ':')
  279. var cp = gyp.spawn(python, argv)
  280. cp.on('exit', onCpExit)
  281. })
  282. }
  283. /**
  284. * Called when the `gyp` child process exits.
  285. */
  286. function onCpExit (code, signal) {
  287. if (code !== 0) {
  288. callback(new Error('`gyp` failed with exit code: ' + code))
  289. } else {
  290. // we're done
  291. callback()
  292. }
  293. }
  294. }
  295. /**
  296. * Returns the first file or directory from an array of candidates that is
  297. * readable by the current user, or undefined if none of the candidates are
  298. * readable.
  299. */
  300. function findAccessibleSync (logprefix, dir, candidates) {
  301. for (var next = 0; next < candidates.length; next++) {
  302. var candidate = path.resolve(dir, candidates[next])
  303. try {
  304. var fd = fs.openSync(candidate, 'r')
  305. } catch (e) {
  306. // this candidate was not found or not readable, do nothing
  307. log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
  308. continue
  309. }
  310. fs.closeSync(fd)
  311. log.silly(logprefix, 'Found readable %s', candidate)
  312. return candidate
  313. }
  314. return undefined
  315. }
  316. function PythonFinder(python, callback) {
  317. this.callback = callback
  318. this.python = python
  319. }
  320. PythonFinder.prototype = {
  321. checkPythonLauncherDepth: 0,
  322. env: process.env,
  323. execFile: cp.execFile,
  324. log: log,
  325. resolve: path.win32 && path.win32.resolve || path.resolve,
  326. stat: fs.stat,
  327. which: which,
  328. win: win,
  329. checkPython: function checkPython () {
  330. this.log.verbose('check python',
  331. 'checking for Python executable "%s" in the PATH',
  332. this.python)
  333. this.which(this.python, function (err, execPath) {
  334. if (err) {
  335. this.log.verbose('`which` failed', this.python, err)
  336. if (this.python === 'python2') {
  337. this.python = 'python'
  338. return this.checkPython()
  339. }
  340. if (this.win) {
  341. this.checkPythonLauncher()
  342. } else {
  343. this.failNoPython()
  344. }
  345. } else {
  346. this.log.verbose('`which` succeeded', this.python, execPath)
  347. // Found the `python` executable, and from now on we use it explicitly.
  348. // This solves #667 and #750 (`execFile` won't run batch files
  349. // (*.cmd, and *.bat))
  350. this.python = execPath
  351. this.checkPythonVersion()
  352. }
  353. }.bind(this))
  354. },
  355. // Distributions of Python on Windows by default install with the "py.exe"
  356. // Python launcher which is more likely to exist than the Python executable
  357. // being in the $PATH.
  358. // Because the Python launcher supports all versions of Python, we have to
  359. // explicitly request a Python 2 version. This is done by supplying "-2" as
  360. // the first command line argument. Since "py.exe -2" would be an invalid
  361. // executable for "execFile", we have to use the launcher to figure out
  362. // where the actual "python.exe" executable is located.
  363. checkPythonLauncher: function checkPythonLauncher () {
  364. this.checkPythonLauncherDepth += 1
  365. this.log.verbose(
  366. 'could not find "' + this.python + '". checking python launcher')
  367. var env = extend({}, this.env)
  368. env.TERM = 'dumb'
  369. var launcherArgs = ['-2', '-c', 'import sys; print sys.executable']
  370. this.execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) {
  371. if (err) {
  372. this.guessPython()
  373. } else {
  374. this.python = stdout.trim()
  375. this.log.verbose('check python launcher',
  376. 'python executable found: %j',
  377. this.python)
  378. this.checkPythonVersion()
  379. }
  380. this.checkPythonLauncherDepth -= 1
  381. }.bind(this))
  382. },
  383. checkPythonVersion: function checkPythonVersion () {
  384. var args = ['-c', 'import platform; print(platform.python_version());']
  385. var env = extend({}, this.env)
  386. env.TERM = 'dumb'
  387. this.execFile(this.python, args, { env: env }, function (err, stdout) {
  388. if (err) {
  389. return this.callback(err)
  390. }
  391. this.log.verbose('check python version',
  392. '`%s -c "' + args[1] + '"` returned: %j',
  393. this.python, stdout)
  394. var version = stdout.trim()
  395. if (~version.indexOf('+')) {
  396. this.log.silly('stripping "+" sign(s) from version')
  397. version = version.replace(/\+/g, '')
  398. }
  399. if (~version.indexOf('rc')) {
  400. this.log.silly('stripping "rc" identifier from version')
  401. version = version.replace(/rc(.*)$/ig, '')
  402. }
  403. var range = semver.Range('>=2.5.0 <3.0.0')
  404. var valid = false
  405. try {
  406. valid = range.test(version)
  407. } catch (e) {
  408. this.log.silly('range.test() error', e)
  409. }
  410. if (valid) {
  411. this.callback(null, this.python)
  412. } else if (this.win && this.checkPythonLauncherDepth === 0) {
  413. this.checkPythonLauncher()
  414. } else {
  415. this.failPythonVersion(version)
  416. }
  417. }.bind(this))
  418. },
  419. failNoPython: function failNoPython () {
  420. var errmsg =
  421. 'Can\'t find Python executable "' + this.python +
  422. '", you can set the PYTHON env variable.'
  423. this.callback(new Error(errmsg))
  424. },
  425. failPythonVersion: function failPythonVersion (badVersion) {
  426. var errmsg =
  427. 'Python executable "' + this.python +
  428. '" is v' + badVersion + ', which is not supported by gyp.\n' +
  429. 'You can pass the --python switch to point to ' +
  430. 'Python >= v2.5.0 & < 3.0.0.'
  431. this.callback(new Error(errmsg))
  432. },
  433. // Called on Windows when "python" isn't available in the current $PATH.
  434. // We are going to check if "%SystemDrive%\python27\python.exe" exists.
  435. guessPython: function guessPython () {
  436. this.log.verbose('could not find "' + this.python + '". guessing location')
  437. var rootDir = this.env.SystemDrive || 'C:\\'
  438. if (rootDir[rootDir.length - 1] !== '\\') {
  439. rootDir += '\\'
  440. }
  441. var pythonPath = this.resolve(rootDir, 'Python27', 'python.exe')
  442. this.log.verbose('ensuring that file exists:', pythonPath)
  443. this.stat(pythonPath, function (err, stat) {
  444. if (err) {
  445. if (err.code == 'ENOENT') {
  446. this.failNoPython()
  447. } else {
  448. this.callback(err)
  449. }
  450. return
  451. }
  452. this.python = pythonPath
  453. this.checkPythonVersion()
  454. }.bind(this))
  455. },
  456. }
  457. function findPython (python, callback) {
  458. var finder = new PythonFinder(python, callback)
  459. finder.checkPython()
  460. }