mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +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>
218 lines
7.9 KiB
C++
218 lines
7.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 "toolchain/driver/driver.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <utility>
|
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
|
#include "llvm/Object/Binary.h"
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
#include "testing/base/gtest_main.h"
|
|
#include "testing/base/test_raw_ostream.h"
|
|
#include "toolchain/testing/yaml_test_helpers.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
using ::Carbon::Testing::TestRawOstream;
|
|
using ::testing::_;
|
|
using ::testing::ContainsRegex;
|
|
using ::testing::HasSubstr;
|
|
using ::testing::StrEq;
|
|
|
|
namespace Yaml = ::Carbon::Testing::Yaml;
|
|
|
|
// Reads a file to string.
|
|
// TODO: Extract this to a helper and share it with other tests.
|
|
static auto ReadFile(std::filesystem::path path) -> std::string {
|
|
std::ifstream proto_file(path);
|
|
std::stringstream buffer;
|
|
buffer << proto_file.rdbuf();
|
|
proto_file.close();
|
|
return buffer.str();
|
|
}
|
|
|
|
class DriverTest : public testing::Test {
|
|
protected:
|
|
DriverTest()
|
|
: installation_(
|
|
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath())),
|
|
driver_(fs_, &installation_, "", test_output_stream_,
|
|
test_error_stream_) {
|
|
char* tmpdir_env = getenv("TEST_TMPDIR");
|
|
CARBON_CHECK(tmpdir_env != nullptr);
|
|
test_tmpdir_ = tmpdir_env;
|
|
}
|
|
|
|
auto MakeTestFile(llvm::StringRef text,
|
|
llvm::StringRef filename = "test_file.carbon")
|
|
-> llvm::StringRef {
|
|
fs_.addFile(filename, /*ModificationTime=*/0,
|
|
llvm::MemoryBuffer::getMemBuffer(text));
|
|
return filename;
|
|
}
|
|
|
|
// Makes a temp directory and changes the working directory to it. Returns an
|
|
// LLVM `scope_exit` that will restore the working directory and remove the
|
|
// temporary directory (and everything it contains) when destroyed.
|
|
auto ScopedTempWorkingDir() {
|
|
// Save our current working directory.
|
|
std::error_code ec;
|
|
auto original_dir = std::filesystem::current_path(ec);
|
|
CARBON_CHECK(!ec) << ec.message();
|
|
|
|
const auto* unit_test = ::testing::UnitTest::GetInstance();
|
|
const auto* test_info = unit_test->current_test_info();
|
|
std::filesystem::path test_dir = test_tmpdir_.append(
|
|
llvm::formatv("{0}_{1}", test_info->test_suite_name(),
|
|
test_info->name())
|
|
.str());
|
|
std::filesystem::create_directory(test_dir, ec);
|
|
CARBON_CHECK(!ec) << "Could not create test working dir '" << test_dir
|
|
<< "': " << ec.message();
|
|
std::filesystem::current_path(test_dir, ec);
|
|
CARBON_CHECK(!ec) << "Could not change the current working dir to '"
|
|
<< test_dir << "': " << ec.message();
|
|
return llvm::make_scope_exit([original_dir, test_dir] {
|
|
std::error_code ec;
|
|
std::filesystem::current_path(original_dir, ec);
|
|
CARBON_CHECK(!ec) << "Could not change the current working dir to '"
|
|
<< original_dir << "': " << ec.message();
|
|
std::filesystem::remove_all(test_dir, ec);
|
|
CARBON_CHECK(!ec) << "Could not remove the test working dir '" << test_dir
|
|
<< "': " << ec.message();
|
|
});
|
|
}
|
|
|
|
llvm::vfs::InMemoryFileSystem fs_;
|
|
const InstallPaths installation_;
|
|
TestRawOstream test_output_stream_;
|
|
TestRawOstream test_error_stream_;
|
|
|
|
// Some tests work directly with files in the test temporary directory.
|
|
std::filesystem::path test_tmpdir_;
|
|
|
|
Driver driver_;
|
|
};
|
|
|
|
TEST_F(DriverTest, BadCommandErrors) {
|
|
EXPECT_FALSE(driver_.RunCommand({}).success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
|
|
|
|
EXPECT_FALSE(driver_.RunCommand({"foo"}).success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
|
|
|
|
EXPECT_FALSE(driver_.RunCommand({"foo --bar --baz"}).success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
|
|
}
|
|
|
|
TEST_F(DriverTest, CompileCommandErrors) {
|
|
// No input file. This error message is important so check all of it.
|
|
EXPECT_FALSE(driver_.RunCommand({"compile"}).success);
|
|
EXPECT_THAT(
|
|
test_error_stream_.TakeStr(),
|
|
StrEq("ERROR: Not all required positional arguments were provided. First "
|
|
"missing and required positional argument: 'FILE'\n"));
|
|
|
|
// Invalid output filename. No reliably error message here.
|
|
// TODO: Likely want a different filename on Windows.
|
|
auto empty_file = MakeTestFile("");
|
|
EXPECT_FALSE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import",
|
|
"--output=/dev/empty", empty_file})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(),
|
|
ContainsRegex("ERROR: .*/dev/empty.*"));
|
|
}
|
|
|
|
TEST_F(DriverTest, DumpTokens) {
|
|
auto file = MakeTestFile("Hello World");
|
|
EXPECT_TRUE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import", "--phase=lex",
|
|
"--dump-tokens", file})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
// Verify there is output without examining it.
|
|
EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
|
|
Yaml::IsYaml(_));
|
|
}
|
|
|
|
TEST_F(DriverTest, DumpParseTree) {
|
|
auto file = MakeTestFile("var v: () = ();");
|
|
EXPECT_TRUE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import",
|
|
"--phase=parse", "--dump-parse-tree", file})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
// Verify there is output without examining it.
|
|
EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
|
|
Yaml::IsYaml(_));
|
|
}
|
|
|
|
TEST_F(DriverTest, StdoutOutput) {
|
|
// Use explicit filenames so we can look for those to validate output.
|
|
MakeTestFile("fn Main() {}", "test.carbon");
|
|
|
|
EXPECT_TRUE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import", "--output=-",
|
|
"test.carbon"})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
// The default is textual assembly.
|
|
EXPECT_THAT(test_output_stream_.TakeStr(), ContainsRegex("Main:"));
|
|
|
|
EXPECT_TRUE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import", "--output=-",
|
|
"--force-obj-output", "test.carbon"})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
std::string output = test_output_stream_.TakeStr();
|
|
auto result =
|
|
llvm::object::createBinary(llvm::MemoryBufferRef(output, "test_output"));
|
|
if (auto error = result.takeError()) {
|
|
FAIL() << toString(std::move(error));
|
|
}
|
|
EXPECT_TRUE(result->get()->isObject());
|
|
}
|
|
|
|
TEST_F(DriverTest, FileOutput) {
|
|
auto scope = ScopedTempWorkingDir();
|
|
|
|
// Use explicit filenames as the default output filename is computed from
|
|
// this, and we can use this to validate output.
|
|
MakeTestFile("fn Main() {}", "test.carbon");
|
|
|
|
// Object output (the default) uses `.o`.
|
|
// TODO: This should actually reflect the platform defaults.
|
|
EXPECT_TRUE(
|
|
driver_.RunCommand({"compile", "--no-prelude-import", "test.carbon"})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
// Ensure we wrote an object file of some form with the correct name.
|
|
auto result = llvm::object::createBinary("test.o");
|
|
if (auto error = result.takeError()) {
|
|
FAIL() << toString(std::move(error));
|
|
}
|
|
EXPECT_TRUE(result->getBinary()->isObject());
|
|
|
|
// Assembly output uses `.s`.
|
|
// TODO: This should actually reflect the platform defaults.
|
|
EXPECT_TRUE(driver_
|
|
.RunCommand({"compile", "--no-prelude-import", "--asm-output",
|
|
"test.carbon"})
|
|
.success);
|
|
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
|
|
// TODO: This may need to be tailored to other assembly formats.
|
|
EXPECT_THAT(ReadFile("test.s"), ContainsRegex("Main:"));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|