configure.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. // If the tarball option is set, always remove and reinstall the headers
  63. // into devdir. Otherwise only install if they're not already there.
  64. gyp.opts.ensure = gyp.opts.tarball ? false : true
  65. gyp.commands.install([ release.version ], function (err, version) {
  66. if (err) return callback(err)
  67. log.verbose('get node dir', 'target node version installed:', release.versionDir)
  68. nodeDir = path.resolve(gyp.devDir, release.versionDir)
  69. createBuildDir()
  70. })
  71. }
  72. }
  73. function createBuildDir () {
  74. log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
  75. mkdirp(buildDir, function (err, isNew) {
  76. if (err) return callback(err)
  77. log.verbose('build dir', '"build" dir needed to be created?', isNew)
  78. if (win && (!gyp.opts.msvs_version || gyp.opts.msvs_version === '2017')) {
  79. findVS2017(function (err, vsSetup) {
  80. if (err) {
  81. log.verbose('Not using VS2017:', err.message)
  82. createConfigFile()
  83. } else {
  84. createConfigFile(null, vsSetup)
  85. }
  86. })
  87. } else {
  88. createConfigFile()
  89. }
  90. })
  91. }
  92. function createConfigFile (err, vsSetup) {
  93. if (err) return callback(err)
  94. var configFilename = 'config.gypi'
  95. var configPath = path.resolve(buildDir, configFilename)
  96. log.verbose('build/' + configFilename, 'creating config file')
  97. var config = process.config || {}
  98. , defaults = config.target_defaults
  99. , variables = config.variables
  100. // default "config.variables"
  101. if (!variables) variables = config.variables = {}
  102. // default "config.defaults"
  103. if (!defaults) defaults = config.target_defaults = {}
  104. // don't inherit the "defaults" from node's `process.config` object.
  105. // doing so could cause problems in cases where the `node` executable was
  106. // compiled on a different machine (with different lib/include paths) than
  107. // the machine where the addon is being built to
  108. defaults.cflags = []
  109. defaults.defines = []
  110. defaults.include_dirs = []
  111. defaults.libraries = []
  112. // set the default_configuration prop
  113. if ('debug' in gyp.opts) {
  114. defaults.default_configuration = gyp.opts.debug ? 'Debug' : 'Release'
  115. }
  116. if (!defaults.default_configuration) {
  117. defaults.default_configuration = 'Release'
  118. }
  119. // set the target_arch variable
  120. variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
  121. // set the node development directory
  122. variables.nodedir = nodeDir
  123. // disable -T "thin" static archives by default
  124. variables.standalone_static_library = gyp.opts.thin ? 0 : 1
  125. if (vsSetup) {
  126. // GYP doesn't (yet) have support for VS2017, so we force it to VS2015
  127. // to avoid pulling a floating patch that has not landed upstream.
  128. // Ref: https://chromium-review.googlesource.com/#/c/433540/
  129. gyp.opts.msvs_version = '2015'
  130. process.env['GYP_MSVS_VERSION'] = 2015
  131. process.env['GYP_MSVS_OVERRIDE_PATH'] = vsSetup.path
  132. defaults['msbuild_toolset'] = 'v141'
  133. defaults['msvs_windows_target_platform_version'] = vsSetup.sdk
  134. variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', '15.0',
  135. 'Bin', 'MSBuild.exe')
  136. }
  137. // loop through the rest of the opts and add the unknown ones as variables.
  138. // this allows for module-specific configure flags like:
  139. //
  140. // $ node-gyp configure --shared-libxml2
  141. Object.keys(gyp.opts).forEach(function (opt) {
  142. if (opt === 'argv') return
  143. if (opt in gyp.configDefs) return
  144. variables[opt.replace(/-/g, '_')] = gyp.opts[opt]
  145. })
  146. // ensures that any boolean values from `process.config` get stringified
  147. function boolsToString (k, v) {
  148. if (typeof v === 'boolean')
  149. return String(v)
  150. return v
  151. }
  152. log.silly('build/' + configFilename, config)
  153. // now write out the config.gypi file to the build/ dir
  154. var prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
  155. , json = JSON.stringify(config, boolsToString, 2)
  156. log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
  157. configs.push(configPath)
  158. fs.writeFile(configPath, [prefix, json, ''].join('\n'), findConfigs)
  159. }
  160. function findConfigs (err) {
  161. if (err) return callback(err)
  162. var name = configNames.shift()
  163. if (!name) return runGyp()
  164. var fullPath = path.resolve(name)
  165. log.verbose(name, 'checking for gypi file: %s', fullPath)
  166. fs.stat(fullPath, function (err, stat) {
  167. if (err) {
  168. if (err.code == 'ENOENT') {
  169. findConfigs() // check next gypi filename
  170. } else {
  171. callback(err)
  172. }
  173. } else {
  174. log.verbose(name, 'found gypi file')
  175. configs.push(fullPath)
  176. findConfigs()
  177. }
  178. })
  179. }
  180. function runGyp (err) {
  181. if (err) return callback(err)
  182. if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
  183. if (win) {
  184. log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
  185. // force the 'make' target for non-Windows
  186. argv.push('-f', 'msvs')
  187. } else {
  188. log.verbose('gyp', 'gyp format was not specified; forcing "make"')
  189. // force the 'make' target for non-Windows
  190. argv.push('-f', 'make')
  191. }
  192. }
  193. function hasMsvsVersion () {
  194. return argv.some(function (arg) {
  195. return arg.indexOf('msvs_version') === 0
  196. })
  197. }
  198. if (win && !hasMsvsVersion()) {
  199. if ('msvs_version' in gyp.opts) {
  200. argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
  201. } else {
  202. argv.push('-G', 'msvs_version=auto')
  203. }
  204. }
  205. // include all the ".gypi" files that were found
  206. configs.forEach(function (config) {
  207. argv.push('-I', config)
  208. })
  209. // For AIX and z/OS we need to set up the path to the exports file
  210. // which contains the symbols needed for linking.
  211. var node_exp_file = undefined
  212. if (process.platform === 'aix' || process.platform === 'os390') {
  213. var ext = process.platform === 'aix' ? 'exp' : 'x'
  214. var node_root_dir = findNodeDirectory()
  215. var candidates = undefined
  216. if (process.platform === 'aix') {
  217. candidates = ['include/node/node',
  218. 'out/Release/node',
  219. 'out/Debug/node',
  220. 'node'
  221. ].map(function(file) {
  222. return file + '.' + ext
  223. })
  224. } else {
  225. candidates = ['out/Release/obj.target/libnode',
  226. 'out/Debug/obj.target/libnode',
  227. 'lib/libnode'
  228. ].map(function(file) {
  229. return file + '.' + ext
  230. })
  231. }
  232. var logprefix = 'find exports file'
  233. node_exp_file = findAccessibleSync(logprefix, node_root_dir, candidates)
  234. if (node_exp_file !== undefined) {
  235. log.verbose(logprefix, 'Found exports file: %s', node_exp_file)
  236. } else {
  237. var msg = msgFormat('Could not find node.%s file in %s', ext, node_root_dir)
  238. log.error(logprefix, 'Could not find exports file')
  239. return callback(new Error(msg))
  240. }
  241. }
  242. // this logic ported from the old `gyp_addon` python file
  243. var gyp_script = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py')
  244. var addon_gypi = path.resolve(__dirname, '..', 'addon.gypi')
  245. var common_gypi = path.resolve(nodeDir, 'include/node/common.gypi')
  246. fs.stat(common_gypi, function (err, stat) {
  247. if (err)
  248. common_gypi = path.resolve(nodeDir, 'common.gypi')
  249. var output_dir = 'build'
  250. if (win) {
  251. // Windows expects an absolute path
  252. output_dir = buildDir
  253. }
  254. var nodeGypDir = path.resolve(__dirname, '..')
  255. var nodeLibFile = path.join(nodeDir,
  256. !gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)',
  257. release.name + '.lib')
  258. argv.push('-I', addon_gypi)
  259. argv.push('-I', common_gypi)
  260. argv.push('-Dlibrary=shared_library')
  261. argv.push('-Dvisibility=default')
  262. argv.push('-Dnode_root_dir=' + nodeDir)
  263. if (process.platform === 'aix' || process.platform === 'os390') {
  264. argv.push('-Dnode_exp_file=' + node_exp_file)
  265. }
  266. argv.push('-Dnode_gyp_dir=' + nodeGypDir)
  267. argv.push('-Dnode_lib_file=' + nodeLibFile)
  268. argv.push('-Dmodule_root_dir=' + process.cwd())
  269. argv.push('-Dnode_engine=' +
  270. (gyp.opts.node_engine || process.jsEngine || 'v8'))
  271. argv.push('--depth=.')
  272. argv.push('--no-parallel')
  273. // tell gyp to write the Makefile/Solution files into output_dir
  274. argv.push('--generator-output', output_dir)
  275. // tell make to write its output into the same dir
  276. argv.push('-Goutput_dir=.')
  277. // enforce use of the "binding.gyp" file
  278. argv.unshift('binding.gyp')
  279. // execute `gyp` from the current target nodedir
  280. argv.unshift(gyp_script)
  281. // make sure python uses files that came with this particular node package
  282. var pypath = [path.join(__dirname, '..', 'gyp', 'pylib')]
  283. if (process.env.PYTHONPATH) {
  284. pypath.push(process.env.PYTHONPATH)
  285. }
  286. process.env.PYTHONPATH = pypath.join(win ? ';' : ':')
  287. var cp = gyp.spawn(python, argv)
  288. cp.on('exit', onCpExit)
  289. })
  290. }
  291. /**
  292. * Called when the `gyp` child process exits.
  293. */
  294. function onCpExit (code, signal) {
  295. if (code !== 0) {
  296. callback(new Error('`gyp` failed with exit code: ' + code))
  297. } else {
  298. // we're done
  299. callback()
  300. }
  301. }
  302. }
  303. /**
  304. * Returns the first file or directory from an array of candidates that is
  305. * readable by the current user, or undefined if none of the candidates are
  306. * readable.
  307. */
  308. function findAccessibleSync (logprefix, dir, candidates) {
  309. for (var next = 0; next < candidates.length; next++) {
  310. var candidate = path.resolve(dir, candidates[next])
  311. try {
  312. var fd = fs.openSync(candidate, 'r')
  313. } catch (e) {
  314. // this candidate was not found or not readable, do nothing
  315. log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
  316. continue
  317. }
  318. fs.closeSync(fd)
  319. log.silly(logprefix, 'Found readable %s', candidate)
  320. return candidate
  321. }
  322. return undefined
  323. }
  324. function PythonFinder(python, callback) {
  325. this.callback = callback
  326. this.python = python
  327. }
  328. PythonFinder.prototype = {
  329. checkPythonLauncherDepth: 0,
  330. env: process.env,
  331. execFile: cp.execFile,
  332. log: log,
  333. resolve: path.win32 && path.win32.resolve || path.resolve,
  334. stat: fs.stat,
  335. which: which,
  336. win: win,
  337. checkPython: function checkPython () {
  338. this.log.verbose('check python',
  339. 'checking for Python executable "%s" in the PATH',
  340. this.python)
  341. this.which(this.python, function (err, execPath) {
  342. if (err) {
  343. this.log.verbose('`which` failed', this.python, err)
  344. if (this.python === 'python2') {
  345. this.python = 'python'
  346. return this.checkPython()
  347. }
  348. if (this.win) {
  349. this.checkPythonLauncher()
  350. } else {
  351. this.failNoPython()
  352. }
  353. } else {
  354. this.log.verbose('`which` succeeded', this.python, execPath)
  355. // Found the `python` executable, and from now on we use it explicitly.
  356. // This solves #667 and #750 (`execFile` won't run batch files
  357. // (*.cmd, and *.bat))
  358. this.python = execPath
  359. this.checkPythonVersion()
  360. }
  361. }.bind(this))
  362. },
  363. // Distributions of Python on Windows by default install with the "py.exe"
  364. // Python launcher which is more likely to exist than the Python executable
  365. // being in the $PATH.
  366. // Because the Python launcher supports all versions of Python, we have to
  367. // explicitly request a Python 2 version. This is done by supplying "-2" as
  368. // the first command line argument. Since "py.exe -2" would be an invalid
  369. // executable for "execFile", we have to use the launcher to figure out
  370. // where the actual "python.exe" executable is located.
  371. checkPythonLauncher: function checkPythonLauncher () {
  372. this.checkPythonLauncherDepth += 1
  373. this.log.verbose(
  374. 'could not find "' + this.python + '". checking python launcher')
  375. var env = extend({}, this.env)
  376. env.TERM = 'dumb'
  377. var launcherArgs = ['-2', '-c', 'import sys; print sys.executable']
  378. this.execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) {
  379. if (err) {
  380. this.guessPython()
  381. } else {
  382. this.python = stdout.trim()
  383. this.log.verbose('check python launcher',
  384. 'python executable found: %j',
  385. this.python)
  386. this.checkPythonVersion()
  387. }
  388. this.checkPythonLauncherDepth -= 1
  389. }.bind(this))
  390. },
  391. checkPythonVersion: function checkPythonVersion () {
  392. var args = ['-c', 'import sys; print "%s.%s.%s" % sys.version_info[:3];']
  393. var env = extend({}, this.env)
  394. env.TERM = 'dumb'
  395. this.execFile(this.python, args, { env: env }, function (err, stdout) {
  396. if (err) {
  397. return this.callback(err)
  398. }
  399. this.log.verbose('check python version',
  400. '`%s -c "' + args[1] + '"` returned: %j',
  401. this.python, stdout)
  402. var version = stdout.trim()
  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. }