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