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.host_target.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 fill_compilers(build: &mut Build) {
66    let targets: HashSet<_> = match build.config.cmd {
67        crate::Subcommand::Clean { .. }
69        | crate::Subcommand::Check { .. }
70        | crate::Subcommand::Format { .. }
71        | crate::Subcommand::Setup { .. } => {
72            build.hosts.iter().cloned().chain(iter::once(build.host_target)).collect()
73        }
74
75        _ => {
76            build
79                .targets
80                .iter()
81                .chain(&build.hosts)
82                .cloned()
83                .chain(iter::once(build.host_target))
84                .collect()
85        }
86    };
87
88    for target in targets.into_iter() {
89        fill_target_compiler(build, target);
90    }
91}
92
93pub fn fill_target_compiler(build: &mut Build, target: TargetSelection) {
99    let mut cfg = new_cc_build(build, target);
100    let config = build.config.target_config.get(&target);
101    if let Some(cc) = config
102        .and_then(|c| c.cc.clone())
103        .or_else(|| default_compiler(&mut cfg, Language::C, target, build))
104    {
105        cfg.compiler(cc);
106    }
107
108    let compiler = cfg.get_compiler();
109    let ar = if let ar @ Some(..) = config.and_then(|c| c.ar.clone()) {
110        ar
111    } else {
112        cfg.try_get_archiver().map(|c| PathBuf::from(c.get_program())).ok()
113    };
114
115    build.cc.insert(target, compiler.clone());
116    let mut cflags = build.cc_handled_clags(target, CLang::C);
117    cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
118
119    let mut cfg = new_cc_build(build, target);
122    cfg.cpp(true);
123    let cxx_configured = if let Some(cxx) = config
124        .and_then(|c| c.cxx.clone())
125        .or_else(|| default_compiler(&mut cfg, Language::CPlusPlus, target, build))
126    {
127        cfg.compiler(cxx);
128        true
129    } else {
130        cfg.try_get_compiler().is_ok()
132    };
133
134    if cxx_configured || target.contains("vxworks") {
136        let compiler = cfg.get_compiler();
137        build.cxx.insert(target, compiler);
138    }
139
140    build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
141    build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
142    if let Ok(cxx) = build.cxx(target) {
143        let mut cxxflags = build.cc_handled_clags(target, CLang::Cxx);
144        cxxflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::Cxx));
145        build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
146        build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
147    }
148    if let Some(ar) = ar {
149        build.verbose(|| println!("AR_{} = {ar:?}", target.triple));
150        build.ar.insert(target, ar);
151    }
152
153    if let Some(ranlib) = config.and_then(|c| c.ranlib.clone()) {
154        build.ranlib.insert(target, ranlib);
155    }
156}
157
158fn default_compiler(
161    cfg: &mut cc::Build,
162    compiler: Language,
163    target: TargetSelection,
164    build: &Build,
165) -> Option<PathBuf> {
166    match &*target.triple {
167        t if t.contains("android") => {
171            build.config.android_ndk.as_ref().map(|ndk| ndk_compiler(compiler, &target.triple, ndk))
172        }
173
174        t if t.contains("openbsd") => {
177            let c = cfg.get_compiler();
178            let gnu_compiler = compiler.gcc();
179            if !c.path().ends_with(gnu_compiler) {
180                return None;
181            }
182
183            let mut cmd = BootstrapCommand::from(c.to_command());
184            let output = cmd.arg("--version").run_capture_stdout(build).stdout();
185            let i = output.find(" 4.")?;
186            match output[i + 3..].chars().next().unwrap() {
187                '0'..='6' => {}
188                _ => return None,
189            }
190            let alternative = format!("e{gnu_compiler}");
191            if command(&alternative).run_capture(build).is_success() {
192                Some(PathBuf::from(alternative))
193            } else {
194                None
195            }
196        }
197
198        "mips-unknown-linux-musl" if compiler == Language::C => {
199            if cfg.get_compiler().path().to_str() == Some("gcc") {
200                Some(PathBuf::from("mips-linux-musl-gcc"))
201            } else {
202                None
203            }
204        }
205        "mipsel-unknown-linux-musl" if compiler == Language::C => {
206            if cfg.get_compiler().path().to_str() == Some("gcc") {
207                Some(PathBuf::from("mipsel-linux-musl-gcc"))
208            } else {
209                None
210            }
211        }
212
213        t if t.contains("musl") && compiler == Language::C => {
214            if let Some(root) = build.musl_root(target) {
215                let guess = root.join("bin/musl-gcc");
216                if guess.exists() { Some(guess) } else { None }
217            } else {
218                None
219            }
220        }
221
222        t if t.contains("-wasi") => {
223            let root = if let Some(path) = build.wasi_sdk_path.as_ref() {
224                path
225            } else {
226                if build.config.is_running_on_ci {
227                    panic!("ERROR: WASI_SDK_PATH must be configured for a -wasi target on CI");
228                }
229                println!("WARNING: WASI_SDK_PATH not set, using default cc/cxx compiler");
230                return None;
231            };
232            let compiler = match compiler {
233                Language::C => format!("{t}-clang"),
234                Language::CPlusPlus => format!("{t}-clang++"),
235            };
236            let compiler = root.join("bin").join(compiler);
237            Some(compiler)
238        }
239
240        _ => None,
241    }
242}
243
244pub(crate) fn ndk_compiler(compiler: Language, triple: &str, ndk: &Path) -> PathBuf {
251    let mut triple_iter = triple.split('-');
252    let triple_translated = if let Some(arch) = triple_iter.next() {
253        let arch_new = match arch {
254            "arm" | "armv7" | "armv7neon" | "thumbv7" | "thumbv7neon" => "armv7a",
255            other => other,
256        };
257        std::iter::once(arch_new).chain(triple_iter).collect::<Vec<&str>>().join("-")
258    } else {
259        triple.to_string()
260    };
261
262    let api_level = "21";
264    let compiler = format!("{}{}-{}", triple_translated, api_level, compiler.clang());
265    let host_tag = if cfg!(target_os = "macos") {
266        "darwin-x86_64"
268    } else if cfg!(target_os = "windows") {
269        "windows-x86_64"
270    } else {
271        "linux-x86_64"
275    };
276    ndk.join("toolchains").join("llvm").join("prebuilt").join(host_tag).join("bin").join(compiler)
277}
278
279#[derive(PartialEq)]
285pub(crate) enum Language {
286    C,
288    CPlusPlus,
290}
291
292impl Language {
293    fn gcc(self) -> &'static str {
295        match self {
296            Language::C => "gcc",
297            Language::CPlusPlus => "g++",
298        }
299    }
300
301    fn clang(self) -> &'static str {
303        match self {
304            Language::C => "clang",
305            Language::CPlusPlus => "clang++",
306        }
307    }
308}
309
310#[cfg(test)]
311mod tests;