@@ -6,13 +6,15 @@ use std::{borrow::Cow, env, str::FromStr};
6
6
7
7
use crate :: { Error , ErrorKind } ;
8
8
9
+ mod apple;
9
10
mod generated;
11
+ mod llvm;
10
12
11
- /// The parts of `rustc`'s target triple .
13
+ /// Information specific to a `rustc` target.
12
14
///
13
15
/// See <https://doc.rust-lang.org/cargo/appendix/glossary.html#target>.
14
16
#[ derive( Debug , PartialEq , Clone ) ]
15
- pub ( crate ) struct Target {
17
+ pub ( crate ) struct TargetInfo {
16
18
/// The full architecture, including the subarchitecture.
17
19
///
18
20
/// This differs from `cfg!(target_arch)`, which only specifies the
@@ -38,9 +40,11 @@ pub(crate) struct Target {
38
40
///
39
41
/// This is the same as the value of `cfg!(target_abi)`.
40
42
pub abi : Cow < ' static , str > ,
43
+ /// The unversioned LLVM/Clang target triple.
44
+ unversioned_llvm_target : Cow < ' static , str > ,
41
45
}
42
46
43
- impl Target {
47
+ impl TargetInfo {
44
48
pub fn from_cargo_environment_variables ( ) -> Result < Self , Error > {
45
49
// `TARGET` must be present.
46
50
//
@@ -90,7 +94,7 @@ impl Target {
90
94
// back back to data from the known set of target triples instead.
91
95
//
92
96
// 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 ( ) ;
94
98
let ft = fallback_target. as_ref ( ) ;
95
99
let arch = cargo_env ( "CARGO_CFG_TARGET_ARCH" , ft. map ( |t| t. arch . clone ( ) ) ) ?;
96
100
let vendor = cargo_env ( "CARGO_CFG_TARGET_VENDOR" , ft. map ( |t| t. vendor . clone ( ) ) ) ?;
@@ -102,27 +106,34 @@ impl Target {
102
106
let abi = cargo_env ( "CARGO_CFG_TARGET_ABI" , ft. map ( |t| t. abi . clone ( ) ) )
103
107
. unwrap_or ( Cow :: Borrowed ( "" ) ) ;
104
108
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
+
105
115
Ok ( Self {
106
116
full_arch : full_arch. to_string ( ) . into ( ) ,
107
117
arch,
108
118
vendor,
109
119
os,
110
120
env,
111
121
abi,
122
+ unversioned_llvm_target,
112
123
} )
113
124
}
114
125
}
115
126
116
- impl FromStr for Target {
127
+ impl FromStr for TargetInfo {
117
128
type Err = Error ;
118
129
119
130
/// This will fail when using a custom target triple unknown to `rustc`.
120
131
fn from_str ( target_triple : & str ) -> Result < Self , Error > {
121
132
if let Ok ( index) =
122
133
generated:: LIST . binary_search_by_key ( & target_triple, |( target_triple, _) | target_triple)
123
134
{
124
- let ( _, target ) = & generated:: LIST [ index] ;
125
- Ok ( target . clone ( ) )
135
+ let ( _, info ) = & generated:: LIST [ index] ;
136
+ Ok ( info . clone ( ) )
126
137
} else {
127
138
Err ( Error :: new (
128
139
ErrorKind :: InvalidTarget ,
@@ -136,7 +147,7 @@ impl FromStr for Target {
136
147
mod tests {
137
148
use std:: str:: FromStr ;
138
149
139
- use super :: Target ;
150
+ use super :: TargetInfo ;
140
151
141
152
// Test tier 1 targets
142
153
#[ test]
@@ -155,7 +166,7 @@ mod tests {
155
166
156
167
for target in targets {
157
168
// Check that it parses
158
- let _ = Target :: from_str ( target) . unwrap ( ) ;
169
+ let _ = TargetInfo :: from_str ( target) . unwrap ( ) ;
159
170
}
160
171
}
161
172
@@ -177,7 +188,7 @@ mod tests {
177
188
178
189
for target in targets {
179
190
// Check that it does not parse
180
- let _ = Target :: from_str ( target) . unwrap_err ( ) ;
191
+ let _ = TargetInfo :: from_str ( target) . unwrap_err ( ) ;
181
192
}
182
193
}
183
194
}
0 commit comments