Files
carbon-lang/toolchain/driver/driver.h
T
Chandler CarruthandJon Ross-Perkins d3a5b0eee7 Add utilities for managing a toolchain install, and install and use LLD. (#3993)
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>
2024-06-01 03:19:52 +00:00

106 lines
3.7 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
#ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
#define CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
#include "common/command_line.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "toolchain/install/install_paths.h"
namespace Carbon {
// Command line interface driver.
//
// Provides simple API to parse and run command lines for Carbon. It is
// generally expected to be used to implement command line tools for working
// with the language.
class Driver {
public:
// The result of RunCommand().
struct RunResult {
// Overall success result.
bool success;
// Per-file success results. May be empty if files aren't individually
// processed.
llvm::SmallVector<std::pair<std::string, bool>> per_file_success;
};
// Constructs a driver with any error or informational output directed to a
// specified stream.
Driver(llvm::vfs::FileSystem& fs, const InstallPaths* installation,
llvm::StringRef data_dir, llvm::raw_pwrite_stream& output_stream,
llvm::raw_pwrite_stream& error_stream)
: fs_(fs),
installation_(installation),
data_dir_(data_dir),
output_stream_(output_stream),
error_stream_(error_stream) {}
// Parses the given arguments into both a subcommand to select the operation
// to perform and any arguments to that subcommand.
//
// Returns true if the operation succeeds. If the operation fails, returns
// false and any information about the failure is printed to the registered
// error stream (stderr by default).
auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> RunResult;
// Finds the source files that define the prelude and returns a list of their
// filenames. On error, writes a message to `error_stream` and returns an
// empty list.
static auto FindPreludeFiles(llvm::StringRef data_dir,
llvm::raw_ostream& error_stream)
-> llvm::SmallVector<std::string>;
private:
struct Options;
struct CodegenOptions;
struct CompileOptions;
struct LinkOptions;
class CompilationUnit;
// Delegates to the command line library to parse the arguments and store the
// results in a custom `Options` structure that the rest of the driver uses.
auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
-> CommandLine::ParseResult;
// Does custom validation of the compile-subcommand options structure beyond
// what the command line parsing library supports.
auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
// Implements the compile subcommand of the driver.
auto Compile(const CompileOptions& options,
const CodegenOptions& codegen_options) -> RunResult;
// Implements the link subcommand of the driver.
auto Link(const LinkOptions& options, const CodegenOptions& codegen_options)
-> RunResult;
// The filesystem for source code.
llvm::vfs::FileSystem& fs_;
// Helper to locate the toolchain installation's files.
const InstallPaths* installation_;
// The path within fs for data files.
// TODO: Replace with use of `installation_` once everything is moved over.
std::string data_dir_;
// Standard output; stdout.
llvm::raw_pwrite_stream& output_stream_;
// Error output; stderr.
llvm::raw_pwrite_stream& error_stream_;
// For CARBON_VLOG.
llvm::raw_pwrite_stream* vlog_stream_ = nullptr;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_