11use std:: env:: consts;
22use std:: path:: { Path , PathBuf } ;
3-
4- use anyhow:: * ;
3+ use std:: str:: FromStr ;
54
65use crate :: build:: CApiConfig ;
6+ use anyhow:: * ;
7+ use cargo:: core:: compiler:: CompileTarget ;
8+ use cargo_platform:: Cfg ;
79
810/// Split a target string to its components
911///
@@ -16,15 +18,18 @@ pub struct Target {
1618 // pub vendor: String,
1719 pub os : String ,
1820 pub env : String ,
21+ pub target : Option < CompileTarget > ,
22+ pub cfg : Vec < Cfg > ,
1923}
2024
2125impl Target {
22- pub fn new < T : AsRef < std:: ffi:: OsStr > > (
26+ pub fn new < T : AsRef < std:: ffi:: OsStr > + AsRef < str > > (
2327 target : Option < T > ,
2428 is_target_overridden : bool ,
25- ) -> Result < Self , anyhow :: Error > {
29+ ) -> Result < Self > {
2630 let rustc = std:: env:: var ( "RUSTC" ) . unwrap_or_else ( |_| "rustc" . into ( ) ) ;
2731 let mut cmd = std:: process:: Command :: new ( rustc) ;
32+ let target = target. as_ref ( ) ;
2833
2934 cmd. arg ( "--print" ) . arg ( "cfg" ) ;
3035 if let Some ( target) = target {
@@ -46,18 +51,37 @@ impl Target {
4651
4752 let s = std:: str:: from_utf8 ( & out. stdout ) . unwrap ( ) ;
4853
54+ let lines = s. lines ( ) ;
55+
56+ let cfg = lines
57+ . map ( |line| Ok ( Cfg :: from_str ( line) ?) )
58+ . collect :: < Result < Vec < _ > > > ( )
59+ . with_context ( || {
60+ format ! (
61+ "failed to parse the cfg from `rustc --print=cfg`, got:\n {}" ,
62+ s
63+ )
64+ } ) ?;
65+
4966 Ok ( Target {
5067 arch : match_re ( arch_re, s) ,
5168 // vendor: match_re(vendor_re, s),
5269 os : match_re ( os_re, s) ,
5370 env : match_re ( env_re, s) ,
5471 is_target_overridden,
72+ target : target. map ( |t| CompileTarget :: new ( t. as_ref ( ) ) ) . transpose ( ) ?,
73+ cfg,
5574 } )
5675 } else {
5776 Err ( anyhow ! ( "Cannot run {:?}" , cmd) )
5877 }
5978 }
6079
80+ /// Produce the target name, if known
81+ pub fn name ( & self ) -> Option < & str > {
82+ self . target . as_ref ( ) . map ( |t| t. short_name ( ) )
83+ }
84+
6185 /// Build a list of linker arguments
6286 pub fn shared_object_link_args (
6387 & self ,
0 commit comments