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