@@ -118,13 +118,38 @@ fn main() {
118
118
}
119
119
}
120
120
121
+ /// Returns the value of an environment variable as a boolean if set
122
+ ///
123
+ /// If the variable is set without a value, that is interpreted as `true`
124
+ ///
125
+ /// ### Panics
126
+ ///
127
+ /// Panics if the value was set, but could not be interpreted as a boolean.
128
+ fn env_var_bool_value ( name : & str ) -> Option < bool > {
129
+ let Some ( os_val) = env:: var_os ( name) else {
130
+ return None ;
131
+ } ;
132
+ let val_lowercase = os_val. to_string_lossy ( ) . to_lowercase ( ) ;
133
+ match val_lowercase. as_str ( ) {
134
+ "1" | "on" | "true" | "yes" | "" => Some ( true ) ,
135
+ "0" | "off" | "false" | "no" => Some ( false ) ,
136
+ _other => {
137
+ panic ! ( "Expected `{name}` to have a boolean-ish value, but got `{os_val:?}`" ) ;
138
+ }
139
+ }
140
+ }
141
+
121
142
/// Check env variable conditions to decide if we need to link pre-built archive first.
122
143
/// And then return bool value to notify if we need to build from source instead.
123
144
fn should_build_from_source ( ) -> bool {
124
- if env:: var_os ( "MOZJS_FROM_SOURCE" ) . is_some ( ) {
125
- println ! ( "Environment variable MOZJS_FROM_SOURCE is set. Building from source directly." ) ;
126
- true
127
- } else if env:: var_os ( "MOZJS_CREATE_ARCHIVE" ) . is_some ( ) {
145
+ if let Some ( from_source) = env_var_bool_value ( "MOZJS_FROM_SOURCE" ) {
146
+ if from_source {
147
+ println ! (
148
+ "Environment variable MOZJS_FROM_SOURCE is set. Building from source directly."
149
+ ) ;
150
+ }
151
+ from_source
152
+ } else if env_var_bool_value ( "MOZJS_CREATE_ARCHIVE" ) == Some ( true ) {
128
153
println ! (
129
154
"Environment variable MOZJS_CREATE_ARCHIVE is set. Building from source directly."
130
155
) ;
0 commit comments