Merge branch 'v0.6.19-release' into v0.6
[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   , which = require('which')
12   , mkdirp = require('./util/mkdirp')
13   , win = process.platform == 'win32'
14   , openbsd = process.platform == 'openbsd'
15
16 exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
17
18 function build (gyp, argv, callback) {
19
20   gyp.verbose('build args', argv)
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       gyp.verbose('build type:', buildType)
63       gyp.verbose('architecture:', arch)
64       gyp.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       gyp.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       gyp.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     gyp.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       gyp.verbose('"' + buildType + '" dir needed to be created?', isNew)
153       var rs = fs.createReadStream(archNodeLibPath)
154         , ws = fs.createWriteStream(buildNodeLibPath)
155       rs.pipe(ws)
156       rs.on('error', callback)
157       ws.on('error', callback)
158       rs.on('end', doBuild)
159     })
160   }
161
162   /**
163    * Actually spawn the process and compile the module.
164    */
165
166   function doBuild () {
167
168     // Enable Verbose build
169     if (!win && gyp.opts.verbose) {
170       argv.push('V=1')
171     }
172     if (win && !gyp.opts.verbose) {
173       argv.push('/clp:Verbosity=minimal')
174     }
175
176     // Turn off the Microsoft logo on Windows
177     if (win) {
178       argv.push('/nologo')
179     }
180
181     // Specify the build type, Release by default
182     if (win) {
183       var p = arch === 'x64' ? 'x64' : 'Win32'
184       argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
185     } else {
186       argv.push('BUILDTYPE=' + buildType)
187       // Invoke the Makefile in the 'build' dir.
188       argv.push('-C')
189       argv.push('build')
190     }
191
192     if (win) {
193       // did the user specify their own .sln file?
194       var hasSln = argv.some(function (arg) {
195         return path.extname(arg) == '.sln'
196       })
197       if (!hasSln) {
198         argv.unshift(gyp.opts.solution || guessedSolution)
199       }
200     }
201
202     var proc = gyp.spawn(command, argv)
203     proc.on('exit', onExit)
204   }
205
206   /**
207    * Invoked after the make/msbuild command exits.
208    */
209
210   function onExit (code, signal) {
211     if (code !== 0) {
212       return callback(new Error('`' + command + '` failed with exit code: ' + code))
213     }
214     if (signal) {
215       return callback(new Error('`' + command + '` got signal: ' + signal))
216     }
217     callback()
218   }
219
220 }