Skip to content

Commit 7dc3078

Browse files
committed
Support multiple shared library outputs
1 parent e2ede36 commit 7dc3078

File tree

2 files changed

+108
-101
lines changed

2 files changed

+108
-101
lines changed

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

Lines changed: 53 additions & 51 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
import * as cmakeFileApi from "cmake-file-api";
@@ -123,56 +122,59 @@ 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-
"2.0",
137-
);
138-
const sharedLibraries = targets.filter(
139-
(target) => target.type === "SHARED_LIBRARY",
140-
);
141-
assert.equal(
142-
sharedLibraries.length,
143-
1,
144-
"Expected exactly one shared library",
145-
);
146-
const [sharedLibrary] = sharedLibraries;
147-
const { artifacts } = sharedLibrary;
148-
assert(
149-
artifacts && artifacts.length,
150-
"Expected exactly one artifact",
151-
);
152-
const [artifact] = artifacts;
153-
return [triplet, path.join(buildPath, artifact.path)] as const;
154-
}),
155-
),
156-
) as Record<Triplet, string>;
157-
const androidLibsFilename = determineAndroidLibsFilename(
158-
Object.values(libraryPathByTriplet),
159-
);
160-
const androidLibsOutputPath = path.resolve(outputPath, androidLibsFilename);
125+
const prebuilds: Record<string, Partial<Record<Triplet, string>>> = {};
161126

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

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

Lines changed: 55 additions & 50 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,54 +135,60 @@ 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-
"2.0",
149-
);
150-
const sharedLibraries = targets.filter(
151-
(target) => target.type === "SHARED_LIBRARY",
152-
);
153-
assert.equal(
154-
sharedLibraries.length,
155-
1,
156-
"Expected exactly one shared library",
157-
);
158-
const [sharedLibrary] = sharedLibraries;
159-
const { artifacts } = sharedLibrary;
160-
assert(artifacts && artifacts.length, "Expected exactly one artifact");
161-
const [artifact] = artifacts;
162-
return path.join(buildPath, artifact.path);
163-
}),
164-
);
165-
const frameworkPaths = libraryPaths.map(createAppleFramework);
166-
const xcframeworkFilename = determineXCFrameworkFilename(
167-
frameworkPaths,
168-
xcframeworkExtension ? ".xcframework" : ".apple.node",
169-
);
170-
171-
// Create the xcframework
172-
const xcframeworkOutputPath = path.resolve(outputPath, xcframeworkFilename);
173-
174-
await oraPromise(
175-
createXCframework({
176-
outputPath: xcframeworkOutputPath,
177-
frameworkPaths,
178-
autoLink,
179-
}),
180-
{
181-
text: "Assembling XCFramework",
182-
successText: `XCFramework assembled into ${chalk.dim(
183-
path.relative(process.cwd(), xcframeworkOutputPath),
184-
)}`,
185-
failText: ({ message }) => `Failed to assemble XCFramework: ${message}`,
186-
},
187-
);
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+
"2.0",
145+
);
146+
const sharedLibraries = targets.filter(
147+
(target) => target.type === "SHARED_LIBRARY",
148+
);
149+
assert.equal(
150+
sharedLibraries.length,
151+
1,
152+
"Expected exactly one shared library",
153+
);
154+
const [sharedLibrary] = sharedLibraries;
155+
const { artifacts } = sharedLibrary;
156+
assert(artifacts && artifacts.length, "Expected exactly one artifact");
157+
const [artifact] = artifacts;
158+
// Add prebuild entry, creating a new entry if needed
159+
if (!(sharedLibrary.name in prebuilds)) {
160+
prebuilds[sharedLibrary.name] = [];
161+
}
162+
prebuilds[sharedLibrary.name].push(path.join(buildPath, artifact.path));
163+
}
164+
165+
const extension = xcframeworkExtension ? ".xcframework" : ".apple.node";
166+
167+
for (const [libraryName, libraryPaths] of Object.entries(prebuilds)) {
168+
const frameworkPaths = await Promise.all(
169+
libraryPaths.map(createAppleFramework),
170+
);
171+
// Create the xcframework
172+
const xcframeworkOutputPath = path.resolve(
173+
outputPath,
174+
`${libraryName}${extension}`,
175+
);
176+
177+
await oraPromise(
178+
createXCframework({
179+
outputPath: xcframeworkOutputPath,
180+
frameworkPaths,
181+
autoLink,
182+
}),
183+
{
184+
text: `Assembling XCFramework (${libraryName})`,
185+
successText: `XCFramework (${libraryName}) assembled into ${chalk.dim(
186+
path.relative(process.cwd(), xcframeworkOutputPath),
187+
)}`,
188+
failText: ({ message }) =>
189+
`Failed to assemble XCFramework (${libraryName}): ${message}`,
190+
},
191+
);
192+
}
188193
},
189194
};

0 commit comments

Comments
 (0)