Update npm to 1.1.9
[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('fs')
9   , path = require('path')
10   , glob = require('glob')
11   , which = require('which')
12   , asyncEmit = require('./util/asyncEmit')
13   , createHook = require('./util/hook')
14   , win = process.platform == 'win32'
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 command = win ? 'msbuild' : 'make'
22     , buildDir = path.resolve('build')
23     , configPath = path.resolve(buildDir, 'config.gypi')
24     , config
25     , emitter
26
27   createHook('gyp-build.js', function (err, _e) {
28     if (err) return callback(err)
29     emitter = _e
30     loadConfigGypi()
31   })
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) return callback(err)
40       config = JSON.parse(data.replace(/\#.+\n/, ''))
41       if (win) {
42         findSolutionFile()
43       } else {
44         doWhich()
45       }
46     })
47   }
48
49   /**
50    * On Windows, find first build/*.sln file.
51    */
52
53   function findSolutionFile () {
54     glob('build/*.sln', function (err, files) {
55       if (err) return callback(err)
56       if (files.length === 0) {
57         return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
58       }
59       guessedSolution = files[0]
60       gyp.verbose('found first Solution file', guessedSolution)
61       doWhich()
62     })
63   }
64
65   function doWhich () {
66     // First make sure we have the build command in the PATH
67     which(command, function (err, execPath) {
68       if (err) {
69         if (win && /not found/.test(err.message)) {
70           // On windows and no 'msbuild' found. Let's guess where it is
71           guessMsbuild()
72         } else {
73           // Some other error or 'make' not found on Unix, report that to the user
74           callback(err)
75         }
76         return
77       }
78       gyp.verbose('`which` succeeded for `' + command + '`', execPath)
79       build()
80     })
81   }
82
83   /**
84    * Guess the location of the "msbuild.exe" file on Windows.
85    */
86
87   function guessMsbuild () {
88     gyp.verbose('could not find "msbuild.exe". guessing location')
89     // This is basically just hard-coded. If this causes problems
90     // then we'll think of something more clever.
91     var windir = process.env.WINDIR || process.env.SYSTEMROOT || 'C:\\WINDOWS'
92       , frameworkDir = path.resolve(windir, 'Microsoft.NET', 'Framework')
93       , versionDir = path.resolve(frameworkDir, 'v4.0.30319') // This is probably the most brittle part...
94       , msbuild = path.resolve(versionDir, 'msbuild.exe')
95     fs.stat(msbuild, function (err, stat) {
96       if (err) {
97         if (err.code == 'ENOENT') {
98           callback(new Error('Can\'t find "msbuild.exe". Do you have Microsoft Visual Studio C++ 2010 installed?'))
99         } else {
100           callback(err)
101         }
102         return
103       }
104       command = msbuild
105       build()
106     })
107   }
108
109   /**
110    * Actually spawn the process and compile the module.
111    */
112
113   function build () {
114     var buildType = config.target_defaults.default_configuration
115       , platform = config.target_arch == 'x64' ? '64' : '32'
116
117     if (gyp.opts.debug) {
118       buildType = 'Debug'
119     }
120
121     // Enable Verbose build
122     if (!win && gyp.opts.verbose) {
123       argv.push('V=1')
124     }
125     if (win && !gyp.opts.verbose) {
126       argv.push('/clp:Verbosity=minimal')
127     }
128
129     // Turn off the Microsoft logo on Windows
130     if (win) {
131       argv.push('/nologo')
132     }
133
134     // Specify the build type, Release by default
135     if (win) {
136       argv.push('/p:Configuration=' + buildType + ';Platform=Win' + platform)
137     } else {
138       argv.push('BUILDTYPE=' + buildType)
139       // Invoke the Makefile in the 'build' dir.
140       argv.push('-C')
141       argv.push('build')
142     }
143
144     if (win) {
145       // did the user specify their own .sln file?
146       var hasSln = argv.some(function (arg) {
147         return path.extname(arg) == '.sln'
148       })
149       if (!hasSln) {
150         argv.unshift(gyp.opts.solution || guessedSolution)
151       }
152     }
153
154     asyncEmit(emitter, 'before', function (err) {
155       if (err) return callback(err)
156       var proc = gyp.spawn(command, argv)
157       proc.on('exit', onExit)
158     })
159   }
160
161   /**
162    * Invoked after the make/msbuild command exits.
163    */
164
165   function onExit (code, signal) {
166     asyncEmit(emitter, 'after', function (err) {
167       if (err) return callback(err)
168       if (code !== 0) {
169         return callback(new Error('`' + command + '` failed with exit code: ' + code))
170       }
171       if (signal) {
172         return callback(new Error('`' + command + '` got signal: ' + signal))
173       }
174       callback()
175     })
176   }
177
178 }