Skip to content

Ninja makefile fallback #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
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
29 changes: 11 additions & 18 deletions packages/react-native-node-api-cmake/src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import path from "node:path";

import { AndroidTriplet } from "react-native-node-api-modules";

import { isNinjaAvailable } from "./ninja.js";

function getCmakeGenerator() {
if (isNinjaAvailable()) {
return "Ninja";
} else {
return "Unix Makefiles";
}
}

export const DEFAULT_ANDROID_TRIPLETS = [
"aarch64-linux-android",
"armv7a-linux-androideabi",
Expand Down Expand Up @@ -48,16 +58,9 @@ export function getAndroidConfigureCmakeArgs({
);
const architecture = ANDROID_ARCHITECTURES[triplet];

const linkerFlags: string[] = [
// `--no-version-undefined`,
// `--whole-archive`,
// `--no-whole-archive`,
];

return [
// Use the XCode as generator for Apple platforms
"-G",
"Ninja",
getCmakeGenerator(),
"--toolchain",
toolchainPath,
"-D",
Expand All @@ -68,8 +71,6 @@ export function getAndroidConfigureCmakeArgs({
// `CMAKE_INSTALL_PREFIX=${installPath}`,
// "-D",
// `CMAKE_BUILD_TYPE=${configuration}`,
"-D",
"CMAKE_MAKE_PROGRAM=ninja",
// "-D",
// "CMAKE_C_COMPILER_LAUNCHER=ccache",
// "-D",
Expand All @@ -84,13 +85,5 @@ export function getAndroidConfigureCmakeArgs({
// `ANDROID_NATIVE_API_LEVEL=${ANDROID_API_LEVEL}`,
"-D",
"ANDROID_STL=c++_shared",
// Pass linker flags to avoid errors from undefined symbols
// TODO: Link against a weak-node-api to avoid this (or whatever other lib which will be providing the symbols)
// "-D",
// `CMAKE_SHARED_LINKER_FLAGS="-Wl,--allow-shlib-undefined"`,
"-D",
`CMAKE_SHARED_LINKER_FLAGS=${linkerFlags
.map((flag) => `-Wl,${flag}`)
.join(" ")}`,
];
}
39 changes: 39 additions & 0 deletions packages/react-native-node-api-cmake/src/ninja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import chalk from "chalk";
import cp from "node:child_process";

export function getNinjaVersion(): string | undefined {
try {
const ninjaVersion = cp
.execSync("ninja --version", {
encoding: "utf-8",
stdio: "pipe",
})
.trim();
return ninjaVersion;
} catch {
return undefined;
}
}

let ninjaAvailable: boolean | undefined;

export function isNinjaAvailable(log = true): boolean {
if (typeof ninjaAvailable === "boolean") {
return ninjaAvailable;
}

const ninjaVersion = getNinjaVersion();
ninjaAvailable = typeof ninjaVersion === "string";

if (log && ninjaAvailable) {
console.log(chalk.dim(`Using Ninja ${ninjaVersion} for Android builds`));
} else if (log && !ninjaAvailable) {
console.log(
`Using Unix Makefiles as fallback, as Ninja was not found.\n${chalk.dim(
"Install Ninja for faster builds."
)}`
);
}

return ninjaAvailable;
}