build.js 7.1 KB

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