Skip to content

Commit 046312b

Browse files
committed
Use CMake file API to read shared library target paths
1 parent 898559b commit 046312b

File tree

6 files changed

+58
-41
lines changed

6 files changed

+58
-41
lines changed

.changeset/evil-vans-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cmake-rn": patch
3+
---
4+
5+
Use CMake file API to read shared library target paths

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cmake-rn/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@react-native-node-api/cli-utils": "0.1.0",
26+
"cmake-file-api": "0.1.0",
2627
"react-native-node-api": "0.5.1"
2728
},
2829
"peerDependencies": {

packages/cmake-rn/src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
wrapAction,
1414
} from "@react-native-node-api/cli-utils";
1515
import { isSupportedTriplet } from "react-native-node-api";
16+
import * as cmakeFileApi from "cmake-file-api";
1617

1718
import {
1819
getCmakeJSVariables,
@@ -327,6 +328,8 @@ async function configureProject<T extends string>(
327328
{ CMAKE_LIBRARY_OUTPUT_DIRECTORY: outputPath },
328329
];
329330

331+
await cmakeFileApi.createSharedStatelessQuery(buildPath, "codemodel", "2");
332+
330333
await spawn(
331334
"cmake",
332335
[

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
determineAndroidLibsFilename,
99
AndroidTriplet as Triplet,
1010
} from "react-native-node-api";
11+
import * as cmakeFileApi from "cmake-file-api";
1112

1213
import type { Platform } from "./types.js";
1314

@@ -121,28 +122,35 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
121122
const { ANDROID_HOME } = process.env;
122123
return typeof ANDROID_HOME === "string" && fs.existsSync(ANDROID_HOME);
123124
},
124-
async postBuild({ outputPath, triplets }, { autoLink }) {
125-
// TODO: Include `configuration` in the output path
125+
async postBuild({ outputPath, triplets }, { autoLink, configuration }) {
126126
const libraryPathByTriplet = Object.fromEntries(
127127
await Promise.all(
128-
triplets.map(async ({ triplet, outputPath }) => {
128+
triplets.map(async ({ triplet, buildPath }) => {
129129
assert(
130-
fs.existsSync(outputPath),
131-
`Expected a directory at ${outputPath}`,
130+
fs.existsSync(buildPath),
131+
`Expected a directory at ${buildPath}`,
132132
);
133-
// Expect binary file(s), either .node or .so
134-
const dirents = await fs.promises.readdir(outputPath, {
135-
withFileTypes: true,
136-
});
137-
const result = dirents
138-
.filter(
139-
(dirent) =>
140-
dirent.isFile() &&
141-
(dirent.name.endsWith(".so") || dirent.name.endsWith(".node")),
142-
)
143-
.map((dirent) => path.join(dirent.parentPath, dirent.name));
144-
assert.equal(result.length, 1, "Expected exactly one library file");
145-
return [triplet, result[0]] as const;
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;
146154
}),
147155
),
148156
) as Record<Triplet, string>;

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "react-native-node-api";
1212

1313
import type { Platform } from "./types.js";
14+
import * as cmakeFileApi from "cmake-file-api";
1415

1516
type XcodeSDKName =
1617
| "iphoneos"
@@ -136,31 +137,29 @@ export const platform: Platform<Triplet[], AppleOpts> = {
136137
{ configuration, autoLink, xcframeworkExtension },
137138
) {
138139
const libraryPaths = await Promise.all(
139-
triplets.map(async ({ outputPath }) => {
140-
const configSpecificPath = path.join(outputPath, configuration);
140+
triplets.map(async ({ buildPath }) => {
141141
assert(
142-
fs.existsSync(configSpecificPath),
143-
`Expected a directory at ${configSpecificPath}`,
142+
fs.existsSync(buildPath),
143+
`Expected a directory at ${buildPath}`,
144144
);
145-
// Expect binary file(s), either .node or .dylib
146-
const files = await fs.promises.readdir(configSpecificPath);
147-
const result = files.map(async (file) => {
148-
const filePath = path.join(configSpecificPath, file);
149-
if (filePath.endsWith(".dylib")) {
150-
return filePath;
151-
} else if (file.endsWith(".node")) {
152-
// Rename the file to .dylib for xcodebuild to accept it
153-
const newFilePath = filePath.replace(/\.node$/, ".dylib");
154-
await fs.promises.rename(filePath, newFilePath);
155-
return newFilePath;
156-
} else {
157-
throw new Error(
158-
`Expected a .node or .dylib file, but found ${file}`,
159-
);
160-
}
161-
});
162-
assert.equal(result.length, 1, "Expected exactly one library file");
163-
return await result[0];
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);
164163
}),
165164
);
166165
const frameworkPaths = libraryPaths.map(createAppleFramework);

0 commit comments

Comments
 (0)