mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:40:12 +01:00
The install directory contains the BUILD logic for creating an installable tree of data files and executables for the toolchain, and a library to facilitate toolchain code accessing the paths to their data within this installation. Then adds an installation of LLD in a synthetic LLVM installation, and teaches the Clang runner to configure this and use it for linking instead of the system linker. Currently, the install paths only really manage access to the LLVM binaries installed and used by the Clang runner for linking, but eventually other data files like the prelude and runtime libraries will be fleshed out as well. There are TODOs for moving more things over here such as the prelude. One interesting aspect of this is where to put helpers like parts of LLVM in our install. This PR suggests nesting those files under `lib/carbon`. While using a `lib` subdirectory isn't a perfect fit for the FHS (Filesystem Hierarchy Standard), having a single location where private data is collected is significantly superior to spreading them across the system. This also matches similar patterns used by Clang itself and several other language toolchains and standard libraries. The install directory also provides a natural place for us to build out packaging rules to create installable packages in various formats, but that remains future work. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
114 lines
3.9 KiB
C++
114 lines
3.9 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 "absl/flags/flag.h"
|
|
#include "explorer/main.h"
|
|
#include "re2/re2.h"
|
|
#include "testing/base/test_raw_ostream.h"
|
|
#include "testing/file_test/file_test_base.h"
|
|
|
|
ABSL_FLAG(bool, trace, false,
|
|
"Set to true to run tests with tracing enabled, even if they don't "
|
|
"otherwise specify it. This does not result in checking trace output "
|
|
"contents; it essentially only verifies there's not a crash bug.");
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
class ExplorerFileTest : public FileTestBase {
|
|
public:
|
|
explicit ExplorerFileTest(llvm::StringRef /*exe_path*/,
|
|
llvm::StringRef test_name)
|
|
: FileTestBase(test_name),
|
|
prelude_line_re_(R"(prelude.carbon:(\d+))"),
|
|
timing_re_(R"((Time elapsed in \w+: )\d+(ms))") {
|
|
CARBON_CHECK(prelude_line_re_.ok()) << prelude_line_re_.error();
|
|
CARBON_CHECK(timing_re_.ok()) << timing_re_.error();
|
|
}
|
|
|
|
auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
|
|
llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
|
|
llvm::raw_pwrite_stream& stderr) -> ErrorOr<RunResult> override {
|
|
// Add the prelude.
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
|
|
llvm::MemoryBuffer::getFile("explorer/data/prelude.carbon");
|
|
if (prelude.getError()) {
|
|
return ErrorBuilder() << prelude.getError().message();
|
|
}
|
|
// TODO: This path is long with a prefix / because of the path expectations
|
|
// in tests. Change those to allow a shorter path (e.g., `prelude.carbon`)
|
|
// here.
|
|
static constexpr llvm::StringLiteral PreludePath =
|
|
"/explorer/data/prelude.carbon";
|
|
if (!fs.addFile(PreludePath, /*ModificationTime=*/0, std::move(*prelude))) {
|
|
return ErrorBuilder() << "Duplicate prelude.carbon";
|
|
}
|
|
|
|
llvm::SmallVector<const char*> args = {"explorer"};
|
|
for (auto arg : test_args) {
|
|
args.push_back(arg.data());
|
|
}
|
|
|
|
int exit_code = ExplorerMain(
|
|
args.size(), args.data(), /*install_path=*/"", PreludePath, stdout,
|
|
stderr, check_trace_output() ? stdout : trace_stream_, fs);
|
|
|
|
return {{.success = exit_code == EXIT_SUCCESS}};
|
|
}
|
|
|
|
auto ValidateRun() -> void override {
|
|
// Skip trace test check as they use stdout stream instead of
|
|
// trace_stream_ostream
|
|
if (absl::GetFlag(FLAGS_trace)) {
|
|
EXPECT_FALSE(trace_stream_.TakeStr().empty())
|
|
<< "Tracing should always do something";
|
|
}
|
|
}
|
|
|
|
auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
|
|
llvm::SmallVector<std::string> args;
|
|
if (absl::GetFlag(FLAGS_trace)) {
|
|
args.push_back("--trace_file=-");
|
|
args.push_back("--trace_phase=all");
|
|
}
|
|
args.push_back("%s");
|
|
return args;
|
|
}
|
|
|
|
auto GetLineNumberReplacements(llvm::ArrayRef<llvm::StringRef> filenames)
|
|
-> llvm::SmallVector<LineNumberReplacement> override {
|
|
if (check_trace_output()) {
|
|
return {};
|
|
}
|
|
return FileTestBase::GetLineNumberReplacements(filenames);
|
|
}
|
|
|
|
auto DoExtraCheckReplacements(std::string& check_line) -> void override {
|
|
// Ignore the resulting column of EndOfFile because it's often the end of
|
|
// the CHECK comment.
|
|
RE2::GlobalReplace(&check_line, prelude_line_re_,
|
|
R"(prelude.carbon:{{\\d+}})");
|
|
if (check_trace_output()) {
|
|
// Replace timings in trace output.
|
|
RE2::GlobalReplace(&check_line, timing_re_, R"(\1{{\\d+}}\2)");
|
|
}
|
|
}
|
|
|
|
private:
|
|
// Trace output is directly checked for a few tests.
|
|
auto check_trace_output() -> bool {
|
|
return test_name().find("/trace/") != std::string::npos;
|
|
}
|
|
|
|
TestRawOstream trace_stream_;
|
|
RE2 prelude_line_re_;
|
|
RE2 timing_re_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
CARBON_FILE_TEST_FACTORY(ExplorerFileTest);
|
|
|
|
} // namespace Carbon::Testing
|