Skip to content

Commit 5b0a17b

Browse files
committed
See issue #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.
1 parent 5eb0972 commit 5b0a17b

File tree

6 files changed

+172
-95
lines changed

6 files changed

+172
-95
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Command Options
181181
| `--python=$path` | Set path to the python (2) binary
182182
| `--msvs_version=$version` | Set Visual Studio version (win)
183183
| `--solution=$solution` | Set Visual Studio Solution version (win)
184+
| `--gypjs` | Use gyp.js instead of gyp
184185

185186

186187
License

gyp/gyp.bat

100755100644
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@rem Copyright (c) 2009 Google Inc. All rights reserved.
2-
@rem Use of this source code is governed by a BSD-style license that can be
3-
@rem found in the LICENSE file.
4-
5-
@python "%~dp0gyp_main.py" %*
1+
@rem Copyright (c) 2009 Google Inc. All rights reserved.
2+
@rem Use of this source code is governed by a BSD-style license that can be
3+
@rem found in the LICENSE file.
4+
5+
@python "%~dp0gyp_main.py" %*

gyp/samples/samples.bat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@rem Copyright (c) 2009 Google Inc. All rights reserved.
2-
@rem Use of this source code is governed by a BSD-style license that can be
3-
@rem found in the LICENSE file.
4-
5-
@python %~dp0/samples %*
1+
@rem Copyright (c) 2009 Google Inc. All rights reserved.
2+
@rem Use of this source code is governed by a BSD-style license that can be
3+
@rem found in the LICENSE file.
4+
5+
@python %~dp0/samples %*

lib/build.js

Lines changed: 94 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var fs = require('graceful-fs')
1515
, processRelease = require('./process-release')
1616
, win = process.platform == 'win32'
1717

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

2021
function build (gyp, argv, callback) {
2122
var platformMake = 'make'
@@ -35,7 +36,11 @@ function build (gyp, argv, callback) {
3536
, config
3637
, arch
3738
, nodeDir
39+
, vcDir
3840

41+
if (gyp.opts.gypjs) {
42+
command = gyp.opts.ninja || process.env.NINJA || 'ninja'
43+
}
3944
loadConfigGypi()
4045

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

7378
if (win) {
74-
findSolutionFile()
79+
gyp.opts.gypjs? findMSVS() : findSolutionFile()
7580
} else {
7681
doWhich()
7782
}
7883
})
7984
}
8085

