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