Skip to content

Commit 69c69f4

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 an empty stdlib.h in `build.rs` when `target_os == uefi` and add it to the include path.
1 parent 8399451 commit 69c69f4

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

build.rs

Lines changed: 17 additions & 1 deletion
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,22 @@ 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. The contents of the
197+
// header aren't actually needed, so create an empty header to
198+
// use instead.
199+
if target_os == "uefi" {
200+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
201+
let include_dir = out_dir.join("include");
202+
if !include_dir.exists() {
203+
fs::create_dir(&include_dir).unwrap();
204+
}
205+
fs::write(include_dir.join("stdlib.h"), "").unwrap();
206+
cfg.flag(&format!("-I{}", include_dir.to_str().unwrap()));
207+
}
208+
193209
let mut sources = Sources::new();
194210
sources.extend(&[
195211
("__absvdi2", "absvdi2.c"),

0 commit comments

Comments
 (0)