86+
/**
87+
* On Windows, find Visual C++ toolset
88+
*/
89+
90+
function findMSVS () {
91+
var msvs_version = gyp.opts.msvs_version || 'auto'
92+
var vs_versions = (msvs_version === 'auto'? [14, 12, 10] : [msvs_version])
93+
vs_versions.find(function(version) {
94+
var vscomntools = process.env['VS' + version + '0COMNTOOLS']
95+
if (vscomntools) {
96+
// remove quotes to work with path.join()
97+
if (vscomntools.substr(0, 1) === '"' && vscomntools.substr(-1, 1) === '"') {
98+
vscomntools = vscomntools.substr(1, vscomntools.length - 2)
99+
}
100+
vcDir = path.join(vscomntools, '..', '..', 'VC')
101+
if (vcDir) {
102+
log.verbose('found Visual C++ in ', vcDir)
103+
return true
104+
}
105+
}
106+
})
107+
if (!vcDir) {
108+
callback(new Error('Visual C++ not found, please setup a C++ compiler toolset'))
109+
}
110+
doWhich()
111+
}
112+
81113
/**
82114
* On Windows, find the first build/*.sln file.
83115
*/
@@ -102,7 +134,7 @@ function build (gyp, argv, callback) {
102134
// First make sure we have the build command in the PATH
103135
which(command, function (err, execPath) {
104136
if (err) {
105-
if (win && /not found/.test(err.message)) {
137+
if (win && !gyp.opts.gypjs && /not found/.test(err.message)) {
106138
// On windows and no 'msbuild' found. Let's guess where it is
107139
findMsbuild()
108140
} else {
@@ -185,54 +217,74 @@ function build (gyp, argv, callback) {
185217

186218
// Enable Verbose build
187219
var verbose = log.levels[log.level] <= log.levels.verbose
188-
if (!win && verbose) {
189-
argv.push('V=1')
190-
}
191-
if (win && !verbose) {
192-
argv.push('/clp:Verbosity=minimal')
193-
}
220+
if (!gyp.opts.gypjs) {
221+
if (!win && verbose) {
222+
argv.push('V=1')
223+
}
224+
if (win && !verbose) {
225+
argv.push('/clp:Verbosity=minimal')
226+
}
194227

195-
if (win) {
196-
// Turn off the Microsoft logo on Windows
197-
argv.push('/nologo')
198-
}
228+
if (win) {
229+
// Turn off the Microsoft logo on Windows
230+
argv.push('/nologo')
231+
}
199232

200-
// Specify the build type, Release by default
201-
if (win) {
202-
var p = arch === 'x64' ? 'x64' : 'Win32'
203-
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
204-
if (jobs) {
205-
var j = parseInt(jobs, 10)
206-
if (!isNaN(j) && j > 0) {
207-
argv.push('/m:' + j)
208-
} else if (jobs.toUpperCase() === 'MAX') {
209-
argv.push('/m:' + require('os').cpus().length)
233+
// Specify the build type, Release by default
234+
if (win) {
235+
var p = arch === 'x64' ? 'x64' : 'Win32'
236+
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
237+
if (jobs) {
238+
var j = parseInt(jobs, 10)
239+
if (!isNaN(j) && j > 0) {
240+
argv.push('/m:' + j)
241+
} else if (jobs.toUpperCase() === 'MAX') {
242+
argv.push('/m:' + require('os').cpus().length)
243+
}
244+
}
245+
} else {
246+
argv.push('BUILDTYPE=' + buildType)
247+
// Invoke the Makefile in the 'build' dir.
248+
argv.push('-C')
249+
argv.push('build')
250+
if (jobs) {
251+
var j = parseInt(jobs, 10)
252+
if (!isNaN(j) && j > 0) {
253+
argv.push('--jobs')
254+
argv.push(j)
255+
} else if (jobs.toUpperCase() === 'MAX') {
256+
argv.push('--jobs')
257+
argv.push(require('os').cpus().length)
258+
}
259+
}
260+
}
261+
262+
if (win) {
263+
// did the user specify their own .sln file?
264+
var hasSln = argv.some(function (arg) {
265+
return path.extname(arg) == '.sln'
266+
})
267+
if (!hasSln) {
268+
argv.unshift(gyp.opts.solution || guessedSolution)
210269
}
211270
}
212271
} else {
213-
argv.push('BUILDTYPE=' + buildType)
214-
// Invoke the Makefile in the 'build' dir.
215-
argv.push('-C')
216-
argv.push('build')
272+
// build with ninja
273+
if (verbose) {
274+
argv.push('-v')
275+
}
276+
// Specify the build type, Release by default
277+
argv.push('-C', path.join('build', buildType))
217278
if (jobs) {
218-
var j = parseInt(jobs, 10)
279+
var j = jobs.toUpperCase() === 'MAX'? require('os').cpus().length : parseInt(jobs, 10)
219280
if (!isNaN(j) && j > 0) {
220-
argv.push('--jobs')
221-
argv.push(j)
222-
} else if (jobs.toUpperCase() === 'MAX') {
223-
argv.push('--jobs')
224-
argv.push(require('os').cpus().length)
281+
argv.push('-j' + j)
225282
}
226283
}
227-
}
228-
229-
if (win) {
230-
// did the user specify their own .sln file?
231-
var hasSln = argv.some(function (arg) {
232-
return path.extname(arg) == '.sln'
233-
})
234-
if (!hasSln) {
235-
argv.unshift(gyp.opts.solution || guessedSolution)
284+
// invoke vcvarsall.bat before build
285+
if (win && vcDir) {
286+
argv.unshift(arch === 'ia32'? 'x86' : arch, '&', command)
287+
command = path.join(vcDir, 'vcvarsall.bat')
236288
}
237289
}
238290

lib/configure.js

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,39 @@ var fs = require('graceful-fs')
2121
, execFile = cp.execFile
2222
, win = process.platform == 'win32'
2323
, findNodeDirectory = require('./find-node-directory')
24-
, msgFormat = require('util').format
24+
, gypjs = undefined
2525

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

2829
function configure (gyp, argv, callback) {
2930

3031
var python = gyp.opts.python || process.env.PYTHON || 'python2'
3132
, buildDir = path.resolve('build')
3233
, configNames = [ 'config.gypi', 'common.gypi' ]
3334
, configs = []
35+
, buildType
36+
, arch
3437
, nodeDir
3538
, release = processRelease(argv, gyp, process.version, process.release)
3639

37-
findPython(python, function (err, found) {
38-
if (err) {
39-
callback(err)
40-
} else {
41-
python = found
42-
getNodeDir()
40+
if (!gyp.opts.gypjs) {
41+
findPython(python, function (err, found) {
42+
if (err) {
43+
callback(err)
44+
} else {
45+
python = found
46+
getNodeDir()
47+
}
48+
})
49+
} else {
50+
try {
51+
gypjs = require('gyp.js')
52+
} catch (err) {
53+
return callback(new Error('Can\'t find module gyp.js, you can install it with `npm install gyp.js`'))
4354
}
44-
})
55+
getNodeDir()
56+
}
4557

4658
function getNodeDir () {
4759

@@ -124,9 +136,10 @@ function configure (gyp, argv, callback) {
124136
if (!defaults.default_configuration) {
125137
defaults.default_configuration = 'Release'
126138
}
139+
buildType = defaults.default_configuration
127140

128141
// set the target_arch variable
129-
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
142+
variables.target_arch = arch = gyp.opts.arch || process.arch || 'ia32'
130143

131144
// set the node development directory
132145
variables.nodedir = nodeDir
@@ -185,35 +198,37 @@ function configure (gyp, argv, callback) {
185198
function runGyp (err) {
186199
if (err) return callback(err)
187200

188-
if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
189-
if (win) {
190-
log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
191-
// force the 'make' target for non-Windows
192-
argv.push('-f', 'msvs')
193-
} else {
194-
log.verbose('gyp', 'gyp format was not specified; forcing "make"')
195-
// force the 'make' target for non-Windows
196-
argv.push('-f', 'make')
201+
if (!gyp.opts.gypjs) {
202+
if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
203+
if (win) {
204+
log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
205+
// force the 'msvs' target for non-Windows
206+
argv.push('-f', 'msvs')
207+
} else {
208+
log.verbose('gyp', 'gyp format was not specified; forcing "make"')
209+
// force the 'make' target for non-Windows
210+
argv.push('-f', 'make')
211+
}
197212
}
198-
}
199213

200-
function hasMsvsVersion () {
201-
return argv.some(function (arg) {
202-
return arg.indexOf('msvs_version') === 0
203-
})
204-
}
214+
function hasMsvsVersion () {
215+
return argv.some(function (arg) {
216+
return arg.indexOf('msvs_version') === 0
217+
})
218+
}
205219

206-
if (win && !hasMsvsVersion()) {
207-
if ('msvs_version' in gyp.opts) {
208-
argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
209-
} else {
210-
argv.push('-G', 'msvs_version=auto')
220+
if (win && !hasMsvsVersion()) {
221+
if ('msvs_version' in gyp.opts) {
222+
argv.push('-G', 'msvs_version=' + gyp.opts.msvs_version)
223+
} else {
224+
argv.push('-G', 'msvs_version=auto')
225+
}
211226
}
212227
}
213228

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

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

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

264-
argv.push('-I', addon_gypi)
265-
argv.push('-I', common_gypi)
279+
argv.push('-I' + addon_gypi)
280+
argv.push('-I' + common_gypi)
266281
argv.push('-Dlibrary=shared_library')
267282
argv.push('-Dvisibility=default')
268283
argv.push('-Dnode_root_dir=' + nodeDir)
@@ -284,15 +299,20 @@ function configure (gyp, argv, callback) {
284299
// enforce use of the "binding.gyp" file
285300
argv.unshift('binding.gyp')
286301

287-
// execute `gyp` from the current target nodedir
288-
argv.unshift(gyp_script)
302+
if (!gyp.opts.gypjs) {
303+
// execute `gyp` from the current target nodedir
304+
argv.unshift(gyp_script)
289305

290-
// make sure python uses files that came with this particular node package
291-
var pypath = new PathArray(process.env, 'PYTHONPATH')
292-
pypath.unshift(path.join(__dirname, '..', 'gyp', 'pylib'))
306+
// make sure python uses files that came with this particular node package
307+
var pypath = new PathArray(process.env, 'PYTHONPATH')
308+
pypath.unshift(path.join(__dirname, '..', 'gyp', 'pylib'))
293309

294-
var cp = gyp.spawn(python, argv)
295-
cp.on('exit', onCpExit)
310+
var cp = gyp.spawn(python, argv)
311+
cp.on('exit', onCpExit)
312+
} else {
313+
argv.push('-Dtarget_arch=' + arch)
314+
onCpExit(gypjs.main(argv))
315+
}
296316
})
297317
}
298318

0 commit comments

Comments
 (0)