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