forked from libpag/vendor_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatform.js
executable file
·189 lines (174 loc) · 5.99 KB
/
Platform.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
const fs = require('fs');
const path = require("path");
const os = require("os");
const childProcess = require("child_process");
const Utils = require("./Utils");
class Platform {
static Create(name, debug, verbose) {
if (name === "android") {
return new AndroidPlatform(debug, verbose);
}
if (name === "win") {
return new WinPlatform(debug, verbose);
}
if (name === "ios") {
return new Platform(name, ["arm", "arm64", "x64", "arm64-simulator"], debug, verbose);
}
if (name === "mac") {
return new Platform(name, ["arm64", "x64"], debug, verbose);
}
if (name === "web") {
return new WebPlatform(name, ["wasm"], debug, verbose);
}
return new Platform(name, ["x64"], debug, verbose);
}
constructor(name, archs, debug, verbose) {
this.name = name;
this.archs = archs;
this.debug = debug;
this.verbose = verbose;
let p = os.platform();
if (p === "darwin") {
this.hostToolPath = path.join(__dirname, "mac");
} else if (p === "win32") {
this.hostToolPath = path.join(__dirname, "win");
} else if (p === "linux") {
this.hostToolPath = path.join(__dirname, "linux");
}
}
toString() {
return this.name;
}
get buildType() {
return this.debug ? "Debug" : "Release";
}
getCommandPath(cmd, arch) {
let cmdPath = path.join(this.hostToolPath, cmd);
if (this.name === "win" && path.extname(cmd) === "") {
cmdPath += ".exe";
}
if (fs.existsSync(cmdPath)) {
return cmdPath;
}
return cmd;
}
}
function FindNDK() {
const NDK_ENVS = ["NDK_HOME", "NDK_PATH", "ANDROID_NDK_HOME", "ANDROID_NDK"];
for (let env of NDK_ENVS) {
let NDK = process.env[env];
if (NDK && fs.existsSync(path.join(NDK, "ndk-build"))) {
return NDK;
}
}
let HOME = process.env["HOME"];
let ANDROID_HOME = path.join(HOME, "Library/Android/sdk");
let NDK_HOME = path.join(ANDROID_HOME, "ndk-bundle");
if (fs.existsSync(path.join(NDK_HOME, "ndk-build"))) {
return NDK_HOME;
}
let NDK_ROOT = path.join(ANDROID_HOME, "ndk");
if (fs.existsSync(NDK_ROOT)) {
let files = fs.readdirSync(NDK_ROOT);
for (let fileName of files) {
NDK_HOME = path.join(NDK_ROOT, fileName)
if (fs.existsSync(path.join(NDK_HOME, "ndk-build"))) {
return NDK_HOME;
}
}
}
return "";
}
class AndroidPlatform extends Platform {
constructor(debug, verbose) {
super("android", ["arm", "arm64"], debug, verbose);
this.ndkHome = FindNDK();
if (!this.ndkHome) {
Utils.error("Could not find NDK_HOME!");
process.exit(1);
}
}
getCommandPath(cmd, arch) {
let prefix = arch === "arm64" ? "aarch64-linux-android-" : "arm-linux-androideabi-";
let cmdPath = path.join(this.ndkHome, "toolchains/llvm/prebuilt/darwin-x86_64/bin/" + prefix + cmd);
if (fs.existsSync(cmdPath)) {
return cmdPath;
}
return super.getCommandPath(cmd, arch);
}
}
function findMSVCNativeToolCommand() {
let programDataPath = childProcess.execSync("echo %ProgramData%").toString();
programDataPath = programDataPath.substring(0, programDataPath.length - 2);
let startMenuPath = path.join(programDataPath, "Microsoft/Windows/Start Menu/Programs");
if (!fs.existsSync(startMenuPath)) {
return "";
}
let files = fs.readdirSync(startMenuPath);
for (let fileName of files) {
if (fileName.indexOf("Visual Studio") === -1) {
continue;
}
let vsPath = path.join(startMenuPath, fileName);
let vsFiles = Utils.findFiles(vsPath);
for (let vsFile of vsFiles) {
if (vsFile.indexOf("x64 Native Tools Command Prompt for VS") !== -1) {
return vsFile;
}
}
}
return "";
}
class WinPlatform extends Platform {
constructor(debug, verbose) {
super("win", ["x64", "x86"], debug, verbose);
let msvcTool = findMSVCNativeToolCommand();
let buildToolPath = childProcess.execSync("\"" + msvcTool + "\"").toString();
let lines = buildToolPath.split("\r\n");
buildToolPath = lines[lines.length - 1];
this.buildToolPath = buildToolPath.substring(0, buildToolPath.length - 1);
let vcvars64Path = path.join(this.buildToolPath, "VC/Auxiliary/Build/vcvars64.bat");
if (fs.existsSync(vcvars64Path)) {
this.vcvars64Path = vcvars64Path;
}
let vcvars32Path = path.join(this.buildToolPath, "VC/Auxiliary/Build/vcvars32.bat");
if (fs.existsSync(vcvars32Path)) {
this.vcvars32Path = vcvars32Path;
}
}
getCommandPath(cmd, arch) {
let cmdPath = super.getCommandPath(cmd, arch);
if (cmdPath !== cmd) {
return cmdPath;
}
let vcVarsPath = arch === "x86" ? this.vcvars32Path : this.vcvars64Path;
if (!vcVarsPath) {
return cmd;
}
let result = childProcess.execSync("\"" + vcVarsPath + "\"&where " + cmd).toString();
let lines = result.split("\r\n");
for (let i = lines.length - 1; i >= 0; i--) {
let line = lines[i];
if (line.indexOf(cmd) !== -1) {
return line;
}
}
return cmd;
}
prependVCVars(cmd, arch) {
let vcVarsPath = arch === "x86" ? this.vcvars32Path : this.vcvars64Path;
if (!vcVarsPath) {
return cmd;
}
return Utils.escapeSpace(vcVarsPath) + "&" + cmd;
}
}
class WebPlatform extends Platform {
getCommandPath(cmd, arch) {
if (cmd === "ninja") {
return super.getCommandPath(cmd, arch);
}
return super.getCommandPath("em" + cmd, arch);
}
}
module.exports = Platform;