bootstrap/utils/
cc_detect.rs1use std::collections::HashSet;
25use std::iter;
26use std::path::{Path, PathBuf};
27
28use crate::core::config::TargetSelection;
29use crate::utils::exec::{BootstrapCommand, command};
30use crate::{Build, CLang, GitRepo};
31
32fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
34 let mut cfg = cc::Build::new();
35 cfg.cargo_metadata(false)
36 .opt_level(2)
37 .warnings(false)
38 .debug(false)
39 .flag_if_supported("-gz")
41 .target(&target.triple)
42 .host(&build.build.triple);
43 match build.crt_static(target) {
44 Some(a) => {
45 cfg.static_crt(a);
46 }
47 None => {
48 if target.is_msvc() {
49 cfg.static_crt(true);
50 }
51 if target.contains("musl") {
52 cfg.static_flag(true);
53 }
54 }
55 }
56 cfg
57}
58
59pub fn find(build: &Build) {
66 let targets: HashSet<_> = match build.config.cmd {
67 crate::Subcommand::Clean { .. }
69 | crate::Subcommand::Check { .. }
70 | crate::Subcommand::Suggest { .. }
71 | crate::Subcommand::Format { .. }
72 | crate::Subcommand::Setup { .. } => {
73 build.hosts.iter().cloned().chain(iter::once(build.build)).collect()
74 }
75
76 _ => {
77 build
80 .targets
81 .iter()
82 .chain(&build.hosts)
83 .cloned()
84 .chain(iter::once(build.build))
85 .collect()
86 }
87 };
88
89 for target in targets.into_iter() {
90 find_target(build, target);
91 }
92}
93
94pub fn find_target(build: &Build, target: TargetSelection) {
100 let mut cfg = new_cc_build(build, target);
101 let config = build.config.target_config.get(&target);
102 if let Some(cc) = config
103 .and_then(|c| c.cc.clone())
104 .or_else(|| default_compiler(&mut cfg, Language::C, target, build))
105 {
106 cfg.compiler(cc);
107 }
108
109 let compiler = cfg.get_compiler();
110 let ar = if let ar @ Some(..) = config.and_then(|c| c.ar.clone()) {
111 ar
112 } else {
113 cfg.try_get_archiver().map(|c| PathBuf::from(c.get_program())).ok()
114 };
115
116 build.cc.borrow_mut().insert(target, compiler.clone());
117 let mut cflags = build.cc_handled_clags(target, CLang::C);
118 cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
119
120 let mut cfg = new_cc_build(build, target);
123 cfg.cpp(true);
124 let cxx_configured = if let Some(cxx) = config
125 .and_then(|c| c.cxx.clone())
126 .or_else(|| default_compiler(&mut cfg, Language::CPlusPlus, target, build))
127 {
128 cfg.compiler(cxx);
129 true
130 } else {
131 cfg.try_get_compiler().is_ok()
133 };
134
135 if cxx_configured || target.contains("vxworks") {
137 let compiler = cfg.get_compiler();
138 build.cxx.borrow_mut().insert(target, compiler);
139 }
140
141 build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
142 build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
143 if let Ok(cxx) = build.cxx(target) {
144 let mut cxxflags = build.cc_handled_clags(target, CLang::Cxx);
145 cxxflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
146 build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
147 build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
148 }
149 if let Some(ar) = ar {
150 build.verbose(|| println!("AR_{} = {ar:?}", target.triple));
151 build.ar.borrow_mut().insert(target, ar);
152 }
153
154 if let Some(ranlib) = config.and_then(|c| c.ranlib.clone()) {
155 build.ranlib.borrow_mut().insert(target, ranlib);
156 }
157}
158
159fn default_compiler(
162 cfg: &mut cc::Build,
163 compiler: Language,
164 target: TargetSelection,
165 build: &Build,
166) -> Option<PathBuf> {
167 match &*target.triple {
168 t if t.contains("android") => {
172 build.config.android_ndk.as_ref().map(|ndk| ndk_compiler(compiler, &target.triple, ndk))
173 }
174
175 t if t.contains("openbsd") => {
178 let c = cfg.get_compiler();
179 let gnu_compiler = compiler.gcc();
180 if !c.path().ends_with(gnu_compiler) {
181 return None;
182 }
183
184 let mut cmd = BootstrapCommand::from(c.to_command());
185 let output = cmd.arg("--version").run_capture_stdout(build).stdout();
186 let i = output.find(" 4.")?;
187 match output[i + 3..].chars().next().unwrap() {
188 '0'..='6' => {}
189 _ => return None,
190 }
191 let alternative = format!("e{gnu_compiler}");
192 if command(&alternative).run_capture(build).is_success() {
193 Some(PathBuf::from(alternative))
194 } else {
195 None
196 }
197 }
198
199 "mips-unknown-linux-musl" if compiler == Language::C => {
200 if cfg.get_compiler().path().to_str() == Some("gcc") {
201 Some(PathBuf::from("mips-linux-musl-gcc"))
202 } else {
203 None
204 }
205 }
206 "mipsel-unknown-linux-musl" if compiler == Language::C => {
207 if cfg.get_compiler().path().to_str() == Some("gcc") {
208 Some(PathBuf::from("mipsel-linux-musl-gcc"))
209 } else {
210 None
211 }
212 }
213
214 t if t.contains("musl") && compiler == Language::C => {
215 if let Some(root) = build.musl_root(target) {
216 let guess = root.join("bin/musl-gcc");
217 if guess.exists() { Some(guess) } else { None }
218 } else {
219 None
220 }
221 }
222
223 t if t.contains("-wasi") => {
224 let root = PathBuf::from(std::env::var_os("WASI_SDK_PATH")?);
225 let compiler = match compiler {
226 Language::C => format!("{t}-clang"),
227 Language::CPlusPlus => format!("{t}-clang++"),
228 };
229 let compiler = root.join("bin").join(compiler);
230 Some(compiler)
231 }
232
233 _ => None,
234 }
235}
236
237pub(crate) fn ndk_compiler(compiler: Language, triple: &str, ndk: &Path) -> PathBuf {
244 let mut triple_iter = triple.split('-');
245 let triple_translated = if let Some(arch) = triple_iter.next() {
246 let arch_new = match arch {
247 "arm" | "armv7" | "armv7neon" | "thumbv7" | "thumbv7neon" => "armv7a",
248 other => other,
249 };
250 std::iter::once(arch_new).chain(triple_iter).collect::<Vec<&str>>().join("-")
251 } else {
252 triple.to_string()
253 };
254
255 let api_level = "21";
257 let compiler = format!("{}{}-{}", triple_translated, api_level, compiler.clang());
258 let host_tag = if cfg!(target_os = "macos") {
259 "darwin-x86_64"
261 } else if cfg!(target_os = "windows") {
262 "windows-x86_64"
263 } else {
264 "linux-x86_64"
268 };
269 ndk.join("toolchains").join("llvm").join("prebuilt").join(host_tag).join("bin").join(compiler)
270}
271
272#[derive(PartialEq)]
278pub(crate) enum Language {
279 C,
281 CPlusPlus,
283}
284
285impl Language {
286 fn gcc(self) -> &'static str {
288 match self {
289 Language::C => "gcc",
290 Language::CPlusPlus => "g++",
291 }
292 }
293
294 fn clang(self) -> &'static str {
296 match self {
297 Language::C => "clang",
298 Language::CPlusPlus => "clang++",
299 }
300 }
301}
302
303#[cfg(test)]
304mod tests;