Skip to content

Commit

Permalink
fix the build.rs error
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Feb 4, 2024
1 parent 3a70fc5 commit 6c3c485
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ rust-project.json

ext_images
.idea
lwext4-sys/lwext4
lwext4-sys/lwext4
lwext4-sys/src/ext4.rs
2 changes: 1 addition & 1 deletion lwext4-rs/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::block::CName;
use crate::error::{errno_to_result, Error, Result};
use crate::types::{FileAttr, FileTimes, Metadata, OpenFlags, Permissions, Time};
use alloc::string::ToString;
use alloc::string::String;
use alloc::string::ToString;
use core::ptr::null_mut;
use embedded_io::{ErrorType, Read, Seek, SeekFrom, Write};
use lwext4_sys::ext4::*;
Expand Down
4 changes: 2 additions & 2 deletions lwext4-rs/src/mkfs.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::alloc::string::ToString;
use crate::error::{errno_to_result, Result};
use crate::types::FsType;
use crate::{BlockDevice, BlockDeviceInterface, Error};
use alloc::boxed::Box;
use alloc::ffi::CString;
use alloc::string::String;
use core::ffi::CStr;
use core::fmt::Debug;
use core::mem::transmute;
use core::pin::Pin;
use core::ptr::null_mut;
use crate::alloc::string::ToString;
use alloc::string::String;
use lwext4_sys::ext4::{ext4_fs, ext4_mkfs, ext4_mkfs_info, ext4_mkfs_read_info, ext4_sblock};
pub struct BuildExtFs<T: BlockDeviceInterface> {
raw_fs: ext4_fs,
Expand Down
14 changes: 7 additions & 7 deletions lwext4-rs/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use core::fmt::{Debug, Formatter};
use core::ops::{Deref, DerefMut};
use lwext4_sys::ext4::*;

pub const S_IFIFO:u32 = 4096;
pub const S_IFCHR:u32 = 8192;
pub const S_IFBLK:u32 = 24576;
pub const S_IFDIR:u32 = 16384;
pub const S_IFREG:u32 = 32768;
pub const S_IFLNK:u32 = 40960;
pub const S_IFSOCK:u32 = 49152;
pub const S_IFIFO: u32 = 4096;
pub const S_IFCHR: u32 = 8192;
pub const S_IFBLK: u32 = 24576;
pub const S_IFDIR: u32 = 16384;
pub const S_IFREG: u32 = 32768;
pub const S_IFLNK: u32 = 40960;
pub const S_IFSOCK: u32 = 49152;
bitflags! {
pub struct DebugFlags: u32 {
const BALLOC = DEBUG_BALLOC;
Expand Down
1 change: 0 additions & 1 deletion lwext4-sys/.gitignore

This file was deleted.

67 changes: 39 additions & 28 deletions lwext4-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{env, fs};
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs};

fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let lwext4 = out_dir.join("lwext4");
if !lwext4.exists(){
if !lwext4.exists() {
fs::create_dir_all(lwext4.clone()).unwrap();
let cp = Command::new("git")
.current_dir(out_dir.clone())
Expand All @@ -14,22 +14,25 @@ fn main() {
.arg("lwext4")
.status()
.expect("failed to clone lwext4");
assert_eq!(cp.success(),true);
assert_eq!(cp.success(), true);
}
let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
if os == "none"{
if os == "none" {
build_for_none(&lwext4);
}else {
} else {
build_for_os(&lwext4);
}
println!("cargo:rustc-link-lib=static=lwext4");
println!("cargo:rerun-if-changed={}",PathBuf::from("ext4.h").canonicalize().unwrap().display());
println!(
"cargo:rerun-if-changed={}",
PathBuf::from("ext4.h").canonicalize().unwrap().display()
);
}

fn build(lwext4:&PathBuf,lwext4_build:&PathBuf,build_arg:&str){
fn build(lwext4: &PathBuf, lwext4_build: &PathBuf, build_arg: &[&str]) {
let make = Command::new("make")
.current_dir(&lwext4)
.arg(build_arg)
.args(build_arg)
.status()
.expect("failed to build lwext4");
assert!(make.success());
Expand All @@ -41,46 +44,54 @@ fn build(lwext4:&PathBuf,lwext4_build:&PathBuf,build_arg:&str){
assert!(make.success());
}

fn build_for_os(lwext4:&PathBuf){
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");
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());
println!(
"cargo:rustc-link-search=native={}",
lwext4_build.join("src").canonicalize().unwrap().display()
);
}


/// 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){
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());
println!("cargo:XXX={}", arch);
let lwext4_build = lwext4.join("build_musl-generic");
let lib_path = lwext4.join("build_musl-generic/src/liblwext4.a");
if !lwext4_build.exists() || !lib_path.exists() {
build(
lwext4,
&lwext4_build,
&["musl-generic", format!("ARCH={}", 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);
env::set_var("TARGET", "x86_64-unknown-linux-gnu");
generates_bindings(&lwext4, "build_musl-generic");
env::set_var("TARGET", target);
}
println!("cargo:rustc-link-search=native={}",lwext4_build.join("src").canonicalize().unwrap().display());
println!(
"cargo:rustc-link-search=native={}",
lwext4_build.join("src").canonicalize().unwrap().display()
);
}


fn generates_bindings(lwext4: &PathBuf, build_dir:&str) {
fn generates_bindings(lwext4: &PathBuf, build_dir: &str) {
let bindings = bindgen::builder()
.header("./ext4.h")
.clang_arg(format!("-I{}/include",lwext4.display()))
.clang_arg(format!("-I{}/{}/include",lwext4.display(),build_dir))
.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();
}

0 comments on commit 6c3c485

Please sign in to comment.