npm: upgrade to 1.1.41
[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   var command = win ? 'msbuild' : makeCommand
24     , buildDir = path.resolve('build')
25     , configPath = path.resolve(buildDir, 'config.gypi')
26     , buildType
27     , config
28     , arch
29     , nodeDir
30     , copyDevLib
31
32   loadConfigGypi()
33
34   /**
35    * Load the "config.gypi" file that was generated during "configure".
36    */
37
38   function loadConfigGypi () {
39     fs.readFile(configPath, 'utf8', function (err, data) {
40       if (err) {
41         if (err.code == 'ENOENT') {
42           callback(new Error('You must run `node-gyp configure` first!'))
43         } else {
44           callback(err)
45         }
46         return
47       }
48       config = JSON.parse(data.replace(/\#.+\n/, ''))
49
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'
55
56       if ('debug' in gyp.opts) {
57         buildType = gyp.opts.debug ? 'Debug' : 'Release'
58       }
59       if (!buildType) {
60         buildType = 'Release'
61       }
62
63       log.verbose('build type:', buildType)
64       log.verbose('architecture:', arch)
65       log.verbose('node dev dir:', nodeDir)
66
67       if (win) {
68         findSolutionFile()
69       } else {
70         doWhich()
71       }
72     })
73   }
74
75   /**
76    * On Windows, find the first build/*.sln file.
77    */
78
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"?'))
84       }
85       guessedSolution = files[0]
86       log.verbose('found first Solution file', guessedSolution)
87       doWhich()
88     })
89   }
90
91   /**
92    * Uses node-which to locate the msbuild / make executable.
93    */
94
95   function doWhich () {
96     // First make sure we have the build command in the PATH
97     which(command, function (err, execPath) {
98       if (err) {
99         if (win && /not found/.test(err.message)) {
100           // On windows and no 'msbuild' found. Let's guess where it is
101           guessMsbuild()
102         } else {
103           // Some other error or 'make' not found on Unix, report that to the user
104           callback(err)
105         }
106         return
107       }
108       log.verbose('`which` succeeded for `' + command + '`', execPath)
109       copyNodeLib()
110     })
111   }
112
113   /**
114    * Guess the location of the "msbuild.exe" file on Windows.
115    */
116
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) {
126       if (err) {
127         if (err.code == 'ENOENT') {
128           callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
129         } else {
130           callback(err)
131         }
132         return
133       }
134       command = msbuild
135       copyNodeLib()
136     })
137   }
138
139   /**
140    * Copies the node.lib file for the current target architecture into the
141    * current proper dev dir location.
142    */
143
144   function copyNodeLib () {
145     if (!win || !copyDevLib) return doBuild()
146
147     var buildDir = path.resolve(nodeDir, buildType)
148       , archNodeLibPath = path.resolve(nodeDir, arch, 'node.lib')
149       , buildNodeLibPath = path.resolve(buildDir, 'node.lib')
150
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)
157       rs.pipe(ws)
158       rs.on('error', callback)
159       ws.on('error', callback)
160       rs.on('end', doBuild)
161     })
162   }
163
164   /**
165    * Actually spawn the process and compile the module.
166    */
167
168   function doBuild () {
169
170     // Enable Verbose build
171     var verbose = log.levels[log.level] <= log.levels.verbose
172     if (!win && verbose) {
173       argv.push('V=1')
174     }
175     if (win && !verbose) {
176       argv.push('/clp:Verbosity=minimal')
177     }
178
179     // Turn off the Microsoft logo on Windows
180     if (win) {
181       argv.push('/nologo')
182     }
183
184     // Specify the build type, Release by default
185     if (win) {
186       var p = arch === 'x64' ? 'x64' : 'Win32'
187       argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
188     } else {
189       argv.push('BUILDTYPE=' + buildType)
190       // Invoke the Makefile in the 'build' dir.
191       argv.push('-C')
192       argv.push('build')
193     }
194
195     if (win) {
196       // did the user specify their own .sln file?
197       var hasSln = argv.some(function (arg) {
198         return path.extname(arg) == '.sln'
199       })
200       if (!hasSln) {
201         argv.unshift(gyp.opts.solution || guessedSolution)
202       }
203     }
204
205     var proc = gyp.spawn(command, argv)
206     proc.on('exit', onExit)
207   }
208
209   /**
210    * Invoked after the make/msbuild command exits.
211    */
212
213   function onExit (code, signal) {
214     if (code !== 0) {
215       return callback(new Error('`' + command + '` failed with exit code: ' + code))
216     }
217     if (signal) {
218       return callback(new Error('`' + command + '` got signal: ' + signal))
219     }
220     //symlinkNodeBinding()
221     callback()
222   }
223
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)
229       function link () {
230         var file = nodeFiles.shift()
231         if (!file) {
232           // no more files to link... done!
233           return callback()
234         }
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) {
240           if (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)
247                 link()
248               })
249             } else {
250               callback(err)
251             }
252             return
253           }
254           // process the next file, if any
255           link()
256         })
257       }
258       // start linking
259       link()
260     })
261   }
262
263 }