2 module.exports = exports = build
8 var fs = require('graceful-fs')
9 , path = require('path')
10 , glob = require('glob')
11 , log = require('npmlog')
12 , which = require('which')
13 , mkdirp = require('mkdirp')
14 , win = process.platform == 'win32'
15 , openbsd = process.platform == 'openbsd'
17 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
19 function build (gyp, argv, callback) {
21 var makeCommand = openbsd ? 'gmake' : 'make'
22 var command = win ? 'msbuild' : makeCommand
23 , buildDir = path.resolve('build')
24 , configPath = path.resolve(buildDir, 'config.gypi')
34 * Load the "config.gypi" file that was generated during "configure".
37 function loadConfigGypi () {
38 fs.readFile(configPath, 'utf8', function (err, data) {
40 if (err.code == 'ENOENT') {
41 callback(new Error('You must run `node-gyp configure` first!'))
47 config = JSON.parse(data.replace(/\#.+\n/, ''))
49 // get the 'arch', 'buildType', and 'nodeDir' vars from the config
50 buildType = config.target_defaults.default_configuration
51 arch = config.variables.target_arch
52 nodeDir = config.variables.nodedir
53 copyDevLib = config.variables.copy_dev_lib == 'true'
55 if ('debug' in gyp.opts) {
56 buildType = gyp.opts.debug ? 'Debug' : 'Release'
62 log.verbose('build type:', buildType)
63 log.verbose('architecture:', arch)
64 log.verbose('node dev dir:', nodeDir)
75 * On Windows, find the first build/*.sln file.
78 function findSolutionFile () {
79 glob('build/*.sln', function (err, files) {
80 if (err) return callback(err)
81 if (files.length === 0) {
82 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
84 guessedSolution = files[0]
85 log.verbose('found first Solution file', guessedSolution)
91 * Uses node-which to locate the msbuild / make executable.
95 // First make sure we have the build command in the PATH
96 which(command, function (err, execPath) {
98 if (win && /not found/.test(err.message)) {
99 // On windows and no 'msbuild' found. Let's guess where it is
102 // Some other error or 'make' not found on Unix, report that to the user
107 log.verbose('`which` succeeded for `' + command + '`', execPath)
113 * Guess the location of the "msbuild.exe" file on Windows.
116 function guessMsbuild () {
117 log.verbose('could not find "msbuild.exe". guessing location')
118 // This is basically just hard-coded. If this causes problems
119 // then we'll think of something more clever.
120 var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
121 , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
122 , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
123 , msbuild = path.resolve(versionDir, 'msbuild.exe')
124 fs.stat(msbuild, function (err, stat) {
126 if (err.code == 'ENOENT') {
127 callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
139 * Copies the node.lib file for the current target architecture into the
140 * current proper dev dir location.
143 function copyNodeLib () {
144 if (!win || !copyDevLib) return doBuild()
146 var buildDir = path.resolve(nodeDir, buildType)
147 , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
148 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
150 mkdirp(buildDir, function (err, isNew) {
151 if (err) return callback(err)
152 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
153 var rs = fs.createReadStream(archNodeLibPath)
154 , ws = fs.createWriteStream(buildNodeLibPath)
155 log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
157 rs.on('error', callback)
158 ws.on('error', callback)
159 rs.on('end', doBuild)
164 * Actually spawn the process and compile the module.
167 function doBuild () {
169 // Enable Verbose build
170 var verbose = log.levels[log.level] <= log.levels.verbose
171 if (!win && verbose) {
174 if (win && !verbose) {
175 argv.push('/clp:Verbosity=minimal')
178 // Turn off the Microsoft logo on Windows
183 // Specify the build type, Release by default
185 var p = arch === 'x64' ? 'x64' : 'Win32'
186 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
188 argv.push('BUILDTYPE=' + buildType)
189 // Invoke the Makefile in the 'build' dir.
195 // did the user specify their own .sln file?
196 var hasSln = argv.some(function (arg) {
197 return path.extname(arg) == '.sln'
200 argv.unshift(gyp.opts.solution || guessedSolution)
204 var proc = gyp.spawn(command, argv)
205 proc.on('exit', onExit)
209 * Invoked after the make/msbuild command exits.
212 function onExit (code, signal) {
214 return callback(new Error('`' + command + '` failed with exit code: ' + code))
217 return callback(new Error('`' + command + '` got signal: ' + signal))