@@ -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,38 @@ 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 ( a) => {
132+ if a. is_empty ( ) {
133+ return Some ( Vec :: new ( ) ) ;
134+ }
135+ return Some ( a. split ( '\x1f' ) . map ( str:: to_string) . collect ( ) ) ;
136+ }
137+ Err ( VarError :: NotPresent ) => { }
138+ Err ( VarError :: NotUnicode ( a) ) => {
139+ panic ! ( "HERMIT_ENCODED_RUSTFLAGS did not contain valid unicode data: {a:?}" ) ;
140+ }
141+ }
142+
143+ match env:: var ( "HERMIT_RUSTFLAGS" ) {
144+ Ok ( a) => {
145+ let args = a
146+ . split ( ' ' )
147+ . map ( str:: trim)
148+ . filter ( |s| !s. is_empty ( ) )
149+ . map ( str:: to_string) ;
150+ return Some ( args. collect ( ) ) ;
151+ }
152+ Err ( VarError :: NotPresent ) => { }
153+ Err ( VarError :: NotUnicode ( a) ) => {
154+ panic ! ( "HERMIT_RUSTFLAGS did not contain valid unicode data: {a:?}" ) ;
155+ }
156+ }
157+
158+ None
159+ }
0 commit comments