Skip to content

Commit

Permalink
chore: update build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Jun 4, 2024
1 parent 7fab170 commit 95db062
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,38 @@ static ET_LIBS: [&str; 16] = [
];

fn main() {
// find available lib / object files in the executorch cmake-out
// all libs should whole-archive link
// libs:
// extension_data_loader
// mpsdelegate
// qnn_executorch_backend : shared
// portable_ops_lib
// extension_module : shared
// xnnpack_backend
// XNNPACK
// cpuinfo
// pthreadpool
// vulkan_backend
// optimized_kernels
// optimized_ops_lib
// optimized_native_cpu_ops_lib
// quantized_kernels
// quantized_ops_lib
// custom_ops
//

// from env EXECUTORCH_INSTALL_PREFIX default "executorch/cmake-out/lib"
let base_path_str = std::env::var("EXECUTORCH_INSTALL_PREFIX").unwrap_or_else(|_| "executorch/cmake-out".to_string());
let base_path = Path::new(&base_path_str).join("lib");
let lib_path = Path::new(&base_path_str).join("lib");

// find path in executorch/cmake-out/lib/*.{so,dylib,dll,a}
let mut lib_paths: Vec<&str> = Vec::new();
let mut lib_paths: Vec<String> = Vec::new();
for lib in ET_LIBS {
let path = match lib {
let filename = match lib {
"extension_module" => format!("lib{}.{}", lib, SO_EXT),
"qnn_executorch_backend" => format!("lib{}.{}", lib, SO_EXT),
_ => format!("lib{}.a", lib),
};
if base_path.join(&path).exists() {
lib_paths.push(lib.trim_start_matches("lib"));
if lib_path.join(&filename).exists() {
lib_paths.push(filename);
}
}

assert!(!lib_paths.is_empty(), "No lib files found in executorch/cmake-out/lib");

let mut config = cpp_build::Config::new();
config.flag_if_supported("-std=c++17");
config.flag("-std=c++17");

for lib in lib_paths {
// POSIX systems
config.flag_if_supported(&format!("--whole-archive -l{} --no-whole-archive", lib));
// Darwin systems
config.flag_if_supported(&format!("-force_load,{}", lib));
config.flag(&format!("-Wl,--whole-archive -l{} -Wl,--no-whole-archive", lib.trim_start_matches("lib").trim_end_matches(".a")));
// if lib.ends_with(SO_EXT) {
// // println!("cargo:rustc-link-lib=dylib={}", lib.trim_start_matches("lib").trim_end_matches(SO_EXT));
// } else {
// config.flag(&format!("--whole-archive -l{} --no-whole-archive", lib));
// // println!("cargo:rustc-link-lib=static={}", lib.trim_start_matches("lib").trim_end_matches(".a"));
// }
}
// println!("cargo:rustc-link-search=native={}", lib_path.display());

config.flag(&format!("-L{}", lib_path.display()));

config.build("src/lib.rs");
}

0 comments on commit 95db062

Please sign in to comment.