]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/cc.rs
Utilize interning to allow Copy/Clone steps
[rust.git] / src / bootstrap / cc.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 //! C-compiler probing and detection.
12 //!
13 //! This module will fill out the `cc` and `cxx` maps of `Build` by looking for
14 //! C and C++ compilers for each target configured. A compiler is found through
15 //! a number of vectors (in order of precedence)
16 //!
17 //! 1. Configuration via `target.$target.cc` in `config.toml`.
18 //! 2. Configuration via `target.$target.android-ndk` in `config.toml`, if
19 //!    applicable
20 //! 3. Special logic to probe on OpenBSD
21 //! 4. The `CC_$target` environment variable.
22 //! 5. The `CC` environment variable.
23 //! 6. "cc"
24 //!
25 //! Some of this logic is implemented here, but much of it is farmed out to the
26 //! `gcc` crate itself, so we end up having the same fallbacks as there.
27 //! Similar logic is then used to find a C++ compiler, just some s/cc/c++/ is
28 //! used.
29 //!
30 //! It is intended that after this module has run no C/C++ compiler will
31 //! ever be probed for. Instead the compilers found here will be used for
32 //! everything.
33
34 use std::process::Command;
35
36 use build_helper::{cc2ar, output};
37 use gcc;
38
39 use Build;
40 use config::Target;
41 use cache::Interned;
42
43 pub fn find(build: &mut Build) {
44     // For all targets we're going to need a C compiler for building some shims
45     // and such as well as for being a linker for Rust code.
46     //
47     // This includes targets that aren't necessarily passed on the commandline
48     // (FIXME: Perhaps it shouldn't?)
49     for target in &build.config.target {
50         let mut cfg = gcc::Config::new();
51         cfg.cargo_metadata(false).opt_level(0).debug(false)
52            .target(target).host(&build.build);
53
54         let config = build.config.target_config.get(&target);
55         if let Some(cc) = config.and_then(|c| c.cc.as_ref()) {
56             cfg.compiler(cc);
57         } else {
58             set_compiler(&mut cfg, "gcc", *target, config, build);
59         }
60
61         let compiler = cfg.get_compiler();
62         let ar = cc2ar(compiler.path(), target);
63         build.verbose(&format!("CC_{} = {:?}", target, compiler.path()));
64         if let Some(ref ar) = ar {
65             build.verbose(&format!("AR_{} = {:?}", target, ar));
66         }
67         build.cc.insert(*target, (compiler, ar));
68     }
69
70     // For all host triples we need to find a C++ compiler as well
71     //
72     // This includes hosts that aren't necessarily passed on the commandline
73     // (FIXME: Perhaps it shouldn't?)
74     for host in &build.config.host {
75         let mut cfg = gcc::Config::new();
76         cfg.cargo_metadata(false).opt_level(0).debug(false).cpp(true)
77            .target(host).host(&build.build);
78         let config = build.config.target_config.get(host);
79         if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
80             cfg.compiler(cxx);
81         } else {
82             set_compiler(&mut cfg, "g++", *host, config, build);
83         }
84         let compiler = cfg.get_compiler();
85         build.verbose(&format!("CXX_{} = {:?}", host, compiler.path()));
86         build.cxx.insert(*host, compiler);
87     }
88 }
89
90 fn set_compiler(cfg: &mut gcc::Config,
91                 gnu_compiler: &str,
92                 target: Interned<String>,
93                 config: Option<&Target>,
94                 build: &Build) {
95     match &*target {
96         // When compiling for android we may have the NDK configured in the
97         // config.toml in which case we look there. Otherwise the default
98         // compiler already takes into account the triple in question.
99         t if t.contains("android") => {
100             if let Some(ndk) = config.and_then(|c| c.ndk.as_ref()) {
101                 let target = target.replace("armv7", "arm");
102                 let compiler = format!("{}-{}", target, gnu_compiler);
103                 cfg.compiler(ndk.join("bin").join(compiler));
104             }
105         }
106
107         // The default gcc version from OpenBSD may be too old, try using egcc,
108         // which is a gcc version from ports, if this is the case.
109         t if t.contains("openbsd") => {
110             let c = cfg.get_compiler();
111             if !c.path().ends_with(gnu_compiler) {
112                 return
113             }
114
115             let output = output(c.to_command().arg("--version"));
116             let i = match output.find(" 4.") {
117                 Some(i) => i,
118                 None => return,
119             };
120             match output[i + 3..].chars().next().unwrap() {
121                 '0' ... '6' => {}
122                 _ => return,
123             }
124             let alternative = format!("e{}", gnu_compiler);
125             if Command::new(&alternative).output().is_ok() {
126                 cfg.compiler(alternative);
127             }
128         }
129
130         "mips-unknown-linux-musl" => {
131             if cfg.get_compiler().path().to_str() == Some("gcc") {
132                 cfg.compiler("mips-linux-musl-gcc");
133             }
134         }
135         "mipsel-unknown-linux-musl" => {
136             if cfg.get_compiler().path().to_str() == Some("gcc") {
137                 cfg.compiler("mipsel-linux-musl-gcc");
138             }
139         }
140
141         t if t.contains("musl") => {
142             if let Some(root) = build.musl_root(target) {
143                 let guess = root.join("bin/musl-gcc");
144                 if guess.exists() {
145                     cfg.compiler(guess);
146                 }
147             }
148         }
149
150         _ => {}
151     }
152 }