Skip to content

Commit

Permalink
Simplify logic in getModulesVersion() function
Browse files Browse the repository at this point in the history
Use value of release.semver to determinate file name and location with NODE_MODULE_VERSION define,
and to read this file depending on target Node.js version.

Tested with Node.js versions: 0.8.4, 0.10.25, 0.11.4, 0.11.5, 0.12.0, iojs-1.0.0, iojs-2.0.0, iojs-3.0.0, 4.X, 5.X

Added verbose log messages for the target_arch, target_platform, and target_modules_version variables.
  • Loading branch information
pmed committed Jan 8, 2016
1 parent 3ecec4b commit 44f04b2
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,35 @@ function configure (gyp, argv, callback) {

function getModulesVersion(nodeDir) {

var sources = [
path.join(nodeDir, 'include/node/node_version.h'),
path.join(nodeDir, 'src/node_version.h'),
]
var name, source, version

// NODE_MODULE_VERSION is defined in different files
// depending on the target Node.js version
if (semver.gte(release.semver, '3.0.0')) {
name = 'include/node/node_version.h'
} else if (semver.gt(release.semver, '0.11.4')) {
name = 'src/node_version.h'
} else {
name = 'src/node.h'
}
name = path.join(nodeDir, name)

for (var i = 0; i < sources.length; ++i) {
try {
var source = fs.readFileSync(sources[i], { encoding: 'utf8'})
var version = source.match(/^#define\s+NODE_MODULE_VERSION\s+(\d+)/m)
if (!version) {
err = new Error("can't find #define NODE_MODULE_VERSION in " + sources[i]);
callback(err);
}
return parseInt(version[1], 10);
}
catch (e) {}
try {
source = fs.readFileSync(name, { encoding: 'utf8'})
} catch (e) {
err = e
callback(err)
return undefined
}

version = source.match(/#define\s+NODE_MODULE_VERSION\s+\(?(\S+)\)?/)
if (version === null) {
err = new Error('#define NODE_MODULE_VERSION not found in ' + name)
callback(err)
return undefined
}
err = new Error("can't read node_version.h in " + nodeDir)
callback(err);
version = parseInt(version[1])
return version
}

function createBuildDir () {
Expand Down Expand Up @@ -149,12 +159,15 @@ function configure (gyp, argv, callback) {

// set the target_arch variable
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
log.verbose('build/' + configFilename, 'target arch: ' + variables.target_arch)

// set the target_platform variable
variables.target_platform = process.platform
log.verbose('build/' + configFilename, 'target platform: ' + variables.target_platform)

// set the target modules version
variables.target_modules_version = getModulesVersion(nodeDir)
log.verbose('build/' + configFilename, 'modules version: ' + variables.target_modules_version)

// set the node development directory
variables.nodedir = nodeDir
Expand Down

0 comments on commit 44f04b2

Please sign in to comment.