Skip to content

Commit 78ce46f

Browse files
committed
Auto merge of #28612 - gandro:targetvendor, r=alexcrichton
This adds a new target property, `target_vendor`. It is to be be used as a matcher for conditional compilation. The vendor is part of the [autoconf target triple](http://llvm.org/docs/doxygen/html/classllvm_1_1Triple.html#details): `<arch><sub>-<vendor>-<os>-<env>`. `arch`, `target_os` and `target_env` are already supported by Rust. This change was suggested in PR #28593. It enables conditional compilation based on the vendor. This is needed for the rumprun target, which needs to match against both, target_os and target_vendor. The default value for `target_vendor` is "unknown", "apple" and "pc" are other common values. Matching against the `target_vendor` is introduced behind the feature gate `#![feature(cfg_target_vendor)]`. This is the first time I messed around with rustc internals. I just added the my code where I found the existing `target_*` variables, hopefully I haven't missed anything. Please review with care. :) r? @alexcrichton
2 parents 69f27c8 + abfedb7 commit 78ce46f

36 files changed

+95
-3
lines changed

src/doc/reference.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,8 @@ The following configurations must be defined by the implementation:
20932093
* `target_pointer_width = "..."` - Target pointer width in bits. This is set
20942094
to `"32"` for targets with 32-bit pointers, and likewise set to `"64"` for
20952095
64-bit pointers.
2096+
* `target_vendor = "..."` - Vendor of the target, for example `apple`, `pc`, or
2097+
simply `"unknown"`.
20962098
* `test` - Enabled when compiling the test harness (using the `--test` flag).
20972099
* `unix` - See `target_family`.
20982100
* `windows` - See `target_family`.
@@ -2269,7 +2271,7 @@ The currently implemented features of the reference compiler are:
22692271
* `advanced_slice_patterns` - See the [match expressions](#match-expressions)
22702272
section for discussion; the exact semantics of
22712273
slice patterns are subject to change, so some types
2272-
are still unstable.
2274+
are still unstable.
22732275

22742276
* `slice_patterns` - OK, actually, slice patterns are just scary and
22752277
completely unstable.
@@ -2290,6 +2292,9 @@ The currently implemented features of the reference compiler are:
22902292
* `box_syntax` - Allows use of `box` expressions, the exact semantics of which
22912293
is subject to change.
22922294

2295+
* `cfg_target_vendor` - Allows conditional compilation using the `target_vendor`
2296+
matcher which is subject to change.
2297+
22932298
* `concat_idents` - Allows use of the `concat_idents` macro, which is in many
22942299
ways insufficient for concatenating identifiers, and may be
22952300
removed entirely for something more wholesome.

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
617617
let wordsz = &sess.target.target.target_pointer_width;
618618
let os = &sess.target.target.target_os;
619619
let env = &sess.target.target.target_env;
620+
let vendor = &sess.target.target.target_vendor;
620621

621622
let fam = match sess.target.target.options.is_like_windows {
622623
true => InternedString::new("windows"),
@@ -632,6 +633,7 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
632633
mk(InternedString::new("target_endian"), intern(end)),
633634
mk(InternedString::new("target_pointer_width"), intern(wordsz)),
634635
mk(InternedString::new("target_env"), intern(env)),
636+
mk(InternedString::new("target_vendor"), intern(vendor)),
635637
];
636638
if sess.opts.debug_assertions {
637639
ret.push(attr::mk_word_item(InternedString::new("debug_assertions")));

src/librustc_back/target/aarch64_apple_ios.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "aarch64".to_string(),
2020
target_os: "ios".to_string(),
2121
target_env: "".to_string(),
22+
target_vendor: "apple".to_string(),
2223
options: TargetOptions {
2324
features: "+neon,+fp-armv8,+cyclone".to_string(),
2425
eliminate_frame_pointer: false,

src/librustc_back/target/aarch64_linux_android.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn target() -> Target {
1818
arch: "aarch64".to_string(),
1919
target_os: "android".to_string(),
2020
target_env: "".to_string(),
21+
target_vendor: "unknown".to_string(),
2122
options: super::android_base::opts(),
2223
}
2324
}

src/librustc_back/target/aarch64_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
target_env: "gnu".to_string(),
2020
arch: "aarch64".to_string(),
2121
target_os: "linux".to_string(),
22+
target_vendor: "unknown".to_string(),
2223
options: base,
2324
}
2425
}

src/librustc_back/target/arm_linux_androideabi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "arm".to_string(),
2222
target_os: "android".to_string(),
2323
target_env: "gnu".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/arm_unknown_linux_gnueabi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "arm".to_string(),
2020
target_os: "linux".to_string(),
2121
target_env: "gnueabi".to_string(),
22+
target_vendor: "unknown".to_string(),
2223

2324
options: TargetOptions {
2425
features: "+v6".to_string(),

src/librustc_back/target/arm_unknown_linux_gnueabihf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "arm".to_string(),
2020
target_os: "linux".to_string(),
2121
target_env: "gnueabihf".to_string(),
22+
target_vendor: "unknown".to_string(),
2223

2324
options: TargetOptions {
2425
features: "+v6,+vfp2".to_string(),

src/librustc_back/target/armv7_apple_ios.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "arm".to_string(),
2020
target_os: "ios".to_string(),
2121
target_env: "".to_string(),
22+
target_vendor: "apple".to_string(),
2223
options: TargetOptions {
2324
features: "+v7,+vfp3,+neon".to_string(),
2425
.. opts(Arch::Armv7)

src/librustc_back/target/armv7s_apple_ios.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "arm".to_string(),
2020
target_os: "ios".to_string(),
2121
target_env: "".to_string(),
22+
target_vendor: "apple".to_string(),
2223
options: TargetOptions {
2324
features: "+v7,+vfp4,+neon".to_string(),
2425
.. opts(Arch::Armv7s)

src/librustc_back/target/i386_apple_ios.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "x86".to_string(),
2020
target_os: "ios".to_string(),
2121
target_env: "".to_string(),
22+
target_vendor: "apple".to_string(),
2223
options: opts(Arch::I386)
2324
}
2425
}

src/librustc_back/target/i686_apple_darwin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86".to_string(),
2323
target_os: "macos".to_string(),
2424
target_env: "".to_string(),
25+
target_vendor: "apple".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/i686_linux_android.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "x86".to_string(),
2222
target_os: "android".to_string(),
2323
target_env: "gnu".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/i686_pc_windows_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn target() -> Target {
3030
arch: "x86".to_string(),
3131
target_os: "windows".to_string(),
3232
target_env: "gnu".to_string(),
33+
target_vendor: "pc".to_string(),
3334
options: options,
3435
}
3536
}

src/librustc_back/target/i686_pc_windows_msvc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "x86".to_string(),
2222
target_os: "windows".to_string(),
2323
target_env: "msvc".to_string(),
24+
target_vendor: "pc".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/i686_unknown_dragonfly.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86".to_string(),
2323
target_os: "dragonfly".to_string(),
2424
target_env: "".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/i686_unknown_freebsd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86".to_string(),
2323
target_os: "freebsd".to_string(),
2424
target_env: "".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/i686_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86".to_string(),
2323
target_os: "linux".to_string(),
2424
target_env: "gnu".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/mips_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn target() -> Target {
1818
arch: "mips".to_string(),
1919
target_os: "linux".to_string(),
2020
target_env: "gnu".to_string(),
21+
target_vendor: "unknown".to_string(),
2122
options: super::linux_base::opts()
2223
}
2324
}

src/librustc_back/target/mipsel_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn target() -> Target {
1818
arch: "mips".to_string(),
1919
target_os: "linux".to_string(),
2020
target_env: "gnu".to_string(),
21+
target_vendor: "unknown".to_string(),
2122

2223
options: super::linux_base::opts()
2324
}

src/librustc_back/target/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub struct Target {
7777
pub target_os: String,
7878
/// Environment name to use for conditional compilation.
7979
pub target_env: String,
80+
/// Vendor name to use for conditional compilation.
81+
pub target_vendor: String,
8082
/// Architecture to use for ABI considerations. Valid options: "x86", "x86_64", "arm",
8183
/// "aarch64", "mips", and "powerpc". "mips" includes "mipsel".
8284
pub arch: String,
@@ -260,14 +262,20 @@ impl Target {
260262
}
261263
};
262264

265+
let get_opt_field = |name: &str, default: &str| {
266+
obj.find(name).and_then(|s| s.as_string())
267+
.map(|s| s.to_string())
268+
.unwrap_or(default.to_string())
269+
};
270+
263271
let mut base = Target {
264272
llvm_target: get_req_field("llvm-target"),
265273
target_endian: get_req_field("target-endian"),
266274
target_pointer_width: get_req_field("target-pointer-width"),
267275
arch: get_req_field("arch"),
268276
target_os: get_req_field("os"),
269-
target_env: obj.find("env").and_then(|s| s.as_string())
270-
.map(|s| s.to_string()).unwrap_or(String::new()),
277+
target_env: get_opt_field("env", ""),
278+
target_vendor: get_opt_field("vendor", "unknown"),
271279
options: Default::default(),
272280
};
273281

src/librustc_back/target/powerpc_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "powerpc".to_string(),
2222
target_os: "linux".to_string(),
2323
target_env: "gnu".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/x86_64_apple_darwin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn target() -> Target {
2323
arch: "x86_64".to_string(),
2424
target_os: "macos".to_string(),
2525
target_env: "".to_string(),
26+
target_vendor: "apple".to_string(),
2627
options: base,
2728
}
2829
}

