]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/target_features.rs
Number of filtered out tests in tests summary
[rust.git] / src / librustc_driver / target_features.rs
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 use syntax::ast;
12 use llvm::LLVMRustHasFeature;
13 use rustc::session::Session;
14 use rustc_trans::back::write::create_target_machine;
15 use syntax::feature_gate::UnstableFeatures;
16 use syntax::symbol::Symbol;
17 use libc::c_char;
18
19 // WARNING: the features must be known to LLVM or the feature
20 // detection code will walk past the end of the feature array,
21 // leading to crashes.
22
23 const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"];
24
25 const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0",
26                                                  "sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0",
27                                                  "ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0",
28                                                  "sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"];
29
30 const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"];
31
32 /// Add `target_feature = "..."` cfgs for a variety of platform
33 /// specific features (SSE, NEON etc.).
34 ///
35 /// This is performed by checking whether a whitelisted set of
36 /// features is available on the target machine, by querying LLVM.
37 pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
38     let target_machine = create_target_machine(sess);
39
40     let whitelist = match &*sess.target.target.arch {
41         "arm" => ARM_WHITELIST,
42         "x86" | "x86_64" => X86_WHITELIST,
43         "hexagon" => HEXAGON_WHITELIST,
44         _ => &[],
45     };
46
47     let tf = Symbol::intern("target_feature");
48     for feat in whitelist {
49         assert_eq!(feat.chars().last(), Some('\0'));
50         if unsafe { LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } {
51             cfg.insert((tf, Some(Symbol::intern(&feat[..feat.len() - 1]))));
52         }
53     }
54
55     let requested_features = sess.opts.cg.target_feature.split(',');
56     let unstable_options = sess.opts.debugging_opts.unstable_options;
57     let is_nightly = UnstableFeatures::from_environment().is_nightly_build();
58     let found_negative = requested_features.clone().any(|r| r == "-crt-static");
59     let found_positive = requested_features.clone().any(|r| r == "+crt-static");
60
61     // If the target we're compiling for requests a static crt by default,
62     // then see if the `-crt-static` feature was passed to disable that.
63     // Otherwise if we don't have a static crt by default then see if the
64     // `+crt-static` feature was passed.
65     let crt_static = if sess.target.target.options.crt_static_default {
66         !found_negative
67     } else {
68         found_positive
69     };
70
71     // If we switched from the default then that's only allowed on nightly, so
72     // gate that here.
73     if (found_positive || found_negative) && (!is_nightly || !unstable_options) {
74         sess.fatal("specifying the `crt-static` target feature is only allowed \
75                     on the nightly channel with `-Z unstable-options` passed \
76                     as well");
77     }
78
79     if crt_static {
80         cfg.insert((tf, Some(Symbol::intern("crt-static"))));
81     }
82 }