forked from libpag/vendor_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMake.js
322 lines (301 loc) · 10.9 KB
/
CMake.js
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const fs = require('fs');
const path = require("path");
const Utils = require("./Utils");
const LibraryTool = require("./LibraryTool");
function CopyIncludes(sourcePath, buildPath, outPath, includes) {
const SourceKey = "${SOURCE_DIR}";
const BuildKey = "${BUILD_DIR}";
for (let filePath of includes) {
let inSource = true;
if (filePath.indexOf(SourceKey) === 0) {
filePath = filePath.substring(SourceKey.length);
} else if (filePath.indexOf(BuildKey) === 0) {
filePath = filePath.substring(BuildKey.length);
inSource = false;
}
let includeFile;
if (inSource) {
includeFile = path.join(sourcePath, filePath);
} else {
includeFile = path.join(buildPath, filePath);
}
let includeDir = path.dirname(includeFile);
let files = Utils.findFiles(includeFile);
for (let file of files) {
if (path.extname(file).toLowerCase() !== ".h") {
continue;
}
let relativePath = path.relative(includeDir, file);
Utils.copyPath(file, path.join(outPath, "include", relativePath));
}
}
}
function transformCMakeLists(configFile, platform) {
let configText = Utils.readFile(configFile);
let oldConfigText = configText;
if (configText.indexOf("install(") !== -1 || configText.indexOf("INSTALL(") !== -1) {
// disable all install()
const macro = "macro (install)\nendmacro ()\n";
if (configText.indexOf(macro) === 0) {
oldConfigText = configText.substring(macro.length);
} else {
configText = macro + configText;
}
}
if (platform === "web") {
const share = "# Disable error for the web platform.\nSET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS true)\n";
let index = oldConfigText.indexOf(share);
if (index !== -1) {
oldConfigText = oldConfigText.substring(0, index) + oldConfigText.substring(index + share.length);
} else {
let keys = ["project(", "project (", "PROJECT(", "PROJECT ("];
for (const key of keys) {
index = configText.indexOf(key);
if (index !== -1) {
break;
}
}
if (index !== -1) {
let tailText = configText.substring(index);
configText = configText.substring(0, index);
index = tailText.indexOf("\n");
configText += tailText.substring(0, index + 1) + share + tailText.substring(index + 1);
}
}
}
if (configText === oldConfigText) {
return "";
}
Utils.writeFile(configFile, configText);
return oldConfigText;
}
class CMake {
static Create(platform) {
if (platform.name === "ios") {
return new IOSCMake(platform);
}
if (platform.name === "android") {
return new AndroidCMake(platform);
}
if (platform.name === "win") {
return new WinCMake(platform);
}
if (platform.name === "mac") {
return new MacCMake(platform);
}
if (platform.name === "web") {
return new WebCMake(platform);
}
return new CMake(platform);
}
constructor(platform) {
this.platform = platform;
}
build(sourcePath, outPath, options) {
outPath = path.join(outPath, this.platform.name);
let buildPath = path.join(outPath, "build");
if (!options.incremental) {
Utils.deletePath(buildPath);
}
if (!fs.existsSync(buildPath)) {
Utils.createDirectory(buildPath);
}
let cmakeConfigFile = path.resolve(sourcePath, "CMakeLists.txt");
let oldCMakeConfig = transformCMakeLists(cmakeConfigFile, this.platform.name);
let libraries = {};
let archs = this.platform.archs;
for (let arch of archs) {
let cmakeArgs = this.getCMakeArgs(options.arguments, options.deps, sourcePath, arch);
let archBuildPath = path.join(buildPath, arch);
libraries[arch] = this.buildArch(sourcePath, archBuildPath, arch, options.targets, cmakeArgs);
}
if (oldCMakeConfig) {
Utils.writeFile(cmakeConfigFile, oldCMakeConfig);
}
let libraryTool = LibraryTool.Create(this.platform);
libraryTool.copyLibraries(libraries, outPath);
if (options.includes) {
let archBuildPath = path.join(buildPath, archs[0]);
CopyIncludes(sourcePath, archBuildPath, outPath, options.includes);
}
if (!options.incremental) {
Utils.deletePath(buildPath);
}
}
buildArch(sourcePath, buildPath, arch, targets, cmakeArgs) {
let buildType = this.platform.buildType;
Utils.log("Building the '" + arch + "' arch of [" + targets.join(", ") + "]:");
if (!fs.existsSync(buildPath)) {
Utils.createDirectory(buildPath);
}
Utils.log("cd " + buildPath);
let verbose = this.platform.verbose;
let verboseArg = verbose ? " -DCMAKE_VERBOSE_MAKEFILE=ON" : "";
let cmake = this.getCMake(arch);
let cmd = cmake + " -G Ninja -DCMAKE_BUILD_TYPE=" + buildType + verboseArg + " " +
cmakeArgs.join(" ") + " " + Utils.escapeSpace(sourcePath);
Utils.log(cmd);
Utils.exec(cmd, buildPath, !verbose);
let ninja = this.getNinja(arch);
let libraries = [];
for (let target of targets) {
cmd = ninja + " " + target;
Utils.log(cmd);
Utils.exec(cmd, buildPath, !verbose);
let found = this.findLibrary(target, buildPath, libraries);
if (!found) {
Utils.error("Could not find the library file matches '" + target + "'!");
process.exit(1);
}
}
return libraries;
}
getCMake(arch) {
let cmake = this.platform.getCommandPath("cmake");
return Utils.escapeSpace(cmake);
}
getNinja(arch) {
let ninja = this.platform.getCommandPath("ninja");
return Utils.escapeSpace(ninja);
}
getCMakeArgs(args, deps, sourcePath, arch) {
let cmakeArgs = this.getPlatformArgs(arch);
if (args) {
cmakeArgs = cmakeArgs.concat(args);
}
if (!deps) {
return cmakeArgs;
}
let keys = Object.keys(deps);
for (let key of keys) {
let depVendor = deps[key];
cmakeArgs.push("-DCMAKE_DISABLE_FIND_PACKAGE_" + key + "=TRUE");
cmakeArgs.push("-D" + key + "_FOUND=TRUE");
let includeDir = path.join(depVendor.out, this.platform.name, "include");
if (!fs.existsSync(includeDir)) {
includeDir = depVendor.source;
}
cmakeArgs.push("-D" + key + "_INCLUDE_DIR=\"" + includeDir + "\"");
cmakeArgs.push("-D" + key + "_INCLUDE_DIRS=\"" + includeDir + "\"");
let libraryPath = path.join(depVendor.out, this.platform.name, arch);
let libraries = [];
if (fs.existsSync(libraryPath)) {
libraries = LibraryTool.findLibraries(libraryPath);
} else {
libraries = LibraryTool.findLibraries(path.join(depVendor.out, this.platform.name));
}
if (libraries.length > 0) {
cmakeArgs.push("-D" + key + "_LIBRARY=\"" + libraries.join(";") + "\"");
cmakeArgs.push("-D" + key + "_LIBRARIES=\"" + libraries.join(";") + "\"");
}
}
return cmakeArgs;
}
getPlatformArgs(arch) {
return [];
}
findLibrary(target, dir, result) {
let libraries = LibraryTool.findLibraries(dir);
let found = false;
for (let library of libraries) {
let fileName = path.parse(library).name;
if (fileName.endsWith(target) && result.indexOf(library) === -1) {
result.push(library);
found = true;
// windows 平台若编译动态库,会产生两个新文件。
if (this.platform.name !== "win") {
return true;
}
}
}
if (found) {
return true;
}
// 可能是配置里设置了重命名,如果没匹配到,默认返回第一个发现的库文件。
for (let library of libraries) {
if (result.indexOf(library) === -1) {
result.push(library);
found = true;
break;
}
}
return found;
}
}
class AndroidCMake extends CMake {
getPlatformArgs(arch) {
let ndkHome = this.platform.ndkHome;
let abi = arch === "arm" ? "armeabi-v7a" : "arm64-v8a";
return [
"-DCMAKE_TOOLCHAIN_FILE=" + path.join(ndkHome, "build/cmake/android.toolchain.cmake"),
"-DANDROID_PLATFORM=android-18",
"-DANDROID_NDK=" + ndkHome,
"-DANDROID_ABI=" + abi
];
}
}
class IOSCMake extends CMake {
constructor(platform) {
super(platform);
this.toolchain = path.resolve(__dirname, "ios.toolchain.cmake");
}
getPlatformArgs(arch) {
let args = [
"-DCMAKE_TOOLCHAIN_FILE=" + this.toolchain,
"-DDEPLOYMENT_TARGET=9.0",
"-DENABLE_ARC=FALSE"
];
if (arch === "x64") {
args.push("-DPLATFORM=SIMULATOR64");
} else if (arch === "arm64-simulator") {
args.push("-DPLATFORM=SIMULATORARM64");
} else if (arch === "arm") {
args.push("-DPLATFORM=OS");
args.push("-DARCHS=armv7");
} else {
args.push("-DPLATFORM=OS64");
}
return args;
}
}
class WinCMake extends CMake {
getCMake(arch) {
let cmake = super.getCMake(arch);
return this.platform.prependVCVars(Utils.escapeSpace(cmake), arch);
}
getNinja(arch) {
let ninja = super.getNinja(arch);
return this.platform.prependVCVars(Utils.escapeSpace(ninja), arch);
}
getPlatformArgs(arch) {
return ["-DCMAKE_C_FLAGS_RELEASE=/Zc:inline"]
}
}
class MacCMake extends CMake {
constructor(platform) {
super(platform);
this.toolchain = path.resolve(__dirname, "ios.toolchain.cmake");
}
getPlatformArgs(arch) {
let args = [
"-DCMAKE_TOOLCHAIN_FILE=" + this.toolchain,
"-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13",
"-DENABLE_BITCODE=FALSE",
"-DENABLE_VISIBILITY=TRUE",
"-DENABLE_ARC=FALSE"
];
if (arch === "x64") {
args.push("-DPLATFORM=MAC");
} else if (arch === "arm64") {
args.push("-DPLATFORM=MAC_ARM64");
}
return args;
}
}
class WebCMake extends CMake {
getCMake(arch) {
let cmake = super.getCMake(arch);
return cmake + " cmake";
}
}
module.exports = CMake;