Build the clang::CompilerInvocation in the driver. (#5784)

Add driver flags to specify clang driver arguments.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Richard Smith
2025-07-18 16:30:47 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent bcba76aca7
commit 553dd6e531
20 changed files with 322 additions and 136 deletions
+54 -2
View File
@@ -15,6 +15,7 @@
#include "common/vlog.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "toolchain/base/clang_invocation.h"
#include "toolchain/base/timings.h"
#include "toolchain/check/check.h"
#include "toolchain/codegen/codegen.h"
@@ -62,6 +63,30 @@ compile to machine code.
&phase);
});
b.AddStringOption(
{
.name = "clang-arg",
.value_name = "CLANG-ARG",
.help = R"""(
An argument to pass to the Clang compiler for use when compiling imported C++
code.
All flags that are accepted by the Clang driver are supported. However, you
cannot specify arguments that would result in additional compilations being
performed. Use `carbon clang` instead to compile additional source files.
)""",
},
[&](auto& arg_b) { arg_b.Append(&clang_args); });
b.AddStringPositionalArg(
{
.name = "CLANG-ARG",
.help = R"""(
Additional Clang arguments. See help for `--clang-arg` for details.
)""",
},
[&](auto& arg_b) { arg_b.Append(&clang_args); });
// TODO: Rearrange the code setting this option and two related ones to
// allow them to reference each other instead of hard-coding their names.
b.AddStringOption(
@@ -820,6 +845,34 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
return {.success = false};
}
std::shared_ptr<clang::CompilerInvocation> clang_invocation;
// Build a clang invocation. We do this regardless of whether we're running
// check, because this is essentially performing further option validation,
// and we generally validate all options even if we're not using them for the
// selected phases of compilation.
// TODO: Share any arguments we specify here with the `carbon clang`
// subcommand.
{
llvm::SmallVector<std::string> clang_path_and_args = {
driver_env.installation->clang_path(),
// Propagate the target to Clang.
llvm::formatv("--target={0}", options_.codegen_options.target).str(),
// Enable PIE by default, but allow it to be overridden by Clang
// arguments. Clang's default is configurable, but we'd like our
// defaults to be more stable.
// TODO: Decide if we want this.
"-fPIE",
};
for (auto str : options_.clang_args) {
clang_path_and_args.push_back(str.str());
}
clang_invocation = BuildClangInvocation(driver_env.consumer, driver_env.fs,
clang_path_and_args);
if (!clang_invocation) {
return {.success = false};
}
}
// Find the files comprising the prelude if we are importing it.
// TODO: Replace this with a search for library api files in a
// package-specific search path based on the library name.
@@ -944,8 +997,7 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
}
}
Check::CheckParseTrees(check_units, cache.tree_and_subtrees_getters(),
driver_env.fs, driver_env.installation->clang_path(),
options_.codegen_options.target, options);
driver_env.fs, options, clang_invocation);
CARBON_VLOG_TO(driver_env.vlog_stream,
"*** Check::CheckParseTrees done ***\n");
for (auto& unit : units) {