Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
See issue nodejs#960

Added initial `gyp.js` support with --gypjs command line option.
Environment variable `npm_config_gypjs` also turns this option on.

Update configure and build usage strings depending on `npm_config_gypjs`
environment variable.

Set `npm_config_gypjs` env variable if `--gypjs` command-line option
was set to affect usage text for `configure` and `build` commands.

Update usage strings if `--gypjs` command-line option was supplied

Trying to load gyp.js module only if --gypjs command-line option was supplied.
  • Loading branch information
pmed committed Jun 23, 2016
1 parent 5eb0972 commit 5b0a17b
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 95 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Command Options
| `--python=$path` | Set path to the python (2) binary
| `--msvs_version=$version` | Set Visual Studio version (win)
| `--solution=$solution` | Set Visual Studio Solution version (win)
| `--gypjs` | Use gyp.js instead of gyp


License
Expand Down
10 changes: 5 additions & 5 deletions gyp/gyp.bat
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@rem Copyright (c) 2009 Google Inc. All rights reserved.
@rem Use of this source code is governed by a BSD-style license that can be
@rem found in the LICENSE file.

@python "%~dp0gyp_main.py" %*
@rem Copyright (c) 2009 Google Inc. All rights reserved.
@rem Use of this source code is governed by a BSD-style license that can be
@rem found in the LICENSE file.

@python "%~dp0gyp_main.py" %*
10 changes: 5 additions & 5 deletions gyp/samples/samples.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@rem Copyright (c) 2009 Google Inc. All rights reserved.
@rem Use of this source code is governed by a BSD-style license that can be
@rem found in the LICENSE file.

@python %~dp0/samples %*
@rem Copyright (c) 2009 Google Inc. All rights reserved.
@rem Use of this source code is governed by a BSD-style license that can be
@rem found in the LICENSE file.

@python %~dp0/samples %*
136 changes: 94 additions & 42 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var fs = require('graceful-fs')
, processRelease = require('./process-release')
, win = process.platform == 'win32'

exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
exports.usage = 'Invokes `' + (process.env.npm_config_gypjs ? 'ninja' :
(win ? 'msbuild' : 'make')) + '` and builds the module'

