2 module.exports = exports = build
8 var fs = require('graceful-fs')
9 , rm = require('rimraf')
10 , path = require('path')
11 , glob = require('glob')
12 , log = require('npmlog')
13 , which = require('which')
14 , mkdirp = require('mkdirp')
15 , exec = require('child_process').exec
16 , win = process.platform == 'win32'
18 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
20 function build (gyp, argv, callback) {
22 var makeCommand = gyp.opts.make || process.env.MAKE
23 || (process.platform.indexOf('bsd') != -1 && process.platform.indexOf('kfreebsd') == -1 ? 'gmake' : 'make')
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
37 * Load the "config.gypi" file that was generated during "configure".
40 function loadConfigGypi () {
41 fs.readFile(configPath, 'utf8', function (err, data) {
43 if (err.code == 'ENOENT') {
44 callback(new Error('You must run `node-gyp configure` first!'))
50 config = JSON.parse(data.replace(/\#.+\n/, ''))
52 // get the 'arch', 'buildType', and 'nodeDir' vars from the config
53 buildType = config.target_defaults.default_configuration
54 arch = config.variables.target_arch
55 nodeDir = config.variables.nodedir
56 copyDevLib = config.variables.copy_dev_lib == 'true'
58 if ('debug' in gyp.opts) {
59 buildType = gyp.opts.debug ? 'Debug' : 'Release'
65 log.verbose('build type', buildType)
66 log.verbose('architecture', arch)
67 log.verbose('node dev dir', nodeDir)
78 * On Windows, find the first build/*.sln file.
81 function findSolutionFile () {
82 glob('build/*.sln', function (err, files) {
83 if (err) return callback(err)
84 if (files.length === 0) {
85 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
87 guessedSolution = files[0]
88 log.verbose('found first Solution file', guessedSolution)
94 * Uses node-which to locate the msbuild / make executable.
98 // First make sure we have the build command in the PATH
99 which(command, function (err, execPath) {
101 if (win && /not found/.test(err.message)) {
102 // On windows and no 'msbuild' found. Let's guess where it is
105 // Some other error or 'make' not found on Unix, report that to the user
110 log.verbose('`which` succeeded for `' + command + '`', execPath)
116 * Search for the location of "msbuild.exe" file on Windows.
119 function findMsbuild () {
120 log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
121 var notfoundErr = new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?')
122 var cmd = 'reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s'
123 if (process.arch !== 'ia32')
125 exec(cmd, function (err, stdout, stderr) {
126 var reVers = /ToolsVersions\\([^\\]+)$/i
127 , rePath = /\r\n[ \t]+MSBuildToolsPath[ \t]+REG_SZ[ \t]+([^\r]+)/i
132 return callback(notfoundErr)
134 stdout.split('\r\n\r\n').forEach(function(l) {
137 if (r = reVers.exec(l.substring(0, l.indexOf('\r\n')))) {
138 var ver = parseFloat(r[1], 10)
140 if (r = rePath.exec(l)) {
149 msbuilds.sort(function (x, y) {
150 return (x.version < y.version ? -1 : 1)
152 ;(function verifyMsbuild () {
153 if (!msbuilds.length) return callback(notfoundErr);
154 msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
155 fs.stat(msbuildPath, function (err, stat) {
157 if (err.code == 'ENOENT') {
158 if (msbuilds.length) {
159 return verifyMsbuild()
161 callback(notfoundErr)
168 command = msbuildPath
176 * Copies the iojs.lib file for the current target architecture into the
177 * current proper dev dir location.
180 function copyNodeLib () {
181 if (!win || !copyDevLib) return doBuild()
183 var buildDir = path.resolve(nodeDir, buildType)
184 , archNodeLibPath = path.resolve(nodeDir, arch, 'iojs.lib')
185 , buildNodeLibPath = path.resolve(buildDir, 'iojs.lib')
187 mkdirp(buildDir, function (err, isNew) {
188 if (err) return callback(err)
189 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
190 var rs = fs.createReadStream(archNodeLibPath)
191 , ws = fs.createWriteStream(buildNodeLibPath)
192 log.verbose('copying "iojs.lib" for ' + arch, buildNodeLibPath)
194 rs.on('error', callback)
195 ws.on('error', callback)
196 rs.on('end', doBuild)
201 * Actually spawn the process and compile the module.
204 function doBuild () {
206 // Enable Verbose build
207 var verbose = log.levels[log.level] <= log.levels.verbose
208 if (!win && verbose) {
211 if (win && !verbose) {
212 argv.push('/clp:Verbosity=minimal')
216 // Turn off the Microsoft logo on Windows
220 // Specify the build type, Release by default
222 var p = arch === 'x64' ? 'x64' : 'Win32'
223 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
225 var j = parseInt(jobs, 10)
226 if (!isNaN(j) && j > 0) {
228 } else if (jobs.toUpperCase() === 'MAX') {
229 argv.push('/m:' + require('os').cpus().length)
233 argv.push('BUILDTYPE=' + buildType)
234 // Invoke the Makefile in the 'build' dir.
238 var j = parseInt(jobs, 10)
239 if (!isNaN(j) && j > 0) {
242 } else if (jobs.toUpperCase() === 'MAX') {
244 argv.push(require('os').cpus().length)
250 // did the user specify their own .sln file?
251 var hasSln = argv.some(function (arg) {
252 return path.extname(arg) == '.sln'
255 argv.unshift(gyp.opts.solution || guessedSolution)
259 var proc = gyp.spawn(command, argv)
260 proc.on('exit', onExit)
264 * Invoked after the make/msbuild command exits.
267 function onExit (code, signal) {
269 return callback(new Error('`' + command + '` failed with exit code: ' + code))
272 return callback(new Error('`' + command + '` got signal: ' + signal))