@flun/nodejs-mobile-gyp 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/configure.js CHANGED
@@ -1,295 +1,213 @@
1
- import gracefulFs from 'graceful-fs'
2
- import path from 'path'
3
- import * as log from './log.js'
4
- import os from 'os'
5
- import { processRelease } from './process-release.js'
6
- import { findNodeDirectory } from './find-node-directory.js'
7
- import { createConfigGypi } from './create-config-gypi.js'
8
- import { format as msgFormat } from 'util'
9
- import { findAccessibleSync } from './util.js'
10
- import { PythonFinder } from './find-python.js'
11
- import { fileURLToPath } from 'url'
12
-
13
- const fs = gracefulFs.promises
14
- const win = process.platform === 'win32'
15
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
1
+ import gracefulFs from 'graceful-fs';
2
+ import path from 'path';
3
+ import * as log from './log.js';
4
+ import os from 'os';
5
+ import { processRelease } from './process-release.js';
6
+ import { findNodeDirectory } from './find-node-directory.js';
7
+ import { createConfigGypi } from './create-config-gypi.js';
8
+ import { format as msgFormat } from 'util';
9
+ import { findAccessibleSync } from './util.js';
10
+ import { PythonFinder } from './find-python.js';
11
+ import { fileURLToPath } from 'url';
12
+
13
+ const fs = gracefulFs.promises, win = process.platform === 'win32', __dirname = path.dirname(fileURLToPath(import.meta.url));
16
14
 
17
15
  // 条件加载:仅在 Windows 上需要
18
- let findVisualStudio = null
16
+ let findVisualStudio = null, nodeDir;
19
17
  if (win) {
20
- const { VisualStudioFinder } = await import('./find-visualstudio.js')
21
- findVisualStudio = VisualStudioFinder.findVisualStudio
18
+ const { VisualStudioFinder } = await import('./find-visualstudio.js');
19
+ findVisualStudio = VisualStudioFinder.findVisualStudio;
22
20
  }
23
21
 
24
22
  /**
25
23
  * 生成构建文件(Makefile / MSVC 工程)
26
24
  */
