-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix rust binding generation error for lwext4
When the target is riscv64gc-unknown-none-elf, bindgen cannot correctly generate the c binding. We temporarily switch the target to the default x86_64-unknow-linux-gnu, and switch to the old value after completing the generation.
- Loading branch information
Showing
8 changed files
with
83 additions
and
8,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
./lwext4 | ||
./src/ext4.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,4 @@ edition = "2021" | |
|
||
|
||
[build-dependencies] | ||
bindgen = "0.65" | ||
fs_extra = "1.2.0" | ||
make-cmd = "0.1.0" | ||
bindgen = "0.65" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,86 @@ | ||
use fs_extra::dir::CopyOptions; | ||
use make_cmd::make; | ||
use std::path::PathBuf; | ||
use std::{env, fs}; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=./lwext4"); | ||
println!("cargo:rerun-if-changed=./ext4.h"); | ||
|
||
std::process::Command::new("git") | ||
.arg("clone") | ||
.arg("https://github.com/gkostka/lwext4.git") | ||
.output() | ||
.unwrap(); | ||
|
||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
fs_extra::dir::remove(&out_dir).unwrap(); | ||
fs::create_dir_all(&out_dir).unwrap(); | ||
let lwext4_dir = out_dir.join("lwext4"); | ||
let mut copy_options = CopyOptions::new(); | ||
copy_options.copy_inside = true; | ||
copy_options.overwrite = true; | ||
fs_extra::dir::copy("./lwext4", &out_dir, ©_options).unwrap(); | ||
make() | ||
.current_dir(&lwext4_dir) | ||
.arg("generic") | ||
let lwext4 = out_dir.join("lwext4"); | ||
if !lwext4.exists(){ | ||
fs::create_dir_all(lwext4.clone()).unwrap(); | ||
let cp = Command::new("git") | ||
.current_dir(out_dir.clone()) | ||
.arg("clone") | ||
.arg("https://github.com/os-module/lwext4-c.git") | ||
.arg("lwext4") | ||
.status() | ||
.expect("failed to clone lwext4"); | ||
assert_eq!(cp.success(),true); | ||
} | ||
let os = env::var("CARGO_CFG_TARGET_OS").unwrap(); | ||
if os == "none"{ | ||
build_for_none(&lwext4); | ||
}else { | ||
build_for_os(&lwext4); | ||
} | ||
println!("cargo:rustc-link-lib=static=lwext4"); | ||
println!("cargo:rerun-if-changed={}",PathBuf::from("ext4.h").canonicalize().unwrap().display()); | ||
} | ||
|
||
fn build(lwext4:&PathBuf,lwext4_build:&PathBuf,build_arg:&str){ | ||
let make = Command::new("make") | ||
.current_dir(&lwext4) | ||
.arg(build_arg) | ||
.status() | ||
.unwrap(); | ||
let build_generic_dir = lwext4_dir.join("build_generic"); | ||
make() | ||
.current_dir(&build_generic_dir) | ||
.expect("failed to build lwext4"); | ||
assert!(make.success()); | ||
let make = Command::new("make") | ||
.current_dir(lwext4_build) | ||
.arg("lwext4") | ||
.status() | ||
.unwrap(); | ||
println!( | ||
"cargo:rustc-link-search={}", | ||
fs::canonicalize(build_generic_dir.join("src")) | ||
.unwrap() | ||
.to_str() | ||
.unwrap() | ||
); | ||
println!("cargo:rustc-link-lib=static=lwext4"); | ||
.expect("failed to build lwext4"); | ||
assert!(make.success()); | ||
} | ||
|
||
fn build_for_os(lwext4:&PathBuf){ | ||
let lwext4_build = lwext4.join("build_generic"); | ||
let lib_path = lwext4.join("build_generic/src/liblwext4.a"); | ||
if !lwext4_build.exists() || !lib_path.exists(){ | ||
build(lwext4,&lwext4_build,"generic"); | ||
generates_bindings(&lwext4,"build_generic"); | ||
} | ||
println!("cargo:rustc-link-search=native={}",lwext4_build.join("src").canonicalize().unwrap().display()); | ||
} | ||
|
||
fs_extra::dir::copy( | ||
lwext4_dir.join("include"), | ||
&build_generic_dir, | ||
©_options, | ||
) | ||
.unwrap(); | ||
// let bindings = bindgen::builder() | ||
// .header( | ||
// build_generic_dir | ||
// .join("include") | ||
// .join("ext4.h") | ||
// .to_str() | ||
// .unwrap(), | ||
// ) | ||
// .clang_arg(format!( | ||
// "-I{}", | ||
// dbg!(build_generic_dir.join("include").to_str().unwrap()) | ||
// )) | ||
// .use_core() | ||
// .parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
// .generate() | ||
// .unwrap(); | ||
// bindings.write_to_file("src/ext4.rs").unwrap(); | ||
|
||
// let path = OpenOptions::new() | ||
// .read(true) | ||
// .open("./ext4.h"); | ||
/// When the target is riscv64gc-unknown-none-elf, | ||
/// bindgen cannot correctly generate the c binding. | ||
/// We temporarily switch the target to the default x86_64-unknow-linux-gnu, | ||
/// and switch to the old value after completing the generation. | ||
fn build_for_none(lwext4:&PathBuf){ | ||
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); | ||
let lwext4_build = lwext4.join(format!("build_musl-generic-{}",arch)); | ||
let lib_path = lwext4.join(format!("build_musl-generic-{}/src/liblwext4.a",arch)); | ||
if !lwext4_build.exists() || !lib_path.exists(){ | ||
build(lwext4,&lwext4_build,format!("musl-generic-{}",arch).as_str()); | ||
let target = env::var("TARGET").unwrap(); | ||
env::set_var("TARGET","x86_64-unknown-linux-gnu"); | ||
generates_bindings(&lwext4,format!("build_musl-generic-{}",arch).as_str()); | ||
env::set_var("TARGET",target); | ||
} | ||
println!("cargo:rustc-link-search=native={}",lwext4_build.join("src").canonicalize().unwrap().display()); | ||
} | ||
|
||
|
||
fn generates_bindings(lwext4: &PathBuf, build_dir:&str) { | ||
let bindings = bindgen::builder() | ||
.header("./ext4.h") | ||
.clang_arg(format!( | ||
"-I{}", | ||
dbg!(build_generic_dir.join("include").to_str().unwrap()) | ||
)) | ||
.clang_arg(format!("-I{}/include",lwext4.display())) | ||
.clang_arg(format!("-I{}/{}/include",lwext4.display(),build_dir)) | ||
.use_core() | ||
.layout_tests(false) | ||
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
.generate() | ||
.unwrap(); | ||
bindings.write_to_file("src/ext4.rs").unwrap() | ||
bindings.write_to_file("src/ext4.rs").unwrap(); | ||
} | ||
|
Oops, something went wrong.