Files
carbon-lang/toolchain/driver/llvm_runner.cpp
T
Chandler CarruthandGeoff Romer 1459332031 Add subcommands and busybox entry points for LLVM tools (#5049)
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>
2025-03-04 18:00:06 +00:00

39 lines
1.2 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_runner.h"
#include <algorithm>
#include <memory>
#include <numeric>
#include <optional>
#include "common/vlog.h"
#include "lld/Common/Driver.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
namespace Carbon {
auto LLVMRunner::Run(LLVMTool tool, llvm::ArrayRef<llvm::StringRef> args)
-> bool {
std::string path = installation_->llvm_tool_path(tool);
// Allocate one chunk of storage for the actual C-strings and a vector of
// pointers into the storage.
llvm::OwningArrayRef<char> cstr_arg_storage;
llvm::SmallVector<const char*, 64> cstr_args = BuildCStrArgs(
tool.name(), path, /*verbose_flag=*/std::nullopt, args, cstr_arg_storage);
CARBON_VLOG("Running LLVM's {0} tool...\n", tool.name());
int exit_code = tool.main_fn()(
cstr_args.size(), const_cast<char**>(cstr_args.data()),
{.Path = path.c_str(), .PrependArg = nullptr, .NeedsPrependArg = false});
// TODO: Should this be forwarding the full exit code?
return exit_code == 0;
}
} // namespace Carbon