27
25
  const configure = async (gyp, argv) => {
28
- const buildDir = path.resolve('build')
29
- const configNames = ['config.gypi', 'common.gypi']
30
- const configs = []
31
- let nodeDir
32
- const release = processRelease(argv, gyp, process.version, process.release)
33
-
34
- const python = await PythonFinder.findPython(gyp.opts.python)
35
- return getNodeDir()
36
-
37
- async function getNodeDir () {
38
- // 此时 python 已确定
39
- process.env.PYTHON = python
40
-
41
- if (gyp.opts.nodedir) {
42
- // 指定了 --nodedir,用它作为开发文件目录
43
- nodeDir = gyp.opts.nodedir.replace(/^~/, os.homedir())
44
- log.verbose('get node dir', 'compiling against specified --nodedir dev files: %s', nodeDir)
45
- } else {
46
- // 未指定 --nodedir,先确保 Node 依赖已安装
47
- if ('v' + release.version !== process.version) {
48
- // 指定了 --target,确定目标版本
49
- log.verbose('get node dir', 'compiling against --target node version: %s', release.version)
50
- } else {
51
- // 未指定 --target,使用当前主机 Node 版本
52
- log.verbose('get node dir', 'no --target version specified, falling back to host node version: %s', release.version)
26
+ const buildDir = path.resolve('build'), configNames = ['config.gypi', 'common.gypi'], configs = [],
27
+ release = processRelease(argv, gyp, process.version, process.release),
28
+ python = await PythonFinder.findPython(gyp.opts.python),
29
+ runGyp = async () => {
30
+ if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
31
+ if (win) {
32
+ log.verbose('gyp', 'gyp format was not specified; forcing "msvs"');
33
+ // 非 Windows 强制使用 make 目标
34
+ argv.push('-f', ('format' in gyp.opts) ? gyp.opts.format : 'msvs');
35
+ } else {
36
+ log.verbose('gyp', 'gyp format was not specified; forcing "make"');
37
+ // 非 Windows 强制使用 make 目标
38
+ argv.push('-f', ('format' in gyp.opts) ? gyp.opts.format : 'make');
39
+ }
53
40
  }
54
41
 
55
- if (!release.semver) {
56
- // 无法用 semver 解析版本字符串
57
- throw new Error('Invalid version number: ' + release.version)
42
+ configs.forEach(config => argv.push('-I', config)); // 引入所有找到的 .gypi 文件
43
+ // AIX 和 z/OS 需要设置导出文件路径,其中包含链接所需符号
44
+ let nodeExpFile, nodeRootDir, candidates, logprefix = 'find exports file';
45
+ if (process.platform === 'aix' || process.platform === 'os390' || process.platform === 'os400') {
46
+ const ext = process.platform === 'os390' ? 'x' : 'exp';
47
+ nodeRootDir = findNodeDirectory();
48
+
49
+ if (process.platform === 'aix' || process.platform === 'os400')
50
+ candidates = ['include/node/node', 'out/Release/node', 'out/Debug/node', 'node'].map(file => file + '.' + ext);
51
+ else
52
+ candidates = [
53
+ 'out/Release/lib.target/libnode', 'out/Debug/lib.target/libnode', 'out/Release/obj.target/libnode',
54
+ 'out/Debug/obj.target/libnode', 'lib/libnode'].map(file => file + '.' + ext);
55
+
56
+ nodeExpFile = findAccessibleSync(logprefix, nodeRootDir, candidates);
57
+ if (nodeExpFile !== undefined) log.verbose(logprefix, 'Found exports file: %s', nodeExpFile);
58
+ else {
59
+ const msg = msgFormat('Could not find node.%s file in %s', ext, nodeRootDir);
60
+ log.error(logprefix, 'Could not find exports file');
61
+ throw new Error(msg);
62
+ }
58
63
  }
59
64
 
60
- // 如果指定了 tarball 选项,总是删除并重装 headers 到 devdir,
61
- // 否则只在不存在时安装
62
- gyp.opts.ensure = !gyp.opts.tarball
63
-
64
- await gyp.commands.install([release.version])
65
-
66
- log.verbose('get node dir', 'target node version installed:', release.versionDir)
67
- nodeDir = path.resolve(gyp.devDir, release.versionDir)
68
- }
69
-
70
- return createBuildDir()
71
- }
72
-
73
- async function createBuildDir () {
74
- log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
75
-
76
- const isNew = await fs.mkdir(buildDir, { recursive: true })
77
- log.verbose(
78
- 'build dir', '"build" dir needed to be created?', isNew ? 'Yes' : 'No'
79
- )
80
- const vsInfo = win ? await findVisualStudio(release.semver, gyp.opts['msvs-version']) : null
81
- return createConfigFile(vsInfo)
82
- }
83
-
84
- async function createConfigFile (vsInfo) {
85
- if (win) {
86
- process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015)
87
- process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path
88
- }
89
- const configPath = await createConfigGypi({ gyp, buildDir, nodeDir, vsInfo, python })
90
- configs.push(configPath)
91
- return findConfigs()
92
- }
93
-
94
- async function findConfigs () {
95
- const name = configNames.shift()
96
- if (!name) {
97
- return runGyp()
98
- }
99
-
100
- const fullPath = path.resolve(name)
101
- log.verbose(name, 'checking for gypi file: %s', fullPath)
102
- try {
103
- await fs.stat(fullPath)
104
- log.verbose(name, 'found gypi file')
105
- configs.push(fullPath)
106
- } catch (err) {
107
- // ENOENT 时继续检查下一个 gypi 文件名
108
- if (err.code !== 'ENOENT') {
109
- throw err
65
+ // z/OS 需要设置 zoslib 头文件目录,其中包含 v8config.h 引用的头文件
66
+ let zoslibIncDir;
67
+ if (process.platform === 'os390') {
68
+ logprefix = "find zoslib's zos-base.h:";
69
+ let msg, zoslibIncPath = process.env.ZOSLIB_INCLUDES;
70
+ if (zoslibIncPath) {
71
+ zoslibIncPath = findAccessibleSync(logprefix, zoslibIncPath, ['zos-base.h']);
72
+ if (zoslibIncPath === undefined) {
73
+ msg = msgFormat('Could not find zos-base.h file in the directory set ' +
74
+ 'in ZOSLIB_INCLUDES environment variable: %s; set it ' +
75
+ 'to the correct path, or unset it to search %s', process.env.ZOSLIB_INCLUDES, nodeRootDir)
76
+ }
77
+ } else {
78
+ candidates = [
79
+ 'include/node/zoslib/zos-base.h', 'include/zoslib/zos-base.h', 'zoslib/include/zos-base.h',
80
+ 'install/include/node/zoslib/zos-base.h'
81
+ ]
82
+ zoslibIncPath = findAccessibleSync(logprefix, nodeRootDir, candidates);
83
+ if (zoslibIncPath === undefined) {
84
+ msg = msgFormat('Could not find any of %s in directory %s; set ' +
85
+ 'environmant variable ZOSLIB_INCLUDES to the path ' +
86
+ 'that contains zos-base.h', candidates.toString(), nodeRootDir)
87
+ }
88
+ }
89
+ if (zoslibIncPath !== undefined) {
90
+ zoslibIncDir = path.dirname(zoslibIncPath);
91
+ log.verbose(logprefix, "Found zoslib's zos-base.h in: %s", zoslibIncDir);
92
+ } else if (release.version.split('.')[0] >= 16) {
93
+ // zoslib 只随 Node v16 及以上发布
94
+ log.error(logprefix, msg);
95
+ throw new Error(msg);
96
+ }
110
97
  }
111
- }
112
-
113
- return findConfigs()
114
- }
115
98
 
116
- async function runGyp () {
117
- if (!~argv.indexOf('-f') && !~argv.indexOf('--format')) {
118
- if (win) {
119
- log.verbose('gyp', 'gyp format was not specified; forcing "msvs"')
120
- // 非 Windows 强制使用 make 目标
121
- argv.push('-f', ('format' in gyp.opts) ? gyp.opts.format : 'msvs')
122
- } else {
123
- log.verbose('gyp', 'gyp format was not specified; forcing "make"')
124
- // 非 Windows 强制使用 make 目标
125
- argv.push('-f', ('format' in gyp.opts) ? gyp.opts.format : 'make')
99
+ // 这段逻辑移植自旧的 `gyp_addon` Python 文件
100
+ const gypScript = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py'),
101
+ addonGypi = path.resolve(__dirname, '..', 'addon.gypi');
102
+ let commonGypi = path.resolve(nodeDir, 'include/node/common.gypi');
103
+ try {
104
+ await fs.stat(commonGypi);
105
+ } catch (err) {
106
+ commonGypi = path.resolve(nodeDir, 'common.gypi');
126
107
  }
127
- }
128
108
 
129
- // 引入所有找到的 .gypi 文件
130
- configs.forEach(function (config) {
131
- argv.push('-I', config)
132
- })
109
+ let outputDir = 'build';
110
+ if (win) outputDir = buildDir; // Windows 需要绝对路径
111
+ const nodeGypDir = path.resolve(__dirname, '..');
133
112
 
134
- // AIX 和 z/OS 需要设置导出文件路径,其中包含链接所需符号
135
- let nodeExpFile
136
- let nodeRootDir
137
- let candidates
138
- let logprefix = 'find exports file'
139
- if (process.platform === 'aix' || process.platform === 'os390' || process.platform === 'os400') {
140
- const ext = process.platform === 'os390' ? 'x' : 'exp'
141
- nodeRootDir = findNodeDirectory()
113
+ let nodeLibFile = path.join(nodeDir, !gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)', release.name + '.lib');
142
114
 
143
- if (process.platform === 'aix' || process.platform === 'os400') {
144
- candidates = [
145
- 'include/node/node',
146
- 'out/Release/node',
147
- 'out/Debug/node',
148
- 'node'
149
- ].map(function (file) {
150
- return file + '.' + ext
151
- })
152
- } else {
153
- candidates = [
154
- 'out/Release/lib.target/libnode',
155
- 'out/Debug/lib.target/libnode',
156
- 'out/Release/obj.target/libnode',
157
- 'out/Debug/obj.target/libnode',
158
- 'lib/libnode'
159
- ].map(function (file) {
160
- return file + '.' + ext
115
+ argv.push('-I', addonGypi), argv.push('-I', commonGypi), argv.push('-Dlibrary=shared_library');
116
+ argv.push('-Dvisibility=default'), argv.push('-Dnode_root_dir=' + nodeDir);
117
+ if (process.platform === 'aix' || process.platform === 'os390' || process.platform === 'os400') {
118
+ argv.push('-Dnode_exp_file=' + nodeExpFile);
119
+ if (process.platform === 'os390' && zoslibIncDir) argv.push('-Dzoslib_include_dir=' + zoslibIncDir);
120
+ }
121
+ argv.push('-Dnode_gyp_dir=' + nodeGypDir);
122
+
123
+ // 这样做是为了让 Cygwin 环境正常工作,否则未转义的 '\' 会被吃掉,
124
+ // 导致路径错误,如 c:parentFolderfolderanotherFolder 而非 c:\parentFolder\folder\anotherFolder
125
+ if (win) nodeLibFile = nodeLibFile.replace(/\\/g, '\\\\');
126
+ argv.push('-Dnode_lib_file=' + nodeLibFile), argv.push('-Dmodule_root_dir=' + process.cwd());
127
+ argv.push('-Dnode_engine=' + (gyp.opts.node_engine || process.jsEngine || 'v8'));
128
+ argv.push('--depth=.'), argv.push('--no-parallel');
129
+ argv.push('--generator-output', outputDir); // 告诉 gyp 把 Makefile/Solution 文件写到 output_dir
130
+ if (win) argv.push('-Gmsvs_toolset=v145');
131
+
132
+ argv.push('-Goutput_dir=.'); // 告诉 make 把输出写到同一目录
133
+ argv.unshift('binding.gyp'); // 强制使用 binding.gyp 文件
134
+ argv.unshift(gypScript); // 从当前目标 nodedir 执行 `gyp`
135
+
136
+ // 确保 python 使用该 node 包自带的文件
137
+ const pypath = [path.join(__dirname, '..', 'gyp', 'pylib')]
138
+ if (process.env.PYTHONPATH) pypath.push(process.env.PYTHONPATH);
139
+ process.env.PYTHONPATH = pypath.join(win ? ';' : ':');
140
+
141
+ await new Promise((resolve, reject) => {
142
+ const cp = gyp.spawn(python, argv);
143
+ cp.on('exit', code => {
144
+ if (code !== 0) reject(new Error('`gyp` failed with exit code: ' + code));
145
+ else resolve(); // 完成
161
146
  })
147
+ })
148
+ },
149
+ findConfigs = async () => {
150
+ const name = configNames.shift();
151
+ if (!name) return runGyp();
152
+
153
+ const fullPath = path.resolve(name);
154
+ log.verbose(name, 'checking for gypi file: %s', fullPath);
155
+ try {
156
+ await fs.stat(fullPath), log.verbose(name, 'found gypi file'), configs.push(fullPath);
157
+ } catch (err) {
158
+ if (err.code !== 'ENOENT') throw err; // ENOENT 时继续检查下一个 gypi 文件名
162
159
  }
163
160
 
164
- nodeExpFile = findAccessibleSync(logprefix, nodeRootDir, candidates)
165
- if (nodeExpFile !== undefined) {
166
- log.verbose(logprefix, 'Found exports file: %s', nodeExpFile)
167
- } else {
168
- const msg = msgFormat('Could not find node.%s file in %s', ext, nodeRootDir)
169
- log.error(logprefix, 'Could not find exports file')
170
- throw new Error(msg)
161
+ return findConfigs();
162
+ },
163
+ createConfigFile = async vsInfo => {
164
+ if (win) {
165
+ process.env.GYP_MSVS_VERSION = Math.min(vsInfo.versionYear, 2015);
166
+ process.env.GYP_MSVS_OVERRIDE_PATH = vsInfo.path;
171
167
  }
172
- }
173
-
174
- // z/OS 需要设置 zoslib 头文件目录,其中包含 v8config.h 引用的头文件
175
- let zoslibIncDir
176
- if (process.platform === 'os390') {
177
- logprefix = "find zoslib's zos-base.h:"
178
- let msg
179
- let zoslibIncPath = process.env.ZOSLIB_INCLUDES
180
- if (zoslibIncPath) {
181
- zoslibIncPath = findAccessibleSync(logprefix, zoslibIncPath, ['zos-base.h'])
182
- if (zoslibIncPath === undefined) {
183
- msg = msgFormat('Could not find zos-base.h file in the directory set ' +
184
- 'in ZOSLIB_INCLUDES environment variable: %s; set it ' +
185
- 'to the correct path, or unset it to search %s', process.env.ZOSLIB_INCLUDES, nodeRootDir)
186
- }
168
+ const configPath = await createConfigGypi({ gyp, buildDir, nodeDir, vsInfo, python });
169
+ configs.push(configPath);
170
+ return findConfigs();
171
+ },
172
+ createBuildDir = async () => {
173
+ log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir);
174
+
175
+ const isNew = await fs.mkdir(buildDir, { recursive: true });
176
+ log.verbose('build dir', '"build" dir needed to be created?', isNew ? 'Yes' : 'No');
177
+ const vsInfo = win ? await findVisualStudio(release.semver, gyp.opts['msvs-version']) : null;
178
+ return createConfigFile(vsInfo);
179
+ },
180
+ getNodeDir = async () => {
181
+ // 此时 python 已确定
182
+ process.env.PYTHON = python;
183
+
184
+ if (gyp.opts.nodedir) {
185
+ // 指定了 --nodedir,用它作为开发文件目录
186
+ nodeDir = gyp.opts.nodedir.replace(/^~/, os.homedir());
187
+ log.verbose('get node dir', 'compiling against specified --nodedir dev files: %s', nodeDir);
187
188
  } else {
188
- candidates = [
189
- 'include/node/zoslib/zos-base.h',
190
- 'include/zoslib/zos-base.h',
191
- 'zoslib/include/zos-base.h',
192
- 'install/include/node/zoslib/zos-base.h'
193
- ]
194
- zoslibIncPath = findAccessibleSync(logprefix, nodeRootDir, candidates)
195
- if (zoslibIncPath === undefined) {
196
- msg = msgFormat('Could not find any of %s in directory %s; set ' +
197
- 'environmant variable ZOSLIB_INCLUDES to the path ' +
198
- 'that contains zos-base.h', candidates.toString(), nodeRootDir)
189
+ // 未指定 --nodedir,先确保 Node 依赖已安装
190
+ if ('v' + release.version !== process.version) {
191
+ // 指定了 --target,确定目标版本
192
+ log.verbose('get node dir', 'compiling against --target node version: %s', release.version);
193
+ } else {
194
+ // 未指定 --target,使用当前主机 Node 版本
195
+ log.verbose('get node dir', 'no --target version specified, falling back to host node version: %s', release.version);
199
196
  }
200
- }
201
- if (zoslibIncPath !== undefined) {
202
- zoslibIncDir = path.dirname(zoslibIncPath)
203
- log.verbose(logprefix, "Found zoslib's zos-base.h in: %s", zoslibIncDir)
204
- } else if (release.version.split('.')[0] >= 16) {
205
- // zoslib 只随 Node v16 及以上发布
206
- log.error(logprefix, msg)
207
- throw new Error(msg)
208
- }
209
- }
210
-
211
- // 这段逻辑移植自旧的 `gyp_addon` Python 文件
212
- const gypScript = path.resolve(__dirname, '..', 'gyp', 'gyp_main.py')
213
- const addonGypi = path.resolve(__dirname, '..', 'addon.gypi')
214
- let commonGypi = path.resolve(nodeDir, 'include/node/common.gypi')
215
- try {
216
- await fs.stat(commonGypi)
217
- } catch (err) {
218
- commonGypi = path.resolve(nodeDir, 'common.gypi')
219
- }
220
-
221
- let outputDir = 'build'
222
- if (win) {
223
- // Windows 需要绝对路径
224
- outputDir = buildDir
225
- }
226
- const nodeGypDir = path.resolve(__dirname, '..')
227
197
 
228
- let nodeLibFile = path.join(nodeDir,
229
- !gyp.opts.nodedir ? '<(target_arch)' : '$(Configuration)',
230
- release.name + '.lib')
198
+ if (!release.semver) throw new Error('版本号无效: ' + release.version); // 无法用 semver 解析版本字符串
231
199
 
232
- argv.push('-I', addonGypi)
233
- argv.push('-I', commonGypi)
234
- argv.push('-Dlibrary=shared_library')
235
- argv.push('-Dvisibility=default')
236
- argv.push('-Dnode_root_dir=' + nodeDir)
237
- if (process.platform === 'aix' || process.platform === 'os390' || process.platform === 'os400') {
238
- argv.push('-Dnode_exp_file=' + nodeExpFile)
239
- if (process.platform === 'os390' && zoslibIncDir) {
240
- argv.push('-Dzoslib_include_dir=' + zoslibIncDir)
200
+ // 如果指定了 tarball 选项,总是删除并重装 headers 到 devdir,
201
+ // 否则只在不存在时安装
202
+ gyp.opts.ensure = !gyp.opts.tarball, await gyp.commands.install([release.version]);
203
+ log.verbose('get node dir', 'target node version installed:', release.versionDir);
204
+ nodeDir = path.resolve(gyp.devDir, release.versionDir);
241
205
  }
242
- }
243
- argv.push('-Dnode_gyp_dir=' + nodeGypDir)
244
-
245
- // 这样做是为了让 Cygwin 环境正常工作,否则未转义的 '\' 会被吃掉,
246
- // 导致路径错误,如 c:parentFolderfolderanotherFolder 而非 c:\parentFolder\folder\anotherFolder
247
- if (win) {
248
- nodeLibFile = nodeLibFile.replace(/\\/g, '\\\\')
249
- }
250
- argv.push('-Dnode_lib_file=' + nodeLibFile)
251
- argv.push('-Dmodule_root_dir=' + process.cwd())
252
- argv.push('-Dnode_engine=' +
253
- (gyp.opts.node_engine || process.jsEngine || 'v8'))
254
- argv.push('--depth=.')
255
- argv.push('--no-parallel')
256
-
257
- // 告诉 gyp 把 Makefile/Solution 文件写到 output_dir
258
- argv.push('--generator-output', outputDir)
259
- if (win) {
260
- argv.push('-Gmsvs_toolset=v145')
261
- }
262
-
263
- // 告诉 make 把输出写到同一目录
264
- argv.push('-Goutput_dir=.')
265
-
266
- // 强制使用 binding.gyp 文件
267
- argv.unshift('binding.gyp')
268
-
269
- // 从当前目标 nodedir 执行 `gyp`
270
- argv.unshift(gypScript)
271
-
272
- // 确保 python 使用该 node 包自带的文件
273
- const pypath = [path.join(__dirname, '..', 'gyp', 'pylib')]
274
- if (process.env.PYTHONPATH) {
275
- pypath.push(process.env.PYTHONPATH)
276
- }
277
- process.env.PYTHONPATH = pypath.join(win ? ';' : ':')
278
-
279
- await new Promise((resolve, reject) => {
280
- const cp = gyp.spawn(python, argv)
281
- cp.on('exit', (code) => {
282
- if (code !== 0) {
283
- reject(new Error('`gyp` failed with exit code: ' + code))
284
- } else {
285
- // 完成
286
- resolve()
287
- }
288
- })
289
- })
290
- }
291
- }
292
206
 
293
- configure.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
207
+ return createBuildDir();
208
+ };
209
+ return getNodeDir();
210
+ };
294
211
 
295
- export { configure }
212
+ configure.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module';
213
+ export { configure };