-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathandroid.ts
More file actions
189 lines (176 loc) · 5.5 KB
/
android.ts
File metadata and controls
189 lines (176 loc) · 5.5 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
178
179
180
181
182
183
184
185
186
187
188
189
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { Option, oraPromise, chalk } from "@react-native-node-api/cli-utils";
import {
createAndroidLibsDirectory,
AndroidTriplet as Triplet,
} from "react-native-node-api";
import * as cmakeFileApi from "cmake-file-api";
import type { Platform } from "./types.js";
// 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<Triplet, 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 };
export const platform: Platform<Triplet[], AndroidOpts> = {
id: "android",
name: "Android",
triplets: [
"aarch64-linux-android",
"armv7a-linux-androideabi",
"i686-linux-android",
"x86_64-linux-android",
],
defaultTriplets() {
if (process.arch === "arm64") {
return ["aarch64-linux-android"];
} else if (process.arch === "x64") {
return ["x86_64-linux-android"];
} else {
return [];
}
},
amendCommand(command) {
return command
.addOption(ndkVersionOption)
.addOption(androidSdkVersionOption);
},
configureArgs({ triplet }, { ndkVersion, androidSdkVersion }) {
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];
return [
"-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=${androidSdkVersion}`,
"-D",
// TODO: Make this configurable
"ANDROID_STL=c++_shared",
];
},
buildArgs() {
return [];
},
isSupportedByHost() {
const { ANDROID_HOME } = process.env;
return typeof ANDROID_HOME === "string" && fs.existsSync(ANDROID_HOME);
},
async postBuild(
{ outputPath, triplets },
{ autoLink, configuration, target },
) {
const prebuilds: Record<
string,
{ triplet: Triplet; libraryPath: string }[]
> = {};
for (const { triplet, buildPath } of triplets) {
assert(fs.existsSync(buildPath), `Expected a directory at ${buildPath}`);
const targets = await cmakeFileApi.readCurrentTargetsDeep(
buildPath,
configuration,
"2.0",
);
const sharedLibraries = targets.filter(
({ type, name }) =>
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] = [];
}
prebuilds[sharedLibrary.name].push({
triplet,
libraryPath: path.join(buildPath, artifact.path),
});
}
for (const [libraryName, libraries] of Object.entries(prebuilds)) {
const prebuildOutputPath = path.resolve(
outputPath,
`${libraryName}.android.node`,
);
await oraPromise(
createAndroidLibsDirectory({
outputPath: prebuildOutputPath,
libraries,
autoLink,
}),
{
text: `Assembling Android libs directory (${libraryName})`,
successText: `Android libs directory (${libraryName}) assembled into ${chalk.dim(
path.relative(process.cwd(), prebuildOutputPath),
)}`,
failText: ({ message }) =>
`Failed to assemble Android libs directory (${libraryName}): ${message}`,
},
);
}
},
};