2 module.exports = exports = build
9 , path = require('path')
10 , glob = require('glob')
11 , which = require('which')
12 , asyncEmit = require('./util/asyncEmit')
13 , createHook = require('./util/hook')
14 , win = process.platform == 'win32'
16 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
18 function build (gyp, argv, callback) {
20 gyp.verbose('build args', argv)
21 var command = win ? 'msbuild' : 'make'
22 , buildDir = path.resolve('build')
23 , configPath = path.resolve(buildDir, 'config.gypi')
27 createHook('gyp-build.js', function (err, _e) {
28 if (err) return callback(err)
34 * Load the "config.gypi" file that was generated during "configure".
37 function loadConfigGypi () {
38 fs.readFile(configPath, 'utf8', function (err, data) {
39 if (err) return callback(err)
40 config = JSON.parse(data.replace(/\#.+\n/, ''))
50 * On Windows, find first build/*.sln file.
53 function findSolutionFile () {
54 glob('build/*.sln', function (err, files) {
55 if (err) return callback(err)
56 if (files.length === 0) {
57 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
59 guessedSolution = files[0]
60 gyp.verbose('found first Solution file', guessedSolution)
66 // First make sure we have the build command in the PATH
67 which(command, function (err, execPath) {
69 if (win && /not found/.test(err.message)) {
70 // On windows and no 'msbuild' found. Let's guess where it is
73 // Some other error or 'make' not found on Unix, report that to the user
78 gyp.verbose('`which` succeeded for `' + command + '`', execPath)
84 * Guess the location of the "msbuild.exe" file on Windows.
87 function guessMsbuild () {
88 gyp.verbose('could not find "msbuild.exe". guessing location')
89 // This is basically just hard-coded. If this causes problems
90 // then we'll think of something more clever.
91 var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
92 , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
93 , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
94 , msbuild = path.resolve(versionDir, 'msbuild.exe')
95 fs.stat(msbuild, function (err, stat) {
97 if (err.code == 'ENOENT') {
98 callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
110 * Actually spawn the process and compile the module.
114 var buildType = config.target_defaults.default_configuration
115 , platform = config.target_arch == 'x64' ? '64' : '32'
117 if (gyp.opts.debug) {
121 // Enable Verbose build
122 if (!win && gyp.opts.verbose) {
125 if (win && !gyp.opts.verbose) {
126 argv.push('/clp:Verbosity=minimal')
129 // Turn off the Microsoft logo on Windows
134 // Specify the build type, Release by default
136 argv.push('/p:Configuration=' + buildType + ';Platform=Win' + platform)
138 argv.push('BUILDTYPE=' + buildType)
139 // Invoke the Makefile in the 'build' dir.
145 // did the user specify their own .sln file?
146 var hasSln = argv.some(function (arg) {
147 return path.extname(arg) == '.sln'
150 argv.unshift(gyp.opts.solution || guessedSolution)
154 asyncEmit(emitter, 'before', function (err) {
155 if (err) return callback(err)
156 var proc = gyp.spawn(command, argv)
157 proc.on('exit', onExit)
162 * Invoked after the make/msbuild command exits.
165 function onExit (code, signal) {
166 asyncEmit(emitter, 'after', function (err) {
167 if (err) return callback(err)
169 return callback(new Error('`' + command + '` failed with exit code: ' + code))
172 return callback(new Error('`' + command + '` got signal: ' + signal))