-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathcreate-config-gypi.js
More file actions
187 lines (160 loc) · 5.89 KB
/
create-config-gypi.js
File metadata and controls
187 lines (160 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict'
const fs = require('graceful-fs').promises
const log = require('./log')
const path = require('path')
function parseConfigGypi (config) {
// translated from tools/js2c.py of Node.js
// 1. string comments
config = config.replace(/#.*/g, '')
// 2. join multiline strings
config = config.replace(/'$\s+'/mg, '')
// 3. normalize string literals from ' into "
config = config.replace(/'/g, '"')
return JSON.parse(config)
}
// Variables that describe the build host (the machine running node-gyp), not
// the target Node binary. The official Node release headers tarball is a
// single universal artifact whose embedded config.gypi reflects the build
// farm host (currently Linux x64 / GCC), so when a consumer on a different
// platform inherits it verbatim via --disturl/--nodedir, these fields end up
// wrong. The most visible symptom on macOS arm64 is `clang=0` silently
// dropping the `clang==1` branches in common.gypi (e.g. `-std=gnu++20`),
// breaking node-addon-api compilation. See PR #2497 for the original code
// path and electron/rebuild#1209 for the first user report.
const HOST_SPECIFIC_VARIABLES = [
'host_arch',
'clang',
'llvm_version',
'xcode_version',
'arm_fpu',
'gas_version',
'shlib_suffix'
]
function overrideHostSpecificVariables (config) {
if (!config || !config.variables || !process.config || !process.config.variables) {
return config
}
for (const key of HOST_SPECIFIC_VARIABLES) {
const value = process.config.variables[key]
if (value !== undefined) {
config.variables[key] = value
} else {
delete config.variables[key]
}
}
return config
}
async function getBaseConfigGypi ({ gyp, nodeDir }) {
// try reading $nodeDir/include/node/config.gypi first when:
// 1. --dist-url or --nodedir is specified
// 2. and --force-process-config is not specified
const useCustomHeaders = gyp.opts.nodedir || gyp.opts.disturl || gyp.opts['dist-url']
const shouldReadConfigGypi = useCustomHeaders && !gyp.opts['force-process-config']
if (shouldReadConfigGypi && nodeDir) {
try {
const baseConfigGypiPath = path.resolve(nodeDir, 'include/node/config.gypi')
const baseConfigGypi = await fs.readFile(baseConfigGypiPath)
return overrideHostSpecificVariables(parseConfigGypi(baseConfigGypi.toString()))
} catch (err) {
log.warn('read config.gypi', err.message)
}
}
// fallback to process.config if it is invalid
return JSON.parse(JSON.stringify(process.config))
}
async function getCurrentConfigGypi ({ gyp, nodeDir, vsInfo, python }) {
const config = await getBaseConfigGypi({ gyp, nodeDir })
if (!config.target_defaults) {
config.target_defaults = {}
}
if (!config.variables) {
config.variables = {}
}
const defaults = config.target_defaults
const variables = config.variables
// don't inherit the "defaults" from the base config.gypi.
// 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 = []
// set the default_configuration prop
if ('debug' in gyp.opts) {
defaults.default_configuration = gyp.opts.debug ? 'Debug' : 'Release'
}
if (!defaults.default_configuration) {
defaults.default_configuration = 'Release'
}
// set the target_arch variable
variables.target_arch = gyp.opts.arch || process.arch || 'ia32'
if (variables.target_arch === 'arm64') {
defaults.msvs_configuration_platform = 'ARM64'
defaults.xcode_configuration_platform = 'arm64'
}
// set the node development directory
variables.nodedir = nodeDir
// set the configured Python path
variables.python = python
// disable -T "thin" static archives by default
variables.standalone_static_library = gyp.opts.thin ? 0 : 1
if (process.platform === 'win32') {
defaults.msbuild_toolset = vsInfo.toolset
if (vsInfo.sdk) {
defaults.msvs_windows_target_platform_version = vsInfo.sdk
}
if (variables.target_arch === 'arm64') {
if (vsInfo.versionMajor > 15 ||
(vsInfo.versionMajor === 15 && vsInfo.versionMajor >= 9)) {
defaults.msvs_enable_marmasm = 1
} else {
log.warn('Compiling ARM64 assembly is only available in\n' +
'Visual Studio 2017 version 15.9 and above')
}
}
variables.msbuild_path = vsInfo.msBuild
if (config.variables.clang === 1) {
config.variables.clang = 0
}
}
// loop through the rest of the opts and add the unknown ones as variables.
// this allows for module-specific configure flags like:
//
// $ node-gyp configure --shared-libxml2
Object.keys(gyp.opts).forEach(function (opt) {
if (opt === 'argv') {
return
}
if (opt in gyp.configDefs) {
return
}
variables[opt.replace(/-/g, '_')] = gyp.opts[opt]
})
return config
}
async function createConfigGypi ({ gyp, buildDir, nodeDir, vsInfo, python }) {
const configFilename = 'config.gypi'
const configPath = path.resolve(buildDir, configFilename)
log.verbose('build/' + configFilename, 'creating config file')
const config = await getCurrentConfigGypi({ gyp, nodeDir, vsInfo, python })
// ensures that any boolean values in config.gypi get stringified
function boolsToString (k, v) {
if (typeof v === 'boolean') {
return String(v)
}
return v
}
log.silly('build/' + configFilename, config)
// now write out the config.gypi file to the build/ dir
const prefix = '# Do not edit. File was generated by node-gyp\'s "configure" step'
const json = JSON.stringify(config, boolsToString, 2)
log.verbose('build/' + configFilename, 'writing out config file: %s', configPath)
await fs.writeFile(configPath, [prefix, json, ''].join('\n'))
return configPath
}
module.exports = {
createConfigGypi,
parseConfigGypi,
getCurrentConfigGypi
}