blob: abde3ca8e25ada431c20d6fc0848531b8b1379a8 [file] [log] [blame]
//===-- Tool.h ------------------------------------------------------------===//
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Defines the Tool class (helper for driver functionality).
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
#include "Artifact.h"
namespace gollvm {
namespace driver {
class Action;
class Compilation;
class ExternalTool;
class InternalTool;
class ToolChain;
// Abstract class representing a compilation helper "tool", such as an
// assembler or linker. A tool reads a set of file inputs and produces
// an output file. Tool objects are either "external" or "internal"; for
// the latter the tool itself is capable or performing an action,
// for the former the tool works by constructing a command line for some
// external program to carry out the action.
class Tool {
enum ToolClass { External, Internal };
Tool(const char *name, ToolChain &tc, ToolClass klass);
virtual ~Tool();
ToolChain &toolchain() { return toolchain_; }
const char *name() const { return name_; }
inline InternalTool *castToInternalTool();
inline const InternalTool *castToInternalTool() const;
inline ExternalTool *castToExternalTool();
inline const ExternalTool *castToExternalTool() const;
const char *name_;
ToolChain &toolchain_;
ToolClass klass_;
// An "internal" tool -- carries out the steps needs to perform some
// action (such as compilation or assembly) within the driver itself
// (as opposed to via invocation of an external program).
class InternalTool : public Tool {
InternalTool(const char *name,
ToolChain &tc,
const std::string &executablePath);
virtual ~InternalTool();
// Given a specific action, perform the action now (as opposed to
// invoking an external program to perform the action). Return value
// is true for success, false otherwise.
virtual bool performAction(Compilation &compilation,
const Action &jobAction,
const ArtifactList &inputArtifacts,
const Artifact &output) = 0;
// Helper to emit output for "-v" or "-###" command line option when supplied
// to an internal tool. Return value is TRUE if if the compilation should be
// stubbed out (-###) or FALSE otherwise.
bool emitMinusVOrHashHashHash(const llvm::Triple &triple,
const Artifact &output,
const Action &jobAction);
// Executable path, if applicable.
std::string executablePath() const {
return executablePath_;
ToolChain &tc_;
std::string executablePath_;
// An "external" tool -- carries out the steps needs to perform
// an action via invocation of a program external to the driver.
class ExternalTool : public Tool {
ExternalTool(const char *name, ToolChain &tc);
virtual ~ExternalTool();
// Given a specific action, add a new Command to 'compilation' to
// carry out that action via the invocation of an external program
// (such as a linker or assembler). Return value is true for
// success, false otherwise.
virtual bool constructCommand(Compilation &compilation,
const Action &jobAction,
const ArtifactList &inputArtifacts,
const Artifact &output) = 0;
inline InternalTool *Tool::castToInternalTool() {
return klass_ == Internal ? static_cast<InternalTool*>(this) : nullptr;
inline const InternalTool *Tool::castToInternalTool() const {
return klass_ == Internal ? static_cast<const InternalTool*>(this) : nullptr;
inline ExternalTool *Tool::castToExternalTool() {
return klass_ == External ? static_cast<ExternalTool*>(this) : nullptr;
inline const ExternalTool *Tool::castToExternalTool() const {
return klass_ == External ? static_cast<const ExternalTool*>(this) : nullptr;
} // end namespace driver
} // end namespace gollvm