src/librustc_back/target/x86_64_apple_ios.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn target() -> Target {
1919
arch: "x86_64".to_string(),
2020
target_os: "ios".to_string(),
2121
target_env: "".to_string(),
22+
target_vendor: "apple".to_string(),
2223
options: opts(Arch::X86_64)
2324
}
2425
}

src/librustc_back/target/x86_64_pc_windows_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn target() -> Target {
2525
arch: "x86_64".to_string(),
2626
target_os: "windows".to_string(),
2727
target_env: "gnu".to_string(),
28+
target_vendor: "pc".to_string(),
2829
options: base,
2930
}
3031
}

src/librustc_back/target/x86_64_pc_windows_msvc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86_64".to_string(),
2323
target_os: "windows".to_string(),
2424
target_env: "msvc".to_string(),
25+
target_vendor: "pc".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/x86_64_unknown_bitrig.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "x86_64".to_string(),
2222
target_os: "bitrig".to_string(),
2323
target_env: "".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/x86_64_unknown_dragonfly.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86_64".to_string(),
2323
target_os: "dragonfly".to_string(),
2424
target_env: "".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/x86_64_unknown_freebsd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86_64".to_string(),
2323
target_os: "freebsd".to_string(),
2424
target_env: "".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/x86_64_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
arch: "x86_64".to_string(),
2323
target_os: "linux".to_string(),
2424
target_env: "gnu".to_string(),
25+
target_vendor: "unknown".to_string(),
2526
options: base,
2627
}
2728
}

