Skip to content

Commit 5c3de89

Browse files
authored
Enable "RelWithDebInfo" and "MinSizeRel" configurations and support Apple debug symbols when building and linking (#284)
* Add RelWithDebInfo and MinSizeRel configurations * Locate and include debug symbols when creating an Xcframework * Use RelWithDebInfo when building examples * Use dsymutil to rebuild DWARF debug symbols * Add changesets * Include config in Android build directory names * Add MinSizeRel and RelWithDebInfo to list of path prefix candidates in babel plugin
1 parent 5c9321b commit 5c3de89

File tree

9 files changed

+102
-30
lines changed

9 files changed

+102
-30
lines changed

.changeset/flat-suits-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-native-node-api": patch
3+
---
4+
5+
Rebuild any dSYM directory when linking frameworks.

.changeset/hot-poems-attack.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+
Locate and include debug symbols when creating an Xcframework.

.changeset/light-buttons-leave.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+
Allow passing "RelWithDebInfo" and "MinSizeRel" as --configuration

packages/cmake-rn/src/cli.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ const sourcePathOption = new Option(
3636
"Specify the source directory containing a CMakeLists.txt file",
3737
).default(process.cwd());
3838

39-
// TODO: Add "MinSizeRel" and "RelWithDebInfo"
4039
const configurationOption = new Option("--configuration <configuration>")
41-
.choices(["Release", "Debug"] as const)
40+
.choices(["Release", "Debug", "RelWithDebInfo", "MinSizeRel"] as const)
4241
.default("Release");
4342

4443
// TODO: Derive default build triplets

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "react-native-node-api";
1414
import * as cmakeFileApi from "cmake-file-api";
1515

16-
import type { Platform } from "./types.js";
16+
import type { BaseOpts, Platform } from "./types.js";
1717
import { toDefineArguments } from "../helpers.js";
1818
import {
1919
getCmakeJSVariables,
@@ -45,8 +45,12 @@ const androidSdkVersionOption = new Option(
4545

4646
type AndroidOpts = { ndkVersion: string; androidSdkVersion: string };
4747

48-
function getBuildPath(baseBuildPath: string, triplet: Triplet) {
49-
return path.join(baseBuildPath, triplet);
48+
function getBuildPath(
49+
baseBuildPath: string,
50+
triplet: Triplet,
51+
configuration: BaseOpts["configuration"],
52+
) {
53+
return path.join(baseBuildPath, triplet + "-" + configuration);
5054
}
5155

5256
function getNdkPath(ndkVersion: string) {
@@ -147,7 +151,7 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
147151

148152
await Promise.all(
149153
triplets.map(async ({ triplet, spawn }) => {
150-
const buildPath = getBuildPath(build, triplet);
154+
const buildPath = getBuildPath(build, triplet, configuration);
151155
const outputPath = path.join(buildPath, "out");
152156
// We want to use the CMake File API to query information later
153157
await cmakeFileApi.createSharedStatelessQuery(
@@ -161,6 +165,8 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
161165
source,
162166
"-B",
163167
buildPath,
168+
// Ideally, we would use the "Ninja Multi-Config" generator here,
169+
// but it doesn't support the "RelWithDebInfo" configuration on Android.
164170
"-G",
165171
"Ninja",
166172
"--toolchain",
@@ -179,8 +185,8 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
179185
}),
180186
);
181187
},
182-
async build({ triplet, spawn }, { target, build }) {
183-
const buildPath = getBuildPath(build, triplet);
188+
async build({ triplet, spawn }, { target, build, configuration }) {
189+
const buildPath = getBuildPath(build, triplet, configuration);
184190
await spawn("cmake", [
185191
"--build",
186192
buildPath,
@@ -201,8 +207,8 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
201207
{ triplet: Triplet; libraryPath: string }[]
202208
> = {};
203209

204-
for (const { spawn, triplet } of triplets) {
205-
const buildPath = getBuildPath(build, triplet);
210+
for (const { triplet, spawn } of triplets) {
211+
const buildPath = getBuildPath(build, triplet, configuration);
206212
assert(fs.existsSync(buildPath), `Expected a directory at ${buildPath}`);
207213
const targets = await cmakeFileApi.readCurrentTargetsDeep(
208214
buildPath,

packages/host/src/node/cli/apple.ts

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const XcframeworkInfoSchema = zod.looseObject({
5959
BinaryPath: zod.string(),
6060
LibraryIdentifier: zod.string(),
6161
LibraryPath: zod.string(),
62+
DebugSymbolsPath: zod.string().optional(),
6263
}),
6364
),
6465
CFBundlePackageType: zod.literal("XFWK"),
@@ -100,13 +101,12 @@ export async function writeFrameworkInfo(
100101

101102
type LinkFrameworkOptions = {
102103
frameworkPath: string;
104+
debugSymbolsPath?: string;
103105
newLibraryName: string;
104106
};
105107

106-
export async function linkFramework({
107-
frameworkPath,
108-
newLibraryName,
109-
}: LinkFrameworkOptions) {
108+
export async function linkFramework(options: LinkFrameworkOptions) {
109+
const { frameworkPath } = options;
110110
assert.equal(
111111
process.platform,
112112
"darwin",
@@ -117,14 +117,15 @@ export async function linkFramework({
117117
`Expected framework at '${frameworkPath}'`,
118118
);
119119
if (fs.existsSync(path.join(frameworkPath, "Versions"))) {
120-
await linkVersionedFramework({ frameworkPath, newLibraryName });
120+
await linkVersionedFramework(options);
121121
} else {
122-
await linkFlatFramework({ frameworkPath, newLibraryName });
122+
await linkFlatFramework(options);
123123
}
124124
}
125125

126126
export async function linkFlatFramework({
127127
frameworkPath,
128+
debugSymbolsPath,
128129
newLibraryName,
129130
}: LinkFrameworkOptions) {
130131
assert.equal(
@@ -151,16 +152,44 @@ export async function linkFlatFramework({
151152
...frameworkInfo,
152153
CFBundleExecutable: newLibraryName,
153154
});
155+
154156
// Rename the actual binary
155157
await fs.promises.rename(
156158
path.join(frameworkPath, frameworkInfo.CFBundleExecutable),
157159
path.join(frameworkPath, newLibraryName),
158160
);
159161
// Rename the framework directory
160-
await fs.promises.rename(
161-
frameworkPath,
162-
path.join(path.dirname(frameworkPath), `${newLibraryName}.framework`),
162+
const newFrameworkPath = path.join(
163+
path.dirname(frameworkPath),
164+
`${newLibraryName}.framework`,
163165
);
166+
await fs.promises.rename(frameworkPath, newFrameworkPath);
167+
168+
if (debugSymbolsPath) {
169+
const frameworkDebugSymbolsPath = path.join(
170+
debugSymbolsPath,
171+
`${path.basename(frameworkPath)}.dSYM`,
172+
);
173+
if (fs.existsSync(frameworkDebugSymbolsPath)) {
174+
// Remove existing DWARF data
175+
await fs.promises.rm(frameworkDebugSymbolsPath, {
176+
recursive: true,
177+
force: true,
178+
});
179+
// Rebuild DWARF data
180+
await spawn(
181+
"dsymutil",
182+
[
183+
path.join(newFrameworkPath, newLibraryName),
184+
"-o",
185+
path.join(debugSymbolsPath, newLibraryName + ".dSYM"),
186+
],
187+
{
188+
outputMode: "buffered",
189+
},
190+
);
191+
}
192+
}
164193
}
165194

166195
export async function linkVersionedFramework({
@@ -282,7 +311,17 @@ export async function linkXcframework({
282311
framework.LibraryIdentifier,
283312
framework.LibraryPath,
284313
);
285-
await linkFramework({ frameworkPath, newLibraryName });
314+
await linkFramework({
315+
frameworkPath,
316+
newLibraryName,
317+
debugSymbolsPath: framework.DebugSymbolsPath
318+
? path.join(
319+
outputPath,
320+
framework.LibraryIdentifier,
321+
framework.DebugSymbolsPath,
322+
)
323+
: undefined,
324+
});
286325
}),
287326
);
288327

packages/host/src/node/path-utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,17 @@ export function getLatestMtime(fromPath: string): number {
521521
// https://github.com/TooTallNate/node-bindings/blob/v1.3.0/bindings.js#L21
522522
const nodeBindingsSubdirs = [
523523
"./",
524+
"./build/MinSizeRel",
525+
"./build/RelWithDebInfo",
524526
"./build/Release",
525527
"./build/Debug",
526528
"./build",
529+
"./out/MinSizeRel",
530+
"./out/RelWithDebInfo",
527531
"./out/Release",
528532
"./out/Debug",
533+
"./MinSizeRel",
534+
"./RelWithDebInfo",
529535
"./Release",
530536
"./Debug",
531537
];

packages/host/src/node/prebuilds/apple.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,19 @@ export async function createXCframework({
9999
"xcodebuild",
100100
[
101101
"-create-xcframework",
102-
...frameworkPaths.flatMap((p) => ["-framework", p]),
102+
...frameworkPaths.flatMap((frameworkPath) => {
103+
const debugSymbolPath = frameworkPath + ".dSYM";
104+
if (fs.existsSync(debugSymbolPath)) {
105+
return [
106+
"-framework",
107+
frameworkPath,
108+
"-debug-symbols",
109+
debugSymbolPath,
110+
];
111+
} else {
112+
return ["-framework", frameworkPath];
113+
}
114+
}),
103115
"-output",
104116
xcodeOutputPath,
105117
],

packages/node-addon-examples/scripts/build-examples.mts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ const projectDirectories = findCMakeProjects();
66

77
for (const projectDirectory of projectDirectories) {
88
console.log(`Running "cmake-rn" in ${projectDirectory}`);
9-
execSync(
10-
"cmake-rn",
11-
// "cmake-rn --android --apple",
12-
// "cmake-rn --triplet aarch64-linux-android --triplet arm64-apple-ios-sim",
13-
{
14-
cwd: projectDirectory,
15-
stdio: "inherit",
16-
},
17-
);
9+
execSync("cmake-rn --configuration RelWithDebInfo", {
10+
cwd: projectDirectory,
11+
stdio: "inherit",
12+
});
1813
console.log();
1914
}

0 commit comments

Comments
 (0)