Skip to content

Commit a336f07

Browse files
authored
Rename target to triplet (#243)
* Rename target to triplet * Add changeset
1 parent beff0f9 commit a336f07

File tree

9 files changed

+136
-124
lines changed

9 files changed

+136
-124
lines changed

.changeset/rotten-melons-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cmake-rn": minor
3+
---
4+
5+
Breaking: Renamed --target to --triplet to free up --target for passing CMake targets

.github/workflows/check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ jobs:
9898
- run: npm ci
9999
- run: npm run bootstrap
100100
env:
101-
CMAKE_RN_TARGETS: arm64-apple-ios-sim
101+
CMAKE_RN_TRIPLETS: arm64-apple-ios-sim
102102
FERRIC_TARGETS: aarch64-apple-ios-sim
103103
- run: npm run pod-install
104104
working-directory: apps/test-app
@@ -129,7 +129,7 @@ jobs:
129129
- run: npm ci
130130
- run: npm run bootstrap
131131
env:
132-
CMAKE_RN_TARGETS: i686-linux-android
132+
CMAKE_RN_TRIPLETS: i686-linux-android
133133
FERRIC_TARGETS: i686-linux-android
134134
- name: Clone patched Hermes version
135135
shell: bash

packages/cmake-rn/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
### Minor Changes
2424

25-
- 8557768: Derive default targets from the CMAKE_RN_TARGETS environment variable
25+
- 8557768: Derive default targets from the CMAKE_RN_TRIPLETS environment variable
2626

2727
### Patch Changes
2828

packages/cmake-rn/src/cli.ts

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import { isSupportedTriplet } from "react-native-node-api";
1717
import { getWeakNodeApiVariables } from "./weak-node-api.js";
1818
import {
1919
platforms,
20-
allTargets,
21-
findPlatformForTarget,
22-
platformHasTarget,
20+
allTriplets as allTriplets,
21+
findPlatformForTriplet,
22+
platformHasTriplet,
2323
} from "./platforms.js";
24-
import { BaseOpts, TargetContext, Platform } from "./platforms/types.js";
24+
import { BaseOpts, TripletContext, Platform } from "./platforms/types.js";
2525

2626
// We're attaching a lot of listeners when spawning in parallel
2727
EventEmitter.defaultMaxListeners = 100;
@@ -43,25 +43,28 @@ const configurationOption = new Option("--configuration <configuration>")
4343
.choices(["Release", "Debug"] as const)
4444
.default("Release");
4545

46-
// TODO: Derive default targets
46+
// TODO: Derive default build triplets
4747
// This is especially important when driving the build from within a React Native app package.
4848

49-
const { CMAKE_RN_TARGETS } = process.env;
49+
const { CMAKE_RN_TRIPLETS } = process.env;
5050

51-
const defaultTargets = CMAKE_RN_TARGETS ? CMAKE_RN_TARGETS.split(",") : [];
51+
const defaultTriplets = CMAKE_RN_TRIPLETS ? CMAKE_RN_TRIPLETS.split(",") : [];
5252

53-
for (const target of defaultTargets) {
53+
for (const triplet of defaultTriplets) {
5454
assert(
55-
(allTargets as string[]).includes(target),
56-
`Unexpected target in CMAKE_RN_TARGETS: ${target}`,
55+
(allTriplets as string[]).includes(triplet),
56+
`Unexpected triplet in CMAKE_RN_TRIPLETS: ${triplet}`,
5757
);
5858
}
5959

60-
const targetOption = new Option("--target <target...>", "Targets to build for")
61-
.choices(allTargets)
60+
const tripletOption = new Option(
61+
"--triplet <triplet...>",
62+
"Triplets to build for",
63+
)
64+
.choices(allTriplets)
6265
.default(
63-
defaultTargets,
64-
"CMAKE_RN_TARGETS environment variable split by ','",
66+
defaultTriplets,
67+
"CMAKE_RN_TRIPLETS environment variable split by ','",
6568
);
6669

6770
const buildPathOption = new Option(
@@ -111,7 +114,7 @@ const noWeakNodeApiLinkageOption = new Option(
111114

112115
let program = new Command("cmake-rn")
113116
.description("Build React Native Node API modules with CMake")
114-
.addOption(targetOption)
117+
.addOption(tripletOption)
115118
.addOption(verboseOption)
116119
.addOption(sourcePathOption)
117120
.addOption(buildPathOption)
@@ -132,7 +135,7 @@ for (const platform of platforms) {
132135
}
133136

134137
program = program.action(
135-
wrapAction(async ({ target: requestedTargets, ...baseOptions }) => {
138+
wrapAction(async ({ triplet: requestedTriplets, ...baseOptions }) => {
136139
assertFixable(
137140
fs.existsSync(path.join(baseOptions.source, "CMakeLists.txt")),
138141
`No CMakeLists.txt found in source directory: ${chalk.dim(baseOptions.source)}`,
@@ -145,34 +148,34 @@ program = program.action(
145148
if (baseOptions.clean) {
146149
await fs.promises.rm(buildPath, { recursive: true, force: true });
147150
}
148-
const targets = new Set<string>(requestedTargets);
151+
const triplets = new Set<string>(requestedTriplets);
149152

150153
for (const platform of Object.values(platforms)) {
151154
// Forcing the types a bit here, since the platform id option is dynamically added
152155
if ((baseOptions as Record<string, unknown>)[platform.id]) {
153-
for (const target of platform.targets) {
154-
targets.add(target);
156+
for (const triplet of platform.triplets) {
157+
triplets.add(triplet);
155158
}
156159
}
157160
}
158161

159-
if (targets.size === 0) {
162+
if (triplets.size === 0) {
160163
for (const platform of Object.values(platforms)) {
161164
if (platform.isSupportedByHost()) {
162-
for (const target of await platform.defaultTargets()) {
163-
targets.add(target);
165+
for (const triplet of await platform.defaultTriplets()) {
166+
triplets.add(triplet);
164167
}
165168
}
166169
}
167-
if (targets.size === 0) {
170+
if (triplets.size === 0) {
168171
throw new Error(
169-
"Found no default targets: Install some platform specific build tools",
172+
"Found no default build triplets: Install some platform specific build tools",
170173
);
171174
} else {
172175
console.error(
173176
chalk.yellowBright("ℹ"),
174-
"Using default targets",
175-
chalk.dim("(" + [...targets].join(", ") + ")"),
177+
"Using default build triplets",
178+
chalk.dim("(" + [...triplets].join(", ") + ")"),
176179
);
177180
}
178181
}
@@ -181,38 +184,40 @@ program = program.action(
181184
baseOptions.out = path.join(buildPath, baseOptions.configuration);
182185
}
183186

184-
const targetContexts = [...targets].map((target) => {
185-
const platform = findPlatformForTarget(target);
186-
const targetBuildPath = getTargetBuildPath(buildPath, target);
187+
const tripletContexts = [...triplets].map((triplet) => {
188+
const platform = findPlatformForTriplet(triplet);
189+
const tripletBuildPath = getTripletBuildPath(buildPath, triplet);
187190
return {
188-
target,
191+
triplet,
189192
platform,
190-
buildPath: targetBuildPath,
191-
outputPath: path.join(targetBuildPath, "out"),
193+
buildPath: tripletBuildPath,
194+
outputPath: path.join(tripletBuildPath, "out"),
192195
options: baseOptions,
193196
};
194197
});
195198

196199
// Configure every triplet project
197-
const targetsSummary = chalk.dim(`(${getTargetsSummary(targetContexts)})`);
200+
const tripletsSummary = chalk.dim(
201+
`(${getTripletsSummary(tripletContexts)})`,
202+
);
198203
await oraPromise(
199204
Promise.all(
200-
targetContexts.map(({ platform, ...context }) =>
205+
tripletContexts.map(({ platform, ...context }) =>
201206
configureProject(platform, context, baseOptions),
202207
),
203208
),
204209
{
205-
text: `Configuring projects ${targetsSummary}`,
210+
text: `Configuring projects ${tripletsSummary}`,
206211
isSilent: baseOptions.verbose,
207-
successText: `Configured projects ${targetsSummary}`,
212+
successText: `Configured projects ${tripletsSummary}`,
208213
failText: ({ message }) => `Failed to configure projects: ${message}`,
209214
},
210215
);
211216

212217
// Build every triplet project
213218
await oraPromise(
214219
Promise.all(
215-
targetContexts.map(async ({ platform, ...context }) => {
220+
tripletContexts.map(async ({ platform, ...context }) => {
216221
// Delete any stale build artifacts before building
217222
// This is important, since we might rename the output files
218223
await fs.promises.rm(context.outputPath, {
@@ -232,36 +237,36 @@ program = program.action(
232237

233238
// Perform post-build steps for each platform in sequence
234239
for (const platform of platforms) {
235-
const relevantTargets = targetContexts.filter(({ target }) =>
236-
platformHasTarget(platform, target),
240+
const relevantTriplets = tripletContexts.filter(({ triplet }) =>
241+
platformHasTriplet(platform, triplet),
237242
);
238-
if (relevantTargets.length == 0) {
243+
if (relevantTriplets.length == 0) {
239244
continue;
240245
}
241246
await platform.postBuild(
242247
{
243248
outputPath: baseOptions.out || baseOptions.source,
244-
targets: relevantTargets,
249+
triplets: relevantTriplets,
245250
},
246251
baseOptions,
247252
);
248253
}
249254
}),
250255
);
251256

252-
function getTargetsSummary(
253-
targetContexts: { target: string; platform: Platform }[],
257+
function getTripletsSummary(
258+
tripletContexts: { triplet: string; platform: Platform }[],
254259
) {
255-
const targetsPerPlatform: Record<string, string[]> = {};
256-
for (const { target, platform } of targetContexts) {
257-
if (!targetsPerPlatform[platform.id]) {
258-
targetsPerPlatform[platform.id] = [];
260+
const tripletsPerPlatform: Record<string, string[]> = {};
261+
for (const { triplet, platform } of tripletContexts) {
262+
if (!tripletsPerPlatform[platform.id]) {
263+
tripletsPerPlatform[platform.id] = [];
259264
}
260-
targetsPerPlatform[platform.id].push(target);
265+
tripletsPerPlatform[platform.id].push(triplet);
261266
}
262-
return Object.entries(targetsPerPlatform)
263-
.map(([platformId, targets]) => {
264-
return `${platformId}: ${targets.join(", ")}`;
267+
return Object.entries(tripletsPerPlatform)
268+
.map(([platformId, triplets]) => {
269+
return `${platformId}: ${triplets.join(", ")}`;
265270
})
266271
.join(" / ");
267272
}
@@ -272,24 +277,24 @@ function getBuildPath({ build, source }: BaseOpts) {
272277
}
273278

274279
/**
275-
* Namespaces the output path with a target name
280+
* Namespaces the output path with a triplet name
276281
*/
277-
function getTargetBuildPath(buildPath: string, target: unknown) {
278-
assert(typeof target === "string", "Expected target to be a string");
279-
return path.join(buildPath, target.replace(/;/g, "_"));
282+
function getTripletBuildPath(buildPath: string, triplet: unknown) {
283+
assert(typeof triplet === "string", "Expected triplet to be a string");
284+
return path.join(buildPath, triplet.replace(/;/g, "_"));
280285
}
281286

282287
async function configureProject<T extends string>(
283288
platform: Platform<T[], Record<string, unknown>>,
284-
context: TargetContext<T>,
289+
context: TripletContext<T>,
285290
options: BaseOpts,
286291
) {
287-
const { target, buildPath, outputPath } = context;
292+
const { triplet, buildPath, outputPath } = context;
288293
const { verbose, source, weakNodeApiLinkage } = options;
289294

290295
const nodeApiDefinitions =
291-
weakNodeApiLinkage && isSupportedTriplet(target)
292-
? getWeakNodeApiVariables(target)
296+
weakNodeApiLinkage && isSupportedTriplet(triplet)
297+
? getWeakNodeApiVariables(triplet)
293298
: // TODO: Make this a part of the platform definition
294299
{};
295300

@@ -311,17 +316,17 @@ async function configureProject<T extends string>(
311316
],
312317
{
313318
outputMode: verbose ? "inherit" : "buffered",
314-
outputPrefix: verbose ? chalk.dim(`[${target}] `) : undefined,
319+
outputPrefix: verbose ? chalk.dim(`[${triplet}] `) : undefined,
315320
},
316321
);
317322
}
318323

319324
async function buildProject<T extends string>(
320325
platform: Platform<T[], Record<string, unknown>>,
321-
context: TargetContext<T>,
326+
context: TripletContext<T>,
322327
options: BaseOpts,
323328
) {
324-
const { target, buildPath } = context;
329+
const { triplet, buildPath } = context;
325330
const { verbose, configuration } = options;
326331
await spawn(
327332
"cmake",
@@ -335,7 +340,7 @@ async function buildProject<T extends string>(
335340
],
336341
{
337342
outputMode: verbose ? "inherit" : "buffered",
338-
outputPrefix: verbose ? chalk.dim(`[${target}] `) : undefined,
343+
outputPrefix: verbose ? chalk.dim(`[${triplet}] `) : undefined,
339344
},
340345
);
341346
}

packages/cmake-rn/src/platforms.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@ import { describe, it } from "node:test";
33

44
import {
55
platforms,
6-
platformHasTarget,
7-
findPlatformForTarget,
6+
platformHasTriplet,
7+
findPlatformForTriplet,
88
} from "./platforms.js";
99
import { Platform } from "./platforms/types.js";
1010

11-
const mockPlatform = { targets: ["target1", "target2"] } as unknown as Platform;
11+
const mockPlatform = {
12+
triplets: ["triplet1", "triplet2"],
13+
} as unknown as Platform;
1214

13-
describe("platformHasTarget", () => {
14-
it("returns true when platform has target", () => {
15-
assert.equal(platformHasTarget(mockPlatform, "target1"), true);
15+
describe("platformHasTriplet", () => {
16+
it("returns true when platform has triplet", () => {
17+
assert.equal(platformHasTriplet(mockPlatform, "triplet1"), true);
1618
});
1719

18-
it("returns false when platform doesn't have target", () => {
19-
assert.equal(platformHasTarget(mockPlatform, "target3"), false);
20+
it("returns false when platform doesn't have triplet", () => {
21+
assert.equal(platformHasTriplet(mockPlatform, "triplet3"), false);
2022
});
2123
});
2224

23-
describe("findPlatformForTarget", () => {
24-
it("returns platform when target is found", () => {
25+
describe("findPlatformForTriplet", () => {
26+
it("returns platform when triplet is found", () => {
2527
assert(platforms.length >= 2, "Expects at least two platforms");
2628
const [platform1, platform2] = platforms;
27-
const platform = findPlatformForTarget(platform1.targets[0]);
29+
const platform = findPlatformForTriplet(platform1.triplets[0]);
2830
assert.equal(platform, platform1);
2931
assert.notEqual(platform, platform2);
3032
});

packages/cmake-rn/src/platforms.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import { platform as apple } from "./platforms/apple.js";
55
import { Platform } from "./platforms/types.js";
66

77
export const platforms: Platform[] = [android, apple] as const;
8-
export const allTargets = [...android.targets, ...apple.targets] as const;
8+
export const allTriplets = [...android.triplets, ...apple.triplets] as const;
99

10-
export function platformHasTarget<P extends Platform>(
10+
export function platformHasTriplet<P extends Platform>(
1111
platform: P,
12-
target: unknown,
13-
): target is P["targets"][number] {
14-
return (platform.targets as unknown[]).includes(target);
12+
triplet: unknown,
13+
): triplet is P["triplets"][number] {
14+
return (platform.triplets as unknown[]).includes(triplet);
1515
}
1616

17-
export function findPlatformForTarget(target: unknown) {
17+
export function findPlatformForTriplet(triplet: unknown) {
1818
const platform = Object.values(platforms).find((platform) =>
19-
platformHasTarget(platform, target),
19+
platformHasTriplet(platform, triplet),
2020
);
2121
assert(
2222
platform,
23-
`Unable to determine platform from target: ${
24-
typeof target === "string" ? target : JSON.stringify(target)
23+
`Unable to determine platform from triplet: ${
24+
typeof triplet === "string" ? triplet : JSON.stringify(triplet)
2525
}`,
2626
);
2727
return platform;

0 commit comments

Comments
 (0)