configure.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const log = require('npmlog')
  5. const os = require('os')
  6. const processRelease = require('./process-release')
  7. const win = process.platform === 'win32'
  8. const findNodeDirectory = require('./find-node-directory')
  9. const createConfigGypi = require('./create-config-gypi')
  10. const msgFormat = require('util').format
  11. var findPython = require('./find-python')
  12. if (win) {
  13. var findVisualStudio = require('./find-visualstudio')
  14. }
  15. function configure (gyp, argv, callback) {
  16. var python
  17. var buildDir = path.resolve('build')
  18. var configNames = ['config.gypi', 'common.gypi']
  19. var configs = []
  20. var nodeDir
  21. var release = processRelease(argv, gyp, process.version, process.release)
  22. findPython(gyp.opts.python, function (err, found) {
  23. if (err) {
  24. callback(err)
  25. } else {
  26. python = found
  27. getNodeDir()
  28. }
  29. })
  30. function getNodeDir () {
  31. // 'python' should be set by now
  32. process.env.PYTHON = python
  33. if (gyp.opts.nodedir) {
  34. // --nodedir was specified. use that for the dev files
  35. nodeDir = gyp.opts.nodedir.replace(/^~/, os.homedir())
  36. log.verbose('get node dir', 'compiling against specified --nodedir dev files: %s', nodeDir)
  37. createBuildDir()
  38. } else {
  39. // if no --nodedir specified, ensure node dependencies are installed
  40. if ('v' + release.version !== process.version) {
  41. // if --target was given, then determine a target version to compile for
  42. log.verbose('get node dir', 'compiling against --target node version: %s', release.version)
  43. } else {
  44. // if no --target was specified then use the current host node version
  45. log.verbose('get node dir', 'no --target version specified, falling back to host node version: %s', release.version)
  46. }
  47. if (!release.semver) {
  48. // could not parse the version string with semver
  49. return callback(new Error('Invalid version number: ' + release.version))
  50. }
  51. // If the tarball option is set, always remove and reinstall the headers
  52. // into devdir. Otherwise only install if they're not already there.
  53. gyp.opts.ensure = !gyp.opts.tarball
  54. gyp.commands.install([release.version], function (err) {
  55. if (err) {
  56. return callback(err)
  57. }
  58. log.verbose('get node dir', 'target node version installed:', release.versionDir)
  59. nodeDir = path.resolve(gyp.devDir, release.versionDir)
  60. createBuildDir()
  61. })
  62. }
  63. }
  64. function createBuildDir () {
  65. log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
  66. fs.mkdir(buildDir, { recursive: true }, function (err, isNew) {
  67. if (err) {
  68. return callback(err)
  69. }
  70. log.verbose(
  71. 'build dir', '"build" dir needed to be created?', isNew ? 'Yes' : 'No'
  72. )
  73. if (win) {
  74. findVisualStudio(release.semver, gyp.opts.msvs_version,
  75. createConfigFile)
  76. } else {
  77. createConfigFile()
  78. }
  79. })
  80. }
  81. function createConfigFile (err, vsInfo) {
  82. if (err) {
  83. return callback(err)
  84. }
  85. if (process.platform === 'win32') {
  86. process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015)
  87. process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path
  88. }
  89. createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }).then(configPath => {
  90. configs.push(configPath)
  91. findConfigs()
  92. }).catch(err => {
  93. callback(err)
  94. })
  95. }
  96. function findConfigs () {
  97. var name = configNames.shift()
  98. if (!name) {
  99. return runGyp()
  100. }
  101. var fullPath = path.resolve(name)
  102. log.verbose(name, 'checking for gypi file: %s', fullPath)
  103. fs.stat(fullPath, function (err) {
  104. if (err) {
  105. if (err.code === 'ENOENT') {
  106. findConfigs() // check next gypi filename
  107. } else {
  108. callback(err)
  109. }
  110. } else {
  111. log.verbose(name, 'found gypi file')
  112. configs.push(fullPath)
  113. findConfigs()
  114. }
  115. })
  116. }
  117. function runGyp (err) {
  118. if (err) {
  119. return callback(err)
  120. }
  121. if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
  122. if (win) {
  123. log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
  124. // force the 'make' target for non-Windows
  125. argv.push('-f', 'msvs')
  126. } else {
  127. log.verbose('gyp', 'gyp format was not specified; forcing "make"')
  128. // force the 'make' target for non-Windows
  129. argv.push('-f', 'make')
  130. }
  131. }
  132. // include all the ".gypi" files that were found
  133. configs.forEach(function (config) {
  134. argv.push('-I', config)
  135. })
  136. // For AIX and z/OS we need to set up the path to the exports file
  137. // which contains the symbols needed for linking.
  138. var nodeExpFile
  139. if (process.platform === 'aix' || process.platform === 'os390') {
  140. var ext = process.platform === 'aix' ? 'exp' : 'x'
  141. var nodeRootDir = findNodeDirectory()
  142. var candidates
  143. if (process.platform === 'aix') {
  144. candidates = [
  145. 'include/node/node',
  146. 'out/Release/node',
  147. 'out/Debug/node',
  148. 'node'
  149. ].map(function (file) {
  150. return file + '.' + ext
  151. })
  152. } else {
  153. candidates = [
  154. 'out/Release/obj.target/libnode',
  155. 'out/Debug/obj.target/libnode',
  156. 'lib/libnode'
  157. ].map(function (file) {
  158. return file + '.' + ext
  159. })
  160. }
  161. var logprefix = 'find exports file'
  162. nodeExpFile = findAccessibleSync(logprefix, nodeRootDir, candidates)
  163. if (nodeExpFile !== undefined) {
  164. log.verbose(logprefix, 'Found exports file: %s', nodeExpFile)
  165. } else {
  166. var msg = msgFormat('Could not find node.%s file in %s', ext, nodeRootDir)
  167. log.error(logprefix, 'Could not find exports file')
  168. return callback(new Error(msg))
  169. }
  170. }
  171. // this logic ported from the old `gyp_addon` python file
  172. var gypScript = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py')
  173. var addonGypi = path.resolve(__dirname, '..', 'addon.gypi')
  174. var commonGypi = path.resolve(nodeDir, 'include/node/common.gypi')
  175. fs.stat(commonGypi, function (err) {
  176. if (err) {
  177. commonGypi = path.resolve(nodeDir, 'common.gypi')
  178. }
  179. var outputDir = 'build'
  180. if (win) {
  181. // Windows expects an absolute path
  182. outputDir = buildDir
  183. }
  184. var nodeGypDir = path.resolve(__dirname, '..')
  185. var nodeLibFile = path.join(nodeDir,
  186. !gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)',
  187. release.name + '.lib')
  188. argv.push('-I', addonGypi)
  189. argv.push('-I', commonGypi)
  190. argv.push('-Dlibrary=shared_library')
  191. argv.push('-Dvisibility=default')
  192. argv.push('-Dnode_root_dir=' + nodeDir)
  193. if (process.platform === 'aix' || process.platform === 'os390') {
  194. argv.push('-Dnode_exp_file=' + nodeExpFile)
  195. }
  196. argv.push('-Dnode_gyp_dir=' + nodeGypDir)
  197. // Do this to keep Cygwin environments happy, else the unescaped '\' gets eaten up,
  198. // resulting in bad paths, Ex c:parentFolderfolderanotherFolder instead of c:\parentFolder\folder\anotherFolder
  199. if (win) {
  200. nodeLibFile = nodeLibFile.replace(/\\/g, '\\\\')
  201. }
  202. argv.push('-Dnode_lib_file=' + nodeLibFile)
  203. argv.push('-Dmodule_root_dir=' + process.cwd())
  204. argv.push('-Dnode_engine=' +
  205. (gyp.opts.node_engine || process.jsEngine || 'v8'))
  206. argv.push('--depth=.')
  207. argv.push('--no-parallel')
  208. // tell gyp to write the Makefile/Solution files into output_dir
  209. argv.push('--generator-output', outputDir)
  210. // tell make to write its output into the same dir
  211. argv.push('-Goutput_dir=.')
  212. // enforce use of the "binding.gyp" file
  213. argv.unshift('binding.gyp')
  214. // execute `gyp` from the current target nodedir
  215. argv.unshift(gypScript)
  216. // make sure python uses files that came with this particular node package
  217. var pypath = [path.join(__dirname, '..', 'gyp', 'pylib')]
  218. if (process.env.PYTHONPATH) {
  219. pypath.push(process.env.PYTHONPATH)
  220. }
  221. process.env.PYTHONPATH = pypath.join(win ? ';' : ':')
  222. var cp = gyp.spawn(python, argv)
  223. cp.on('exit', onCpExit)
  224. })
  225. }
  226. function onCpExit (code) {
  227. if (code !== 0) {
  228. callback(new Error('`gyp` failed with exit code: ' + code))
  229. } else {
  230. // we're done
  231. callback()
  232. }
  233. }
  234. }
  235. /**
  236. * Returns the first file or directory from an array of candidates that is
  237. * readable by the current user, or undefined if none of the candidates are
  238. * readable.
  239. */
  240. function findAccessibleSync (logprefix, dir, candidates) {
  241. for (var next = 0; next < candidates.length; next++) {
  242. var candidate = path.resolve(dir, candidates[next])
  243. try {
  244. var fd = fs.openSync(candidate, 'r')
  245. } catch (e) {
  246. // this candidate was not found or not readable, do nothing
  247. log.silly(logprefix, 'Could not open %s: %s', candidate, e.message)
  248. continue
  249. }
  250. fs.closeSync(fd)
  251. log.silly(logprefix, 'Found readable %s', candidate)
  252. return candidate
  253. }
  254. return undefined
  255. }
  256. module.exports = configure
  257. module.exports.test = {
  258. findAccessibleSync: findAccessibleSync
  259. }
  260. module.exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'