@@ -41,14 +41,51 @@ impl<'a> Iterator for PathAncestors<'a> {
4141 }
4242}
4343
44- #[ derive( Default , Deserialize ) ]
44+ /// `build.target` can be a single target or a list of targets.
45+ #[ derive( Deserialize ) ]
46+ #[ serde( untagged) ]
47+ enum BuildTarget {
48+ Single ( String ) ,
49+ Multiple ( Vec < String > ) ,
50+ }
51+
52+ impl BuildTarget {
53+ fn into_target ( self ) -> Option < String > {
54+ match self {
55+ Self :: Single ( target) => Some ( target) ,
56+ Self :: Multiple ( targets) => {
57+ if targets. len ( ) > 1 {
58+ log:: warn!(
59+ "Multiple targets configured in `build.target` ({}), using the first one: {}" ,
60+ targets. join( ", " ) ,
61+ targets[ 0 ]
62+ ) ;
63+ }
64+ targets. into_iter ( ) . next ( )
65+ }
66+ }
67+ }
68+ }
69+
70+ #[ derive( Deserialize ) ]
71+ struct BuildConfigSchema {
72+ target : Option < BuildTarget > ,
73+ }
74+
75+ #[ derive( Default ) ]
4576pub struct BuildConfig {
4677 target : Option < String > ,
4778}
4879
4980#[ derive( Deserialize ) ]
50- pub struct ConfigSchema {
51- build : Option < BuildConfig > ,
81+ struct ConfigSchema {
82+ build : Option < BuildConfigSchema > ,
83+ }
84+
85+ impl ConfigSchema {
86+ fn build_target ( self ) -> Option < String > {
87+ self . build ?. target ?. into_target ( )
88+ }
5289}
5390
5491#[ derive( Default ) ]
@@ -69,21 +106,30 @@ impl Config {
69106 ) )
70107 } ;
71108
72- for current in PathAncestors :: new ( path) {
73- if let Some ( path) = get_file_path ( & current. join ( ".cargo" ) , "config" , true ) ? {
74- let toml = get_config ( path) ?;
75- if let Some ( target) = toml. build . and_then ( |b| b. target ) {
76- config. build . target = Some ( target) ;
77- break ;
109+ // the environment variable takes precedence over the configuration files
110+ if let Ok ( target) = std:: env:: var ( "CARGO_BUILD_TARGET" ) {
111+ let targets = target
112+ . split_whitespace ( )
113+ . map ( ToString :: to_string)
114+ . collect :: < Vec < _ > > ( ) ;
115+ config. build . target = BuildTarget :: Multiple ( targets) . into_target ( ) ;
116+ }
117+
118+ if config. build . target . is_none ( ) {
119+ for current in PathAncestors :: new ( path) {
120+ if let Some ( path) = get_file_path ( & current. join ( ".cargo" ) , "config" , true ) ? {
121+ if let Some ( target) = get_config ( path) ?. build_target ( ) {
122+ config. build . target = Some ( target) ;
123+ break ;
124+ }
78125 }
79126 }
80127 }
81128
82129 if config. build . target . is_none ( ) {
83130 if let Ok ( cargo_home) = std:: env:: var ( "CARGO_HOME" ) {
84131 if let Some ( path) = get_file_path ( & PathBuf :: from ( cargo_home) , "config" , true ) ? {
85- let toml = get_config ( path) ?;
86- if let Some ( target) = toml. build . and_then ( |b| b. target ) {
132+ if let Some ( target) = get_config ( path) ?. build_target ( ) {
87133 config. build . target = Some ( target) ;
88134 }
89135 }
@@ -147,3 +193,31 @@ fn get_file_path(
147193 Ok ( None )
148194 }
149195}
196+
197+ #[ cfg( test) ]
198+ mod tests {
199+ use super :: ConfigSchema ;
200+
201+ fn build_target ( toml : & str ) -> Option < String > {
202+ toml:: from_str :: < ConfigSchema > ( toml) . unwrap ( ) . build_target ( )
203+ }
204+
205+ #[ test]
206+ fn parse_build_target ( ) {
207+ assert_eq ! ( build_target( "" ) , None ) ;
208+ assert_eq ! ( build_target( "[build]\n jobs = 2" ) , None ) ;
209+ assert_eq ! (
210+ build_target( "[build]\n target = \" x86_64-pc-windows-msvc\" " ) ,
211+ Some ( "x86_64-pc-windows-msvc" . into( ) )
212+ ) ;
213+ assert_eq ! (
214+ build_target( "[build]\n target = [\" aarch64-apple-darwin\" ]" ) ,
215+ Some ( "aarch64-apple-darwin" . into( ) )
216+ ) ;
217+ assert_eq ! (
218+ build_target( "[build]\n target = [\" aarch64-apple-darwin\" , \" x86_64-apple-darwin\" ]" ) ,
219+ Some ( "aarch64-apple-darwin" . into( ) )
220+ ) ;
221+ assert_eq ! ( build_target( "[build]\n target = []" ) , None ) ;
222+ }
223+ }
0 commit comments