Skip to content

Commit c10cfb3

Browse files
Use a stub stdlib.h when compiling for UEFI targets
int_util.c includes stdlib.h if `_WIN32` is defined. When compiling the UEFI targets with clang they are treated as Windows targets (e.g. if the Rust target is x86_64-unknown-uefi, the clang target is x86_64-unknown-windows-gnu). So stdlib.h gets included, even though we are compilling with `-ffreestanding` and don't want stdlib.h to be used. That file may not be present, or an incompatible version might be installed leading to typedef redefinition errors. The contents of stdlib.h aren't actually needed for these targets anyway (due to `__STDC_HOSTED__` being 0), so create a minimal stdlib.h in `build.rs` when `target_os == uefi` and add it to the include path.
1 parent 8399451 commit c10cfb3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Diff for: build.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod c {
9898

9999
use std::collections::{BTreeMap, HashSet};
100100
use std::env;
101-
use std::fs::File;
101+
use std::fs::{self, File};
102102
use std::io::Write;
103103
use std::path::{Path, PathBuf};
104104

@@ -190,6 +190,21 @@ mod c {
190190
cfg.define("VISIBILITY_HIDDEN", None);
191191
}
192192

193+
// int_util.c tries to include stdlib.h if `_WIN32` is defined,
194+
// which it is when compiling UEFI targets with clang. This is
195+
// at odds with compiling with `-ffreestanding`, as the header
196+
// may be incompatible or not present. Create a minimal stub
197+
// header to use instead.
198+
if target_os == "uefi" {
199+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
200+
let include_dir = out_dir.join("include");
201+
if !include_dir.exists() {
202+
fs::create_dir(&include_dir).unwrap();
203+
}
204+
fs::write(include_dir.join("stdlib.h"), "#include <stddef.h>").unwrap();
205+
cfg.flag(&format!("-I{}", include_dir.to_str().unwrap()));
206+
}
207+
193208
let mut sources = Sources::new();
194209
sources.extend(&[
195210
("__absvdi2", "absvdi2.c"),

0 commit comments

Comments
 (0)