build.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. module.exports = exports = build
  2. /**
  3. * Module dependencies.
  4. */
  5. var fs = require('graceful-fs')
  6. , rm = require('rimraf')
  7. , path = require('path')
  8. , glob = require('glob')
  9. , log = require('npmlog')
  10. , which = require('which')
  11. , exec = require('child_process').exec
  12. , processRelease = require('./process-release')
  13. , win = process.platform === 'win32'
  14. exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
  15. function build (gyp, argv, callback) {
  16. var platformMake = 'make'
  17. if (process.platform === 'aix') {
  18. platformMake = 'gmake'
  19. } else if (process.platform.indexOf('bsd') !== -1) {
  20. platformMake = 'gmake'
  21. } else if (win && argv.length > 0) {
  22. argv = argv.map(function(target) {
  23. return '/t:' + target
  24. })
  25. }
  26. var release = processRelease(argv, gyp, process.version, process.release)
  27. , makeCommand = gyp.opts.make || process.env.MAKE || platformMake
  28. , command = win ? 'msbuild' : makeCommand
  29. , buildDir = path.resolve('build')
  30. , configPath = path.resolve(buildDir, 'config.gypi')
  31. , jobs = gyp.opts.jobs || process.env.JOBS
  32. , buildType
  33. , config
  34. , arch
  35. , nodeDir
  36. loadConfigGypi()
  37. /**
  38. * Load the "config.gypi" file that was generated during "configure".
  39. */
  40. function loadConfigGypi () {
  41. fs.readFile(configPath, 'utf8', function (err, data) {
  42. if (err) {
  43. if (err.code == 'ENOENT') {
  44. callback(new Error('You must run `node-gyp configure` first!'))
  45. } else {
  46. callback(err)
  47. }
  48. return
  49. }
  50. config = JSON.parse(data.replace(/\#.+\n/, ''))
  51. // get the 'arch', 'buildType', and 'nodeDir' vars from the config
  52. buildType = config.target_defaults.default_configuration
  53. arch = config.variables.target_arch
  54. nodeDir = config.variables.nodedir
  55. if ('debug' in gyp.opts) {
  56. buildType = gyp.opts.debug ? 'Debug' : 'Release'
  57. }
  58. if (!buildType) {
  59. buildType = 'Release'
  60. }
  61. log.verbose('build type', buildType)
  62. log.verbose('architecture', arch)
  63. log.verbose('node dev dir', nodeDir)
  64. if (win) {
  65. findSolutionFile()
  66. } else {
  67. doWhich()
  68. }
  69. })
  70. }
  71. /**
  72. * On Windows, find the first build/*.sln file.
  73. */
  74. function findSolutionFile () {
  75. glob('build/*.sln', function (err, files) {
  76. if (err) return callback(err)
  77. if (files.length === 0) {
  78. return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
  79. }
  80. guessedSolution = files[0]
  81. log.verbose('found first Solution file', guessedSolution)
  82. doWhich()
  83. })
  84. }
  85. /**
  86. * Uses node-which to locate the msbuild / make executable.
  87. */
  88. function doWhich () {
  89. // First make sure we have the build command in the PATH
  90. which(command, function (err, execPath) {
  91. if (err) {
  92. if (win && /not found/.test(err.message)) {
  93. // On windows and no 'msbuild' found. Let's guess where it is
  94. findMsbuild()
  95. } else {
  96. // Some other error or 'make' not found on Unix, report that to the user
  97. callback(err)
  98. }
  99. return
  100. }
  101. log.verbose('`which` succeeded for `' + command + '`', execPath)
  102. doBuild()
  103. })
  104. }
  105. /**
  106. * Search for the location of "msbuild.exe" file on Windows.
  107. */
  108. function findMsbuild () {
  109. if (config.variables.msbuild_path) {
  110. command = config.variables.msbuild_path
  111. log.verbose('using MSBuild:', command)
  112. doBuild()
  113. return
  114. }
  115. log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
  116. var notfoundErr = 'Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?'
  117. var cmd = 'reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'
  118. if (process.arch !== 'ia32')
  119. cmd += ' /reg:32'
  120. exec(cmd, function (err, stdout, stderr) {
  121. if (err) {
  122. return callback(new Error(err.message + '\n' + notfoundErr))
  123. }
  124. var reVers = /ToolsVersions\\([^\\]+)$/i
  125. , rePath = /\r\n[ \t]+MSBuildToolsPath[ \t]+REG_SZ[ \t]+([^\r]+)/i
  126. , msbuilds = []
  127. , r
  128. , msbuildPath
  129. stdout.split('\r\n\r\n').forEach(function(l) {
  130. if (!l) return
  131. l = l.trim()
  132. if (r = reVers.exec(l.substring(0, l.indexOf('\r\n')))) {
  133. var ver = parseFloat(r[1], 10)
  134. if (ver >= 3.5) {
  135. if (r = rePath.exec(l)) {
  136. msbuilds.push({
  137. version: ver,
  138. path: r[1]
  139. })
  140. }
  141. }
  142. }
  143. })
  144. msbuilds.sort(function (x, y) {
  145. return (x.version < y.version ? -1 : 1)
  146. })
  147. ;(function verifyMsbuild () {
  148. if (!msbuilds.length) return callback(new Error(notfoundErr))
  149. msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
  150. fs.stat(msbuildPath, function (err, stat) {
  151. if (err) {
  152. if (err.code == 'ENOENT') {
  153. if (msbuilds.length) {
  154. return verifyMsbuild()
  155. } else {
  156. callback(new Error(notfoundErr))
  157. }
  158. } else {
  159. callback(err)
  160. }
  161. return
  162. }
  163. command = msbuildPath
  164. doBuild()
  165. })
  166. })()
  167. })
  168. }
  169. /**
  170. * Actually spawn the process and compile the module.
  171. */
  172. function doBuild () {
  173. // Enable Verbose build
  174. var verbose = log.levels[log.level] <= log.levels.verbose
  175. if (!win && verbose) {
  176. argv.push('V=1')
  177. }
  178. if (win && !verbose) {
  179. argv.push('/clp:Verbosity=minimal')
  180. }
  181. if (win) {
  182. // Turn off the Microsoft logo on Windows
  183. argv.push('/nologo')
  184. }
  185. // Specify the build type, Release by default
  186. if (win) {
  187. var archLower = arch.toLowerCase()
  188. var p = archLower === 'x64' ? 'x64' :
  189. (archLower === 'arm' ? 'ARM' : 'Win32')
  190. argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
  191. if (jobs) {
  192. var j = parseInt(jobs, 10)
  193. if (!isNaN(j) && j > 0) {
  194. argv.push('/m:' + j)
  195. } else if (jobs.toUpperCase() === 'MAX') {
  196. argv.push('/m:' + require('os').cpus().length)
  197. }
  198. }
  199. } else {
  200. argv.push('BUILDTYPE=' + buildType)
  201. // Invoke the Makefile in the 'build' dir.
  202. argv.push('-C')
  203. argv.push('build')
  204. if (jobs) {
  205. var j = parseInt(jobs, 10)
  206. if (!isNaN(j) && j > 0) {
  207. argv.push('--jobs')
  208. argv.push(j)
  209. } else if (jobs.toUpperCase() === 'MAX') {
  210. argv.push('--jobs')
  211. argv.push(require('os').cpus().length)
  212. }
  213. }
  214. }
  215. if (win) {
  216. // did the user specify their own .sln file?
  217. var hasSln = argv.some(function (arg) {
  218. return path.extname(arg) == '.sln'
  219. })
  220. if (!hasSln) {
  221. argv.unshift(gyp.opts.solution || guessedSolution)
  222. }
  223. }
  224. var proc = gyp.spawn(command, argv)
  225. proc.on('exit', onExit)
  226. }
  227. /**
  228. * Invoked after the make/msbuild command exits.
  229. */
  230. function onExit (code, signal) {
  231. if (code !== 0) {
  232. return callback(new Error('`' + command + '` failed with exit code: ' + code))
  233. }
  234. if (signal) {
  235. return callback(new Error('`' + command + '` got signal: ' + signal))
  236. }
  237. callback()
  238. }
  239. }