Skip to content

Commit 7ccbe2e

Browse files
committed
Support multiple shared library outputs
1 parent 6d68445 commit 7ccbe2e

File tree

2 files changed

+106
-99
lines changed

2 files changed

+106
-99
lines changed

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

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from "node:path";
55
import { Option, oraPromise, chalk } from "@react-native-node-api/cli-utils";
66
import {
77
createAndroidLibsDirectory,
8-
determineAndroidLibsFilename,
98
AndroidTriplet as Triplet,
109
} from "react-native-node-api";
1110

@@ -123,55 +122,58 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
123122
return typeof ANDROID_HOME === "string" && fs.existsSync(ANDROID_HOME);
124123
},
125124
async postBuild({ outputPath, triplets }, { autoLink, configuration }) {
126-
const libraryPathByTriplet = Object.fromEntries(
127-
await Promise.all(
128-
triplets.map(async ({ triplet, buildPath }) => {
129-
assert(
130-
fs.existsSync(buildPath),
131-
`Expected a directory at ${buildPath}`,
132-
);
133-
const targets = await cmakeFileApi.readCurrentTargetsDeep(
134-
buildPath,
135-
configuration,
136-
);
137-
const sharedLibraries = targets.filter(
138-
(target) => target.type === "SHARED_LIBRARY",
139-
);
140-
assert.equal(
141-
sharedLibraries.length,
142-
1,
143-
"Expected exactly one shared library",
144-
);
145-
const [sharedLibrary] = sharedLibraries;
146-
const { artifacts } = sharedLibrary;
147-
assert(
148-
artifacts && artifacts.length,
149-
"Expected exactly one artifact",
150-
);
151-
const [artifact] = artifacts;
152-
return [triplet, path.join(buildPath, artifact.path)] as const;
153-
}),
154-
),
155-
) as Record<Triplet, string>;
156-
const androidLibsFilename = determineAndroidLibsFilename(
157-
Object.values(libraryPathByTriplet),
158-
);
159-
const androidLibsOutputPath = path.resolve(outputPath, androidLibsFilename);
125+
const prebuilds: Record<string, Partial<Record<Triplet, string>>> = {};
160126

161-
await oraPromise(
162-
createAndroidLibsDirectory({
163-
outputPath: androidLibsOutputPath,
164-
libraryPathByTriplet,
165-
autoLink,
166-
}),
167-
{
168-
text: "Assembling Android libs directory",
169-
successText: `Android libs directory assembled into ${chalk.dim(
170-
path.relative(process.cwd(), androidLibsOutputPath),
171-
)}`,
172-
failText: ({ message }) =>
173-
`Failed to assemble Android libs directory: ${message}`,
174-
},
175-
);
127+
for (const { triplet, buildPath } of triplets) {
128+
assert(fs.existsSync(buildPath), `Expected a directory at ${buildPath}`);
129+
const targets = await cmakeFileApi.readCurrentTargetsDeep(
130+
buildPath,
131+
configuration,
132+
);
133+
const sharedLibraries = targets.filter(
134+
(target) => target.type === "SHARED_LIBRARY",
135+
);
136+
assert.equal(
137+
sharedLibraries.length,
138+
1,
139+
"Expected exactly one shared library",
140+
);
141+
const [sharedLibrary] = sharedLibraries;
142+
const { artifacts } = sharedLibrary;
143+
assert(artifacts && artifacts.length, "Expected exactly one artifact");
144+
const [artifact] = artifacts;
145+
// Add prebuild entry, creating a new entry if needed
146+
if (!(sharedLibrary.name in prebuilds)) {
147+
prebuilds[sharedLibrary.name] = {};
148+
}
149+
prebuilds[sharedLibrary.name][triplet] = path.join(
150+
buildPath,
151+
artifact.path,
152+
);
153+
}
154+
155+
for (const [libraryName, libraryPathByTriplet] of Object.entries(
156+
prebuilds,
157+
)) {
158+
const prebuildOutputPath = path.resolve(
159+
outputPath,
160+
`${libraryName}.android.node`,
161+
);
162+
await oraPromise(
163+
createAndroidLibsDirectory({
164+
outputPath: prebuildOutputPath,
165+
libraryPathByTriplet,
166+
autoLink,
167+
}),
168+
{
169+
text: `Assembling Android libs directory (${libraryName})`,
170+
successText: `Android libs directory (${libraryName}) assembled into ${chalk.dim(
171+
path.relative(process.cwd(), prebuildOutputPath),
172+
)}`,
173+
failText: ({ message }) =>
174+
`Failed to assemble Android libs directory (${libraryName}): ${message}`,
175+
},
176+
);
177+
}
176178
},
177179
};

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

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
AppleTriplet as Triplet,
88
createAppleFramework,
99
createXCframework,
10-
determineXCFrameworkFilename,
1110
} from "react-native-node-api";
1211

