11use std:: ffi:: OsString ;
22use std:: path:: PathBuf ;
33
4- use anyhow:: { Context , Result } ;
4+ use anyhow:: { Context , Result , bail } ;
55use regex:: Regex ;
66
77use crate :: cargo_cmd:: { CargoCmd , cargo_cmd} ;
@@ -21,7 +21,7 @@ struct CargoMetadataPackage {
2121 version : semver:: Version ,
2222}
2323
24- pub fn prepare ( args : & Args ) -> Result < ( ) > {
24+ pub fn find_libc_dir ( args : & Args ) -> Result < PathBuf > {
2525 let metadata = cargo_cmd ( ) ?
2626 . env_clear ( )
2727 . envs ( args. env . iter ( ) )
@@ -37,44 +37,59 @@ pub fn prepare(args: &Args) -> Result<()> {
3737 let metadata = serde_json:: from_slice :: < CargoMetadata > ( & metadata. stdout )
3838 . context ( "Failed to parse cargo metadata" ) ?;
3939
40+ let hyperlight_libc = metadata
41+ . packages
42+ . iter ( )
43+ . find ( |pkg| pkg. name == "hyperlight-libc" ) ;
44+
45+ if let Some ( hyperlight_libc) = hyperlight_libc {
46+ let hyperlight_libc_dir = hyperlight_libc
47+ . manifest_path
48+ . parent ( )
49+ . context ( "Failed to get directory for hyperlight-libc" ) ?;
50+ return Ok ( hyperlight_libc_dir. to_path_buf ( ) ) ;
51+ }
52+
4053 let hyperlight_guest_bin = metadata
4154 . packages
42- . into_iter ( )
43- . find ( |pkg| pkg. name == "hyperlight-guest-bin" )
44- . context ( "Could not find hyperlight-guest-bin package in cargo metadata" ) ?;
55+ . iter ( )
56+ . find ( |pkg| pkg. name == "hyperlight-guest-bin" ) ;
57+
58+ if let Some ( hyperlight_guest_bin) = hyperlight_guest_bin {
59+ let hyperlight_guest_bin_dir = hyperlight_guest_bin
60+ . manifest_path
61+ . parent ( )
62+ . context ( "Failed to get directory for hyperlight-guest-bin" ) ?;
63+ return Ok ( hyperlight_guest_bin_dir. to_path_buf ( ) ) ;
64+ }
4565
46- let hyperlight_guest_bin_dir = hyperlight_guest_bin
47- . manifest_path
48- . parent ( )
49- . context ( "Failed to get directory for hyperlight-guest-bin" ) ?;
66+ bail ! ( "Could not find hyperlight-libc or hyperlight-guest-bin package in cargo metadata" ) ;
67+ }
68+
69+ pub fn prepare ( args : & Args ) -> Result < ( ) > {
70+ let libc_dir = find_libc_dir ( args) ?;
5071
5172 let include_dst_dir = args. includes_dir ( ) ;
5273
5374 std:: fs:: create_dir_all ( & include_dst_dir)
5475 . context ( "Failed to create sysroot include directory" ) ?;
5576
5677 // Detect which libc variant is present: picolibc or legacy musl
57- let include_dirs: & [ & str ] = if hyperlight_guest_bin_dir
58- . join ( "third_party/musl/include" )
59- . is_dir ( )
60- {
61- & [
62- "third_party/printf/" ,
63- "third_party/musl/include" ,
64- "third_party/musl/arch/generic" ,
65- "third_party/musl/arch/x86_64" ,
66- "third_party/musl/src/internal" ,
67- ]
68- } else {
69- & [
70- "third_party/picolibc/libc/include" ,
71- "third_party/picolibc/libc/stdio" ,
72- "include" ,
73- ]
74- } ;
78+ let include_dirs: & [ & str ] = & [
79+ // directories for musl
80+ "third_party/printf/" ,
81+ "third_party/musl/include" ,
82+ "third_party/musl/arch/generic" ,
83+ "third_party/musl/arch/x86_64" ,
84+ "third_party/musl/src/internal" ,
85+ // directories for picolibc
86+ "third_party/picolibc/libc/include" ,
87+ "third_party/picolibc/libc/stdio" ,
88+ "include" ,
89+ ] ;
7590
7691 for dir in include_dirs {
77- let include_src_dir = hyperlight_guest_bin_dir . join ( dir) ;
92+ let include_src_dir = libc_dir . join ( dir) ;
7893 let files = glob:: glob ( & format ! ( "{}/**/*.h" , include_src_dir. display( ) ) )
7994 . context ( "Failed to read include source directory" ) ?;
8095
0 commit comments