]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/cc.rs
Auto merge of #43051 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
[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
42 pub fn find(build: &mut Build) {
43     // For all targets we're going to need a C compiler for building some shims
44     // and such as well as for being a linker for Rust code.
45     //
46     // This includes targets that aren't necessarily passed on the commandline
47     // (FIXME: Perhaps it shouldn't?)
48     for target in &build.config.target {
49         let mut cfg = gcc::Config::new();
50         cfg.cargo_metadata(false).opt_level(0).debug(false)
51            .target(target).host(&build.build);
52
53         let config = build.config.target_config.get(target);
54         if let Some(cc) = config.and_then(|c| c.cc.as_ref()) {
55             cfg.compiler(cc);
56         } else {
57             set_compiler(&mut cfg, "gcc", target, config, build);
58         }
59
60         let compiler = cfg.get_compiler();
61         let ar = cc2ar(compiler.path(), target);
62         build.verbose(&format!("CC_{} = {:?}", target, compiler.path()));
63         if let Some(ref ar) = ar {
64             build.verbose(&format!("AR_{} = {:?}", target, ar));
65         }
66         build.cc.insert(target.to_string(), (compiler, ar));
67     }
68
69     // For all host triples we need to find a C++ compiler as well
70     //
71     // This includes hosts that aren't necessarily passed on the commandline
72     // (FIXME: Perhaps it shouldn't?)
73     for host in &build.config.host {
74         let mut cfg = gcc::Config::new();
75         cfg.cargo_metadata(false).opt_level(0).debug(false).cpp(true)
76            .target(host).host(&build.build);
77         let config = build.config.target_config.get(host);
78         if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
79             cfg.compiler(cxx);
80         } else {
81             set_compiler(&mut cfg, "g++", host, config, build);
82         }
83         let compiler = cfg.get_compiler();
84         build.verbose(&format!("CXX_{} = {:?}", host, compiler.path()));
85         build.cxx.insert(host.to_string(), compiler);
86     }
87 }
88
89 fn set_compiler(cfg: &mut gcc::Config,
90                 gnu_compiler: &str,
91                 target: &str,
92                 config: Option<&Target>,
93                 build: &Build) {
94     match target {
95         // When compiling for android we may have the NDK configured in the
96         // config.toml in which case we look there. Otherwise the default
97         // compiler already takes into account the triple in question.
98         t if t.contains("android") => {
99             if let Some(ndk) = config.and_then(|c| c.ndk.as_ref()) {
100                 let target = target.replace("armv7", "arm");
101                 let compiler = format!("{}-{}", target, gnu_compiler);
102                 cfg.compiler(ndk.join("bin").join(compiler));
103             }
104         }
105
106         // The default gcc version from OpenBSD may be too old, try using egcc,
107         // which is a gcc version from ports, if this is the case.
108         t if t.contains("openbsd") => {
109             let c = cfg.get_compiler();
110             if !c.path().ends_with(gnu_compiler) {
111                 return
112             }
113
114             let output = output(c.to_command().arg("--version"));
115             let i = match output.find(" 4.") {
116                 Some(i) => i,
117                 None => return,
118             };
119             match output[i + 3..].chars().next().unwrap() {
120                 '0' ... '6' => {}
121                 _ => return,
122             }
123             let alternative = format!("e{}", gnu_compiler);
124             if Command::new(&alternative).output().is_ok() {
125                 cfg.compiler(alternative);
126             }
127         }
128
129         "mips-unknown-linux-musl" => {
130             if cfg.get_compiler().path().to_str() == Some("gcc") {
131                 cfg.compiler("mips-linux-musl-gcc");
132             }
133         }
134         "mipsel-unknown-linux-musl" => {
135             if cfg.get_compiler().path().to_str() == Some("gcc") {
136                 cfg.compiler("mipsel-linux-musl-gcc");
137             }
138         }
139
140         t if t.contains("musl") => {
141             if let Some(root) = build.musl_root(target) {
142                 let guess = root.join("bin/musl-gcc");
143                 if guess.exists() {
144                     cfg.compiler(guess);
145                 }
146             }
147         }
148
149         _ => {}
150     }
151 }