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