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.
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.
11 //! C-compiler probing and detection.
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)
17 //! 1. Configuration via `target.$target.cc` in `config.toml`.
18 //! 2. Configuration via `target.$target.android-ndk` in `config.toml`, if
20 //! 3. Special logic to probe on OpenBSD
21 //! 4. The `CC_$target` environment variable.
22 //! 5. The `CC` environment variable.
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
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
34 use std::process::Command;
37 use build_helper::{cc2ar, output};
44 pub fn find(build: &mut Build) {
45 // For all targets we're going to need a C compiler for building some shims
46 // and such as well as for being a linker for Rust code.
47 for target in build.targets.iter().chain(&build.hosts).cloned().chain(iter::once(build.build)) {
48 let mut cfg = gcc::Build::new();
49 cfg.cargo_metadata(false).opt_level(0).warnings(false).debug(false)
50 .target(&target).host(&build.build);
52 let config = build.config.target_config.get(&target);
53 if let Some(cc) = config.and_then(|c| c.cc.as_ref()) {
56 set_compiler(&mut cfg, "gcc", target, config, build);
59 let compiler = cfg.get_compiler();
60 let ar = cc2ar(compiler.path(), &target);
61 build.verbose(&format!("CC_{} = {:?}", &target, compiler.path()));
62 if let Some(ref ar) = ar {
63 build.verbose(&format!("AR_{} = {:?}", &target, ar));
65 build.cc.insert(target, (compiler, ar));
68 // For all host triples we need to find a C++ compiler as well
69 for host in build.hosts.iter().cloned().chain(iter::once(build.build)) {
70 let mut cfg = gcc::Build::new();
71 cfg.cargo_metadata(false).opt_level(0).warnings(false).debug(false).cpp(true)
72 .target(&host).host(&build.build);
73 let config = build.config.target_config.get(&host);
74 if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
77 set_compiler(&mut cfg, "g++", host, config, build);
79 let compiler = cfg.get_compiler();
80 build.verbose(&format!("CXX_{} = {:?}", host, compiler.path()));
81 build.cxx.insert(host, compiler);
85 fn set_compiler(cfg: &mut gcc::Build,
87 target: Interned<String>,
88 config: Option<&Target>,
91 // When compiling for android we may have the NDK configured in the
92 // config.toml in which case we look there. Otherwise the default
93 // compiler already takes into account the triple in question.
94 t if t.contains("android") => {
95 if let Some(ndk) = config.and_then(|c| c.ndk.as_ref()) {
96 let target = target.replace("armv7", "arm");
97 let compiler = format!("{}-{}", target, gnu_compiler);
98 cfg.compiler(ndk.join("bin").join(compiler));
102 // The default gcc version from OpenBSD may be too old, try using egcc,
103 // which is a gcc version from ports, if this is the case.
104 t if t.contains("openbsd") => {
105 let c = cfg.get_compiler();
106 if !c.path().ends_with(gnu_compiler) {
110 let output = output(c.to_command().arg("--version"));
111 let i = match output.find(" 4.") {
115 match output[i + 3..].chars().next().unwrap() {
119 let alternative = format!("e{}", gnu_compiler);
120 if Command::new(&alternative).output().is_ok() {
121 cfg.compiler(alternative);
125 "mips-unknown-linux-musl" => {
126 if cfg.get_compiler().path().to_str() == Some("gcc") {
127 cfg.compiler("mips-linux-musl-gcc");
130 "mipsel-unknown-linux-musl" => {
131 if cfg.get_compiler().path().to_str() == Some("gcc") {
132 cfg.compiler("mipsel-linux-musl-gcc");
136 t if t.contains("musl") => {
137 if let Some(root) = build.musl_root(target) {
138 let guess = root.join("bin/musl-gcc");