function build (gyp, argv, callback) {
var platformMake = 'make'
Expand All @@ -35,7 +36,11 @@ function build (gyp, argv, callback) {
, config
, arch
, nodeDir
, vcDir

if (gyp.opts.gypjs) {
command = gyp.opts.ninja || process.env.NINJA || 'ninja'
}
loadConfigGypi()

/**
Expand Down Expand Up @@ -71,13 +76,40 @@ function build (gyp, argv, callback) {
log.verbose('node dev dir', nodeDir)

if (win) {
findSolutionFile()
gyp.opts.gypjs? findMSVS() : findSolutionFile()
} else {
doWhich()
}
})
}

/**
* On Windows, find Visual C++ toolset
*/

function findMSVS () {
var msvs_version = gyp.opts.msvs_version || 'auto'
var vs_versions = (msvs_version === 'auto'? [14, 12, 10] : [msvs_version])
vs_versions.find(function(version) {
var vscomntools = process.env['VS' + version + '0COMNTOOLS']
if (vscomntools) {
// remove quotes to work with path.join()
if (vscomntools.substr(0, 1) === '"' && vscomntools.substr(-1, 1) === '"') {
vscomntools = vscomntools.substr(1, vscomntools.length - 2)
}
vcDir = path.join(vscomntools, '..', '..', 'VC')
if (vcDir) {
log.verbose('found Visual C++ in ', vcDir)
return true
}
}
})
if (!vcDir) {
callback(new Error('Visual C++ not found, please setup a C++ compiler toolset'))
}
doWhich()
}

/**
* On Windows, find the first build/*.sln file.
*/
Expand All @@ -102,7 +134,7 @@ function build (gyp, argv, callback) {
// First make sure we have the build command in the PATH
which(command, function (err, execPath) {
if (err) {
if (win && /not found/.test(err.message)) {
if (win && !gyp.opts.gypjs && /not found/.test(err.message)) {
// On windows and no 'msbuild' found. Let's guess where it is
findMsbuild()
} else {
Expand Down Expand Up @@ -185,54 +217,74 @@ function build (gyp, argv, callback) {

// Enable Verbose build
var verbose = log.levels[log.level] <= log.levels.verbose
if (!win && verbose) {
argv.push('V=1')
}
if (win && !verbose) {
argv.push('/clp:Verbosity=minimal')
}
if (!gyp.opts.gypjs) {
if (!win && verbose) {
argv.push('V=1')
}
if (win && !verbose) {
argv.push('/clp:Verbosity=minimal')
}

if (win) {
// Turn off the Microsoft logo on Windows
argv.push('/nologo')
}
if (win) {
// Turn off the Microsoft logo on Windows
argv.push('/nologo')
}

// Specify the build type, Release by default
if (win) {
var p = arch === 'x64' ? 'x64' : 'Win32'
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
if (jobs) {
var j = parseInt(jobs, 10)
if (!isNaN(j) && j > 0) {
argv.push('/m:' + j)
} else if (jobs.toUpperCase() === 'MAX') {
argv.push('/m:' + require('os').cpus().length)
// Specify the build type, Release by default
if (win) {
var p = arch === 'x64' ? 'x64' : 'Win32'
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
if (jobs) {
var j = parseInt(jobs, 10)
if (!isNaN(j) && j > 0) {
argv.push('/m:' + j)
} else if (jobs.toUpperCase() === 'MAX') {
argv.push('/m:' + require('os').cpus().length)
}
}
} else {
argv.push('BUILDTYPE=' + buildType)
// Invoke the Makefile in the 'build' dir.
argv.push('-C')
argv.push('build')
if (jobs) {
var j = parseInt(jobs, 10)
if (!isNaN(j) && j > 0) {
argv.push('--jobs')
argv.push(j)
} else if (jobs.toUpperCase() === 'MAX') {
argv.push('--jobs')
argv.push(require('os').cpus().length)
}
}
}

if (win) {
// did the user specify their own .sln file?
var hasSln = argv.some(function (arg) {
return path.extname(arg) == '.sln'
})
if (!hasSln) {
argv.unshift(gyp.opts.solution || guessedSolution)
}
}
} else {
argv.push('BUILDTYPE=' + buildType)
// Invoke the Makefile in the 'build' dir.
argv.push('-C')
argv.push('build')
// build with ninja
if (verbose) {
argv.push('-v')
}
// Specify the build type, Release by default
argv.push('-C', path.join('build', buildType))
if (jobs) {
var j = parseInt(jobs, 10)
var j = jobs.toUpperCase() === 'MAX'? require('os').cpus().length : parseInt(jobs, 10)
if (!isNaN(j) && j > 0) {
argv.push('--jobs')
argv.push(j)
} else if (jobs.toUpperCase() === 'MAX') {
argv.push('--jobs')
argv.push(require('os').cpus().length)
argv.push('-j' + j)
}
}
}

if (win) {
// did the user specify their own .sln file?
var hasSln = argv.some(function (arg) {
return path.extname(arg) == '.sln'
})
if (!hasSln) {
argv.unshift(gyp.opts.solution || guessedSolution)
// invoke vcvarsall.bat before build
if (win && vcDir) {
argv.unshift(arch === 'ia32'? 'x86' : arch, '&', command)
command = path.join(vcDir, 'vcvarsall.bat')
}
}

Expand Down
106 changes: 63 additions & 43 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@ var fs = require('graceful-fs')
, execFile = cp.execFile
, win = process.platform == 'win32'
, findNodeDirectory = require('./find-node-directory')
, msgFormat = require('util').format
, gypjs = undefined

exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
exports.usage = 'Generates ' + (process.env.npm_config_gypjs ? 'ninja build files' :
(win ? 'MSVC project files' : 'a Makefile')) + ' for the current module'

function configure (gyp, argv, callback) {

var python = gyp.opts.python || process.env.PYTHON || 'python2'
, buildDir = path.resolve('build')
, configNames = [ 'config.gypi', 'common.gypi' ]
, configs = []
, buildType
, arch
, nodeDir
, release = processRelease(argv, gyp, process.version, process.release)

findPython(python, function (err, found) {
if (err) {
callback(err)
} else {
python = found
getNodeDir()
if (!gyp.opts.gypjs) {
findPython(python, function (err, found) {
if (err) {
callback(err)
} else {
python = found
getNodeDir()
}
})
} else {
try {
gypjs = require('gyp.js')
} catch (err) {
return callback(new Error('Can\'t find module gyp.js, you can install it with `npm install gyp.js`'))
}
})
getNodeDir()
}

function getNodeDir () {

Expand Down Expand Up @@ -124,9 +136,10 @@ function configure (gyp, argv, callback) {
if (!defaults.default_configuration) {
defaults.default_configuration = 'Release'
}
buildType = defaults.default_configuration

// set the target_arch variable
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
variables.target_arch = arch = gyp.opts.arch || process.arch || 'ia32'

// set the node development directory
variables.nodedir = nodeDir
Expand Down Expand Up @@ -185,35 +198,37 @@ function configure (gyp, argv, callback) {
function runGyp (err) {
if (err) return callback(err)

if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
if (win) {
log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
// force the 'make' target for non-Windows
argv.push('-f', 'msvs')
} else {
log.verbose('gyp', 'gyp format was not specified; forcing "make"')
// force the 'make' target for non-Windows
argv.push('-f', 'make')
if (!gyp.opts.gypjs) {
if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
if (win) {
log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
// force the 'msvs' target for non-Windows
argv.push('-f', 'msvs')
} else {
log.verbose('gyp', 'gyp format was not specified; forcing "make"')
// force the 'make' target for non-Windows
argv.push('-f', 'make')
}
}
}

function hasMsvsVersion () {
return argv.some(function (arg) {
return arg.indexOf('msvs_version') === 0
})
}
function hasMsvsVersion () {
return argv.some(function (arg) {
return arg.indexOf('msvs_version') === 0
})
}

if (win && !hasMsvsVersion()) {
if ('msvs_version' in gyp.opts) {
argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
} else {
argv.push('-G', 'msvs_version=auto')
if (win && !hasMsvsVersion()) {
if ('msvs_version' in gyp.opts) {
argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
} else {
argv.push('-G', 'msvs_version=auto')
}
}
}

// include all the ".gypi" files that were found
configs.forEach(function (config) {
argv.push('-I', config)
argv.push('-I' + config)
})

// for AIX we need to set up the path to the exp file
Expand All @@ -237,9 +252,8 @@ function configure (gyp, argv, callback) {
if (node_exp_file !== undefined) {
log.verbose(logprefix, 'Found exports file: %s', node_exp_file)
} else {
var msg = msgFormat('Could not find node.exp file in %s', node_root_dir)
log.error(logprefix, 'Could not find exports file')
return callback(new Error(msg))
return callback(new Error('Could not find node.exp file in ' + node_root_dir))
}
}

Expand All @@ -258,11 +272,12 @@ function configure (gyp, argv, callback) {
}
var nodeGypDir = path.resolve(__dirname, '..')
var nodeLibFile = path.join(nodeDir,
!gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)',
!gyp.opts.nodedir ? '<(target_arch)' :
(gyp.opts.gypjs ? buildType : '$(Configuration)'),
release.name + '.lib')

argv.push('-I', addon_gypi)
argv.push('-I', common_gypi)
argv.push('-I' + addon_gypi)
argv.push('-I' + common_gypi)
argv.push('-Dlibrary=shared_library')
argv.push('-Dvisibility=default')
argv.push('-Dnode_root_dir=' + nodeDir)
Expand All @@ -284,15 +299,20 @@ function configure (gyp, argv, callback) {
// enforce use of the "binding.gyp" file
argv.unshift('binding.gyp')

// execute `gyp` from the current target nodedir
argv.unshift(gyp_script)
if (!gyp.opts.gypjs) {
// execute `gyp` from the current target nodedir
argv.unshift(gyp_script)

// make sure python uses files that came with this particular node package
var pypath = new PathArray(process.env, 'PYTHONPATH')
pypath.unshift(path.join(__dirname, '..', 'gyp', 'pylib'))
// make sure python uses files that came with this particular node package
var pypath = new PathArray(process.env, 'PYTHONPATH')
pypath.unshift(path.join(__dirname, '..', 'gyp', 'pylib'))

var cp = gyp.spawn(python, argv)
cp.on('exit', onCpExit)
var cp = gyp.spawn(python, argv)
cp.on('exit', onCpExit)
} else {
argv.push('-Dtarget_arch=' + arch)
onCpExit(gypjs.main(argv))
}
})
}

Expand Down
Loading

0 comments on commit 5b0a17b

Please sign in to comment.