Skip to content

Commit 6b3945a

Browse files
committed
derive host sysroot in builder from clang include paths
1 parent f80d106 commit 6b3945a

3 files changed

Lines changed: 61 additions & 11 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ ebcc = { version = "0.1", default-features = false }
9393
format_serde_error = { version = "0.3", default-features = false }
9494
indexmap = { version = "2.10", default-features = false }
9595
itertools = { version = "0.14", default-features = false }
96-
libpressio = { version = "0.1", git = "https://github.com/juntyr/libpressio-rs.git", rev = "fea47ef", default-features = false }
96+
libpressio = { version = "0.1", git = "https://github.com/juntyr/libpressio-rs.git", rev = "6329913", default-features = false }
9797
log = { version = "0.4.27", default-features = false }
9898
miniz_oxide = { version = "0.8.5", default-features = false }
9999
ndarray = { version = "0.16.1", default-features = false } # keep in sync with numpy

crates/numcodecs-wasm-builder/buildenv/flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
MY_WASI_SYSROOT = "${wasi-sysroot}";
8282
MY_LIBCLANG_RT = "${libclang_rt}";
8383
MY_WASM_OPT = "${pkgs.binaryen}/bin/wasm-opt";
84-
MY_HOST_LIBCXX = "${pkgs."llvmPackages_${llvmVersion}".libcxx}";
84+
MY_HOST_LIBCXX = "${pkgs."llvmPackages_${llvmVersion}".libcxx.dev}";
8585
};
8686
};
8787
});

crates/numcodecs-wasm-builder/src/main.rs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
use std::{
55
collections::HashMap,
6-
env, fs, io,
6+
env,
7+
ffi::OsStr,
8+
fs, io,
9+
os::unix::ffi::OsStrExt,
710
path::{Path, PathBuf},
8-
process::Command,
11+
process::{Command, Stdio},
912
str::FromStr,
1013
};
1114

@@ -69,9 +72,11 @@ fn main() -> io::Result<()> {
6972
copy_buildenv_to_crate(&crate_dir)?;
7073

7174
let nix_env = NixEnv::new(&crate_dir)?;
75+
let host_sysroot = find_clang_host_sysroot(&nix_env, &crate_dir)?;
7276

7377
let wasm = build_wasm_codec(
7478
&nix_env,
79+
&host_sysroot,
7580
&target_dir,
7681
&crate_dir,
7782
&format!("{}-wasm", args.crate_),
@@ -201,7 +206,6 @@ struct NixEnv {
201206
libclang_rt: PathBuf,
202207
wasm_opt: PathBuf,
203208
host_libcxx: PathBuf,
204-
host_sysroot: PathBuf,
205209
}
206210

207211
impl NixEnv {
@@ -268,17 +272,63 @@ impl NixEnv {
268272
libclang_rt: try_read_env(&env, "MY_LIBCLANG_RT")?,
269273
wasm_opt: try_read_env(&env, "MY_WASM_OPT")?,
270274
host_libcxx: try_read_env(&env, "MY_HOST_LIBCXX")?,
271-
// FIXME
272-
host_sysroot: PathBuf::from(
273-
"/nix/store/5gfsv5n8zhpnl9yhggjpxrxg0jyflwja-apple-sdk-11.3/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk",
274-
),
275275
})
276276
}
277277
}
278278

279+
fn find_clang_host_sysroot(nix_env: &NixEnv, flake_parent_dir: &Path) -> io::Result<PathBuf> {
280+
let NixEnv { clang, .. } = nix_env;
281+
282+
let mut cmd = Command::new("nix");
283+
cmd.current_dir(flake_parent_dir);
284+
cmd.arg("develop");
285+
// cmd.arg("--store");
286+
// cmd.arg(nix_store_path);
287+
cmd.arg("--no-update-lock-file");
288+
cmd.arg("--ignore-environment");
289+
cmd.arg("path:.");
290+
cmd.arg("--command");
291+
cmd.arg(clang.join("clang"));
292+
cmd.arg("-v");
293+
cmd.arg("-x");
294+
cmd.arg("c");
295+
cmd.arg("-c");
296+
cmd.arg("-");
297+
cmd.stdin(Stdio::null());
298+
299+
eprintln!("executing {cmd:?}");
300+
301+
let output = cmd.output()?;
302+
let Some(include) = output
303+
.stderr
304+
.split(|x| *x == b'\n')
305+
.skip_while(|x| x.trim_ascii() != b"#include <...> search starts here:")
306+
.nth(1)
307+
else {
308+
return Err(io::Error::other(
309+
"failed to find #include <...> search path for clang",
310+
));
311+
};
312+
let include = Path::new(OsStr::from_bytes(include.trim_ascii()));
313+
let include = if include.ends_with("include")
314+
&& let Some(include) = include.parent()
315+
&& include.ends_with("usr")
316+
&& let Some(include) = include.parent()
317+
{
318+
include
319+
} else {
320+
return Err(io::Error::other(
321+
"clang #include <...> search path should end in /usr/include",
322+
));
323+
};
324+
325+
Ok(PathBuf::from(include))
326+
}
327+
279328
#[expect(clippy::too_many_lines)]
280329
fn configure_cargo_cmd(
281330
nix_env: &NixEnv,
331+
host_sysroot: &Path,
282332
target_dir: &Path,
283333
crate_dir: &Path,
284334
debug: bool,
@@ -297,7 +347,6 @@ fn configure_cargo_cmd(
297347
wasi_sysroot,
298348
libclang_rt,
299349
host_libcxx,
300-
host_sysroot,
301350
..
302351
} = nix_env;
303352

@@ -451,13 +500,14 @@ fn configure_cargo_cmd(
451500

452501
fn build_wasm_codec(
453502
nix_env: &NixEnv,
503+
host_sysroot: &Path,
454504
target_dir: &Path,
455505
crate_dir: &Path,
456506
crate_name: &str,
457507
debug: bool,
458508
verbose: bool,
459509
) -> io::Result<PathBuf> {
460-
let mut cmd = configure_cargo_cmd(nix_env, target_dir, crate_dir, debug);
510+
let mut cmd = configure_cargo_cmd(nix_env, host_sysroot, target_dir, crate_dir, debug);
461511
cmd.arg("rustc")
462512
.arg("--crate-type=cdylib")
463513
.arg("-Z")

0 commit comments

Comments
 (0)