build.js 6.4 KB

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