Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-trains-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cmake-rn": minor
---

Add support for building projects declaring multiple shared object libraries into Node-API addons
5 changes: 5 additions & 0 deletions .changeset/real-emus-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gyp-to-cmake": minor
---

Add --namespaced-targets to allow a root project to add many sub-projects
62 changes: 42 additions & 20 deletions packages/cmake-rn/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import path from "node:path";
import fs from "node:fs";
import { EventEmitter } from "node:events";
import os from "node:os";

import {
chalk,
Expand All @@ -11,6 +12,7 @@ import {
oraPromise,
assertFixable,
wrapAction,
pLimit,
} from "@react-native-node-api/cli-utils";

import {
Expand All @@ -23,7 +25,7 @@ import { Platform } from "./platforms/types.js";
import { getCcachePath } from "./ccache.js";

// We're attaching a lot of listeners when spawning in parallel
EventEmitter.defaultMaxListeners = 100;
EventEmitter.defaultMaxListeners = 500;

const verboseOption = new Option(
"--verbose",
Expand Down Expand Up @@ -129,6 +131,16 @@ const ccachePathOption = new Option(
"Specify the path to the ccache executable",
).default(getCcachePath());

const concurrencyOption = new Option(
"--concurrency <limit>",
"Limit the number of concurrent tasks",
)
.argParser((value) => parseInt(value, 10))
.default(
os.availableParallelism(),
`${os.availableParallelism()} or 1 when verbose is enabled`,
);

let program = new Command("cmake-rn")
.description("Build React Native Node API modules with CMake")
.addOption(tripletOption)
Expand All @@ -144,7 +156,8 @@ let program = new Command("cmake-rn")
.addOption(noAutoLinkOption)
.addOption(noWeakNodeApiLinkageOption)
.addOption(cmakeJsOption)
.addOption(ccachePathOption);
.addOption(ccachePathOption)
.addOption(concurrencyOption);

for (const platform of platforms) {
const allOption = new Option(
Expand Down Expand Up @@ -181,6 +194,7 @@ program = program.action(
out,
build: buildPath,
ccachePath,
concurrency,
} = baseOptions;

assertFixable(
Expand Down Expand Up @@ -232,6 +246,8 @@ program = program.action(
}
}

const limit = pLimit(concurrency);

const tripletContexts = [...triplets].map((triplet) => {
const platform = findPlatformForTriplet(triplet);

Expand All @@ -244,17 +260,21 @@ program = program.action(
triplet,
platform,
async spawn(command: string, args: string[], cwd?: string) {
const outputPrefix = verbose ? chalk.dim(`[${triplet}] `) : undefined;
if (verbose) {
console.log(
`${outputPrefix}» ${command} ${args.map((arg) => chalk.dim(`${arg}`)).join(" ")}`,
cwd ? `(in ${chalk.dim(cwd)})` : "",
);
}
await spawn(command, args, {
outputMode: verbose ? "inherit" : "buffered",
outputPrefix,
cwd,
await limit(async () => {
const outputPrefix = verbose
? chalk.dim(`[${triplet}] `)
: undefined;
if (verbose) {
console.log(
`${outputPrefix}» ${command} ${args.map((arg) => chalk.dim(`${arg}`)).join(" ")}`,
cwd ? `(in ${chalk.dim(cwd)})` : "",
);
}
await spawn(command, args, {
outputMode: verbose ? "inherit" : "buffered",
outputPrefix,
cwd,
});
});
},
};
Expand All @@ -280,13 +300,15 @@ program = program.action(
relevantTriplets,
baseOptions,
(command, args, cwd) =>
spawn(command, args, {
outputMode: verbose ? "inherit" : "buffered",
outputPrefix: verbose
? chalk.dim(`[${platform.name}] `)
: undefined,
cwd,
}),
limit(() =>
spawn(command, args, {
outputMode: verbose ? "inherit" : "buffered",
outputPrefix: verbose
? chalk.dim(`[${platform.name}] `)
: undefined,
cwd,
}),
),
);
}
}),
Expand Down
68 changes: 32 additions & 36 deletions packages/cmake-rn/src/platforms/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
await Promise.all(
triplets.map(async ({ triplet, spawn }) => {
const buildPath = getBuildPath(build, triplet, configuration);
const outputPath = path.join(buildPath, "out");
// We want to use the CMake File API to query information later
await cmakeFileApi.createSharedStatelessQuery(
buildPath,
Expand All @@ -189,7 +188,6 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
...commonDefinitions,
{
// "CPACK_SYSTEM_NAME": `Android-${architecture}`,
CMAKE_LIBRARY_OUTPUT_DIRECTORY: outputPath,
ANDROID_ABI: ANDROID_ARCHITECTURES[triplet],
},
]),
Expand Down Expand Up @@ -232,41 +230,39 @@ export const platform: Platform<Triplet[], AndroidOpts> = {
type === "SHARED_LIBRARY" &&
(target.length === 0 || target.includes(name)),
);
assert.equal(
sharedLibraries.length,
1,
"Expected exactly one shared library",
);
const [sharedLibrary] = sharedLibraries;
const { artifacts } = sharedLibrary;
assert(
artifacts && artifacts.length === 1,
"Expected exactly one artifact",
);
const [artifact] = artifacts;
// Add prebuild entry, creating a new entry if needed
if (!(sharedLibrary.name in prebuilds)) {
prebuilds[sharedLibrary.name] = [];
}
const libraryPath = path.join(buildPath, artifact.path);
assert(
fs.existsSync(libraryPath),
`Expected built library at ${libraryPath}`,
);
await Promise.all(
sharedLibraries.map(async (sharedLibrary) => {
const { artifacts } = sharedLibrary;
assert(
artifacts && artifacts.length === 1,
"Expected exactly one artifact",
);
const [artifact] = artifacts;
// Add prebuild entry, creating a new entry if needed
if (!(sharedLibrary.name in prebuilds)) {
prebuilds[sharedLibrary.name] = [];
}
const libraryPath = path.join(buildPath, artifact.path);
assert(
fs.existsSync(libraryPath),
`Expected built library at ${libraryPath}`,
);

if (strip) {
const llvmBinPath = getNdkLlvmBinPath(getNdkPath(ndkVersion));
const stripToolPath = path.join(llvmBinPath, `llvm-strip`);
assert(
fs.existsSync(stripToolPath),
`Expected llvm-strip to exist at ${stripToolPath}`,
);
await spawn(stripToolPath, [libraryPath]);
}
prebuilds[sharedLibrary.name].push({
triplet,
libraryPath,
});
if (strip) {
const llvmBinPath = getNdkLlvmBinPath(getNdkPath(ndkVersion));
const stripToolPath = path.join(llvmBinPath, `llvm-strip`);
assert(
fs.existsSync(stripToolPath),
`Expected llvm-strip to exist at ${stripToolPath}`,
);
await spawn(stripToolPath, [libraryPath]);
}
prebuilds[sharedLibrary.name].push({
triplet,
libraryPath,
});
}),
);
}

for (const [libraryName, libraries] of Object.entries(prebuilds)) {
Expand Down
Loading
Loading