Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions packages/cmake-rn/src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export const ANDROID_ARCHITECTURES = {
type AndroidConfigureOptions = {
triplet: AndroidTriplet;
ndkVersion: string;
sdkVersion: string;
};

export function getAndroidConfigureCmakeArgs({
triplet,
ndkVersion,
sdkVersion,
}: AndroidConfigureOptions) {
const { ANDROID_HOME } = process.env;
assert(typeof ANDROID_HOME === "string", "Missing env variable ANDROID_HOME");
Expand Down Expand Up @@ -80,8 +82,8 @@ export function getAndroidConfigureCmakeArgs({
`ANDROID_ABI=${architecture}`,
"-D",
"ANDROID_TOOLCHAIN=clang",
// "-D",
// `ANDROID_NATIVE_API_LEVEL=${ANDROID_API_LEVEL}`,
"-D",
`ANDROID_PLATFORM=${sdkVersion}`,
"-D",
"ANDROID_STL=c++_shared",
// Pass linker flags to avoid errors from undefined symbols
Expand Down
44 changes: 38 additions & 6 deletions packages/cmake-rn/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ EventEmitter.defaultMaxListeners = 100;

// This should match https://github.com/react-native-community/template/blob/main/template/android/build.gradle#L7
const DEFAULT_NDK_VERSION = "27.1.12297006";
const DEFAULT_ANDROID_SDK_VERSION = "24";

// TODO: Add automatic ccache support

const verboseOption = new Option(
"--verbose",
"Print more output during the build"
).default(process.env.CI === "true");

const sourcePathOption = new Option(
"--source <path>",
"Specify the source directory containing a CMakeLists.txt file"
Expand Down Expand Up @@ -83,6 +89,11 @@ const ndkVersionOption = new Option(
"The NDK version to use for Android builds"
).default(DEFAULT_NDK_VERSION);

const androidSdkVersionOption = new Option(
"--android-sdk-version <version>",
"The Android SDK version to use for Android builds"
).default(DEFAULT_ANDROID_SDK_VERSION);

const noAutoLinkOption = new Option(
"--no-auto-link",
"Don't mark the output as auto-linkable by react-native-node-api"
Expand All @@ -100,6 +111,7 @@ const xcframeworkExtensionOption = new Option(

export const program = new Command("cmake-rn")
.description("Build React Native Node API modules with CMake")
.addOption(verboseOption)
.addOption(sourcePathOption)
.addOption(configurationOption)
.addOption(tripletOption)
Expand All @@ -109,6 +121,7 @@ export const program = new Command("cmake-rn")
.addOption(outPathOption)
.addOption(cleanOption)
.addOption(ndkVersionOption)
.addOption(androidSdkVersionOption)
.addOption(noAutoLinkOption)
.addOption(noWeakNodeApiLinkageOption)
.addOption(xcframeworkExtensionOption)
Expand Down Expand Up @@ -163,6 +176,7 @@ export const program = new Command("cmake-rn")
// Configure every triplet project
await oraPromise(Promise.all(tripletContext.map(configureProject)), {
text: "Configuring projects",
isSilent: globalContext.verbose,
successText: "Configured projects",
failText: ({ message }) => `Failed to configure projects: ${message}`,
});
Expand All @@ -182,6 +196,7 @@ export const program = new Command("cmake-rn")
),
{
text: "Building projects",
isSilent: globalContext.verbose,
successText: "Built projects",
failText: ({ message }) => `Failed to build projects: ${message}`,
}
Expand Down Expand Up @@ -340,12 +355,19 @@ function getTripletBuildPath(buildPath: string, triplet: SupportedTriplet) {

function getTripletConfigureCmakeArgs(
triplet: SupportedTriplet,
{ ndkVersion }: Pick<GlobalContext, "ndkVersion" | "weakNodeApiLinkage">
{
ndkVersion,
androidSdkVersion,
}: Pick<
GlobalContext,
"ndkVersion" | "androidSdkVersion" | "weakNodeApiLinkage"
>
) {
if (isAndroidTriplet(triplet)) {
return getAndroidConfigureCmakeArgs({
triplet,
ndkVersion,
sdkVersion: androidSdkVersion,
});
} else if (isAppleTriplet(triplet)) {
return getAppleConfigureCmakeArgs({ triplet });
Expand All @@ -365,8 +387,15 @@ function getBuildArgs(triplet: SupportedTriplet) {
}

async function configureProject(context: TripletScopedContext) {
const { triplet, tripletBuildPath, source, ndkVersion, weakNodeApiLinkage } =
context;
const {
verbose,
triplet,
tripletBuildPath,
source,
ndkVersion,
androidSdkVersion,
weakNodeApiLinkage,
} = context;
await spawn(
"cmake",
[
Expand All @@ -378,16 +407,18 @@ async function configureProject(context: TripletScopedContext) {
...getTripletConfigureCmakeArgs(triplet, {
ndkVersion,
weakNodeApiLinkage,
androidSdkVersion,
}),
],
{
outputMode: "buffered",
outputMode: verbose ? "inherit" : "buffered",
outputPrefix: verbose ? chalk.dim(`[${triplet}] `) : undefined,
}
);
}

async function buildProject(context: TripletScopedContext) {
const { triplet, tripletBuildPath, configuration } = context;
const { verbose, triplet, tripletBuildPath, configuration } = context;
await spawn(
"cmake",
[
Expand All @@ -399,7 +430,8 @@ async function buildProject(context: TripletScopedContext) {
...getBuildArgs(triplet),
],
{
outputMode: "buffered",
outputMode: verbose ? "inherit" : "buffered",
outputPrefix: verbose ? chalk.dim(`[${triplet}] `) : undefined,
}
);
}
Expand Down
Loading