node-gyp/lib/configure.js
2012-02-09 11:24:45 -08:00

79 lines
2.3 KiB
JavaScript

module.exports = exports = configure
/**
* Module dependencies.
*/
var path = require('path')
, win = process.platform == 'win32'
exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
function configure (gyp, argv, callback) {
var python = gyp.opts.python || 'python'
, version
if (gyp.opts.target) {
// if '--target=x.x' was given, then ensure that version is installed
version = parseFloat(gyp.opts.target)
gyp.opts.ensure = true
gyp.commands.install([ version ], go)
} else {
// otherwise look up the 'current' version
gyp.commands.current([], function (err, _version) {
if (err) return callback(err)
if (!_version) return callback(new Error('No dev files installed. Run `node-gyp use x.x` where "x.x" is a node version like "0.7"'))
version = _version
go()
})
}
function go (err) {
if (err) return callback(err)
var target = String(version)
, devDir = path.join(process.env.HOME, '.node-gyp', target)
, gyp_addon = path.join(devDir, 'tools', 'gyp_addon')
if (win && version < 0.8) {
gyp.verbose('on Windows and target version is less than 0.8; applying #2685 patch')
// if < 0.8, we need to manually apply the patch at joyent/node#2685,
// since it got merged somewhere in 0.7.x.
argv.push('-Dnode_root_dir=' + devDir)
argv.push('-I')
argv.push(path.join(devDir, 'tools', 'patch.gypi'))
}
if (!win && version < 0.8) {
gyp.verbose('on Unix and target version is less than 0.8; applying #2722 patch')
argv.push('-I')
argv.push(path.join(devDir, 'tools', 'patch2722.gypi'))
}
if (!win && !~argv.indexOf('-f') && !~argv.indexOf('--format')) {
gyp.verbose('gyp format was not specified; forcing "make"')
// use a 'gyp' suffix on the Makefile, as to not overwrite an existing one
argv.unshift('.gyp')
argv.unshift('--suffix')
// force the 'make' target for non-Windows
argv.unshift('make')
argv.unshift('-f')
}
argv.unshift(gyp_addon)
var cp = gyp.spawn(python, argv)
cp.on('exit', function (code, signal) {
if (code !== 0) {
callback(new Error('`gyp_addon` failed with exit code: ' + code))
} else {
callback()
}
})
}
}