Skip to content

Commit

Permalink
fix: hotfix to get sysroot path with esp-idf v5.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevleon committed Jan 14, 2025
1 parent d7fd124 commit 8ff2571
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions build/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn generate_bindings(rss_path: &Path) -> Result<()> {

// Get GCC sysroot
let sysroot = get_gcc_sysroot()?;

// Base bindgen configuration
let mut builder = Builder::default()
.use_core()
Expand Down Expand Up @@ -50,10 +50,10 @@ pub fn generate_bindings(rss_path: &Path) -> Result<()> {
builder = builder.clang_arg(format!("-I{}/include-fixed", libgcc_dir.display()));
}
}
}
else if target.contains("riscv32imac-esp-espidf") || target.contains("riscv32imc-esp-espidf") { // TODO also include no_std targets (riscv32imac-unknown-none-elf and riscv32imc-unknown-none-elf)?
builder = builder
.clang_arg("--target=riscv32-esp-elf");
} else if target.contains("riscv32imac-esp-espidf") || target.contains("riscv32imc-esp-espidf")
// TODO also include no_std targets (riscv32imac-unknown-none-elf and riscv32imc-unknown-none-elf)?
{
builder = builder.clang_arg("--target=riscv32-esp-elf");

// Add sysroot includes
builder = builder
Expand Down Expand Up @@ -97,29 +97,44 @@ pub fn generate_bindings(rss_path: &Path) -> Result<()> {
fn get_gcc_sysroot() -> Result<String> {
let target = env::var("TARGET").unwrap_or_default();

if target.contains("thumb") || target.contains("arm")
{
if target.contains("thumb") || target.contains("arm") {
let output = Command::new("arm-none-eabi-gcc")
.args(["-print-sysroot"])
.output()
.map_err(|e| BuildError::BindgenError(format!("Failed to get GCC sysroot: {}", e)))?;
.args(["-print-sysroot"])
.output()
.map_err(|e| BuildError::BindgenError(format!("Failed to get GCC sysroot: {}", e)))?;

String::from_utf8(output.stdout)
.map(|s| s.trim().to_string())
.map_err(|e| BuildError::BindgenError(format!("Invalid sysroot path: {}", e)))
}
else if target.contains("riscv32imac-esp-espidf") || target.contains("riscv32imc-esp-espidf") // TODO also include no_std targets (riscv32imac-unknown-none-elf and riscv32imc-unknown-none-elf)?
} else if target.contains("riscv32imac-esp-espidf") || target.contains("riscv32imc-esp-espidf")
// TODO also include no_std targets (riscv32imac-unknown-none-elf and riscv32imc-unknown-none-elf)?
{
let output = Command::new("riscv32-esp-elf-gcc")
.args(["-print-sysroot"])
.output()
.map_err(|e| BuildError::BindgenError(format!("Failed to get GCC sysroot: {}", e)))?;
.args(["-print-sysroot"])
.output()
.map_err(|e| BuildError::BindgenError(format!("Failed to get GCC sysroot: {}", e)))?;

// NOTE: there seems to be a bug in v5.2.3 esp-idf where riscv32-esp-elf-gcc doesn't print a sysroot path - so we have to build it manually
if output.stdout == b"" {
let path_output = Command::new("which")
.args(["riscv32-esp-elf-gcc"])
.output()
.map_err(|e| {
BuildError::BindgenError(format!("Failed to get path via which: {}", e))
})?;

let path_string = String::from_utf8(path_output.stdout)
.map_err(|e| BuildError::BindgenError(format!("Invalid path: {}", e)))?;

let path = Path::new(&path_string).parent().unwrap();
let path = path.join("../riscv32-esp-elf");
return Ok(path.to_string_lossy().to_string());
}

String::from_utf8(output.stdout)
.map(|s| s.trim().to_string())
.map_err(|e| BuildError::BindgenError(format!("Invalid sysroot path: {}", e)))
}
else {
} else {
Err(BuildError::BindgenError("Unsupported target".to_string()))
}
}
Expand Down Expand Up @@ -154,6 +169,9 @@ fn add_log_wrapper(mut bindings: Builder) -> Result<Builder> {
.flag("-mthumb")
.flag("-mfloat-abi=hard")
.flag("-mfpu=fpv4-sp-d16");
} else if target.contains("riscv32imac-esp-espidf") || target.contains("riscv32imc-esp-espidf")
{
build.compiler("riscv32-esp-elf-gcc");
}

build
Expand Down

0 comments on commit 8ff2571

Please sign in to comment.