-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathandroid.ts
More file actions
177 lines (162 loc) · 5.63 KB
/
android.ts
File metadata and controls
177 lines (162 loc) · 5.63 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { Option } from "@commander-js/extra-typings";
import {
createAndroidLibsDirectory,
determineAndroidLibsFilename,
AndroidTriplet,
isAndroidTriplet,
} from "react-native-node-api";
import type { Platform } from "./types.js";
import { oraPromise } from "ora";
import chalk from "chalk";
import { getWeakNodeApiVariables } from "../weak-node-api.js";
import { toDeclarationArguments } from "../cmake.js";
type Target = `${AndroidTriplet}-reactnative`;
// 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";
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>;
const ndkVersionOption = new Option(
"--ndk-version <version>",
"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);
type AndroidOpts = { ndkVersion: string; androidSdkVersion: string };
function tripletFromTarget(target: Target): AndroidTriplet {
const result = target.replaceAll(/-reactnative$/g, "") as AndroidTriplet;
assert(isAndroidTriplet(result), `Invalid Android triplet: ${target}`);
return result;
}
export const platform: Platform<Target[], AndroidOpts> = {
id: "android",
name: "Android",
targets: [
"aarch64-linux-android-reactnative",
"armv7a-linux-androideabi-reactnative",
"i686-linux-android-reactnative",
"x86_64-linux-android-reactnative",
],
defaultTargets() {
if (process.arch === "arm64") {
return ["aarch64-linux-android-reactnative"];
} else if (process.arch === "x64") {
return ["x86_64-linux-android-reactnative"];
} else {
return [];
}
},
amendCommand(command) {
return command
.addOption(ndkVersionOption)
.addOption(androidSdkVersionOption);
},
configureArgs(
{ target },
{ configuration, ndkVersion, androidSdkVersion, weakNodeApiLinkage },
) {
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 triplet = tripletFromTarget(target);
return [
"-G",
"Ninja",
"--toolchain",
toolchainPath,
...toDeclarationArguments({
CMAKE_SYSTEM_NAME: "Android",
CMAKE_MAKE_PROGRAM: "ninja",
CMAKE_BUILD_TYPE: configuration,
// "CMAKE_C_COMPILER_LAUNCHER": "ccache",
// "CMAKE_CXX_COMPILER_LAUNCHER": "ccache",
ANDROID_NDK: ndkPath,
ANDROID_ABI: ANDROID_ARCHITECTURES[triplet],
ANDROID_TOOLCHAIN: "clang",
ANDROID_PLATFORM: androidSdkVersion,
// TODO: Make this configurable
ANDROID_STL: "c++_shared",
...(weakNodeApiLinkage ? getWeakNodeApiVariables(triplet) : {}),
}),
];
},
buildArgs() {
return [];
},
isSupportedByHost() {
const { ANDROID_HOME } = process.env;
return typeof ANDROID_HOME === "string" && fs.existsSync(ANDROID_HOME);
},
async postBuild({ outputPath, targets }, { autoLink }) {
// TODO: Include `configuration` in the output path
const libraryPathByTriplet = Object.fromEntries(
await Promise.all(
targets.map(async ({ target, outputPath }) => {
assert(
fs.existsSync(outputPath),
`Expected a directory at ${outputPath}`,
);
// Expect binary file(s), either .node or .so
const dirents = await fs.promises.readdir(outputPath, {
withFileTypes: true,
});
const result = dirents
.filter(
(dirent) =>
dirent.isFile() &&
(dirent.name.endsWith(".so") || dirent.name.endsWith(".node")),
)
.map((dirent) => path.join(dirent.parentPath, dirent.name));
assert.equal(result.length, 1, "Expected exactly one library file");
const triplet = tripletFromTarget(target);
return [triplet, result[0]] as const;
}),
),
) as Record<AndroidTriplet, string>;
const androidLibsFilename = determineAndroidLibsFilename(
Object.values(libraryPathByTriplet),
);
const androidLibsOutputPath = path.resolve(outputPath, androidLibsFilename);
await oraPromise(
createAndroidLibsDirectory({
outputPath: androidLibsOutputPath,
libraryPathByTriplet,
autoLink,
}),
{
text: "Assembling Android libs directory",
successText: `Android libs directory assembled into ${chalk.dim(
path.relative(process.cwd(), androidLibsOutputPath),
)}`,
failText: ({ message }) =>
`Failed to assemble Android libs directory: ${message}`,
},
);
},
};