1 //! C-compiler probing and detection.
3 //! This module will fill out the `cc` and `cxx` maps of `Build` by looking for
4 //! C and C++ compilers for each target configured. A compiler is found through
5 //! a number of vectors (in order of precedence)
7 //! 1. Configuration via `target.$target.cc` in `config.toml`.
8 //! 2. Configuration via `target.$target.android-ndk` in `config.toml`, if
10 //! 3. Special logic to probe on OpenBSD
11 //! 4. The `CC_$target` environment variable.
12 //! 5. The `CC` environment variable.
15 //! Some of this logic is implemented here, but much of it is farmed out to the
16 //! `cc` crate itself, so we end up having the same fallbacks as there.
17 //! Similar logic is then used to find a C++ compiler, just some s/cc/c++/ is
20 //! It is intended that after this module has run no C/C++ compiler will
21 //! ever be probed for. Instead the compilers found here will be used for
24 use std::collections::HashSet;
25 use std::path::{Path, PathBuf};
26 use std::process::Command;
29 use crate::config::{Target, TargetSelection};
30 use crate::util::output;
31 use crate::{Build, CLang, GitRepo};
33 // The `cc` crate doesn't provide a way to obtain a path to the detected archiver,
34 // so use some simplified logic here. First we respect the environment variable `AR`, then
35 // try to infer the archiver path from the C compiler path.
36 // In the future this logic should be replaced by calling into the `cc` crate.
37 fn cc2ar(cc: &Path, target: TargetSelection) -> Option<PathBuf> {
38 if let Some(ar) = env::var_os(format!("AR_{}", target.triple.replace("-", "_"))) {
39 Some(PathBuf::from(ar))
40 } else if let Some(ar) = env::var_os("AR") {
41 Some(PathBuf::from(ar))
42 } else if target.contains("msvc") {
44 } else if target.contains("musl") {
45 Some(PathBuf::from("ar"))
46 } else if target.contains("openbsd") {
47 Some(PathBuf::from("ar"))
48 } else if target.contains("vxworks") {
49 Some(PathBuf::from("wr-ar"))
51 let parent = cc.parent().unwrap();
52 let file = cc.file_name().unwrap().to_str().unwrap();
53 for suffix in &["gcc", "cc", "clang"] {
54 if let Some(idx) = file.rfind(suffix) {
55 let mut file = file[..idx].to_owned();
57 return Some(parent.join(&file));
60 Some(parent.join(file))
64 pub fn find(build: &mut Build) {
65 // For all targets we're going to need a C compiler for building some shims
66 // and such as well as for being a linker for Rust code.
72 .chain(iter::once(build.build))
73 .collect::<HashSet<_>>();
74 for target in targets.into_iter() {
75 let mut cfg = cc::Build::new();
76 cfg.cargo_metadata(false)
80 .target(&target.triple)
81 .host(&build.build.triple);
82 match build.crt_static(target) {
87 if target.contains("msvc") {
90 if target.contains("musl") {
91 cfg.static_flag(true);
96 let config = build.config.target_config.get(&target);
97 if let Some(cc) = config.and_then(|c| c.cc.as_ref()) {
100 set_compiler(&mut cfg, Language::C, target, config, build);
103 let compiler = cfg.get_compiler();
104 let ar = if let ar @ Some(..) = config.and_then(|c| c.ar.clone()) {
107 cc2ar(compiler.path(), target)
110 build.cc.insert(target, compiler.clone());
111 let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
113 // If we use llvm-libunwind, we will need a C++ compiler as well for all targets
114 // We'll need one anyways if the target triple is also a host triple
115 let mut cfg = cc::Build::new();
116 cfg.cargo_metadata(false)
121 .target(&target.triple)
122 .host(&build.build.triple);
124 let cxx_configured = if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
127 } else if build.hosts.contains(&target) || build.build == target {
128 set_compiler(&mut cfg, Language::CPlusPlus, target, config, build);
131 // Use an auto-detected compiler (or one configured via `CXX_target_triple` env vars).
132 cfg.try_get_compiler().is_ok()
135 // for VxWorks, record CXX compiler which will be used in lib.rs:linker()
136 if cxx_configured || target.contains("vxworks") {
137 let compiler = cfg.get_compiler();
138 build.cxx.insert(target, compiler);
141 build.verbose(&format!("CC_{} = {:?}", &target.triple, build.cc(target)));
142 build.verbose(&format!("CFLAGS_{} = {:?}", &target.triple, cflags));
143 if let Ok(cxx) = build.cxx(target) {
144 let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
145 build.verbose(&format!("CXX_{} = {:?}", &target.triple, cxx));
146 build.verbose(&format!("CXXFLAGS_{} = {:?}", &target.triple, cxxflags));
148 if let Some(ar) = ar {
149 build.verbose(&format!("AR_{} = {:?}", &target.triple, ar));
150 build.ar.insert(target, ar);
158 target: TargetSelection,
159 config: Option<&Target>,
162 match &*target.triple {
163 // When compiling for android we may have the NDK configured in the
164 // config.toml in which case we look there. Otherwise the default
165 // compiler already takes into account the triple in question.
166 t if t.contains("android") => {
167 if let Some(ndk) = config.and_then(|c| c.ndk.as_ref()) {
170 .replace("armv7neon", "arm")
171 .replace("armv7", "arm")
172 .replace("thumbv7neon", "arm")
173 .replace("thumbv7", "arm");
174 let compiler = format!("{}-{}", target, compiler.clang());
175 cfg.compiler(ndk.join("bin").join(compiler));
179 // The default gcc version from OpenBSD may be too old, try using egcc,
180 // which is a gcc version from ports, if this is the case.
181 t if t.contains("openbsd") => {
182 let c = cfg.get_compiler();
183 let gnu_compiler = compiler.gcc();
184 if !c.path().ends_with(gnu_compiler) {
188 let output = output(c.to_command().arg("--version"));
189 let i = match output.find(" 4.") {
193 match output[i + 3..].chars().next().unwrap() {
197 let alternative = format!("e{}", gnu_compiler);
198 if Command::new(&alternative).output().is_ok() {
199 cfg.compiler(alternative);
203 "mips-unknown-linux-musl" => {
204 if cfg.get_compiler().path().to_str() == Some("gcc") {
205 cfg.compiler("mips-linux-musl-gcc");
208 "mipsel-unknown-linux-musl" => {
209 if cfg.get_compiler().path().to_str() == Some("gcc") {
210 cfg.compiler("mipsel-linux-musl-gcc");
214 t if t.contains("musl") => {
215 if let Some(root) = build.musl_root(target) {
216 let guess = root.join("bin/musl-gcc");
227 /// The target programming language for a native compiler.
229 /// The compiler is targeting C.
231 /// The compiler is targeting C++.
236 /// Obtains the name of a compiler in the GCC collection.
237 fn gcc(self) -> &'static str {
239 Language::C => "gcc",
240 Language::CPlusPlus => "g++",
244 /// Obtains the name of a compiler in the clang suite.
245 fn clang(self) -> &'static str {
247 Language::C => "clang",
248 Language::CPlusPlus => "clang++",