Skip to main content

find_compiler

Function find_compiler 

Source
pub fn find_compiler(
    files: &impl FileProvider,
    commands: Option<&CompileCommands>,
    for_file: &Path,
    environment: &Environment,
) -> Option<PathBuf>
Expand description

The compiler to ask, or None when nothing usable was found.

§The order, and why it is this one

1. the compiler compile_commands.json names for this file  — it is what actually builds the file
2. `CXX`, then `CC`                                        — the project's stated choice
3. `clang++`, `g++`, … where an installer put them         — a compiler this machine has
4. `g++`, `clang++`, `c++`, … on `PATH`                    — a compiler the shell can see

Each step is a weaker claim than the one before it. The build database is the only one that knows this file is built by that compiler — a project with two toolchains in it is what step 1 exists for — while PATH is a guess about the shell and is last because of it.

Step 3 before step 4, and that is the whole of what this function changed. A machine can have both a clang 18 under C:\Program Files\LLVM and a MinGW g++ on PATH, and the measured answer used to be the g++ — a different standard library, so every std:: answer described declarations the project’s build never sees. An installation is evidence of what this machine is for; PATH is evidence of what one shell was told. So the installed compiler is asked first, and PATH is what answers when nothing is installed where installers put things.

A name with a separator in it is a path and is looked for where it says (CXX=/opt/gcc/bin/g++); a bare name is searched on PATH, which is what a shell would do with it.