@@ -86,22 +86,25 @@ impl Build {
8686 }
8787
8888 fn cargo_encoded_rustflags ( & self ) -> Result < String > {
89- assert_eq ! (
90- env:: var( "CARGO_ENCODED_RUSTFLAGS" ) ,
91- Err ( VarError :: NotPresent )
92- ) ;
93- let mut rustflags = Vec :: new ( ) ;
89+ let mut rustflags = hermit_rustflags_from_env ( ) . unwrap_or_default ( ) ;
9490
9591 if self . instrument_mcount {
96- rustflags. push ( "-Zinstrument-mcount" ) ;
97- rustflags. push ( "-Cpasses=ee-instrument<post-inline>" ) ;
92+ rustflags. push ( "-Zinstrument-mcount" . to_owned ( ) ) ;
93+ rustflags. push ( "-Cpasses=ee-instrument<post-inline>" . to_owned ( ) ) ;
9894 }
9995
10096 if self . randomize_layout {
101- rustflags. push ( "-Zrandomize-layout" )
97+ rustflags. push ( "-Zrandomize-layout" . to_owned ( ) )
10298 }
10399
104- rustflags. extend ( self . cargo_build . artifact . arch . rustflags ( ) ) ;
100+ rustflags. extend (
101+ self . cargo_build
102+ . artifact
103+ . arch
104+ . rustflags ( )
105+ . iter ( )
106+ . map ( |& s| s. to_owned ( ) ) ,
107+ ) ;
105108
106109 Ok ( rustflags. join ( "\x1f " ) )
107110 }
@@ -119,3 +122,39 @@ impl Build {
119122 Ok ( ( ) )
120123 }
121124}
125+
126+ /// Gets Hermit-specific compiler flags from environment variables.
127+ ///
128+ /// Adapted from Cargo's [`rustflags_from_env`](https://github.com/rust-lang/cargo/blob/2a7c4960677971f88458b0f8b461a866836dff59/src/cargo/core/compiler/build_context/target_info.rs#L815-L839).
129+ fn hermit_rustflags_from_env ( ) -> Option < Vec < String > > {
130+ match env:: var ( "HERMIT_ENCODED_RUSTFLAGS" ) {
131+ Ok ( s) => {
132+ if s. is_empty ( ) {
133+ return Some ( Vec :: new ( ) ) ;
134+ }
135+ let hermit_rustflags = s. split ( '\x1f' ) . map ( str:: to_owned) . collect ( ) ;
136+ return Some ( hermit_rustflags) ;
137+ }
138+ Err ( VarError :: NotPresent ) => { }
139+ Err ( VarError :: NotUnicode ( s) ) => {
140+ panic ! ( "HERMIT_ENCODED_RUSTFLAGS did not contain valid unicode data: {s:?}" )
141+ }
142+ }
143+
144+ match env:: var ( "HERMIT_RUSTFLAGS" ) {
145+ Ok ( s) => {
146+ let hermit_rustflags = s
147+ . split ( ' ' )
148+ . filter ( |s| !s. is_empty ( ) )
149+ . map ( str:: to_owned)
150+ . collect ( ) ;
151+ return Some ( hermit_rustflags) ;
152+ }
153+ Err ( VarError :: NotPresent ) => { }
154+ Err ( VarError :: NotUnicode ( s) ) => {
155+ panic ! ( "HERMIT_RUSTFLAGS did not contain valid unicode data: {s:?}" )
156+ }
157+ }
158+
159+ None
160+ }
0 commit comments