-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib-build
executable file
·293 lines (284 loc) · 9.4 KB
/
lib-build
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const crypto = require("crypto");
const {performance} = require('perf_hooks');
const CommandLine = require('./lib/CommandLine');
const CmakeBuild = require("./lib/CMake");
const Platform = require("./lib/Platform");
const Utils = require('./lib/Utils');
const LibraryTool = require("./lib/LibraryTool");
const optionDeclarations = [
{
name: "source",
shortName: "s",
type: "string",
description: "Specify the project source path."
},
{
name: "output",
shortName: "o",
type: "string",
description: "Specify the library output path."
},
{
name: "platform",
shortName: "p",
type: "string",
description: "Specify the current platform. Supported platforms: [\"win\", \"mac\", \"ios\", \"linux\", \"android\", \"web\", \"ohos\"]."
},
{
name: "arch",
shortName: "a",
type: "string",
description: "Builds the specified arch only. Supported arches: [\"x86\", \"x64\", \"arm\", \"arm64\", \"arm64-simulator\", \"wasm\", \"wasm-mt\"]. Ignored if --xcframework is specified."
},
{
name: "debug",
shortName: "d",
type: "boolean",
description: "Build with debug mode enabled."
},
{
name: "incremental",
shortName: "i",
type: "boolean",
description: "Uses incremental build. The build directory will not be removed after the building finished."
},
{
name: "xcframework",
shortName: "x",
type: "boolean",
description: "Merges all arches of the output libraries into one xcframework if current platform supports it."
},
{
name: "native",
shortName: "n",
type: "boolean",
description: "Use the native generator with cmake to build the library if current platform supports it. Default is true if --xcframework is specified."
},
{
name: "help",
shortName: "h",
type: "boolean",
description: "Print help message."
}
];
function printHelp(cmd) {
let output = "";
output += "Syntax: " + cmd + " [-Dcmake_variable=value]... [-Dcmake_variable=value] [options]\n";
output += "Examples: " + cmd + " -p ios -o ./out/ios\n";
output += "Examples: " + cmd + " --debug\n";
output += "Examples: " + cmd + " -DTGFX_USE_WEBP_ENCODE=ON -p mac\n";
output += CommandLine.printOptions(optionDeclarations);
Utils.log(output);
}
function getHash(options, cmakeArgs, platform) {
let list = [];
list.push(options.native ? "native" : "ninja");
list.push(options.debug ? "debug" : "release");
list.push(platform.name);
list.push(platform.toolVersion());
if (options.strip) {
list.push("strip");
}
list = list.concat(cmakeArgs);
let sourcePath = options.source;
let targetName = options.targets[0];
let hashConfigFile = path.resolve(sourcePath, targetName + ".hash");
let xcHashFiles = [];
if (fs.existsSync(hashConfigFile)) {
let hasConfig = Utils.readFile(hashConfigFile);
let files = hasConfig.split("\n");
for (let file of files) {
let filePath = path.resolve(sourcePath, file.trim());
let fileList = Utils.findFiles(filePath);
xcHashFiles = xcHashFiles.concat(fileList);
}
} else {
let shallowFile = path.resolve(sourcePath, ".git/shallow");
let commit = "";
if (fs.existsSync(shallowFile)) {
commit = Utils.readFile(shallowFile).trim();
} else {
commit = Utils.execSafe("git rev-parse HEAD", sourcePath).trim();
}
list.push(commit);
let diff = Utils.execSafe("git diff --name-only HEAD", sourcePath);
if (diff) {
let files = diff.split("\n");
for (let file of files) {
let filePath = path.resolve(sourcePath, file.trim());
xcHashFiles.push(filePath);
}
}
}
let hash = crypto.createHash('md5')
let content = list.join("\n");
hash.update(content)
for (let filePath of xcHashFiles) {
hash.update(filePath);
let fileContent = Utils.readFile(filePath);
if (fileContent) {
hash.update(fileContent);
}
}
return hash.digest('hex')
}
function buildArch(options, cmakeArgs, platform, libraryTool, versionHash, message) {
let targetName = options.targets[0];
let arch = platform.archs[0];
let sourcePath = options.source;
let outPath = options.output;
let xcHashFile = path.join(outPath, "." + targetName + "." + arch + ".md5");
let currentHash = Utils.readFile(xcHashFile);
if (versionHash === currentHash) {
return false;
}
Utils.deletePath(xcHashFile);
currentHash = versionHash;
let cmakeOptions = {};
cmakeOptions.targets = [targetName];
cmakeOptions.arguments = cmakeArgs;
cmakeOptions.incremental = true;
cmakeOptions.native = options.native;
cmakeOptions.symbols = options.strip;
if (message) {
Utils.log(message);
}
let cmake = CmakeBuild.Create(platform);
let libraryMap = cmake.build(sourcePath, outPath, cmakeOptions, platform.archs);
if (options.strip) {
let libraries = libraryMap[arch];
for (let library of libraries) {
let result = libraryTool.stripAndGenerateSymbolFile(library, arch);
if (!result) {
libraryTool.stripDebugSymbols(library, arch)
}
}
}
if (currentHash) {
Utils.writeFile(xcHashFile, currentHash);
}
return true;
}
let args = process.argv;
let execPath = path.resolve(process.argv[1]);
let isCustomBuild = execPath !== __filename;
let cmd = "node " + path.basename(args[1]);
args = args.slice(2);
let cmakeArgs = [];
let cmdArgs = [];
for (let arg of args) {
if (arg.indexOf("-D") === 0) {
cmakeArgs.push(arg);
} else {
cmdArgs.push(arg);
}
}
let options = CommandLine.parse(cmdArgs, optionDeclarations);
if (!options.source) {
if (isCustomBuild) {
options.source = path.dirname(execPath);
} else {
options.source = process.cwd();
}
} else {
options.source = path.resolve(options.source);
}
if ((!options.targets || options.targets.length === 0) && isCustomBuild) {
let basename = path.basename(process.argv[1]);
let target = "";
if (basename.endsWith("-build") || basename.endsWith("_build")) {
target = basename.substring(0, basename.length - 6);
} else if (basename.startsWith("build-") || basename.startsWith("build_")) {
target = basename.substring(6);
}
if (target) {
options.targets = [target];
}
}
if (!options.targets || options.targets.length !== 1) {
options.help = true;
}
if (options.help) {
printHelp(cmd);
if (options.errors.length > 0) {
process.exit(1);
}
return;
}
if (!options.debug) {
if (process.env["VENDOR_BUILD_TYPE"] === "Debug") {
options.debug = true;
}
}
let platform = Platform.Create(options.platform, options.debug, true);
let libraryTool = LibraryTool.Create(platform);
if (options.xcframework) {
options.native = true;
delete options.arch;
}
if (options.arch) {
platform.archs = [options.arch.toLowerCase()];
}
let targetName = options.targets[0];
let buildType = platform.debug ? "debug" : "release";
if (!options.output) {
options.output = path.resolve(options.source, "out", buildType, platform.name);
} else {
options.output = path.resolve(options.output);
}
if (!options.debug) {
options.strip = libraryTool.stripAndGenerateSymbolFile !== undefined;
}
let startMessage = "========================== build " + targetName + "-" + platform.name + "-" +
buildType + " start ==========================";
let startTime = performance.now();
startMessage += "\n[Source] " + options.source;
startMessage += "\n[Output] " + options.output;
let versionHash = getHash(options, cmakeArgs, platform);
let xcHashFile = path.join(options.output, "." + targetName + ".xcframework.md5");
let currentXCHash = "";
if (options.xcframework) {
currentXCHash = Utils.readFile(xcHashFile);
if (versionHash === currentXCHash) {
return;
}
Utils.deletePath(xcHashFile);
}
if (!options.incremental) {
Utils.deletePath(path.join(options.output, "build-" + targetName));
}
let archs = platform.archs;
for (let arch of archs) {
platform.archs = [arch];
if (buildArch(options, cmakeArgs, platform, libraryTool, versionHash, startMessage)) {
startMessage = "";
}
}
platform.archs = archs;
if (options.xcframework) {
if (startMessage) {
Utils.log(startMessage);
startMessage = "";
}
currentXCHash = versionHash;
libraryTool.createXCFramework(options.output, options.output, false, function (library) {
let ext = path.extname(library);
let name = path.parse(library).name;
return ext && name.indexOf(targetName) !== -1;
});
if (currentXCHash) {
Utils.writeFile(xcHashFile, currentXCHash);
}
}
if (!options.incremental) {
Utils.deletePath(path.join(options.output, "build-" + targetName));
}
let costTime = performance.now() - startTime;
let costTimeString = Utils.formatTime(costTime);
if (!startMessage) {
Utils.log("========================== build " + targetName + "-" + platform.name + "-" +
buildType + " end (" + costTimeString + ") ==========================");
}