@@ -6,13 +6,15 @@ use std::{borrow::Cow, env, str::FromStr};
66
77use crate :: { Error , ErrorKind } ;
88
9+ mod apple;
910mod generated;
11+ mod llvm;
1012
11- /// The parts of `rustc`'s target triple .
13+ /// Information specific to a `rustc` target.
1214///
1315/// See <https://doc.rust-lang.org/cargo/appendix/glossary.html#target>.
1416#[ derive( Debug , PartialEq , Clone ) ]
15- pub ( crate ) struct Target {
17+ pub ( crate ) struct TargetInfo {
1618 /// The full architecture, including the subarchitecture.
1719 ///
1820 /// This differs from `cfg!(target_arch)`, which only specifies the
@@ -38,9 +40,11 @@ pub(crate) struct Target {
3840 ///
3941 /// This is the same as the value of `cfg!(target_abi)`.
4042 pub abi : Cow < ' static , str > ,
43+ /// The unversioned LLVM/Clang target triple.
44+ unversioned_llvm_target : Cow < ' static , str > ,
4145}
4246
43- impl Target {
47+ impl TargetInfo {
4448 pub fn from_cargo_environment_variables ( ) -> Result < Self , Error > {
4549 // `TARGET` must be present.
4650 //
@@ -90,7 +94,7 @@ impl Target {
9094 // back back to data from the known set of target triples instead.
9195 //
9296 // See discussion in #1225 for further details.
93- let fallback_target = Target :: from_str ( & target_triple) . ok ( ) ;
97+ let fallback_target = TargetInfo :: from_str ( & target_triple) . ok ( ) ;
9498 let ft = fallback_target. as_ref ( ) ;
9599 let arch = cargo_env ( "CARGO_CFG_TARGET_ARCH" , ft. map ( |t| t. arch . clone ( ) ) ) ?;
96100 let vendor = cargo_env ( "CARGO_CFG_TARGET_VENDOR" , ft. map ( |t| t. vendor . clone ( ) ) ) ?;
@@ -102,27 +106,34 @@ impl Target {
102106 let abi = cargo_env ( "CARGO_CFG_TARGET_ABI" , ft. map ( |t| t. abi . clone ( ) ) )
103107 . unwrap_or ( Cow :: Borrowed ( "" ) ) ;
104108
109+ // Prefer `rustc`'s LLVM target triple information.
110+ let unversioned_llvm_target = match fallback_target {
111+ Some ( ft) => ft. unversioned_llvm_target ,
112+ None => llvm:: guess_llvm_target_triple ( full_arch, & vendor, & os, & env, & abi) . into ( ) ,
113+ } ;
114+
105115 Ok ( Self {
106116 full_arch : full_arch. to_string ( ) . into ( ) ,
107117 arch,
108118 vendor,
109119 os,
110120 env,
111121 abi,
122+ unversioned_llvm_target,
112123 } )
113124 }
114125}
115126
116- impl FromStr for Target {
127+ impl FromStr for TargetInfo {
117128 type Err = Error ;
118129
119130 /// This will fail when using a custom target triple unknown to `rustc`.
120131 fn from_str ( target_triple : & str ) -> Result < Self , Error > {
121132 if let Ok ( index) =
122133 generated:: LIST . binary_search_by_key ( & target_triple, |( target_triple, _) | target_triple)
123134 {
124- let ( _, target ) = & generated:: LIST [ index] ;
125- Ok ( target . clone ( ) )
135+ let ( _, info ) = & generated:: LIST [ index] ;
136+ Ok ( info . clone ( ) )
126137 } else {
127138 Err ( Error :: new (
128139 ErrorKind :: InvalidTarget ,
@@ -136,7 +147,7 @@ impl FromStr for Target {
136147mod tests {
137148 use std:: str:: FromStr ;
138149
139- use super :: Target ;
150+ use super :: TargetInfo ;
140151
141152 // Test tier 1 targets
142153 #[ test]
@@ -155,7 +166,7 @@ mod tests {
155166
156167 for target in targets {
157168 // Check that it parses
158- let _ = Target :: from_str ( target) . unwrap ( ) ;
169+ let _ = TargetInfo :: from_str ( target) . unwrap ( ) ;
159170 }
160171 }
161172
@@ -177,7 +188,7 @@ mod tests {
177188
178189 for target in targets {
179190 // Check that it does not parse
180- let _ = Target :: from_str ( target) . unwrap_err ( ) ;
191+ let _ = TargetInfo :: from_str ( target) . unwrap_err ( ) ;
181192 }
182193 }
183194}
0 commit comments