mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This adds support for most of the remaining LLVM command line tools using a generic, generated wrapper. The subcommand interface for these is (much) less interesting than our other subcommands, but it gives us a uniform and consistent layer. Note that I structured these as nested sub-sub-commands below an `llvm` subcommand because of an expectation that we will want to add more, and ones that don't use this generic layer. Some concrete future work: - Add the `opt` and `llc` tools as subcommands for easier debugging and experimentation with LLVM IR output from Carbon's toolchain. - Potentially sink `lld` below the `llvm` layer given that it has significantly less user visibility than commands like `clang`. Unfortunately, the current driver subcommand APIs make nested subcommands awkward. I've added a somewhat rough hack here to let the LLVM tools go in, but there are some TODOs that I want to address in a follow-up that works to adjust the structure of this code to be more conducive to nesting like this. Depends on #5048 -- only the last commit should be reviewed here. --------- Co-authored-by: Geoff Romer <gromer@google.com>
74 lines
2.6 KiB
C++
74 lines
2.6 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/driver/llvm_subcommand.h"
|
|
|
|
#include "common/command_line.h"
|
|
#include "llvm/TargetParser/Host.h"
|
|
#include "llvm/TargetParser/Triple.h"
|
|
#include "toolchain/base/llvm_tools.h"
|
|
#include "toolchain/driver/llvm_runner.h"
|
|
|
|
namespace Carbon {
|
|
|
|
static constexpr CommandLine::CommandInfo SubcommandInfo = {
|
|
.name = "llvm",
|
|
.help = R"""(
|
|
Runs LLVM's command line tools with the provided arguments.
|
|
|
|
This subcommand provides access to a collection of LLVM's command line tools via
|
|
further subcommands. For each of these tools, their command line can be provided
|
|
using positional arguments.
|
|
)""",
|
|
};
|
|
|
|
auto LLVMOptions::Build(CommandLine::CommandBuilder& b,
|
|
DriverSubcommand* subcommand,
|
|
DriverSubcommand** selected_subcommand) -> void {
|
|
// Add further subcommands for each LLVM tool.
|
|
for (LLVMTool tool : LLVMTool::Tools) {
|
|
// TODO: The subcommand info for each tool is weirdly stored in the
|
|
// `LLVMTool` class instead of here where it makes more logical sense.
|
|
// Either we should figure out how to move it to here or we should more
|
|
// fully document the oddity of having it in the `LLVMTool` class.
|
|
//
|
|
// TODO: Currently, the command line subsystem's help isn't as user friendly
|
|
// for the generated subcommands below this as it could be. Because each of
|
|
// these has a completely generic and stamped out info, the `help` output is
|
|
// very repetitive and doesn't actually contribute much information.
|
|
b.AddSubcommand(tool.subcommand_info(), [&](auto& sub_b) {
|
|
sub_b.AddStringPositionalArg(
|
|
{
|
|
.name = "ARG",
|
|
.help = R"""(
|
|
Arguments passed to the LLVM tool.
|
|
)""",
|
|
},
|
|
[&](auto& arg_b) { arg_b.Append(&args); });
|
|
sub_b.Do([=, this] {
|
|
subcommand_tool = tool;
|
|
*selected_subcommand = subcommand;
|
|
});
|
|
});
|
|
}
|
|
|
|
// The `llvm` subcommand can't be used directly.
|
|
b.RequiresSubcommand();
|
|
}
|
|
|
|
LLVMSubcommand::LLVMSubcommand() : DriverSubcommand(SubcommandInfo) {}
|
|
|
|
auto LLVMSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
|
|
LLVMRunner runner(driver_env.installation, driver_env.vlog_stream);
|
|
|
|
// Don't run arbitrary LLVM tools and libraries when fuzzing.
|
|
if (!DisableFuzzingExternalLibraries(driver_env, "llvm")) {
|
|
return {.success = false};
|
|
}
|
|
|
|
return {.success = runner.Run(options_.subcommand_tool, options_.args)};
|
|
}
|
|
|
|
} // namespace Carbon
|