From 46aa4656df654201c08d238d3bbc88a4d5792051 Mon Sep 17 00:00:00 2001 From: Pierre Ferru Date: Fri, 19 Jun 2026 14:57:17 +0200 Subject: [PATCH 1/2] Inject sysroot into build environment flags When a `sysroot` field is defined in `dinghy.toml`, automatically append `--sysroot=` to `CFLAGS`, `CPPFLAGS`, `CXXFLAGS` and `LDFLAGS` environment variables. This avoids requiring users to manually add `--sysroot=` to each flag variable in their configuration. --- dinghy-lib/src/config.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/dinghy-lib/src/config.rs b/dinghy-lib/src/config.rs index 5820aae..1b795bc 100644 --- a/dinghy-lib/src/config.rs +++ b/dinghy-lib/src/config.rs @@ -118,14 +118,31 @@ impl PlatformConfiguration { } pub fn env(&self) -> Vec<(String, String)> { - self.env + let mut env = self + .env .as_ref() .map(|it| { it.iter() .map(|(key, value)| (key.to_string(), value.to_string())) .collect_vec() }) - .unwrap_or(vec![]) + .unwrap_or(vec![]); + + if let Some(sysroot) = self.sysroot.as_ref() { + let flags = ["CFLAGS", "CPPFLAGS", "CXXFLAGS", "LDFLAGS"]; + + '_flags: for flag in flags { + for (key, value) in &mut env { + if key == flag { + value.push_str(format!(" --sysroot={sysroot}").as_str()); + continue '_flags; + } + } + env.push((flag.to_string(), format!("--sysroot={sysroot}"))); + } + } + + env } } From 47ca1af31ede6d1781149259b6629ab4ba550215 Mon Sep 17 00:00:00 2001 From: Pierre Ferru Date: Fri, 19 Jun 2026 17:24:35 +0200 Subject: [PATCH 2/2] Use hashmap's entry function to simplify code --- dinghy-lib/src/config.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/dinghy-lib/src/config.rs b/dinghy-lib/src/config.rs index 1b795bc..c348a32 100644 --- a/dinghy-lib/src/config.rs +++ b/dinghy-lib/src/config.rs @@ -118,31 +118,27 @@ impl PlatformConfiguration { } pub fn env(&self) -> Vec<(String, String)> { - let mut env = self - .env - .as_ref() - .map(|it| { - it.iter() - .map(|(key, value)| (key.to_string(), value.to_string())) - .collect_vec() - }) - .unwrap_or(vec![]); + let Some(env) = self.env.as_ref() else { + return vec![]; + }; if let Some(sysroot) = self.sysroot.as_ref() { - let flags = ["CFLAGS", "CPPFLAGS", "CXXFLAGS", "LDFLAGS"]; - - '_flags: for flag in flags { - for (key, value) in &mut env { - if key == flag { - value.push_str(format!(" --sysroot={sysroot}").as_str()); - continue '_flags; - } - } - env.push((flag.to_string(), format!("--sysroot={sysroot}"))); + let mut env = env.clone(); + for flag in ["CFLAGS", "CPPFLAGS", "CXXFLAGS", "LDFLAGS"] { + let sr_argument = format!("--sysroot={sysroot}"); + env.entry(flag.to_string()) + .and_modify(|value| { + value.push(' '); + value.push_str(&sr_argument); + }) + .or_insert(sr_argument); } + env.into_iter().collect_vec() + } else { + env.iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect() } - - env } }