Merge remote-tracking branch 'ry/v0.8' into master
[platform/upstream/nodejs.git] / deps / npm / node_modules / node-gyp / lib / build.js
1
2 module.exports = exports = build
3
4 /**
5  * Module dependencies.
6  */
7
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   , exec = require('child_process').exec
16   , win = process.platform == 'win32'
17
18 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
19
20 function build (gyp, argv, callback) {
21
22   var makeCommand = gyp.opts.make || process.env.MAKE
23       || (process.platform.indexOf('bsd') != -1 ? 'gmake' : 'make')
24     , command = win ? 'msbuild' : makeCommand
25     , buildDir = path.resolve('build')
26     , configPath = path.resolve(buildDir, 'config.gypi')
27     , jobs = gyp.opts.jobs || process.env.JOBS
28     , buildType
29     , config
30     , arch
31     , nodeDir
32     , copyDevLib
33
34   loadConfigGypi()
35
36   /**
37    * Load the "config.gypi" file that was generated during "configure".
38    */
39
40   function loadConfigGypi () {
41     fs.readFile(configPath, 'utf8', function (err, data) {
42       if (err) {
43         if (err.code == 'ENOENT') {
44           callback(new Error('You must run `node-gyp configure` first!'))
45         } else {
46           callback(err)
47         }
48         return
49       }
50       config = JSON.parse(data.replace(/\#.+\n/, ''))
51
52       // get the 'arch', 'buildType', and 'nodeDir' vars from the config
53       buildType = config.target_defaults.default_configuration
54       arch = config.variables.target_arch
55       nodeDir = config.variables.nodedir
56       copyDevLib = config.variables.copy_dev_lib == 'true'
57
58       if ('debug' in gyp.opts) {
59         buildType = gyp.opts.debug ? 'Debug' : 'Release'
60       }
61       if (!buildType) {
62         buildType = 'Release'
63       }
64
65       log.verbose('build type', buildType)
66       log.verbose('architecture', arch)
67       log.verbose('node dev dir', nodeDir)
68
69       if (win) {
70         findSolutionFile()
71       } else {
72         doWhich()
73       }
74     })
75   }
76
77   /**
78    * On Windows, find the first build/*.sln file.
79    */
80
81   function findSolutionFile () {
82     glob('build/*.sln', function (err, files) {
83       if (err) return callback(err)
84       if (files.length === 0) {
85         return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
86       }
87       guessedSolution = files[0]
88       log.verbose('found first Solution file', guessedSolution)
89       doWhich()
90     })
91   }
92
93   /**
94    * Uses node-which to locate the msbuild / make executable.
95    */
96
97   function doWhich () {
98     // First make sure we have the build command in the PATH
99     which(command, function (err, execPath) {
100       if (err) {
101         if (win && /not found/.test(err.message)) {
102           // On windows and no 'msbuild' found. Let's guess where it is
103           findMsbuild()
104         } else {
105           // Some other error or 'make' not found on Unix, report that to the user
106           callback(err)
107         }
108         return
109       }
110       log.verbose('`which` succeeded for `' + command + '`', execPath)
111       copyNodeLib()
112     })
113   }
114
115   /**
116    * Search for the location of "msbuild.exe" file on Windows.
117    */
118
119   function findMsbuild () {
120     log.verbose('could not find "msbuild.exe" in PATH - finding location in registry')
121     var notfoundErr = new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2008+ installed?')
122     exec('reg query "HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions" /s', function (err, stdout, stderr) {
123       var reVers = /Software\\Microsoft\\MSBuild\\ToolsVersions\\([^\r]+)\r\n\s+MSBuildToolsPath\s+REG_SZ\s+([^\r]+)/gi
124         , msbuilds = []
125         , r
126         , msbuildPath
127       if (err) {
128         return callback(notfoundErr)
129       }
130       while (r = reVers.exec(stdout)) {
131         if (parseFloat(r[1], 10) >= 3.5) {
132           msbuilds.push({
133             version: parseFloat(r[1], 10),
134             path: r[2]
135           })
136         }
137       }
138       msbuilds.sort(function (x, y) {
139         return (x.version < y.version ? -1 : 1)
140       })
141       ;(function verifyMsbuild () {
142         if (!msbuilds.length) return callback(notfoundErr);
143         msbuildPath = path.resolve(msbuilds.pop().path, 'msbuild.exe')
144         fs.stat(msbuildPath, function (err, stat) {
145           if (err) {
146             if (err.code == 'ENOENT') {
147               if (msbuilds.length) {
148                 return verifyMsbuild()
149               } else {
150                 callback(notfoundErr)
151               }
152             } else {
153               callback(err)
154             }
155             return
156           }
157           command = msbuildPath
158           copyNodeLib()
159         })
160       })()
161     })
162   }
163
164   /**
165    * Copies the node.lib file for the current target architecture into the
166    * current proper dev dir location.
167    */
168
169   function copyNodeLib () {
170     if (!win || !copyDevLib) return doBuild()
171
172     var buildDir = path.resolve(nodeDir, buildType)
173       , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
174       , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
175
176     mkdirp(buildDir, function (err, isNew) {
177       if (err) return callback(err)
178       log.verbose('"' + buildType + '" dir needed to be created?', isNew)
179       var rs = fs.createReadStream(archNodeLibPath)
180         , ws = fs.createWriteStream(buildNodeLibPath)
181       log.verbose('copying "node.lib" for ' + arch, buildNodeLibPath)
182       rs.pipe(ws)
183       rs.on('error', callback)
184       ws.on('error', callback)
185       rs.on('end', doBuild)
186     })
187   }
188
189   /**
190    * Actually spawn the process and compile the module.
191    */
192
193   function doBuild () {
194
195     // Enable Verbose build
196     var verbose = log.levels[log.level] <= log.levels.verbose
197     if (!win && verbose) {
198       argv.push('V=1')
199     }
200     if (win && !verbose) {
201       argv.push('/clp:Verbosity=minimal')
202     }
203
204     if (win) {
205       // Turn off the Microsoft logo on Windows
206       argv.push('/nologo')
207     }
208
209     // Specify the build type, Release by default
210     if (win) {
211       var p = arch === 'x64' ? 'x64' : 'Win32'
212       argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
213       if (jobs) {
214         if (!isNaN(parseInt(jobs, 10))) {
215           argv.push('/m:' + parseInt(jobs, 10))
216         } else if (jobs.toUpperCase() === 'MAX') {
217           argv.push('/m:' + require('os').cpus().length)
218         }
219       }
220     } else {
221       argv.push('BUILDTYPE=' + buildType)
222       // Invoke the Makefile in the 'build' dir.
223       argv.push('-C')
224       argv.push('build')
225       if (jobs) {
226         if (!isNaN(parseInt(jobs, 10))) {
227           argv.push('--jobs')
228           argv.push(parseInt(jobs, 10))
229         } else if (jobs.toUpperCase() === 'MAX') {
230           argv.push('--jobs')
231           argv.push(require('os').cpus().length)
232         }
233       }
234     }
235
236     if (win) {
237       // did the user specify their own .sln file?
238       var hasSln = argv.some(function (arg) {
239         return path.extname(arg) == '.sln'
240       })
241       if (!hasSln) {
242         argv.unshift(gyp.opts.solution || guessedSolution)
243       }
244     }
245
246     var proc = gyp.spawn(command, argv)
247     proc.on('exit', onExit)
248   }
249
250   /**
251    * Invoked after the make/msbuild command exits.
252    */
253
254   function onExit (code, signal) {
255     if (code !== 0) {
256       return callback(new Error('`' + command + '` failed with exit code: ' + code))
257     }
258     if (signal) {
259       return callback(new Error('`' + command + '` got signal: ' + signal))
260     }
261     //symlinkNodeBinding()
262     callback()
263   }
264
265   function symlinkNodeBinding () {
266     var source, target
267     var buildDir = 'build/' + buildType + '/*.node'
268     log.verbose('globbing for files', buildDir)
269     glob(buildDir, function (err, nodeFiles) {
270       if (err) return callback(err)
271       log.silly('symlink', 'linking files', nodeFiles)
272       function link () {
273         var file = nodeFiles.shift()
274         if (!file) {
275           // no more files to link... done!
276           return callback()
277         }
278         if (win) {
279           // windows requires absolute paths for junctions
280           source = path.resolve('build', path.basename(file))
281           target = path.resolve(file)
282         } else {
283           // on unix, use only relative paths since they're nice
284           source = path.join('build', path.basename(file))
285           target = path.relative('build', file)
286         }
287         log.info('symlink', 'creating %s "%s" pointing to "%s"', win ? 'junction' : 'symlink', source, target)
288         fs.symlink(target, source, 'junction', function (err) {
289           if (err) {
290             if (err.code === 'EEXIST') {
291               log.verbose('destination already exists; deleting', dest)
292               rm(dest, function (err) {
293                 if (err) return callback(err)
294                 log.verbose('delete successful; trying symlink again')
295                 nodeFiles.unshift(file)
296                 link()
297               })
298             } else {
299               callback(err)
300             }
301             return
302           }
303           // process the next file, if any
304           link()
305         })
306       }
307       // start linking
308       link()
309     })
310   }
311
312 }