Skip to content

Commit e5173f5

Browse files
committed
add docs to cc-detect
1 parent a567209 commit e5173f5

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/bootstrap/src/utils/cc_detect.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::utils::exec::{BootstrapCommand, command};
3030
use crate::{Build, CLang, GitRepo};
3131

3232
/// FIXME(onur-ozkan): This logic should be replaced by calling into the `cc` crate.
33+
/// Determines the archiver tool to use for the given target based on the compiler.
3334
fn cc2ar(cc: &Path, target: TargetSelection, default_ar: PathBuf) -> Option<PathBuf> {
3435
if let Some(ar) = env::var_os(format!("AR_{}", target.triple.replace('-', "_"))) {
3536
Some(PathBuf::from(ar))
@@ -58,6 +59,11 @@ fn cc2ar(cc: &Path, target: TargetSelection, default_ar: PathBuf) -> Option<Path
5859
}
5960
}
6061

62+
/// Creates and configures a new [`cc::Build`] instance for the given target.
63+
///
64+
/// This function initializes a new `cc::Build` object with a set of default options
65+
/// including optimization level, warning settings, and target/host configuration. It also
66+
/// applies static runtime configuration based on the target and build settings.
6167
fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
6268
let mut cfg = cc::Build::new();
6369
cfg.cargo_metadata(false)
@@ -84,6 +90,12 @@ fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
8490
cfg
8591
}
8692

93+
/// Probes for C and C++ compilers and configures the corresponding entries in the [`Build`]
94+
/// structure.
95+
///
96+
/// This function determines which targets need a C compiler (and, if needed, a C++ compiler)
97+
/// by combining the primary build target, host targets, and any additional targets. For
98+
/// each target, it calls [`find_target`] to configure the necessary compiler tools.
8799
pub fn find(build: &Build) {
88100
let targets: HashSet<_> = match build.config.cmd {
89101
// We don't need to check cross targets for these commands.
@@ -112,6 +124,11 @@ pub fn find(build: &Build) {
112124
}
113125
}
114126

127+
/// Probes and configures the C and C++ compilers for a single target.
128+
///
129+
/// This function uses both user-specified configuration (from `config.toml`) and auto-detection
130+
/// logic to determine the correct C/C++ compilers for the target. It also determines the appropriate
131+
/// archiver (`ar`) and sets up additional compilation flags (both handled and unhandled).
115132
pub fn find_target(build: &Build, target: TargetSelection) {
116133
let mut cfg = new_cc_build(build, target);
117134
let config = build.config.target_config.get(&target);
@@ -172,6 +189,22 @@ pub fn find_target(build: &Build, target: TargetSelection) {
172189
}
173190
}
174191

192+
/// Determines the default compiler for a given target and language when not explicitly
193+
/// configured in `config.toml`.
194+
///
195+
/// This function handles special cases for certain targets:
196+
///
197+
/// - Android Targets:
198+
/// If an Android NDF is configured, it returns the NDK compiler path.
199+
/// - OpenBDS Targets:
200+
/// Checks if the default GCC os too old and attempts to use alternative (such as `egcc`).
201+
/// - MIPS and musl Targets:
202+
/// For certain musl-based targets (including MIPS), it returns a specialized compiler path.
203+
/// - WASI Targets:
204+
/// Constructs the compiler path based on the WASI SDK.
205+
/// - Fallback:
206+
/// For all other targets, returns `None`, meaning the auto-detected compiler from [`cc`]
207+
/// will be used.
175208
fn default_compiler(
176209
cfg: &mut cc::Build,
177210
compiler: Language,
@@ -248,6 +281,12 @@ fn default_compiler(
248281
}
249282
}
250283

284+
/// Constructs the path to the Android NDK compiler for the given target triple and language.
285+
///
286+
/// This helper function transform the target triple by converting certain architecture names
287+
/// (for example, translating "arm" to "arm7a"), appends the minimum API level (hardcoded as "21"
288+
/// for NDK r26d), and then constructs the full path based on the provided NDK directory and host
289+
/// platform.
251290
pub(crate) fn ndk_compiler(compiler: Language, triple: &str, ndk: &Path) -> PathBuf {
252291
let mut triple_iter = triple.split('-');
253292
let triple_translated = if let Some(arch) = triple_iter.next() {
@@ -277,7 +316,11 @@ pub(crate) fn ndk_compiler(compiler: Language, triple: &str, ndk: &Path) -> Path
277316
ndk.join("toolchains").join("llvm").join("prebuilt").join(host_tag).join("bin").join(compiler)
278317
}
279318

280-
/// The target programming language for a native compiler.
319+
/// An enum representing the target programming language for a native compiler.
320+
///
321+
/// This enum is used to indicate whether a particular compiler is intended for C or C++.
322+
/// It also provides helper methods for obtaining the standard executable names for GCC and
323+
/// clang-based compilers.
281324
#[derive(PartialEq)]
282325
pub(crate) enum Language {
283326
/// The compiler is targeting C.
@@ -287,15 +330,15 @@ pub(crate) enum Language {
287330
}
288331

289332
impl Language {
290-
/// Obtains the name of a compiler in the GCC collection.
333+
/// Returns the executable name for a GCC compiler corresponding to this language.
291334
fn gcc(self) -> &'static str {
292335
match self {
293336
Language::C => "gcc",
294337
Language::CPlusPlus => "g++",
295338
}
296339
}
297340

298-
/// Obtains the name of a compiler in the clang suite.
341+
/// Returns the executable name for a clang-based compiler corresponding to this language.
299342
fn clang(self) -> &'static str {
300343
match self {
301344
Language::C => "clang",

0 commit comments

Comments
 (0)