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 , win = process.platform == 'win32'
17 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
19 function build (gyp, argv, callback) {
21 var makeCommand = gyp.opts.make || process.env.MAKE
22 || (process.platform.indexOf('bsd') != -1 ? 'gmake' : 'make')
23 , command = win ? 'msbuild' : makeCommand
24 , buildDir = path.resolve('build')
25 , configPath = path.resolve(buildDir, 'config.gypi')
26 , jobs = gyp.opts.jobs || process.env.JOBS
36 * Load the "config.gypi" file that was generated during "configure".
39 function loadConfigGypi () {
40 fs.readFile(configPath, 'utf8', function (err, data) {
42 if (err.code == 'ENOENT') {
43 callback(new Error('You must run `node-gyp configure` first!'))
49 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 copyDevLib = config.variables.copy_dev_lib == 'true'
57 if ('debug' in gyp.opts) {
58 buildType = gyp.opts.debug ? 'Debug' : 'Release'
64 log.verbose('build type', buildType)
65 log.verbose('architecture', arch)
66 log.verbose('node dev dir', nodeDir)
77 * On Windows, find the first build/*.sln file.
80 function findSolutionFile () {
81 glob('build/*.sln', function (err, files) {
82 if (err) return callback(err)
83 if (files.length === 0) {
84 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
86 guessedSolution = files[0]
87 log.verbose('found first Solution file', guessedSolution)
93 * Uses node-which to locate the msbuild / make executable.
97 // First make sure we have the build command in the PATH
98 which(command, function (err, execPath) {
100 if (win && /not found/.test(err.message)) {
101 // On windows and no 'msbuild' found. Let's guess where it is
104 // Some other error or 'make' not found on Unix, report that to the user
109 log.verbose('`which` succeeded for `' + command + '`', execPath)
115 * Guess the location of the "msbuild.exe" file on Windows.
118 function guessMsbuild () {
119 log.verbose('could not find "msbuild.exe". guessing location')
120 // This is basically just hard-coded. If this causes problems
121 // then we'll think of something more clever.
122 var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
123 , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
124 , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
125 , msbuild = path.resolve(versionDir, 'msbuild.exe')
126 fs.stat(msbuild, function (err, stat) {
128 if (err.code == 'ENOENT') {
129 callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
141 * Copies the node.lib file for the current target architecture into the
142 * current proper dev dir location.
145 function copyNodeLib () {
146 if (!win || !copyDevLib) return doBuild()
148 var buildDir = path.resolve(nodeDir, buildType)
149 , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
150 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
152 mkdirp(buildDir, function (err, isNew) {
153 if (err) return callback(err)
154 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
155 var rs = fs.createReadStream(archNodeLibPath)
156 , ws = fs.createWriteStream(buildNodeLibPath)
157 log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
159 rs.on('error', callback)
160 ws.on('error', callback)
161 rs.on('end', doBuild)
166 * Actually spawn the process and compile the module.
169 function doBuild () {
171 // Enable Verbose build
172 var verbose = log.levels[log.level] <= log.levels.verbose
173 if (!win && verbose) {
176 if (win && !verbose) {
177 argv.push('/clp:Verbosity=minimal')
181 // Turn off the Microsoft logo on Windows
185 // Specify the build type, Release by default
187 var p = arch === 'x64' ? 'x64' : 'Win32'
188 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
190 if (!isNaN(parseInt(jobs, 10))) {
191 argv.push('/m:' + parseInt(jobs, 10))
192 } else if (jobs.toUpperCase() === 'MAX') {
193 argv.push('/m:' + require('os').cpus().length)
197 argv.push('BUILDTYPE=' + buildType)
198 // Invoke the Makefile in the 'build' dir.
202 if (!isNaN(parseInt(jobs, 10))) {
204 argv.push(parseInt(jobs, 10))
205 } else if (jobs.toUpperCase() === 'MAX') {
207 argv.push(require('os').cpus().length)
213 // did the user specify their own .sln file?
214 var hasSln = argv.some(function (arg) {
215 return path.extname(arg) == '.sln'
218 argv.unshift(gyp.opts.solution || guessedSolution)
222 var proc = gyp.spawn(command, argv)
223 proc.on('exit', onExit)
227 * Invoked after the make/msbuild command exits.
230 function onExit (code, signal) {
232 return callback(new Error('`' + command + '` failed with exit code: ' + code))
235 return callback(new Error('`' + command + '` got signal: ' + signal))
237 //symlinkNodeBinding()
241 function symlinkNodeBinding () {
243 var buildDir = 'build/' + buildType + '/*.node'
244 log.verbose('globbing for files', buildDir)
245 glob(buildDir, function (err, nodeFiles) {
246 if (err) return callback(err)
247 log.silly('symlink', 'linking files', nodeFiles)
249 var file = nodeFiles.shift()
251 // no more files to link... done!
255 // windows requires absolute paths for junctions
256 source = path.resolve('build', path.basename(file))
257 target = path.resolve(file)
259 // on unix, use only relative paths since they're nice
260 source = path.join('build', path.basename(file))
261 target = path.relative('build', file)
263 log.info('symlink', 'creating %s "%s" pointing to "%s"', win ? 'junction' : 'symlink', source, target)
264 fs.symlink(target, source, 'junction', function (err) {
266 if (err.code === 'EEXIST') {
267 log.verbose('destination already exists; deleting', dest)
268 rm(dest, function (err) {
269 if (err) return callback(err)
270 log.verbose('delete successful; trying symlink again')
271 nodeFiles.unshift(file)
279 // process the next file, if any