mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 22:02:37 +01:00
Multiple subcommands all need the ability to disable on-demand runtime building, and this may be needed outside of using _prebuilt_ runtimes. For example, with Bazel the plan is to not build runtimes at all and have Bazel provide them as native Bazel libraries. Updates the `link` subcommand to respect this flag when running Clang to perform links. We didn't have any real testing of the `link` subcommand, in part because it was difficult -- it would try to link runtime libraries. Now that we can prevent building them on demand, we can use that to test the link command. That in turn helped uncover a couple of bugs that are fixed here. 1) The `driver_env_` member of the `Driver` was re-used across `RunCommand` invocations. Some of its fields are constant across these, others can be updated, and still more are not necessarily something we would expect to be re-used. This fixes that by removing the `driver_env_` member, and replacing it with members for just the fields of `DriverEnv` that we want to set initially based on the construction of the `Driver` object. This causes multiple, sequential `RunCommand` calls to not clobber or erroneously inherit state. 2) The temporary directory support in the driver unittest didn't allow the driver to observe the things it wrote to the temporary directory. This PR updates the test logic to create an overlay VFS so that both the in-memory test inputs are observed, but so are the real files written into the temporary directory. 3) The Clang runner, when asked to run Clang without runtimes would still attempt to include runtimes in any link command. This isn't quite what we want, as the whole reason to use this without building runtimes is to reuse ones built in some other way and potentially in some other location. For now, this PR uses a hack to suppress these issues so that we can have a basic test, but in the future we'll need a better solution here. 4) The driver test didn't include the actual driver in the install data. The test even worked around this, but it makes it impossible to link reliably as the `lld` binary isn't available. This adds the data dependency and updates the test to the available digest, etc.
65 lines
2.5 KiB
C++
65 lines
2.5 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 "toolchain/driver/driver_env.h"
|
|
#include "toolchain/driver/driver_subcommand.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:
|
|
// Constructs a driver with the provided environment. `input_stream` is
|
|
// optional; other parameters are required.
|
|
explicit Driver(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
|
|
const InstallPaths* installation, FILE* input_stream,
|
|
llvm::raw_pwrite_stream* output_stream,
|
|
llvm::raw_pwrite_stream* error_stream, bool fuzzing = false,
|
|
bool enable_leaking = false)
|
|
: fs_(std::move(fs)),
|
|
installation_(installation),
|
|
input_stream_(input_stream),
|
|
output_stream_(output_stream),
|
|
error_stream_(error_stream),
|
|
fuzzing_(fuzzing),
|
|
enable_leaking_(enable_leaking) {}
|
|
|
|
// 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) -> DriverResult;
|
|
|
|
private:
|
|
// We store the initial values in the `DriverEnv` that will be used for each
|
|
// subcommand invocation here. These are used as the _starting_ values of the
|
|
// environment, but individual `RunCommand` invocations may customize the
|
|
// `DriverEnv` instance changing these values.
|
|
//
|
|
// For details on each of these fields, see the documentation in `DriverEnv`.
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
|
|
const InstallPaths* installation_;
|
|
FILE* input_stream_;
|
|
llvm::raw_pwrite_stream* output_stream_;
|
|
llvm::raw_pwrite_stream* error_stream_;
|
|
bool fuzzing_;
|
|
bool enable_leaking_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
|