configure.js 16 KB

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