2 module.exports = exports = build
8 var fs = require('graceful-fs')
9 , path = require('path')
10 , glob = require('glob')
11 , which = require('which')
12 , mkdirp = require('./util/mkdirp')
13 , win = process.platform == 'win32'
15 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
17 function build (gyp, argv, callback) {
19 gyp.verbose('build args', argv)
20 var command = win ? 'msbuild' : 'make'
21 , buildDir = path.resolve('build')
22 , configPath = path.resolve(buildDir, 'config.gypi')
31 * Load the "config.gypi" file that was generated during "configure".
34 function loadConfigGypi () {
35 fs.readFile(configPath, 'utf8', function (err, data) {
37 if (err.code == 'ENOENT') {
38 callback(new Error('You must run `node-gyp configure` first!'))
44 config = JSON.parse(data.replace(/\#.+\n/, ''))
46 // get the 'arch', 'buildType', and 'version' vars from the config
47 buildType = config.target_defaults.default_configuration
48 arch = config.variables.target_arch
49 version = config.variables.target_version
51 if ('debug' in gyp.opts) {
52 buildType = gyp.opts.debug ? 'Debug' : 'Release'
58 gyp.verbose('build type:', buildType)
59 gyp.verbose('architecture:', arch)
60 gyp.verbose('node version:', version)
71 * On Windows, find the first build/*.sln file.
74 function findSolutionFile () {
75 glob('build/*.sln', function (err, files) {
76 if (err) return callback(err)
77 if (files.length === 0) {
78 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
80 guessedSolution = files[0]
81 gyp.verbose('found first Solution file', guessedSolution)
87 * Uses node-which to locate the msbuild / make executable.
91 // First make sure we have the build command in the PATH
92 which(command, function (err, execPath) {
94 if (win && /not found/.test(err.message)) {
95 // On windows and no 'msbuild' found. Let's guess where it is
98 // Some other error or 'make' not found on Unix, report that to the user
103 gyp.verbose('`which` succeeded for `' + command + '`', execPath)
109 * Guess the location of the "msbuild.exe" file on Windows.
112 function guessMsbuild () {
113 gyp.verbose('could not find "msbuild.exe". guessing location')
114 // This is basically just hard-coded. If this causes problems
115 // then we'll think of something more clever.
116 var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
117 , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
118 , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
119 , msbuild = path.resolve(versionDir, 'msbuild.exe')
120 fs.stat(msbuild, function (err, stat) {
122 if (err.code == 'ENOENT') {
123 callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
135 * Copies the node.lib file for the current target architecture into the
136 * current proper dev dir location.
139 function copyNodeLib () {
140 if (!win) return doBuild()
142 var devDir = path.resolve(gyp.devDir, version)
143 , buildDir = path.resolve(devDir, buildType)
144 , archNodeLibPath = path.resolve(devDir, arch, 'node.lib')
145 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
147 mkdirp(buildDir, function (err, isNew) {
148 if (err) return callback(err)
149 gyp.verbose('"' + buildType + '" dir needed to be created?', isNew)
150 var rs = fs.createReadStream(archNodeLibPath)
151 , ws = fs.createWriteStream(buildNodeLibPath)
153 rs.on('error', callback)
154 ws.on('error', callback)
155 rs.on('end', doBuild)
160 * Actually spawn the process and compile the module.
163 function doBuild () {
165 // Enable Verbose build
166 if (!win && gyp.opts.verbose) {
169 if (win && !gyp.opts.verbose) {
170 argv.push('/clp:Verbosity=minimal')
173 // Turn off the Microsoft logo on Windows
178 // Specify the build type, Release by default
180 var p = arch === 'x64' ? 'x64' : 'Win32'
181 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
183 argv.push('BUILDTYPE=' + buildType)
184 // Invoke the Makefile in the 'build' dir.
190 // did the user specify their own .sln file?
191 var hasSln = argv.some(function (arg) {
192 return path.extname(arg) == '.sln'
195 argv.unshift(gyp.opts.solution || guessedSolution)
199 var proc = gyp.spawn(command, argv)
200 proc.on('exit', onExit)
204 * Invoked after the make/msbuild command exits.
207 function onExit (code, signal) {
209 return callback(new Error('`' + command + '` failed with exit code: ' + code))
212 return callback(new Error('`' + command + '` got signal: ' + signal))