33
44use 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
207211impl 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) ]
280329fn 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
452501fn 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