build.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use strict'
  2. const gracefulFs = require('graceful-fs')
  3. const fs = gracefulFs.promises
  4. const path = require('path')
  5. const { glob } = require('tinyglobby')
  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', { expandDirectories: false })
  75. if (files.length === 0) {
  76. if (gracefulFs.existsSync('build/Makefile') ||
  77. (await glob('build/*.mk', { expandDirectories: false })).length !== 0) {
  78. command = makeCommand
  79. await doWhich(false)
  80. return
  81. } else {
  82. throw new Error('Could not find *.sln file or Makefile. Did you run "configure"?')
  83. }
  84. }
  85. guessedSolution = files[0]
  86. log.verbose('found first Solution file', guessedSolution)
  87. await doWhich(true)
  88. }
  89. /**
  90. * Uses node-which to locate the msbuild / make executable.
  91. */
  92. async function doWhich (msvs) {
  93. // On Windows use msbuild provided by node-gyp configure
  94. if (msvs) {
  95. if (!config.variables.msbuild_path) {
  96. throw new Error('MSBuild is not set, please run `node-gyp configure`.')
  97. }
  98. command = config.variables.msbuild_path
  99. log.verbose('using MSBuild:', command)
  100. await doBuild(msvs)
  101. return
  102. }
  103. // First make sure we have the build command in the PATH
  104. const execPath = await which(command)
  105. log.verbose('`which` succeeded for `' + command + '`', execPath)
  106. await doBuild(msvs)
  107. }
  108. /**
  109. * Actually spawn the process and compile the module.
  110. */
  111. async function doBuild (msvs) {
  112. // Enable Verbose build
  113. const verbose = log.logger.isVisible('verbose')
  114. let j
  115. if (!msvs && verbose) {
  116. argv.push('V=1')
  117. }
  118. if (msvs && !verbose) {
  119. argv.push('/clp:Verbosity=minimal')
  120. }
  121. if (msvs) {
  122. // Turn off the Microsoft logo on Windows
  123. argv.push('/nologo')
  124. // No lingering msbuild processes and open file handles
  125. argv.push('/nodeReuse:false')
  126. }
  127. // Specify the build type, Release by default
  128. if (msvs) {
  129. // Convert .gypi config target_arch to MSBuild /Platform
  130. // Since there are many ways to state '32-bit Intel', default to it.
  131. // N.B. msbuild's Condition string equality tests are case-insensitive.
  132. const archLower = arch.toLowerCase()
  133. const p = archLower === 'x64'
  134. ? 'x64'
  135. : (archLower === 'arm'
  136. ? 'ARM'
  137. : (archLower === 'arm64' ? 'ARM64' : 'Win32'))
  138. argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
  139. if (jobs) {
  140. j = parseInt(jobs, 10)
  141. if (!isNaN(j) && j > 0) {
  142. argv.push('/m:' + j)
  143. } else if (jobs.toUpperCase() === 'MAX') {
  144. argv.push('/m:' + require('os').cpus().length)
  145. }
  146. }
  147. } else {
  148. argv.push('BUILDTYPE=' + buildType)
  149. // Invoke the Makefile in the 'build' dir.
  150. argv.push('-C')
  151. argv.push('build')
  152. if (jobs) {
  153. j = parseInt(jobs, 10)
  154. if (!isNaN(j) && j > 0) {
  155. argv.push('--jobs')
  156. argv.push(j)
  157. } else if (jobs.toUpperCase() === 'MAX') {
  158. argv.push('--jobs')
  159. argv.push(require('os').cpus().length)
  160. }
  161. }
  162. }
  163. if (msvs) {
  164. // did the user specify their own .sln file?
  165. const hasSln = argv.some(function (arg) {
  166. return path.extname(arg) === '.sln'
  167. })
  168. if (!hasSln) {
  169. argv.unshift(gyp.opts.solution || guessedSolution)
  170. }
  171. }
  172. if (!win) {
  173. // Add build-time dependency symlinks (such as Python) to PATH
  174. buildBinsDir = path.resolve('build', 'node_gyp_bins')
  175. process.env.PATH = `${buildBinsDir}:${process.env.PATH}`
  176. await fs.mkdir(buildBinsDir, { recursive: true })
  177. const symlinkDestination = path.join(buildBinsDir, 'python3')
  178. try {
  179. await fs.unlink(symlinkDestination)
  180. } catch (err) {
  181. if (err.code !== 'ENOENT') throw err
  182. }
  183. await fs.symlink(python, symlinkDestination)
  184. log.verbose('bin symlinks', `created symlink to "${python}" in "${buildBinsDir}" and added to PATH`)
  185. }
  186. const proc = gyp.spawn(command, argv)
  187. await new Promise((resolve, reject) => proc.on('exit', async (code, signal) => {
  188. if (buildBinsDir) {
  189. // Clean up the build-time dependency symlinks:
  190. await fs.rm(buildBinsDir, { recursive: true, maxRetries: 3 })
  191. }
  192. if (code !== 0) {
  193. return reject(new Error('`' + command + '` failed with exit code: ' + code))
  194. }
  195. if (signal) {
  196. return reject(new Error('`' + command + '` got signal: ' + signal))
  197. }
  198. resolve()
  199. }))
  200. }
  201. }
  202. module.exports = build
  203. module.exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'