Upgrade npm to 1.1.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('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     // TODO: Check to see if this file actually exists and error out if it doesn't
96     command = msbuild
97     build()
98   }
99
100   /**
101    * Actually spawn the process and compile the module.
102    */
103
104   function build () {
105     var buildType = config.target_defaults.default_configuration
106       , platform = config.target_arch == 'x64' ? '64' : '32'
107
108     if (gyp.opts.debug) {
109       buildType = 'Debug'
110     }
111
112     // Enable Verbose build
113     if (!win && gyp.opts.verbose) {
114       argv.push('V=1')
115     }
116     if (win && !gyp.opts.verbose) {
117       argv.push('/clp:Verbosity=minimal')
118     }
119
120     // Turn off the Microsoft logo on Windows
121     if (win) {
122       argv.push('/nologo')
123     }
124
125     // Specify the build type, Release by default
126     if (win) {
127       argv.push('/p:Configuration=' + buildType + ';Platform=Win' + platform)
128     } else {
129       argv.push('BUILDTYPE=' + buildType)
130       // Invoke the Makefile in the 'build' dir.
131       argv.push('-C')
132       argv.push('build')
133     }
134
135     if (win) {
136       // did the user specify their own .sln file?
137       var hasSln = argv.some(function (arg) {
138         return path.extname(arg) == '.sln'
139       })
140       if (!hasSln) {
141         argv.unshift(gyp.opts.solution || guessedSolution)
142       }
143     }
144
145     asyncEmit(emitter, 'before', function (err) {
146       if (err) return callback(err)
147       var proc = gyp.spawn(command, argv)
148       proc.on('exit', onExit)
149     })
150   }
151
152   /**
153    * Invoked after the make/msbuild command exits.
154    */
155
156   function onExit (code, signal) {
157     asyncEmit(emitter, 'after', function (err) {
158       if (err) return callback(err)
159       if (code !== 0) {
160         return callback(new Error('`' + command + '` failed with exit code: ' + code))
161       }
162       if (signal) {
163         return callback(new Error('`' + command + '` got signal: ' + signal))
164       }
165       callback()
166     })
167   }
168
169 }