src/librustc_back/target/x86_64_unknown_linux_musl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub fn target() -> Target {
7676
arch: "x86_64".to_string(),
7777
target_os: "linux".to_string(),
7878
target_env: "musl".to_string(),
79+
target_vendor: "unknown".to_string(),
7980
options: base,
8081
}
8182
}

src/librustc_back/target/x86_64_unknown_netbsd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "x86_64".to_string(),
2222
target_os: "netbsd".to_string(),
2323
target_env: "".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/librustc_back/target/x86_64_unknown_openbsd.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn target() -> Target {
2121
arch: "x86_64".to_string(),
2222
target_os: "openbsd".to_string(),
2323
target_env: "".to_string(),
24+
target_vendor: "unknown".to_string(),
2425
options: base,
2526
}
2627
}

src/libsyntax/feature_gate.rs

+7
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
203203

204204
// allow `#[omit_gdb_pretty_printer_section]`
205205
("omit_gdb_pretty_printer_section", "1.5.0", None, Active),
206+
207+
// Allows cfg(target_vendor = "...").
208+
("cfg_target_vendor", "1.5.0", None, Active),
206209
];
207210
// (changing above list without updating src/doc/reference.md makes @cmr sad)
208211

@@ -377,6 +380,7 @@ macro_rules! cfg_fn {
377380
const GATED_CFGS: &'static [(&'static str, &'static str, fn(&Features) -> bool)] = &[
378381
// (name in cfg, feature, function to check if the feature is enabled)
379382
("target_feature", "cfg_target_feature", cfg_fn!(|x| x.cfg_target_feature)),
383+
("target_vendor", "cfg_target_vendor", cfg_fn!(|x| x.cfg_target_vendor)),
380384
];
381385

382386
#[derive(Debug, Eq, PartialEq)]
@@ -471,6 +475,7 @@ pub struct Features {
471475
pub default_type_parameter_fallback: bool,
472476
pub type_macros: bool,
473477
pub cfg_target_feature: bool,
478+
pub cfg_target_vendor: bool,
474479
pub augmented_assignments: bool,
475480
}
476481

@@ -500,6 +505,7 @@ impl Features {
500505
default_type_parameter_fallback: false,
501506
type_macros: false,
502507
cfg_target_feature: false,
508+
cfg_target_vendor: false,
503509
augmented_assignments: false,
504510
}
505511
}
@@ -1069,6 +1075,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
10691075
default_type_parameter_fallback: cx.has_feature("default_type_parameter_fallback"),
10701076
type_macros: cx.has_feature("type_macros"),
10711077
cfg_target_feature: cx.has_feature("cfg_target_feature"),
1078+
cfg_target_vendor: cx.has_feature("cfg_target_vendor"),
10721079
augmented_assignments: cx.has_feature("augmented_assignments"),
10731080
}
10741081
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[cfg(target_vendor = "x")] //~ ERROR `cfg(target_vendor)` is experimental
12+
#[cfg_attr(target_vendor = "x", x)] //~ ERROR `cfg(target_vendor)` is experimental
13+
struct Foo(u64, u64);
14+
15+
#[cfg(not(any(all(target_vendor = "x"))))] //~ ERROR `cfg(target_vendor)` is experimental
16+
fn foo() {}
17+
18+
fn main() {
19+
cfg!(target_vendor = "x");
20+
//~^ ERROR `cfg(target_vendor)` is experimental and subject to change
21+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(cfg_target_vendor)]
12+
13+
#[cfg(target_vendor = "unknown")]
14+
pub fn main() {
15+
}
16+
17+
#[cfg(not(target_vendor = "unknown"))]
18+
pub fn main() {
19+
}

0 commit comments

Comments
 (0)