Skip to content

Commit

Permalink
Allow build directory to be changed.
Browse files Browse the repository at this point in the history
Change with `--build-dir` parameter.

ie:
```
node-gyp --build-dir=foo rebuild
```

PR nodejs#919
Fixes nodejs#263
  • Loading branch information
peterbraden committed Nov 23, 2016
1 parent 05c4494 commit 63d9266
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions bin/node-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var path = require('path')
var prog = gyp()
var completed = false
prog.parseArgv(process.argv)
prog.buildDir = prog.opts['build-dir'] || 'build'
prog.devDir = prog.opts.devdir

var homeDir = osenv.home()
Expand Down
6 changes: 3 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function build (gyp, argv, callback) {
var release = processRelease(argv, gyp, process.version, process.release)
, makeCommand = gyp.opts.make || process.env.MAKE || platformMake
, command = win ? 'msbuild' : makeCommand
, buildDir = path.resolve('build')
, buildDir = path.resolve(gyp.buildDir)
, configPath = path.resolve(buildDir, 'config.gypi')
, jobs = gyp.opts.jobs || process.env.JOBS
, buildType
Expand Down Expand Up @@ -86,7 +86,7 @@ function build (gyp, argv, callback) {
*/

function findSolutionFile () {
glob('build/*.sln', function (err, files) {
glob(buildDir + '/*.sln', function (err, files) {
if (err) return callback(err)
if (files.length === 0) {
return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
Expand Down Expand Up @@ -240,7 +240,7 @@ function build (gyp, argv, callback) {
argv.push('BUILDTYPE=' + buildType)
// Invoke the Makefile in the 'build' dir.
argv.push('-C')
argv.push('build')
argv.push(buildDir)
if (jobs) {
var j = parseInt(jobs, 10)
if (!isNaN(j) && j > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var log = require('npmlog')
function clean (gyp, argv, callback) {

// Remove the 'build' dir
var buildDir = 'build'
var buildDir = gyp.buildDir

log.verbose('clean', 'removing "%s" directory', buildDir)
rm(buildDir, callback)
Expand Down
9 changes: 5 additions & 4 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' f
function configure (gyp, argv, callback) {

var python = gyp.opts.python || process.env.PYTHON || 'python2'
, buildDir = path.resolve('build')
, buildDir = path.resolve(gyp.buildDir)
, configNames = [ 'config.gypi', 'common.gypi' ]
, configs = []
, nodeDir
Expand Down Expand Up @@ -154,12 +154,13 @@ function configure (gyp, argv, callback) {
return v
}

log.silly('build/' + configFilename, config)
log.silly(gyp.buildDir + '/' + configFilename, config)

// now write out the config.gypi file to the build/ dir
var prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
, json = JSON.stringify(config, boolsToString, 2)
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
log.verbose(gyp.buildDir + '/' + configFilename,
'writing out config file: %s', configPath)
configs.push(configPath)
fs.writeFile(configPath, [prefix, json, ''].join('\n'), findConfigs)
}
Expand Down Expand Up @@ -254,7 +255,7 @@ function configure (gyp, argv, callback) {
if (err)
common_gypi = path.resolve(nodeDir, 'common.gypi')

var output_dir = 'build'
var output_dir = gyp.buildDir
if (win) {
// Windows expects an absolute path
output_dir = buildDir
Expand Down
2 changes: 2 additions & 0 deletions lib/node-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function Gyp () {

this.devDir = ''
this.commands = {}
this.buildDir = ''

commands.forEach(function (command) {
self.commands[command] = function (argv, callback) {
Expand Down Expand Up @@ -89,6 +90,7 @@ proto.configDefs = {
, 'tarball': String // 'install'
, jobs: String // 'build'
, thin: String // 'configure'
, 'build-dir': String // everywhere
}

/**
Expand Down
28 changes: 27 additions & 1 deletion test/test-addon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

var test = require('tape')
var execFile = require('child_process').execFile
var childProcess = require('child_process')
var execFile = childProcess.execFile
var exec = childProcess.exec
var path = require('path')
var addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
var nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
Expand All @@ -26,3 +28,27 @@ test('build simple addon', function (t) {
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})

test('build with different build dir', function(t) {
var cmd = [nodeGyp, 'rebuild', '-C',
addonPath, '--loglevel=verbose', '--build-dir=foo']
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
var logLines = stderr.toString().trim().split(/\r?\n/)
var lastLine = logLines[logLines.length-1]
t.strictEqual(err, null)
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')

try {
var binding = require('hello_world')
t.strictEqual(binding.hello(), 'world')

// Cleanup
exec('node-gyp --build-dir=foo clean', t.end)
} catch (error) {
t.error(error, 'load module')
}
})

proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})

0 comments on commit 63d9266

Please sign in to comment.