-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapple.ts
More file actions
201 lines (185 loc) · 5.78 KB
/
apple.ts
File metadata and controls
201 lines (185 loc) · 5.78 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
import assert from "node:assert/strict";
import path from "node:path";
import fs from "node:fs";
import {
Option,
oraPromise,
prettyPath,
} from "@react-native-node-api/cli-utils";
import {
AppleTriplet as Triplet,
createAppleFramework,
createXCframework,
} from "react-native-node-api";
import type { Platform } from "./types.js";
import * as cmakeFileApi from "cmake-file-api";
type XcodeSDKName =
| "iphoneos"
| "iphonesimulator"
| "catalyst"
| "xros"
| "xrsimulator"
| "appletvos"
| "appletvsimulator"
| "macosx";
const XCODE_SDK_NAMES = {
"x86_64-apple-darwin": "macosx",
"arm64-apple-darwin": "macosx",
"arm64;x86_64-apple-darwin": "macosx",
"arm64-apple-ios": "iphoneos",
"arm64-apple-ios-sim": "iphonesimulator",
"arm64-apple-tvos": "appletvos",
// "x86_64-apple-tvos": "appletvos",
"arm64-apple-tvos-sim": "appletvsimulator",
"arm64-apple-visionos": "xros",
"arm64-apple-visionos-sim": "xrsimulator",
} satisfies Record<Triplet, XcodeSDKName>;
type CMakeSystemName = "Darwin" | "iOS" | "tvOS" | "watchOS" | "visionOS";
const CMAKE_SYSTEM_NAMES = {
"x86_64-apple-darwin": "Darwin",
"arm64-apple-darwin": "Darwin",
"arm64;x86_64-apple-darwin": "Darwin",
"arm64-apple-ios": "iOS",
"arm64-apple-ios-sim": "iOS",
"arm64-apple-tvos": "tvOS",
// "x86_64-apple-tvos": "appletvos",
"arm64-apple-tvos-sim": "tvOS",
"arm64-apple-visionos": "visionOS",
"arm64-apple-visionos-sim": "visionOS",
} satisfies Record<Triplet, CMakeSystemName>;
type AppleArchitecture = "arm64" | "x86_64" | "arm64;x86_64";
export const APPLE_ARCHITECTURES = {
"x86_64-apple-darwin": "x86_64",
"arm64-apple-darwin": "arm64",
"arm64;x86_64-apple-darwin": "arm64;x86_64",
"arm64-apple-ios": "arm64",
"arm64-apple-ios-sim": "arm64",
"arm64-apple-tvos": "arm64",
// "x86_64-apple-tvos": "x86_64",
"arm64-apple-tvos-sim": "arm64",
"arm64-apple-visionos": "arm64",
"arm64-apple-visionos-sim": "arm64",
} satisfies Record<Triplet, AppleArchitecture>;
export function createPlistContent(values: Record<string, string>) {
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
'<plist version="1.0">',
"<dict>",
...Object.entries(values).flatMap(([key, value]) => [
`<key>${key}</key>`,
`<string>${value}</string>`,
]),
"</dict>",
"</plist>",
].join("\n");
}
export function getAppleBuildArgs() {
// We expect the final application to sign these binaries
return ["CODE_SIGNING_ALLOWED=NO"];
}
const xcframeworkExtensionOption = new Option(
"--xcframework-extension",
"Don't rename the xcframework to .apple.node",
).default(false);
type AppleOpts = {
xcframeworkExtension: boolean;
};
export const platform: Platform<Triplet[], AppleOpts> = {
id: "apple",
name: "Apple",
triplets: [
"arm64;x86_64-apple-darwin",
"arm64-apple-ios",
"arm64-apple-ios-sim",
"arm64-apple-tvos",
"arm64-apple-tvos-sim",
"arm64-apple-visionos",
"arm64-apple-visionos-sim",
],
defaultTriplets() {
return process.arch === "arm64" ? ["arm64-apple-ios-sim"] : [];
},
amendCommand(command) {
return command.addOption(xcframeworkExtensionOption);
},
configureArgs({ triplet }) {
return [
"-G",
"Xcode",
"-D",
`CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAMES[triplet]}`,
"-D",
`CMAKE_OSX_SYSROOT=${XCODE_SDK_NAMES[triplet]}`,
"-D",
`CMAKE_OSX_ARCHITECTURES=${APPLE_ARCHITECTURES[triplet]}`,
];
},
buildArgs() {
// We expect the final application to sign these binaries
return ["CODE_SIGNING_ALLOWED=NO"];
},
isSupportedByHost: function (): boolean | Promise<boolean> {
return process.platform === "darwin";
},
async postBuild(
{ outputPath, triplets },
{ configuration, autoLink, xcframeworkExtension, target },
) {
const prebuilds: Record<string, string[]> = {};
for (const { buildPath } of triplets) {
assert(fs.existsSync(buildPath), `Expected a directory at ${buildPath}`);
const targets = await cmakeFileApi.readCurrentTargetsDeep(
buildPath,
configuration,
"2.0",
);
const sharedLibraries = targets.filter(
({ type, name }) =>
type === "SHARED_LIBRARY" &&
(target.length === 0 || target.includes(name)),
);
assert.equal(
sharedLibraries.length,
1,
"Expected exactly one shared library",
);
const [sharedLibrary] = sharedLibraries;
const { artifacts } = sharedLibrary;
assert(
artifacts && artifacts.length === 1,
"Expected exactly one artifact",
);
const [artifact] = artifacts;
// Add prebuild entry, creating a new entry if needed
if (!(sharedLibrary.name in prebuilds)) {
prebuilds[sharedLibrary.name] = [];
}
prebuilds[sharedLibrary.name].push(path.join(buildPath, artifact.path));
}
const extension = xcframeworkExtension ? ".xcframework" : ".apple.node";
for (const [libraryName, libraryPaths] of Object.entries(prebuilds)) {
const frameworkPaths = await Promise.all(
libraryPaths.map(createAppleFramework),
);
// Create the xcframework
const xcframeworkOutputPath = path.resolve(
outputPath,
`${libraryName}${extension}`,
);
await oraPromise(
createXCframework({
outputPath: xcframeworkOutputPath,
frameworkPaths,
autoLink,
}),
{
text: `Assembling XCFramework (${libraryName})`,
successText: `XCFramework (${libraryName}) assembled into ${prettyPath(xcframeworkOutputPath)}`,
failText: ({ message }) =>
`Failed to assemble XCFramework (${libraryName}): ${message}`,
},
);
}
},
};