Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,52 @@ fn test_macro_fallback_non_system_dir() {
}
}

#[test]
fn test_macro_fallback_cross_target() {
if let Some((9, _)) = clang_version().parsed {
return;
}

// Setting TARGET in a parallel test is not robust, so run the actual
// assertion in a child process with a non-host target.
if env::var("__BINDGEN_CROSS_TARGET_INNER").is_ok() {
let tmpdir = tempfile::tempdir().unwrap();
let header = tmpdir.path().join("test.h");
fs::write(&header, "#define PTR_BYTES __SIZEOF_POINTER__\n").unwrap();

let actual = builder()
.disable_header_comment()
.header(header.to_str().unwrap())
.clang_macro_fallback()
.clang_macro_fallback_build_dir(tmpdir.path())
.generate()
.unwrap()
.to_string();

assert!(
actual.contains("pub const PTR_BYTES: u32 = 4;"),
"Expected 4-byte pointers for armv7 target, got:\n{actual}"
);
return;
}

let output = std::process::Command::new(env::current_exe().unwrap())
.arg("test_macro_fallback_cross_target")
.arg("--exact")
.arg("--test-threads=1")
.env("TARGET", "armv7-unknown-linux-gnueabihf")
.env("__BINDGEN_CROSS_TARGET_INNER", "1")
.output()
.unwrap();

let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
output.status.success(),
"Cross-target fallback test failed.\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
}

#[test]
// Doesn't support executing sh file on Windows.
// We may want to implement it in Rust so that we support all systems.
Expand Down
8 changes: 4 additions & 4 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,10 @@ impl Bindings {
// opening libclang.so, it has to be the same architecture and thus the
// check is fine.
if !explicit_target && !is_host_build {
options.clang_args.insert(
0,
format!("--target={effective_target}").into_boxed_str(),
);
let target_arg =
format!("--target={effective_target}").into_boxed_str();
options.clang_args.insert(0, target_arg.clone());
options.fallback_clang_args.insert(0, target_arg);
}

fn detect_include_paths(options: &mut BindgenOptions) {
Expand Down
Loading