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 msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
143 fs.stat(msbuildPath, function (err, stat) {
145 if (err.code == 'ENOENT') {
146 if (msbuilds.length) {
147 return verifyMsbuild()
149 callback(notfoundErr)
156 command = msbuildPath
164 * Copies the node.lib file for the current target architecture into the
165 * current proper dev dir location.
168 function copyNodeLib () {
169 if (!win || !copyDevLib) return doBuild()
171 var buildDir = path.resolve(nodeDir, buildType)
172 , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
173 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
175 mkdirp(buildDir, function (err, isNew) {
176 if (err) return callback(err)
177 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
178 var rs = fs.createReadStream(archNodeLibPath)
179 , ws = fs.createWriteStream(buildNodeLibPath)
180 log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
182 rs.on('error', callback)
183 ws.on('error', callback)
184 rs.on('end', doBuild)
189 * Actually spawn the process and compile the module.
192 function doBuild () {
194 // Enable Verbose build
195 var verbose = log.levels[log.level] <= log.levels.verbose
196 if (!win && verbose) {
199 if (win && !verbose) {
200 argv.push('/clp:Verbosity=minimal')
204 // Turn off the Microsoft logo on Windows
208 // Specify the build type, Release by default
210 var p = arch === 'x64' ? 'x64' : 'Win32'
211 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
213 if (!isNaN(parseInt(jobs, 10))) {
214 argv.push('/m:' + parseInt(jobs, 10))
215 } else if (jobs.toUpperCase() === 'MAX') {
216 argv.push('/m:' + require('os').cpus().length)
220 argv.push('BUILDTYPE=' + buildType)
221 // Invoke the Makefile in the 'build' dir.
225 if (!isNaN(parseInt(jobs, 10))) {
227 argv.push(parseInt(jobs, 10))
228 } else if (jobs.toUpperCase() === 'MAX') {
230 argv.push(require('os').cpus().length)
236 // did the user specify their own .sln file?
237 var hasSln = argv.some(function (arg) {
238 return path.extname(arg) == '.sln'
241 argv.unshift(gyp.opts.solution || guessedSolution)
245 var proc = gyp.spawn(command, argv)
246 proc.on('exit', onExit)
250 * Invoked after the make/msbuild command exits.
253 function onExit (code, signal) {
255 return callback(new Error('`' + command + '` failed with exit code: ' + code))
258 return callback(new Error('`' + command + '` got signal: ' + signal))
260 //symlinkNodeBinding()
264 function symlinkNodeBinding () {
266 var buildDir = 'build/' + buildType + '/*.node'
267 log.verbose('globbing for files', buildDir)
268 glob(buildDir, function (err, nodeFiles) {
269 if (err) return callback(err)
270 log.silly('symlink', 'linking files', nodeFiles)
272 var file = nodeFiles.shift()
274 // no more files to link... done!
278 // windows requires absolute paths for junctions
279 source = path.resolve('build', path.basename(file))
280 target = path.resolve(file)
282 // on unix, use only relative paths since they're nice
283 source = path.join('build', path.basename(file))
284 target = path.relative('build', file)
286 log.info('symlink', 'creating %s "%s" pointing to "%s"', win ? 'junction' : 'symlink', source, target)
287 fs.symlink(target, source, 'junction', function (err) {
289 if (err.code === 'EEXIST') {
290 log.verbose('destination already exists; deleting', dest)
291 rm(dest, function (err) {
292 if (err) return callback(err)
293 log.verbose('delete successful; trying symlink again')
294 nodeFiles.unshift(file)
302 // process the next file, if any