@@ -21,6 +21,7 @@ use cargo_util::{paths, ProcessBuilder};
2121use serde:: { Deserialize , Serialize } ;
2222use std:: cell:: RefCell ;
2323use std:: collections:: hash_map:: { Entry , HashMap } ;
24+ use std:: ops:: Deref ;
2425use std:: path:: { Path , PathBuf } ;
2526use std:: str:: { self , FromStr } ;
2627
@@ -850,6 +851,40 @@ fn rustflags_from_build(config: &Config, flag: Flags) -> CargoResult<Option<Vec<
850851 Ok ( list. as_ref ( ) . map ( |l| l. as_slice ( ) . to_vec ( ) ) )
851852}
852853
854+ pub struct RustcTargetDataBuilder < ' cfg > {
855+ data : RustcTargetData < ' cfg > ,
856+ }
857+
858+ impl < ' cfg > RustcTargetDataBuilder < ' cfg > {
859+ /// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
860+ pub fn merge_compile_kind ( & mut self , kind : CompileKind ) -> CargoResult < ( ) > {
861+ if let CompileKind :: Target ( target) = kind {
862+ if !self . data . target_config . contains_key ( & target) {
863+ self . data . target_config
864+ . insert ( target, self . data . config . target_cfg_triple ( target. short_name ( ) ) ?) ;
865+ }
866+ if !self . data . target_info . contains_key ( & target) {
867+ self . data . target_info . insert (
868+ target,
869+ TargetInfo :: new ( self . data . config , & self . data . requested_kinds , & self . data . rustc , kind) ?,
870+ ) ;
871+ }
872+ }
873+ Ok ( ( ) )
874+ }
875+
876+ pub fn build ( self ) -> RustcTargetData < ' cfg > {
877+ self . data
878+ }
879+ }
880+
881+ impl < ' cfg > Deref for RustcTargetDataBuilder < ' cfg > {
882+ type Target = RustcTargetData < ' cfg > ;
883+ fn deref ( & self ) -> & Self :: Target {
884+ & self . data
885+ }
886+ }
887+
853888/// Collection of information about `rustc` and the host and target.
854889pub struct RustcTargetData < ' cfg > {
855890 /// Information about `rustc` itself.
@@ -876,7 +911,7 @@ impl<'cfg> RustcTargetData<'cfg> {
876911 pub fn new (
877912 ws : & Workspace < ' cfg > ,
878913 requested_kinds : & [ CompileKind ] ,
879- ) -> CargoResult < RustcTargetData < ' cfg > > {
914+ ) -> CargoResult < RustcTargetDataBuilder < ' cfg > > {
880915 let config = ws. config ( ) ;
881916 let rustc = config. load_global_rustc ( Some ( ws) ) ?;
882917 let mut target_config = HashMap :: new ( ) ;
@@ -900,7 +935,7 @@ impl<'cfg> RustcTargetData<'cfg> {
900935 target_config. insert ( ct, config. target_cfg_triple ( & rustc. host ) ?) ;
901936 } ;
902937
903- let mut res = RustcTargetData {
938+ let data = RustcTargetData {
904939 rustc,
905940 config,
906941 requested_kinds : requested_kinds. into ( ) ,
@@ -910,30 +945,17 @@ impl<'cfg> RustcTargetData<'cfg> {
910945 target_info,
911946 } ;
912947
948+ let mut res = RustcTargetDataBuilder {
949+ data
950+ } ;
951+
913952 for & kind in requested_kinds {
914953 res. merge_compile_kind ( kind) ?;
915954 }
916955
917956 Ok ( res)
918957 }
919958
920- /// Insert `kind` into our `target_info` and `target_config` members if it isn't present yet.
921- pub ( crate ) fn merge_compile_kind ( & mut self , kind : CompileKind ) -> CargoResult < ( ) > {
922- if let CompileKind :: Target ( target) = kind {
923- if !self . target_config . contains_key ( & target) {
924- self . target_config
925- . insert ( target, self . config . target_cfg_triple ( target. short_name ( ) ) ?) ;
926- }
927- if !self . target_info . contains_key ( & target) {
928- self . target_info . insert (
929- target,
930- TargetInfo :: new ( self . config , & self . requested_kinds , & self . rustc , kind) ?,
931- ) ;
932- }
933- }
934- Ok ( ( ) )
935- }
936-
937959 /// Returns a "short" name for the given kind, suitable for keying off
938960 /// configuration in Cargo or presenting to users.
939961 pub fn short_name < ' a > ( & ' a self , kind : & ' a CompileKind ) -> & ' a str {
0 commit comments