You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const glob = require('glob')
  5. const log = require('npmlog')
  6. const which = require('which')
  7. const win = process.platform === 'win32'
  8. function build (gyp, argv, callback) {
  9. var platformMake = 'make'
  10. if (process.platform === 'aix') {
  11. platformMake = 'gmake'
  12. } else if (process.platform === 'os400') {
  13. platformMake = 'gmake'
  14. } else if (process.platform.indexOf('bsd') !== -1) {
  15. platformMake = 'gmake'
  16. } else if (win && argv.length > 0) {
  17. argv = argv.map(function (target) {
  18. return '/t:' + target
  19. })
  20. }
  21. var makeCommand = gyp.opts.make || process.env.MAKE || platformMake
  22. var command = win ? 'msbuild' : makeCommand
  23. var jobs = gyp.opts.jobs || process.env.JOBS
  24. var buildType
  25. var config
  26. var arch
  27. var nodeDir
  28. var guessedSolution
  29. loadConfigGypi()
  30. /**
  31. * Load the "config.gypi" file that was generated during "configure".
  32. */
  33. function loadConfigGypi () {
  34. var configPath = path.resolve('build', 'config.gypi')
  35. fs.readFile(configPath, 'utf8', function (err, data) {
  36. if (err) {
  37. if (err.code === 'ENOENT') {
  38. callback(new Error('You must run `node-gyp configure` first!'))
  39. } else {
  40. callback(err)
  41. }
  42. return
  43. }
  44. config = JSON.parse(data.replace(/#.+\n/, ''))
  45. // get the 'arch', 'buildType', and 'nodeDir' vars from the config
  46. buildType = config.target_defaults.default_configuration
  47. arch = config.variables.target_arch
  48. nodeDir = config.variables.nodedir
  49. if ('debug' in gyp.opts) {
  50. buildType = gyp.opts.debug ? 'Debug' : 'Release'
  51. }
  52. if (!buildType) {
  53. buildType = 'Release'
  54. }
  55. log.verbose('build type', buildType)
  56. log.verbose('architecture', arch)
  57. log.verbose('node dev dir', nodeDir)
  58. if (win) {
  59. findSolutionFile()
  60. } else {
  61. doWhich()
  62. }
  63. })
  64. }
  65. /**
  66. * On Windows, find the first build/*.sln file.
  67. */
  68. function findSolutionFile () {
  69. glob('build/*.sln', function (err, files) {
  70. if (err) {
  71. return callback(err)
  72. }
  73. if (files.length === 0) {
  74. return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
  75. }
  76. guessedSolution = files[0]
  77. log.verbose('found first Solution file', guessedSolution)
  78. doWhich()
  79. })
  80. }
  81. /**
  82. * Uses node-which to locate the msbuild / make executable.
  83. */
  84. function doWhich () {
  85. // On Windows use msbuild provided by node-gyp configure
  86. if (win) {
  87. if (!config.variables.msbuild_path) {
  88. return callback(new Error(
  89. 'MSBuild is not set, please run `node-gyp configure`.'))
  90. }
  91. command = config.variables.msbuild_path
  92. log.verbose('using MSBuild:', command)
  93. doBuild()
  94. return
  95. }
  96. // First make sure we have the build command in the PATH
  97. which(command, function (err, execPath) {
  98. if (err) {
  99. // Some other error or 'make' not found on Unix, report that to the user
  100. callback(err)
  101. return
  102. }
  103. log.verbose('`which` succeeded for `' + command + '`', execPath)
  104. doBuild()
  105. })
  106. }
  107. /**
  108. * Actually spawn the process and compile the module.
  109. */
  110. function doBuild () {
  111. // Enable Verbose build
  112. var verbose = log.levels[log.level] <= log.levels.verbose
  113. var j
  114. if (!win && verbose) {
  115. argv.push('V=1')
  116. }
  117. if (win && !verbose) {
  118. argv.push('/clp:Verbosity=minimal')
  119. }
  120. if (win) {
  121. // Turn off the Microsoft logo on Windows
  122. argv.push('/nologo')
  123. }
  124. // Specify the build type, Release by default
  125. if (win) {
  126. // Convert .gypi config target_arch to MSBuild /Platform
  127. // Since there are many ways to state '32-bit Intel', default to it.
  128. // N.B. msbuild's Condition string equality tests are case-insensitive.
  129. var archLower = arch.toLowerCase()
  130. var p = archLower === 'x64' ? 'x64'
  131. : (archLower === 'arm' ? 'ARM'
  132. : (archLower === 'arm64' ? 'ARM64' : 'Win32'))
  133. argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
  134. if (jobs) {
  135. j = parseInt(jobs, 10)
  136. if (!isNaN(j) && j > 0) {
  137. argv.push('/m:' + j)
  138. } else if (jobs.toUpperCase() === 'MAX') {
  139. argv.push('/m:' + require('os').cpus().length)
  140. }
  141. }
  142. } else {
  143. argv.push('BUILDTYPE=' + buildType)
  144. // Invoke the Makefile in the 'build' dir.
  145. argv.push('-C')
  146. argv.push('build')
  147. if (jobs) {
  148. j = parseInt(jobs, 10)
  149. if (!isNaN(j) && j > 0) {
  150. argv.push('--jobs')
  151. argv.push(j)
  152. } else if (jobs.toUpperCase() === 'MAX') {
  153. argv.push('--jobs')
  154. argv.push(require('os').cpus().length)
  155. }
  156. }
  157. }
  158. if (win) {
  159. // did the user specify their own .sln file?
  160. var hasSln = argv.some(function (arg) {
  161. return path.extname(arg) === '.sln'
  162. })
  163. if (!hasSln) {
  164. argv.unshift(gyp.opts.solution || guessedSolution)
  165. }
  166. }
  167. if (!win) {
  168. // Add build-time dependency symlinks (such as Python) to PATH
  169. const buildBinsDir = path.resolve('build', 'node_gyp_bins')
  170. process.env.PATH = `${buildBinsDir}:${process.env.PATH}`
  171. log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`)
  172. }
  173. var proc = gyp.spawn(command, argv)
  174. proc.on('exit', onExit)
  175. }
  176. function onExit (code, signal) {
  177. if (code !== 0) {
  178. return callback(new Error('`' + command + '` failed with exit code: ' + code))
  179. }
  180. if (signal) {
  181. return callback(new Error('`' + command + '` got signal: ' + signal))
  182. }
  183. callback()
  184. }
  185. }
  186. module.exports = build
  187. module.exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'