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
use ansi_term::Colour::Red;
use cargo::core::compiler::{CompileKind, CompileMode, CompileTarget, CrateType};
use cargo::core::manifest::TargetKind;
use cargo::core::Workspace;
use cargo::ops::{compile, CompileOptions};
use cargo::util::interning::InternedString;
use cargo::util::Config;
use std::fs::{create_dir_all, read_dir};
use std::path::{Path, PathBuf};
use super::{AndroidContext, AndroidError};
use crate::android::Target;
use crate::core::{Manager, Task};
pub struct BuildRuntimeLibrary {
pub target: Target<'static>,
pub profile: &'static str,
}
impl BuildRuntimeLibrary {
pub fn build(
&self,
toolchain: &Path,
manifest_path: &Path,
polyhorn_jar_dir: &Path,
) -> Result<PathBuf, AndroidError> {
let mut sysroot = toolchain.to_path_buf();
sysroot.push("sysroot");
let mut toolchain = toolchain.to_path_buf();
toolchain.push("bin");
let mut ar = toolchain.to_path_buf();
ar.push(&self.target.ar);
let mut cc = toolchain.to_path_buf();
cc.push(&self.target.cc);
let mut cxx = toolchain.to_path_buf();
cxx.push(&self.target.cxx);
let mut linker = toolchain.to_path_buf();
linker.push(&self.target.linker);
std::env::set_var("TARGET_AR", ar);
std::env::set_var("TARGET_CC", cc);
std::env::set_var("TARGET_CXX", cxx);
std::env::set_var("POLYHORN_JAR_DIR", polyhorn_jar_dir);
std::env::set_var(
"CARGO_TARGET_".to_owned()
+ &self.target.llvm_triple.replace("-", "_").to_uppercase()
+ "_LINKER",
linker,
);
std::env::set_var(
"BINDGEN_EXTRA_CLANG_ARGS",
format!("--sysroot={}", sysroot.to_str().unwrap()),
);
let mut config = Config::default().unwrap();
config
.configure(0, false, None, false, false, false, &None, &[], &[])
.unwrap();
let mut workspace = Workspace::new(manifest_path, &config).unwrap();
for target in workspace
.current_mut()
.unwrap()
.manifest_mut()
.targets_mut()
{
match target.kind() {
TargetKind::Lib(_) => {
target.set_kind(TargetKind::Lib(vec![CrateType::Cdylib]));
}
_ => {}
}
}
let mut options = CompileOptions::new(&config, CompileMode::Build).unwrap();
options.target_rustc_args = Some(vec![
"-Clink-arg=-lc++_static".to_owned(),
"-Clink-arg=-lc++abi".to_owned(),
"-Clink-arg=-fuse-ld=lld".to_owned(),
]);
options.build_config.requested_profile = InternedString::new(self.profile);
options.build_config.requested_kinds = vec![CompileKind::Target(
CompileTarget::new(self.target.llvm_triple).unwrap(),
)];
match compile(&workspace, &options) {
Ok(mut compilation) => Ok(compilation.cdylibs.remove(0).1),
Err(error) => {
eprintln!("{}: {:?}", Red.bold().paint("error"), error);
Err(AndroidError::CompilationFailure)
}
}
}
}
impl Task for BuildRuntimeLibrary {
type Context = AndroidContext;
type Error = AndroidError;
fn verb(&self) -> &str {
"Building"
}
fn message(&self) -> &str {
"runtime library"
}
fn detail(&self) -> &str {
"for Android"
}
fn run(
&self,
mut context: AndroidContext,
_manager: &mut Manager,
) -> Result<AndroidContext, AndroidError> {
let mut toolchain = context.android_sdk_root.clone().unwrap();
toolchain.push("ndk-bundle/toolchains/llvm/prebuilt");
let toolchain = match read_dir(&toolchain) {
Ok(mut dir) => match dir.next() {
Some(Ok(entry)) => entry.path(),
_ => return Err(AndroidError::AndroidNDKNotFound(toolchain)),
},
Err(_) => return Err(AndroidError::AndroidNDKNotFound(toolchain)),
};
let mut manifest_path = context.config.manifest_dir.clone();
manifest_path.push("Cargo.toml");
let mut polyhorn_jar_dir = context.config.manifest_dir.clone();
polyhorn_jar_dir.push("target/polyhorn-android/app/libs");
let _ = create_dir_all(&polyhorn_jar_dir);
eprintln!("");
if let Some(android_sdk_root) = context.android_sdk_root.as_ref() {
std::env::set_var("ANDROID_SDK_ROOT", android_sdk_root);
}
if let Some(java_home) = context.java_home.as_ref() {
std::env::set_var("JAVA_HOME", java_home);
}
let result = self.build(&toolchain, &manifest_path, &polyhorn_jar_dir);
match result {
Ok(path) => {
context.products.insert(self.target.abi.to_owned(), path);
Ok(context)
}
Err(error) => Err(error),
}
}
}