Files
carbon-lang/toolchain/install/busybox_main.cpp
T
Jon Ross-Perkins 957599b2ab Implement a basic busybox for carbon/clang. (#4406)
For reference, we're going down the busyboxing route because Carbon
depends on Clang, and we want both to be available as binaries.
Busyboxing allows this while avoiding duplicating symbols between
multiple binaries.

I'm removing the `cc_binary` for `driver:carbon` because I want to avoid
a significant increase in binary outputs; `bazel run //toolchain` still
works great.

This still doesn't have great test coverage (but non-zero:
`//examples:sieve` still builds/runs, for example). The problem is that
we want to avoid subprocessing for performance, but this mainly deals
with subprocessing. I'm still thinking about good approaches for that,
since we'll probably want more significant testing for `clang`
interaction... the solution might involve busyboxing `file_test` too.

Note development on this ran into the argv issue being fixed in #4405
2024-10-30 17:07:55 +00:00

94 lines
2.8 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 <unistd.h>
#include <cstdlib>
#include <filesystem>
#include "common/bazel_working_dir.h"
#include "common/error.h"
#include "common/exe_path.h"
#include "common/init_llvm.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/LLVMDriver.h"
#include "toolchain/driver/driver.h"
#include "toolchain/install/install_paths.h"
namespace Carbon {
namespace {
struct BusyboxInfo {
// The path to `carbon-busybox`.
std::filesystem::path bin_path;
// The mode, such as `carbon` or `clang`.
std::optional<std::string> mode;
};
} // namespace
// Returns the busybox information, given argv[0]. This primarily handles
// resolving symlinks that point at the busybox.
static auto GetBusyboxInfo(llvm::StringRef argv0) -> ErrorOr<BusyboxInfo> {
BusyboxInfo info = BusyboxInfo{argv0.str(), std::nullopt};
while (true) {
std::string filename = info.bin_path.filename();
if (filename == "carbon-busybox") {
return info;
}
std::error_code ec;
auto symlink_target = std::filesystem::read_symlink(info.bin_path, ec);
if (ec) {
return ErrorBuilder()
<< "expected carbon-busybox symlink at `" << info.bin_path << "`";
}
info.mode = filename;
info.bin_path = symlink_target;
}
}
// The actual `main` implementation. Can return an exit code or an `Error`
// (which causes EXIT_FAILRUE).
static auto Main(int argc, char** argv) -> ErrorOr<int> {
Carbon::InitLLVM init_llvm(argc, argv);
// Start by resolving any symlinks.
CARBON_ASSIGN_OR_RETURN(auto busybox_info, Carbon::GetBusyboxInfo(argv[0]));
auto fs = llvm::vfs::getRealFileSystem();
// Resolve paths before calling SetWorkingDirForBazel.
std::string exe_path =
Carbon::FindExecutablePath(busybox_info.bin_path.string());
const auto install_paths = Carbon::InstallPaths::MakeExeRelative(exe_path);
if (install_paths.error()) {
return Error(*install_paths.error());
}
Carbon::SetWorkingDirForBazel();
llvm::SmallVector<llvm::StringRef> args;
args.reserve(argc + 1);
if (busybox_info.mode && busybox_info.mode != "carbon") {
args.append({*busybox_info.mode, "--"});
}
args.append(argv + 1, argv + argc);
Carbon::Driver driver(*fs, &install_paths, llvm::outs(), llvm::errs());
bool success = driver.RunCommand(args).success;
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
} // namespace Carbon
auto main(int argc, char** argv) -> int {
auto result = Carbon::Main(argc, argv);
if (result.ok()) {
return *result;
} else {
llvm::errs() << "error: " << result.error() << "\n";
return EXIT_FAILURE;
}
}