-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
216 lines (186 loc) · 7.61 KB
/
build.rs
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
#[allow(unused_imports)]
use dotenvy::from_filename_override;
#[allow(unused_imports)]
use std::{env, path::Path, path::PathBuf};
#[cfg(target_os = "macos")]
#[allow(dead_code)]
const TARGET_OS: &str = "darwin";
#[cfg(target_os = "linux")]
#[allow(dead_code)]
const TARGET_OS: &str = "linux";
fn main() {
let manifest_dir_path =
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is not set");
let manifest_dir_path = Path::new(&manifest_dir_path);
#[allow(unused_variables)]
let target_triple = env::var("TARGET").unwrap();
let profile = env::var("PROFILE").unwrap();
let executorch_home =
if let Some(executorch_home) = env::var("EXECUTORCH_HOME").ok() {
executorch_home
} else {
Path::new(&manifest_dir_path)
.join("executorch-prebuilt")
.display()
.to_string()
};
/* ------------------------------------------------------------------------
* Compile C Interface for Executorch used in Rust
* ------------------------------------------------------------------------ */
let mut builder = &mut cc::Build::new();
builder = builder
.cpp(true)
.file("src/cpp/c_interface.cpp")
.include("src/cpp")
.include(format!("{}/include", executorch_home))
.flag("-std=c++17");
#[cfg(feature = "android")]
{
let android_env_path =
if let Some(android_env_path) = env::var("ANDROID_ENV_PATH").ok() {
PathBuf::from(&android_env_path)
} else {
Path::new(&manifest_dir_path).join("android.env")
};
// Check the android.env file exists
android_env_path.try_exists().expect(
"android.env is not found, so please create it from android.env.sample and export \"ANDROID_ENV_PATH\" environment variable",
);
from_filename_override(android_env_path)
.expect(&format!("Failed to load {}", manifest_dir_path.display()));
// check the ANDROID_NDK_HOME environment variable
let ndk_home = env::var("ANDROID_NDK_HOME").expect(
"ANDROID_NDK_HOME is not set, so please set it from android.env",
);
let ndk_home = Path::new(&ndk_home);
let min_api_level = env::var("ANDROID_MIN_API_LEVEL").expect(
"ANDROID_MIN_API_LEVEL is not set, so please set it from android.env",
);
let min_api_level = min_api_level
.parse::<u32>()
.expect("Failed to parse ANDROID_MIN_API_LEVEL as u32");
let sysroot_target_path = ndk_home.join(format!(
"toolchains/llvm/prebuilt/{}-x86_64/sysroot",
TARGET_OS
));
if target_triple != "aarch64-linux-android" {
panic!("Only aarch64-linux-android target is supported");
}
let target_lib_path =
sysroot_target_path.join("usr/lib").join(&target_triple);
let platform_lib_path = sysroot_target_path
.join("usr/lib")
.join("aarch64-linux-android")
.join(min_api_level.to_string());
let include_path = sysroot_target_path.join("usr/include");
/* **IMPORTANT**
* 以下のパスの順番はapi level に対応した ライブラリのパス -> NDK のパス にすること
* 出ないと実行時にエラーが発生する
* `library "libc++_shared.so" not found: needed by main executable`
* */
println!("cargo:rustc-link-search={}", platform_lib_path.display());
println!("cargo:rustc-link-search={}", target_lib_path.display());
println!("cargo:include={}", include_path.display());
builder = builder
.flag("-fexceptions")
.flag("-frtti")
.cpp_link_stdlib("c++_static");
}
/* Compile C++ */
builder.compile("c_interface");
println!("cargo:rerun-if-changed=cpp/src/c_interface.cpp");
println!("cargo:rerun-if-changed=cpp/include/c_interface.h");
println!(
"cargo:rerun-if-changed={}/build.rs",
manifest_dir_path.display()
);
/* ------------------------------------------------------------------------
* Basic Linking Configuration
* ------------------------------------------------------------------------ */
println!(
"cargo:rustc-link-search={}/{}/{}/lib",
executorch_home, target_triple, profile
);
#[allow(unused_mut)]
let mut libs = vec![
"extension_tensor",
"extension_module_static",
"extension_data_loader",
"executorch_core",
];
#[rustfmt::skip]
#[allow(unused_mut)]
let mut whole_archive_libs = vec![
"executorch", // Why should this library be linked as a whole archive?
"portable_ops_lib",
"portable_kernels",
];
/* ---------- Common extra library configuration ---------------------- */
#[cfg(feature = "xnnpack")]
{
whole_archive_libs.push("xnnpack_backend");
whole_archive_libs.push("XNNPACK");
whole_archive_libs.push("pthreadpool");
whole_archive_libs.push("cpuinfo");
whole_archive_libs.push("microkernels-prod");
whole_archive_libs.push("kleidiai");
}
/* ---------- MacOS, iOS extra library configuration ---------------------- */
#[cfg(feature = "apple")]
{
println!("cargo:rustc-link-arg=-framework");
println!("cargo:rustc-link-arg=Foundation");
println!("cargo:rustc-link-arg=-fapple-link-rtlib");
}
/* Metal backend configuration */
#[cfg(feature = "mps")]
{
whole_archive_libs.push("mpsdelegate");
println!("cargo:rustc-link-arg=-weak_framework");
println!("cargo:rustc-link-arg=MetalPerformanceShaders");
println!("cargo:rustc-link-arg=-weak_framework");
println!("cargo:rustc-link-arg=MetalPerformanceShadersGraph");
println!("cargo:rustc-link-arg=-weak_framework");
println!("cargo:rustc-link-arg=Metal");
}
/* CoreML backend configuration */
#[cfg(feature = "coreml")]
{
whole_archive_libs.push("coremldelegate");
println!("cargo:rustc-link-arg=-framework");
println!("cargo:rustc-link-arg=CoreML");
println!("cargo:rustc-link-arg=-framework");
println!("cargo:rustc-link-arg=Accelerate");
println!("cargo:rustc-link-lib=sqlite3");
}
/* ---------- Android extra library configuration ---------------------- */
#[cfg(feature = "android")]
{
println!("cargo:rustc-link-lib=static=c++abi");
}
#[cfg(feature = "vulkan")]
{
whole_archive_libs.push("vulkan_backend");
}
for lib in libs {
println!("cargo:rustc-link-lib=static={}", lib);
}
for lib in whole_archive_libs {
println!("cargo:rustc-link-lib=static:+whole-archive={}", lib);
}
/* ------------------------------------------------------------------------
* Rerun build.rs if the following files are changed
* ------------------------------------------------------------------------ */
// C Interface
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/cpp/c_interface.cpp");
println!("cargo:rerun-if-changed=src/cpp/c_interface.h");
// Executorch configurations
println!("cargo:rerun-if-env-changed=EXECUTORCH_HOME");
println!("cargo:rerun-if-changed=executorch-prebuilt");
// Android configurations
println!("cargo:rerun-if-env-changed=ANDROID_ENV_PATH");
println!("cargo:rerun-if-env-changed=ANDROID_NDK_HOME");
println!("cargo:rerun-if-env-changed=ANDROID_MIN_API_LEVEL");
println!("cargo:rerun-if-changed=android.env");
}