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 var command = win ? 'msbuild' : makeCommand
24 , buildDir = path.resolve('build')
25 , configPath = path.resolve(buildDir, 'config.gypi')
35 * Load the "config.gypi" file that was generated during "configure".
38 function loadConfigGypi () {
39 fs.readFile(configPath, 'utf8', function (err, data) {
41 if (err.code == 'ENOENT') {
42 callback(new Error('You must run `node-gyp configure` first!'))
48 config = JSON.parse(data.replace(/\#.+\n/, ''))
50 // get the 'arch', 'buildType', and 'nodeDir' vars from the config
51 buildType = config.target_defaults.default_configuration
52 arch = config.variables.target_arch
53 nodeDir = config.variables.nodedir
54 copyDevLib = config.variables.copy_dev_lib == 'true'
56 if ('debug' in gyp.opts) {
57 buildType = gyp.opts.debug ? 'Debug' : 'Release'
63 log.verbose('build type:', buildType)
64 log.verbose('architecture:', arch)
65 log.verbose('node dev dir:', nodeDir)
76 * On Windows, find the first build/*.sln file.
79 function findSolutionFile () {
80 glob('build/*.sln', function (err, files) {
81 if (err) return callback(err)
82 if (files.length === 0) {
83 return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
85 guessedSolution = files[0]
86 log.verbose('found first Solution file', guessedSolution)
92 * Uses node-which to locate the msbuild / make executable.
96 // First make sure we have the build command in the PATH
97 which(command, function (err, execPath) {
99 if (win && /not found/.test(err.message)) {
100 // On windows and no 'msbuild' found. Let's guess where it is
103 // Some other error or 'make' not found on Unix, report that to the user
108 log.verbose('`which` succeeded for `' + command + '`', execPath)
114 * Guess the location of the "msbuild.exe" file on Windows.
117 function guessMsbuild () {
118 log.verbose('could not find "msbuild.exe". guessing location')
119 // This is basically just hard-coded. If this causes problems
120 // then we'll think of something more clever.
121 var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
122 , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
123 , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
124 , msbuild = path.resolve(versionDir, 'msbuild.exe')
125 fs.stat(msbuild, function (err, stat) {
127 if (err.code == 'ENOENT') {
128 callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
140 * Copies the node.lib file for the current target architecture into the
141 * current proper dev dir location.
144 function copyNodeLib () {
145 if (!win || !copyDevLib) return doBuild()
147 var buildDir = path.resolve(nodeDir, buildType)
148 , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
149 , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
151 mkdirp(buildDir, function (err, isNew) {
152 if (err) return callback(err)
153 log.verbose('"' + buildType + '" dir needed to be created?', isNew)
154 var rs = fs.createReadStream(archNodeLibPath)
155 , ws = fs.createWriteStream(buildNodeLibPath)
156 log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
158 rs.on('error', callback)
159 ws.on('error', callback)
160 rs.on('end', doBuild)
165 * Actually spawn the process and compile the module.
168 function doBuild () {
170 // Enable Verbose build
171 var verbose = log.levels[log.level] <= log.levels.verbose
172 if (!win && verbose) {
175 if (win && !verbose) {
176 argv.push('/clp:Verbosity=minimal')
179 // Turn off the Microsoft logo on Windows
184 // Specify the build type, Release by default
186 var p = arch === 'x64' ? 'x64' : 'Win32'
187 argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
189 argv.push('BUILDTYPE=' + buildType)
190 // Invoke the Makefile in the 'build' dir.
196 // did the user specify their own .sln file?
197 var hasSln = argv.some(function (arg) {
198 return path.extname(arg) == '.sln'
201 argv.unshift(gyp.opts.solution || guessedSolution)
205 var proc = gyp.spawn(command, argv)
206 proc.on('exit', onExit)
210 * Invoked after the make/msbuild command exits.
213 function onExit (code, signal) {
215 return callback(new Error('`' + command + '` failed with exit code: ' + code))
218 return callback(new Error('`' + command + '` got signal: ' + signal))
220 //symlinkNodeBinding()
224 function symlinkNodeBinding () {
225 var buildDir = path.join('build', buildType, '*.node')
226 log.verbose('globbing for files', buildDir)
227 glob(buildDir, function (err, nodeFiles) {
228 if (err) return callback(err)
230 var file = nodeFiles.shift()
232 // no more files to link... done!
235 var dest = path.join('build', path.basename(file))
236 log.info('symlink', 'creating link %j pointing to %j', file, dest)
237 var rel = path.relative('build', file)
238 log.verbose('symlink data', rel)
239 fs.symlink(rel, dest, 'file', function (err) {
241 if (err.code === 'EEXIST') {
242 log.verbose('destination already exists; deleting', dest)
243 rm(dest, function (err) {
244 if (err) return callback(err)
245 log.verbose('delete successful; trying symlink again')
246 nodeFiles.unshift(file)
254 // process the next file, if any