1312
import type { Platform } from "./types.js";
@@ -136,53 +135,59 @@ export const platform: Platform<Triplet[], AppleOpts> = {
136135
{ outputPath, triplets },
137136
{ configuration, autoLink, xcframeworkExtension },
138137
) {
139-
const libraryPaths = await Promise.all(
140-
triplets.map(async ({ buildPath }) => {
141-
assert(
142-
fs.existsSync(buildPath),
143-
`Expected a directory at ${buildPath}`,
144-
);
145-
const targets = await cmakeFileApi.readCurrentTargetsDeep(
146-
buildPath,
147-
configuration,
148-
);
149-
const sharedLibraries = targets.filter(
150-
(target) => target.type === "SHARED_LIBRARY",
151-
);
152-
assert.equal(
153-
sharedLibraries.length,
154-
1,
155-
"Expected exactly one shared library",
156-
);
157-
const [sharedLibrary] = sharedLibraries;
158-
const { artifacts } = sharedLibrary;
159-
assert(artifacts && artifacts.length, "Expected exactly one artifact");
160-
const [artifact] = artifacts;
161-
return path.join(buildPath, artifact.path);
162-
}),
163-
);
164-
const frameworkPaths = libraryPaths.map(createAppleFramework);
165-
const xcframeworkFilename = determineXCFrameworkFilename(
166-
frameworkPaths,
167-
xcframeworkExtension ? ".xcframework" : ".apple.node",
168-
);
169-
170-
// Create the xcframework
171-
const xcframeworkOutputPath = path.resolve(outputPath, xcframeworkFilename);
172-
173-
await oraPromise(
174-
createXCframework({
175-
outputPath: xcframeworkOutputPath,
176-
frameworkPaths,
177-
autoLink,
178-
}),
179-
{
180-
text: "Assembling XCFramework",
181-
successText: `XCFramework assembled into ${chalk.dim(
182-
path.relative(process.cwd(), xcframeworkOutputPath),
183-
)}`,
184-
failText: ({ message }) => `Failed to assemble XCFramework: ${message}`,
185-
},
186-
);
138+
const prebuilds: Record<string, string[]> = {};
139+
for (const { buildPath } of triplets) {
140+
assert(fs.existsSync(buildPath), `Expected a directory at ${buildPath}`);
141+
const targets = await cmakeFileApi.readCurrentTargetsDeep(
142+
buildPath,
143+
configuration,
144+
);
145+
const sharedLibraries = targets.filter(
146+
(target) => target.type === "SHARED_LIBRARY",
147+
);
148+
assert.equal(
149+
sharedLibraries.length,
150+
1,
151+
"Expected exactly one shared library",
152+
);
153+
const [sharedLibrary] = sharedLibraries;
154+
const { artifacts } = sharedLibrary;
155+
assert(artifacts && artifacts.length, "Expected exactly one artifact");
156+
const [artifact] = artifacts;
157+
// Add prebuild entry, creating a new entry if needed
158+
if (!(sharedLibrary.name in prebuilds)) {
159+
prebuilds[sharedLibrary.name] = [];
160+
}
161+
prebuilds[sharedLibrary.name].push(path.join(buildPath, artifact.path));
162+
}
163+
164+
const extension = xcframeworkExtension ? ".xcframework" : ".apple.node";
165+
166+
for (const [libraryName, libraryPaths] of Object.entries(prebuilds)) {
167+
const frameworkPaths = await Promise.all(
168+
libraryPaths.map(createAppleFramework),
169+
);
170+
// Create the xcframework
171+
const xcframeworkOutputPath = path.resolve(
172+
outputPath,
173+
`${libraryName}${extension}`,
174+
);
175+
176+
await oraPromise(
177+
createXCframework({
178+
outputPath: xcframeworkOutputPath,
179+
frameworkPaths,
180+
autoLink,
181+
}),
182+
{
183+
text: `Assembling XCFramework (${libraryName})`,
184+
successText: `XCFramework (${libraryName}) assembled into ${chalk.dim(
185+
path.relative(process.cwd(), xcframeworkOutputPath),
186+
)}`,
187+
failText: ({ message }) =>
188+
`Failed to assemble XCFramework (${libraryName}): ${message}`,
189+
},
190+
);
191+
}
187192
},
188193
};

0 commit comments

Comments
 (0)