@@ -202,7 +202,7 @@ pub struct Config {
202202 pub llvm_use_libcxx : bool ,
203203
204204 // rust codegen options
205- pub rust_optimize : bool ,
205+ pub rust_optimize : RustOptimize ,
206206 pub rust_codegen_units : Option < u32 > ,
207207 pub rust_codegen_units_std : Option < u32 > ,
208208 pub rust_debug_assertions : bool ,
@@ -875,17 +875,55 @@ impl Default for StringOrBool {
875875 }
876876}
877877
878+ #[ derive( Clone , Debug , Deserialize , PartialEq , Eq ) ]
879+ #[ serde( untagged) ]
880+ pub enum RustOptimize {
881+ #[ serde( deserialize_with = "deserialize_and_validate_opt_level" ) ]
882+ String ( String ) ,
883+ Bool ( bool ) ,
884+ }
885+
886+ impl Default for RustOptimize {
887+ fn default ( ) -> RustOptimize {
888+ RustOptimize :: Bool ( false )
889+ }
890+ }
891+
892+ fn deserialize_and_validate_opt_level < ' de , D > ( d : D ) -> Result < String , D :: Error >
893+ where
894+ D : serde:: de:: Deserializer < ' de > ,
895+ {
896+ let v = String :: deserialize ( d) ?;
897+ if [ "0" , "1" , "2" , "3" , "s" , "z" ] . iter ( ) . find ( |x| * * x == v) . is_some ( ) {
898+ Ok ( v)
899+ } else {
900+ Err ( format ! ( r#"unrecognized option for rust optimize: "{}", expected one of "0", "1", "2", "3", "s", "z""# , v) ) . map_err ( serde:: de:: Error :: custom)
901+ }
902+ }
903+
904+ impl RustOptimize {
905+ pub ( crate ) fn is_release ( & self ) -> bool {
906+ if let RustOptimize :: Bool ( true ) | RustOptimize :: String ( _) = & self { true } else { false }
907+ }
908+
909+ pub ( crate ) fn get_opt_level ( & self ) -> Option < String > {
910+ match & self {
911+ RustOptimize :: String ( s) => Some ( s. clone ( ) ) ,
912+ RustOptimize :: Bool ( _) => None ,
913+ }
914+ }
915+ }
916+
878917#[ derive( Deserialize ) ]
879918#[ serde( untagged) ]
880919enum StringOrInt < ' a > {
881920 String ( & ' a str ) ,
882921 Int ( i64 ) ,
883922}
884-
885923define_config ! {
886924 /// TOML representation of how the Rust build is configured.
887925 struct Rust {
888- optimize: Option <bool > = "optimize" ,
926+ optimize: Option <RustOptimize > = "optimize" ,
889927 debug: Option <bool > = "debug" ,
890928 codegen_units: Option <u32 > = "codegen-units" ,
891929 codegen_units_std: Option <u32 > = "codegen-units-std" ,
@@ -971,7 +1009,7 @@ impl Config {
9711009 config. ninja_in_file = true ;
9721010 config. llvm_static_stdcpp = false ;
9731011 config. backtrace = true ;
974- config. rust_optimize = true ;
1012+ config. rust_optimize = RustOptimize :: Bool ( true ) ;
9751013 config. rust_optimize_tests = true ;
9761014 config. submodules = None ;
9771015 config. docs = true ;
@@ -1546,7 +1584,7 @@ impl Config {
15461584 config. llvm_assertions = llvm_assertions. unwrap_or ( false ) ;
15471585 config. llvm_tests = llvm_tests. unwrap_or ( false ) ;
15481586 config. llvm_plugins = llvm_plugins. unwrap_or ( false ) ;
1549- config. rust_optimize = optimize. unwrap_or ( true ) ;
1587+ config. rust_optimize = optimize. unwrap_or ( RustOptimize :: Bool ( true ) ) ;
15501588
15511589 let default = debug == Some ( true ) ;
15521590 config. rust_debug_assertions = debug_assertions. unwrap_or ( default) ;
0 commit comments