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 ? '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 exec('reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s', function (err, stdout, stderr) {
123 var reVers = /Software\\Microsoft\\MSBuild\\ToolsVersions\\([^\r]+)\r\n\s+MSBuildToolsPath\s+REG_SZ\s+([^\r]+)/gi
128 return callback(notfoundErr)
130 while (r = reVers.exec(stdout)) {
131 if (parseFloat(r[1], 10) >= 3.5) {
133 version: parseFloat(r[1], 10),
138 msbuilds.sort(function (x, y) {
139 return (x.version < y.version ? -1 : 1)
141 ;(function verifyMsbuild () {
142 if (!msbuilds.length) return callback(notfoundErr);
143 msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
144 fs.stat(msbuildPath, function (err, stat) {
146 if (err.code == 'ENOENT') {
147 if (msbuilds.length) {
148 return verifyMsbuild()
150 callback(notfoundErr)
157 command = msbuildPath
165 * Copies the node.lib file for the current target architecture into the
166 * current proper dev dir location.
169 function copyNodeLib () {
170 if (!win || !copyDevLib) return doBuild()
172 var buildDir = path.resolve(nodeDir, buildType)
173 , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
174 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
176 mkdirp(buildDir, function (err, isNew) {
177 if (err) return callback(err)
178 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
179 var rs = fs.createReadStream(archNodeLibPath)
180 , ws = fs.createWriteStream(buildNodeLibPath)
181 log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
183 rs.on('error', callback)
184 ws.on('error', callback)
185 rs.on('end', doBuild)
190 * Actually spawn the process and compile the module.
193 function doBuild () {
195 // Enable Verbose build
196 var verbose = log.levels[log.level] <= log.levels.verbose
197 if (!win && verbose) {
200 if (win && !verbose) {
201 argv.push('/clp:Verbosity=minimal')
205 // Turn off the Microsoft logo on Windows
209 // Specify the build type, Release by default
211 var p = arch === 'x64' ? 'x64' : 'Win32'
212 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
214 if (!isNaN(parseInt(jobs, 10))) {
215 argv.push('/m:' + parseInt(jobs, 10))
216 } else if (jobs.toUpperCase() === 'MAX') {
217 argv.push('/m:' + require('os').cpus().length)
221 argv.push('BUILDTYPE=' + buildType)
222 // Invoke the Makefile in the 'build' dir.
226 if (!isNaN(parseInt(jobs, 10))) {
228 argv.push(parseInt(jobs, 10))
229 } else if (jobs.toUpperCase() === 'MAX') {
231 argv.push(require('os').cpus().length)
237 // did the user specify their own .sln file?
238 var hasSln = argv.some(function (arg) {
239 return path.extname(arg) == '.sln'
242 argv.unshift(gyp.opts.solution || guessedSolution)
246 var proc = gyp.spawn(command, argv)
247 proc.on('exit', onExit)
251 * Invoked after the make/msbuild command exits.
254 function onExit (code, signal) {
256 return callback(new Error('`' + command + '` failed with exit code: ' + code))
259 return callback(new Error('`' + command + '` got signal: ' + signal))
261 //symlinkNodeBinding()
265 function symlinkNodeBinding () {
267 var buildDir = 'build/' + buildType + '/*.node'
268 log.verbose('globbing for files', buildDir)
269 glob(buildDir, function (err, nodeFiles) {
270 if (err) return callback(err)
271 log.silly('symlink', 'linking files', nodeFiles)
273 var file = nodeFiles.shift()
275 // no more files to link... done!
279 // windows requires absolute paths for junctions
280 source = path.resolve('build', path.basename(file))
281 target = path.resolve(file)
283 // on unix, use only relative paths since they're nice
284 source = path.join('build', path.basename(file))
285 target = path.relative('build', file)
287 log.info('symlink', 'creating %s "%s" pointing to "%s"', win ? 'junction' : 'symlink', source, target)
288 fs.symlink(target, source, 'junction', function (err) {
290 if (err.code === 'EEXIST') {
291 log.verbose('destination already exists; deleting', dest)
292 rm(dest, function (err) {
293 if (err) return callback(err)
294 log.verbose('delete successful; trying symlink again')
295 nodeFiles.unshift(file)
303 // process the next file, if any