-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathandroid.ts
More file actions
103 lines (93 loc) · 2.84 KB
/
android.ts
File metadata and controls
103 lines (93 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { AndroidTriplet } from "react-native-node-api";
export const DEFAULT_ANDROID_TRIPLETS = [
"aarch64-linux-android",
"armv7a-linux-androideabi",
"i686-linux-android",
"x86_64-linux-android",
] as const satisfies AndroidTriplet[];
type AndroidArchitecture = "armeabi-v7a" | "arm64-v8a" | "x86" | "x86_64";
export const ANDROID_ARCHITECTURES = {
"armv7a-linux-androideabi": "armeabi-v7a",
"aarch64-linux-android": "arm64-v8a",
"i686-linux-android": "x86",
"x86_64-linux-android": "x86_64",
} satisfies Record<AndroidTriplet, AndroidArchitecture>;
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");
assert(
fs.existsSync(ANDROID_HOME),
`Expected the Android SDK at ${ANDROID_HOME}`
);
const installNdkCommand = `sdkmanager --install "ndk;${ndkVersion}"`;
const ndkPath = path.resolve(ANDROID_HOME, "ndk", ndkVersion);
assert(
fs.existsSync(ndkPath),
`Missing Android NDK v${ndkVersion} (at ${ndkPath}) - run: ${installNdkCommand}`
);
const toolchainPath = path.join(
ndkPath,
"build/cmake/android.toolchain.cmake"
);
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",
"--toolchain",
toolchainPath,
"-D",
"CMAKE_SYSTEM_NAME=Android",
// "-D",
// `CPACK_SYSTEM_NAME=Android-${architecture}`,
// "-D",
// `CMAKE_INSTALL_PREFIX=${installPath}`,
// "-D",
// `CMAKE_BUILD_TYPE=${configuration}`,
"-D",
"CMAKE_MAKE_PROGRAM=ninja",
// "-D",
// "CMAKE_C_COMPILER_LAUNCHER=ccache",
// "-D",
// "CMAKE_CXX_COMPILER_LAUNCHER=ccache",
"-D",
`ANDROID_NDK=${ndkPath}`,
"-D",
`ANDROID_ABI=${architecture}`,
"-D",
"ANDROID_TOOLCHAIN=clang",
"-D",
`ANDROID_PLATFORM=${sdkVersion}`,
"-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(" ")}`,
];
}
export function isAndroidSupported() {
const { ANDROID_HOME } = process.env;
return typeof ANDROID_HOME === "string" && fs.existsSync(ANDROID_HOME);
}