-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_libffi.js
More file actions
executable file
·291 lines (263 loc) · 8.31 KB
/
Copy pathbuild_libffi.js
File metadata and controls
executable file
·291 lines (263 loc) · 8.31 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
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
#!/usr/bin/env node
const { spawnSync } = require("node:child_process");
const fs = require("node:fs/promises");
const path = require("node:path");
const archs = ["arm64", "x86_64"];
const buildTargets = [
{
dir: "build_iphoneos-arm64",
sdk: "iphoneos",
arch: "arm64",
host: "aarch64-apple-darwin13",
minVersionFlag: "-miphoneos-version-min=13.0",
},
{
dir: "build_iphonesimulator-x86_64",
sdk: "iphonesimulator",
arch: "x86_64",
host: "x86_64-apple-darwin13",
minVersionFlag: "-mios-simulator-version-min=13.0",
},
{
dir: "build_iphonesimulator-arm64",
sdk: "iphonesimulator",
arch: "arm64",
host: "aarch64-apple-darwin13",
minVersionFlag: "-mios-simulator-version-min=13.0",
},
{
dir: "build_macosx-x86_64",
sdk: "macosx",
arch: "x86_64",
host: "x86_64-apple-darwin13",
minVersionFlag: "-mmacosx-version-min=11.0",
},
{
dir: "build_macosx-arm64",
sdk: "macosx",
arch: "arm64",
host: "aarch64-apple-darwin13",
minVersionFlag: "-mmacosx-version-min=11.0",
},
{
dir: "build_xros-arm64",
sdk: "xros",
arch: "arm64",
host: "aarch64-apple-darwin13",
minVersionFlag: "-target arm64-apple-xros2.0",
},
{
dir: "build_xrsimulator-arm64",
sdk: "xrsimulator",
arch: "arm64",
host: "aarch64-apple-darwin13",
minVersionFlag: "-target arm64-apple-xros2.0-simulator",
},
];
async function main() {
const libffiDir = path.resolve(__dirname, "..", "libffi");
const libffiPath = (...parts) => path.join(libffiDir, ...parts);
const runtimeLibffiDir = path.resolve(__dirname, "..", "NativeScript", "libffi");
const env = {
...process.env,
CC: "clang",
CFLAGS: "-w",
};
try {
await fs.access(libffiPath("configure"));
} catch {
run("sh", ["./autogen.sh"], { cwd: libffiDir, env });
}
const skipGenerateSource =
Boolean(process.env.SKIP_GENERATE_SOURCE) ||
process.argv.includes("--skip-generate-source");
if (!skipGenerateSource) {
for (const target of buildTargets) {
await configureBuildTarget(target, libffiPath, env);
}
} else {
for (const target of buildTargets) {
const configurePath = libffiPath(target.dir, "config.status");
try {
await fs.access(configurePath);
} catch {
throw new Error(
`Missing ${configurePath}. Re-run without --skip-generate-source to configure build directories.`,
);
}
run("make", ["libffi_convenience.la"], {
cwd: libffiPath(target.dir),
env: buildTargetEnv(env, target),
});
}
}
await prepareDir(libffiPath("prebuilt", "iphoneos-arm64", "include"));
await fs.copyFile(
libffiPath("build_iphoneos-arm64", "include", "ffi.h"),
libffiPath("prebuilt", "iphoneos-arm64", "include", "ffi.h"),
);
await fs.copyFile(
libffiPath("build_iphoneos-arm64", "include", "ffitarget.h"),
libffiPath("prebuilt", "iphoneos-arm64", "include", "ffitarget.h"),
);
await fs.copyFile(
libffiPath("build_iphoneos-arm64", ".libs", "libffi_convenience.a"),
libffiPath("prebuilt", "iphoneos-arm64", "libffi.a"),
);
await prepareDir(libffiPath("prebuilt", "macosx-universal", "include"));
await combineHeaders("macosx", libffiPath);
run("lipo", [
"-create",
"-output",
"prebuilt/macosx-universal/libffi.a",
"build_macosx-x86_64/.libs/libffi_convenience.a",
"build_macosx-arm64/.libs/libffi_convenience.a",
], { cwd: libffiDir, env });
await prepareDir(
libffiPath("prebuilt", "iphonesimulator-universal", "include"),
);
run("lipo", [
"-create",
"-output",
"prebuilt/iphonesimulator-universal/libffi.a",
"build_iphonesimulator-x86_64/.libs/libffi_convenience.a",
"build_iphonesimulator-arm64/.libs/libffi_convenience.a",
], { cwd: libffiDir, env });
await combineHeaders("iphonesimulator", libffiPath);
await copySingleArchBuild({
buildDir: "build_xros-arm64",
outputDir: path.join("prebuilt", "xros-arm64"),
libffiPath,
});
await copySingleArchBuild({
buildDir: "build_xrsimulator-arm64",
outputDir: path.join("prebuilt", "xrsimulator-arm64"),
libffiPath,
});
await syncRuntimePrebuilts({
runtimeLibffiDir,
sourceLibffiPath: libffiPath,
targets: [
"iphoneos-arm64",
"iphonesimulator-universal",
"macosx-universal",
"xros-arm64",
"xrsimulator-arm64",
],
});
}
async function configureBuildTarget(target, libffiPath, baseEnv) {
const targetDir = libffiPath(target.dir);
await fs.rm(targetDir, { recursive: true, force: true });
await fs.mkdir(targetDir, { recursive: true });
const env = buildTargetEnv(baseEnv, target);
run("../configure", ["--disable-docs", "--disable-shared", "--host", target.host], {
cwd: targetDir,
env,
});
run("make", ["libffi_convenience.la"], { cwd: targetDir, env });
}
function buildTargetEnv(baseEnv, target) {
return {
...baseEnv,
CC: `xcrun -sdk ${target.sdk} clang -arch ${target.arch}`,
LD: `xcrun -sdk ${target.sdk} ld -arch ${target.arch}`,
CFLAGS: `${target.minVersionFlag} -w`,
// Old libffi asm trips Apple clang's CFI handling on modern toolchains.
// Force configure to skip CFI pseudo-op usage in generated config headers.
gcc_cv_as_cfi_pseudo_op: "no",
};
}
function run(command, args, options = {}) {
console.log(`$ ${command} ${args.join(" ")}`);
const result = spawnSync(command, args, {
stdio: "inherit",
env: options.env ?? process.env,
cwd: options.cwd,
});
if (result.status !== 0) {
throw new Error(
`Command failed: ${command} ${args.join(" ")} (exit code ${result.status})${options.cwd ? ` in ${options.cwd}` : ""}`,
);
}
}
async function prepareDir(includePath) {
const root = path.dirname(includePath);
await fs.rm(root, { recursive: true, force: true });
await fs.mkdir(includePath, { recursive: true });
}
async function syncRuntimePrebuilts({ runtimeLibffiDir, sourceLibffiPath, targets }) {
for (const target of targets) {
const destination = path.join(runtimeLibffiDir, target);
await fs.rm(destination, { recursive: true, force: true });
await fs.mkdir(path.dirname(destination), { recursive: true });
await fs.cp(sourceLibffiPath("prebuilt", target), destination, {
recursive: true,
});
}
}
async function copySingleArchBuild({ buildDir, outputDir, libffiPath }) {
await prepareDir(libffiPath(outputDir, "include"));
await fs.copyFile(
libffiPath(buildDir, "include", "ffi.h"),
libffiPath(outputDir, "include", "ffi.h"),
);
await fs.copyFile(
libffiPath(buildDir, "include", "ffitarget.h"),
libffiPath(outputDir, "include", "ffitarget.h"),
);
await fs.copyFile(
libffiPath(buildDir, ".libs", "libffi_convenience.a"),
libffiPath(outputDir, "libffi.a"),
);
}
async function combineHeaders(target, libffiPath) {
const ffi_h_arm64 = await fs.readFile(
libffiPath(`build_${target}-${archs[0]}`, "include", "ffi.h"),
"utf8",
);
const ffi_h_x86_64 = await fs.readFile(
libffiPath(`build_${target}-${archs[1]}`, "include", "ffi.h"),
"utf8",
);
const ffitarget_h_arm64 = await fs.readFile(
libffiPath(`build_${target}-${archs[0]}`, "include", "ffitarget.h"),
"utf8",
);
const ffitarget_h_x86_64 = await fs.readFile(
libffiPath(`build_${target}-${archs[1]}`, "include", "ffitarget.h"),
"utf8",
);
const ffi_h_universal = `// This file is generated by scripts/build_libffi.js
// Merged from build_${target}-${archs[0]}/include/ffi.h and build_${target}-${archs[1]}/include/ffi.h
#if defined(__aarch64__)
${ffi_h_arm64}
#elif defined(__x86_64__)
${ffi_h_x86_64}
#else
#error "Unsupported architecture"
#endif
`;
const ffitarget_h_universal = `// This file is generated by scripts/build_libffi.js
// Merged from build_${target}-${archs[0]}/include/ffitarget.h and build_${target}-${archs[1]}/include/ffitarget.h
#if defined(__aarch64__)
${ffitarget_h_arm64}
#elif defined(__x86_64__)
${ffitarget_h_x86_64}
#else
#error "Unsupported architecture"
#endif
`;
await fs.writeFile(
libffiPath("prebuilt", `${target}-universal`, "include", "ffi.h"),
ffi_h_universal,
);
await fs.writeFile(
libffiPath("prebuilt", `${target}-universal`, "include", "ffitarget.h"),
ffitarget_h_universal,
);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});