Skip to content

Commit 5cd0130

Browse files
committed
Expand env-vars search for bindgen flags default value
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent 1647aeb commit 5cd0130

1 file changed

Lines changed: 38 additions & 23 deletions

File tree

src/cargo_cmd.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ pub trait CargoCmd {
1616
fn entrypoint(&mut self, entry: impl AsRef<str>) -> &mut Self;
1717
fn append_rustflags(&mut self, flags: impl AsRef<OsStr>) -> &mut Self;
1818
fn append_cflags(&mut self, triplet: impl AsRef<str>, flags: impl AsRef<OsStr>) -> &mut Self;
19-
fn append_bindgen_cflags(&mut self, flags: impl AsRef<OsStr>) -> &mut Self;
19+
fn append_bindgen_cflags(
20+
&mut self,
21+
triplet: impl AsRef<str>,
22+
flags: impl AsRef<OsStr>,
23+
) -> &mut Self;
2024
fn allow_unstable(&mut self) -> &mut Self;
2125
fn resolve_env(
2226
&self,
@@ -148,37 +152,25 @@ impl CargoCmd for Command {
148152
}
149153

150154
let triplet = triplet.as_ref();
151-
let triplet_snake_case = triplet.replace('-', "_");
152-
let triplet_snake_case_upper = triplet_snake_case.to_uppercase();
153-
154-
let search_keys = [
155-
format!("CFLAGS_{triplet}"),
156-
format!("CFLAGS_{triplet_snake_case}"),
157-
format!("CFLAGS_{triplet_snake_case_upper}"),
158-
"CFLAGS_hyperlight".to_string(),
159-
"CFLAGS_HYPERLIGHT".to_string(),
160-
"HYPERLIGHT_CFLAGS".to_string(),
161-
"TARGET_CFLAGS".to_string(),
162-
"CFLAGS".to_string(),
163-
];
164-
165-
let mut new_flags = search_keys
166-
.iter()
167-
.find_map(|key| get_env(self, key))
168-
.unwrap_or_default();
155+
156+
let mut new_flags = find_cflags(self, triplet);
169157

170158
if !new_flags.is_empty() {
171159
new_flags.push(" ");
172160
}
173161
new_flags.push(flags.as_ref());
174-
self.env(&search_keys[0], new_flags);
162+
self.env(format!("CFLAGS_{triplet}"), new_flags);
175163

176-
self.append_bindgen_cflags(flags);
164+
self.append_bindgen_cflags(triplet, flags);
177165

178166
self
179167
}
180168

181-
fn append_bindgen_cflags(&mut self, flags: impl AsRef<OsStr>) -> &mut Self {
169+
fn append_bindgen_cflags(
170+
&mut self,
171+
triplet: impl AsRef<str>,
172+
flags: impl AsRef<OsStr>,
173+
) -> &mut Self {
182174
if flags.as_ref().is_empty() {
183175
return self;
184176
}
@@ -189,7 +181,8 @@ impl CargoCmd for Command {
189181

190182
// TODO(jprendes): account and use the target specific variants of BINDGEN_EXTRA_CLANG_ARGS
191183
// see https://github.com/rust-lang/rust-bindgen/tree/main?tab=readme-ov-file#environment-variables
192-
let mut new_flags = get_env(self, "BINDGEN_EXTRA_CLANG_ARGS").unwrap_or_default();
184+
let mut new_flags =
185+
get_env(self, "BINDGEN_EXTRA_CLANG_ARGS").unwrap_or_else(|| find_cflags(self, triplet));
193186
if !new_flags.is_empty() {
194187
new_flags.push(" ");
195188
}
@@ -246,6 +239,28 @@ fn get_env(cmd: &Command, key: &str) -> Option<OsString> {
246239
}
247240
}
248241

242+
fn find_cflags(cmd: &Command, triplet: impl AsRef<str>) -> OsString {
243+
let triplet = triplet.as_ref();
244+
let triplet_snake_case = triplet.replace('-', "_");
245+
let triplet_snake_case_upper = triplet_snake_case.to_uppercase();
246+
247+
let search_keys = [
248+
format!("CFLAGS_{triplet}"),
249+
format!("CFLAGS_{triplet_snake_case}"),
250+
format!("CFLAGS_{triplet_snake_case_upper}"),
251+
"CFLAGS_hyperlight".to_string(),
252+
"CFLAGS_HYPERLIGHT".to_string(),
253+
"HYPERLIGHT_CFLAGS".to_string(),
254+
"TARGET_CFLAGS".to_string(),
255+
"CFLAGS".to_string(),
256+
];
257+
258+
search_keys
259+
.iter()
260+
.find_map(|key| get_env(cmd, key))
261+
.unwrap_or_default()
262+
}
263+
249264
pub fn merge_env(
250265
base: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
251266
envs: impl IntoIterator<Item = (impl AsRef<OsStr>, Option<impl AsRef<OsStr>>)>,

0 commit comments

Comments
 (0)