Skip to content

Commit

Permalink
Use post_config.gypi to use shared libraries
Browse files Browse the repository at this point in the history
Node can be compiled with external shared libraries;
in this case append their compile and linker flags last.
  • Loading branch information
saper committed Jun 21, 2019
1 parent d3478d7 commit ef57808
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function configure (gyp, argv, callback) {
, buildDir = path.resolve('build')
, configNames = [ 'config.gypi', 'common.gypi' ]
, configs = []
, post_configs = []
, nodeDir
, release = processRelease(argv, gyp, process.version, process.release)

Expand Down Expand Up @@ -97,27 +98,42 @@ function configure (gyp, argv, callback) {

var configFilename = 'config.gypi'
var configPath = path.resolve(buildDir, configFilename)
var postConfigFilename = 'post_config.gypi'
var postConfigPath = path.resolve(buildDir, postConfigFilename)

log.verbose('build/' + configFilename, 'creating config file')

var config = process.config || {}
, defaults = config.target_defaults
, variables = config.variables
, fallback_target_defaults = {}
, post_variables = ['cflags', 'defines', 'include_dirs', 'libraries']

// default "config.variables"
if (!variables) variables = config.variables = {}

// default "config.defaults"
if (!defaults) defaults = config.target_defaults = {}

// don't inherit the "defaults" from node's `process.config` object.
if ( variables.node_shared_cares ||
variables.node_shared_http_parser ||
variables.node_shared_libuv ||
variables.node_shared_openssl ||
variables.node_shared_zlib ) {
Object.keys(defaults).forEach(function (entry) {
if (post_variables.indexOf(entry) > 0) {
fallback_target_defaults[entry] = defaults[entry]
}
})
}
// if node engine does not have to rely on shared libraries in the system
// do not inherit the "defaults" from node's `process.config` object.
// doing so could cause problems in cases where the `node` executable was
// compiled on a different machine (with different lib/include paths) than
// the machine where the addon is being built to
defaults.cflags = []
defaults.defines = []
defaults.include_dirs = []
defaults.libraries = []
post_variables.forEach(function (post_var) {
defaults[post_var] = []
})

// set the default_configuration prop
if ('debug' in gyp.opts) {
Expand Down Expand Up @@ -182,7 +198,17 @@ function configure (gyp, argv, callback) {
, json = JSON.stringify(config, boolsToString, 2)
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
configs.push(configPath)
fs.writeFile(configPath, [prefix, json, ''].join('\n'), findConfigs)
fs.writeFile(configPath, [prefix, json, ''].join('\n'), function() {

var config = {
"target_defaults": fallback_target_defaults
}
, prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
, json = JSON.stringify(config, boolsToString, 2)
log.verbose('build/' + postConfigFilename, 'writing out config file: %s', postConfigPath)
post_configs.push(postConfigPath)
fs.writeFile(postConfigPath, [prefix, json, ''].join('\n'), findConfigs)
})
}

function findConfigs (err) {
Expand Down Expand Up @@ -280,6 +306,9 @@ function configure (gyp, argv, callback) {

argv.push('-I', addon_gypi)
argv.push('-I', common_gypi)
post_configs.forEach(function (config) {
argv.push('-I', config)
})
argv.push('-Dlibrary=shared_library')
argv.push('-Dvisibility=default')
argv.push('-Dnode_root_dir=' + nodeDir)
Expand Down

0 comments on commit ef57808

Please sign in to comment.