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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use crate::config::{AndroidBuildTarget, AndroidConfig};
use cargo::core::{Target, TargetKind, Workspace};
use cargo::util::{process, CargoResult, ProcessBuilder};
use failure::format_err;
use std::ffi::OsStr;
use std::path::PathBuf;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BuildTarget {
name: String,
kind: TargetKind,
}
impl BuildTarget {
pub fn new(name: String, kind: TargetKind) -> Self {
Self { name, kind }
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> &TargetKind {
&self.kind
}
}
impl From<&Target> for BuildTarget {
fn from(target: &Target) -> Self {
Self {
name: target.name().to_owned(),
kind: target.kind().to_owned(),
}
}
}
pub fn get_root_build_directory(workspace: &Workspace, config: &AndroidConfig) -> PathBuf {
let android_artifacts_dir = workspace
.target_dir()
.join("android-artifacts")
.into_path_unlocked();
if config.release {
android_artifacts_dir.join("release")
} else {
android_artifacts_dir.join("debug")
}
}
pub fn get_target_directory(
root_build_dir: &PathBuf,
target: &BuildTarget,
) -> CargoResult<PathBuf> {
let target_directory = match target.kind() {
TargetKind::Bin => root_build_dir.join("bin"),
TargetKind::ExampleBin => root_build_dir.join("examples"),
_ => unreachable!("Unexpected target kind"),
};
let target_directory = target_directory.join(target.name());
Ok(target_directory)
}
pub fn make_path(config: &AndroidConfig) -> PathBuf {
config.ndk_path.join("prebuild").join(HOST_TAG).join("make")
}
pub fn llvm_toolchain_root(config: &AndroidConfig) -> PathBuf {
config
.ndk_path
.join("toolchains")
.join("llvm")
.join("prebuilt")
.join(HOST_TAG)
}
pub fn find_ndk_path<F>(platform: u32, path_builder: F) -> CargoResult<PathBuf>
where
F: Fn(u32) -> PathBuf,
{
let mut tmp_platform = platform;
while tmp_platform > 1 {
let path = path_builder(tmp_platform);
if path.exists() {
return Ok(path);
}
tmp_platform -= 1;
}
tmp_platform = platform;
while tmp_platform < 100 {
let path = path_builder(tmp_platform);
if path.exists() {
return Ok(path);
}
tmp_platform += 1;
}
Err(format_err!("Unable to find NDK file"))
}
pub fn find_clang(
config: &AndroidConfig,
build_target: AndroidBuildTarget,
) -> CargoResult<PathBuf> {
let bin_folder = llvm_toolchain_root(config).join("bin");
find_ndk_path(config.min_sdk_version, |platform| {
bin_folder.join(format!(
"{}{}-clang{}",
build_target.ndk_llvm_triple(),
platform,
EXECUTABLE_SUFFIX_CMD
))
})
.map_err(|_| format_err!("Unable to find NDK clang"))
}
pub fn find_clang_cpp(
config: &AndroidConfig,
build_target: AndroidBuildTarget,
) -> CargoResult<PathBuf> {
let bin_folder = llvm_toolchain_root(config).join("bin");
find_ndk_path(config.min_sdk_version, |platform| {
bin_folder.join(format!(
"{}{}-clang++{}",
build_target.ndk_llvm_triple(),
platform,
EXECUTABLE_SUFFIX_CMD
))
})
.map_err(|_| format_err!("Unable to find NDK clang++"))
}
pub fn find_ar(config: &AndroidConfig, build_target: AndroidBuildTarget) -> CargoResult<PathBuf> {
let ar_path = llvm_toolchain_root(config).join("bin").join(format!(
"{}-ar{}",
build_target.ndk_triple(),
EXECUTABLE_SUFFIX_EXE
));
if ar_path.exists() {
Ok(ar_path)
} else {
Err(format_err!(
"Unable to find ar at `{}`",
ar_path.to_string_lossy()
))
}
}
pub fn find_readelf(
config: &AndroidConfig,
build_target: AndroidBuildTarget,
) -> CargoResult<PathBuf> {
let readelf_path = llvm_toolchain_root(config).join("bin").join(format!(
"{}-readelf{}",
build_target.ndk_triple(),
EXECUTABLE_SUFFIX_EXE
));
if readelf_path.exists() {
Ok(readelf_path)
} else {
Err(format_err!(
"Unable to find readelf at `{}`",
readelf_path.to_string_lossy()
))
}
}
pub fn script_process(cmd: impl AsRef<OsStr>) -> ProcessBuilder {
if cfg!(target_os = "windows") {
let mut pb = process("cmd");
pb.arg("/C").arg(cmd);
pb
} else {
process(cmd)
}
}
#[cfg(all(target_os = "windows", target_pointer_width = "64"))]
const HOST_TAG: &str = "windows-x86_64";
#[cfg(all(target_os = "windows", target_pointer_width = "32"))]
const HOST_TAG: &str = "windows";
#[cfg(target_os = "linux")]
const HOST_TAG: &str = "linux-x86_64";
#[cfg(target_os = "macos")]
const HOST_TAG: &str = "darwin-x86_64";
#[cfg(target_os = "windows")]
const EXECUTABLE_SUFFIX_EXE: &str = ".exe";
#[cfg(not(target_os = "windows"))]
const EXECUTABLE_SUFFIX_EXE: &str = "";
#[cfg(target_os = "windows")]
const EXECUTABLE_SUFFIX_CMD: &str = ".cmd";
#[cfg(not(target_os = "windows"))]
const EXECUTABLE_SUFFIX_CMD: &str = "";
#[cfg(target_os = "windows")]
pub const EXECUTABLE_SUFFIX_BAT: &str = ".bat";
#[cfg(not(target_os = "windows"))]
pub const EXECUTABLE_SUFFIX_BAT: &str = "";