Skip to content

Commit f2ad87f

Browse files
zcbenzrvagg
authored andcommittedSep 10, 2021
chore: refactor the creation of config.gypi file
1 parent bc47cd6 commit f2ad87f

File tree

4 files changed

+162
-97
lines changed

4 files changed

+162
-97
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*.swp
12
gyp/test
23
node_modules
34
test/.node-gyp

‎lib/configure.js

+5-97
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const os = require('os')
77
const processRelease = require('./process-release')
88
const win = process.platform === 'win32'
99
const findNodeDirectory = require('./find-node-directory')
10+
const createConfigGypi = require('./create-config-gypi')
1011
const msgFormat = require('util').format
1112
var findPython = require('./find-python')
1213
if (win) {
@@ -92,107 +93,14 @@ function configure (gyp, argv, callback) {
9293
if (err) {
9394
return callback(err)
9495
}
95-
96-
var configFilename = 'config.gypi'
97-
var configPath = path.resolve(buildDir, configFilename)
98-
99-
log.verbose('build/' + configFilename, 'creating config file')
100-
101-
var config = process.config ? JSON.parse(JSON.stringify(process.config)) : {}
102-
var defaults = config.target_defaults
103-
var variables = config.variables
104-
105-
// default "config.variables"
106-
if (!variables) {
107-
variables = config.variables = {}
108-
}
109-
110-
// default "config.defaults"
111-
if (!defaults) {
112-
defaults = config.target_defaults = {}
113-
}
114-
115-
// don't inherit the "defaults" from node's `process.config` object.
116-
// doing so could cause problems in cases where the `node` executable was
117-
// compiled on a different machine (with different lib/include paths) than
118-
// the machine where the addon is being built to
119-
defaults.cflags = []
120-
defaults.defines = []
121-
defaults.include_dirs = []
122-
defaults.libraries = []
123-
124-
// set the default_configuration prop
125-
if ('debug' in gyp.opts) {
126-
defaults.default_configuration = gyp.opts.debug ? 'Debug' : 'Release'
127-
}
128-
129-
if (!defaults.default_configuration) {
130-
defaults.default_configuration = 'Release'
131-
}
132-
133-
// set the target_arch variable
134-
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
135-
if (variables.target_arch === 'arm64') {
136-
defaults.msvs_configuration_platform = 'ARM64'
137-
defaults.xcode_configuration_platform = 'arm64'
138-
}
139-
140-
// set the node development directory
141-
variables.nodedir = nodeDir
142-
143-
// disable -T "thin" static archives by default
144-
variables.standalone_static_library = gyp.opts.thin ? 0 : 1
145-
146-
if (win) {
96+
if (process.platform === 'win32') {
14797
process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015)
14898
process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path
149-
defaults.msbuild_toolset = vsInfo.toolset
150-
if (vsInfo.sdk) {
151-
defaults.msvs_windows_target_platform_version = vsInfo.sdk
152-
}
153-
if (variables.target_arch === 'arm64') {
154-
if (vsInfo.versionMajor > 15 ||
155-
(vsInfo.versionMajor === 15 && vsInfo.versionMajor >= 9)) {
156-
defaults.msvs_enable_marmasm = 1
157-
} else {
158-
log.warn('Compiling ARM64 assembly is only available in\n' +
159-
'Visual Studio 2017 version 15.9 and above')
160-
}
161-
}
162-
variables.msbuild_path = vsInfo.msBuild
16399
}
164-
165-
// loop through the rest of the opts and add the unknown ones as variables.
166-
// this allows for module-specific configure flags like:
167-
//
168-
// $ node-gyp configure --shared-libxml2
169-
Object.keys(gyp.opts).forEach(function (opt) {
170-
if (opt === 'argv') {
171-
return
172-
}
173-
if (opt in gyp.configDefs) {
174-
return
175-
}
176-
variables[opt.replace(/-/g, '_')] = gyp.opts[opt]
100+
createConfigGypi({ gyp, buildDir, nodeDir, vsInfo }, (err, configPath) => {
101+
configs.push(configPath)
102+
findConfigs(err)
177103
})
178-
179-
// ensures that any boolean values from `process.config` get stringified
180-
function boolsToString (k, v) {
181-
if (typeof v === 'boolean') {
182-
return String(v)
183-
}
184-
return v
185-
}
186-
187-
log.silly('build/' + configFilename, config)
188-
189-
// now write out the config.gypi file to the build/ dir
190-
var prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
191-
192-
var json = JSON.stringify(config, boolsToString, 2)
193-
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
194-
configs.push(configPath)
195-
fs.writeFile(configPath, [prefix, json, ''].join('\n'), findConfigs)
196104
}
197105

198106
function findConfigs (err) {

‎lib/create-config-gypi.js

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict'
2+
3+
const fs = require('graceful-fs')
4+
const log = require('npmlog')
5+
const path = require('path')
6+
7+
function getBaseConfigGypi () {
8+
const config = JSON.parse(JSON.stringify(process.config))
9+
if (!config.target_defaults) {
10+
config.target_defaults = {}
11+
}
12+
if (!config.variables) {
13+
config.variables = {}
14+
}
15+
return config
16+
}
17+
18+
function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo }) {
19+
const config = getBaseConfigGypi()
20+
const defaults = config.target_defaults
21+
const variables = config.variables
22+
23+
// don't inherit the "defaults" from the base config.gypi.
24+
// doing so could cause problems in cases where the `node` executable was
25+
// compiled on a different machine (with different lib/include paths) than
26+
// the machine where the addon is being built to
27+
defaults.cflags = []
28+
defaults.defines = []
29+
defaults.include_dirs = []
30+
defaults.libraries = []
31+
32+
// set the default_configuration prop
33+
if ('debug' in gyp.opts) {
34+
defaults.default_configuration = gyp.opts.debug ? 'Debug' : 'Release'
35+
}
36+
37+
if (!defaults.default_configuration) {
38+
defaults.default_configuration = 'Release'
39+
}
40+
41+
// set the target_arch variable
42+
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
43+
if (variables.target_arch === 'arm64') {
44+
defaults.msvs_configuration_platform = 'ARM64'
45+
defaults.xcode_configuration_platform = 'arm64'
46+
}
47+
48+
// set the node development directory
49+
variables.nodedir = nodeDir
50+
51+
// disable -T "thin" static archives by default
52+
variables.standalone_static_library = gyp.opts.thin ? 0 : 1
53+
54+
if (process.platform === 'win32') {
55+
defaults.msbuild_toolset = vsInfo.toolset
56+
if (vsInfo.sdk) {
57+
defaults.msvs_windows_target_platform_version = vsInfo.sdk
58+
}
59+
if (variables.target_arch === 'arm64') {
60+
if (vsInfo.versionMajor > 15 ||
61+
(vsInfo.versionMajor === 15 && vsInfo.versionMajor >= 9)) {
62+
defaults.msvs_enable_marmasm = 1
63+
} else {
64+
log.warn('Compiling ARM64 assembly is only available in\n' +
65+
'Visual Studio 2017 version 15.9 and above')
66+
}
67+
}
68+
variables.msbuild_path = vsInfo.msBuild
69+
}
70+
71+
// loop through the rest of the opts and add the unknown ones as variables.
72+
// this allows for module-specific configure flags like:
73+
//
74+
// $ node-gyp configure --shared-libxml2
75+
Object.keys(gyp.opts).forEach(function (opt) {
76+
if (opt === 'argv') {
77+
return
78+
}
79+
if (opt in gyp.configDefs) {
80+
return
81+
}
82+
variables[opt.replace(/-/g, '_')] = gyp.opts[opt]
83+
})
84+
85+
return config
86+
}
87+
88+
function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo }, callback) {
89+
const configFilename = 'config.gypi'
90+
const configPath = path.resolve(buildDir, configFilename)
91+
92+
log.verbose('build/' + configFilename, 'creating config file')
93+
94+
const config = getCurrentConfigGypi({ gyp, nodeDir, vsInfo })
95+
96+
// ensures that any boolean values in config.gypi get stringified
97+
function boolsToString (k, v) {
98+
if (typeof v === 'boolean') {
99+
return String(v)
100+
}
101+
return v
102+
}
103+
104+
log.silly('build/' + configFilename, config)
105+
106+
// now write out the config.gypi file to the build/ dir
107+
const prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
108+
109+
const json = JSON.stringify(config, boolsToString, 2)
110+
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
111+
fs.writeFile(configPath, [prefix, json, ''].join('\n'), (err) => {
112+
callback(err, configPath)
113+
})
114+
}
115+
116+
module.exports = createConfigGypi
117+
module.exports.test = {
118+
getCurrentConfigGypi: getCurrentConfigGypi
119+
}

‎test/test-create-config-gypi.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const gyp = require('../lib/node-gyp')
5+
const createConfigGypi = require('../lib/create-config-gypi')
6+
const { getCurrentConfigGypi } = createConfigGypi.test
7+
8+
test('config.gypi with no options', function (t) {
9+
t.plan(2)
10+
11+
const prog = gyp()
12+
prog.parseArgv([])
13+
14+
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
15+
t.equal(config.target_defaults.default_configuration, 'Release')
16+
t.equal(config.variables.target_arch, process.arch)
17+
})
18+
19+
test('config.gypi with --debug', function (t) {
20+
t.plan(1)
21+
22+
const prog = gyp()
23+
prog.parseArgv(['_', '_', '--debug'])
24+
25+
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
26+
t.equal(config.target_defaults.default_configuration, 'Debug')
27+
})
28+
29+
test('config.gypi with custom options', function (t) {
30+
t.plan(1)
31+
32+
const prog = gyp()
33+
prog.parseArgv(['_', '_', '--shared-libxml2'])
34+
35+
const config = getCurrentConfigGypi({ gyp: prog, vsInfo: {} })
36+
t.equal(config.variables.shared_libxml2, true)
37+
})

0 commit comments

Comments
 (0)
Please sign in to comment.