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