blob: 42c7936d4be41b37922963f1a91b153331e74315 [file] [log] [blame]
Than McIntosha71e06e2018-04-23 18:22:53 -04001//===-- Driver.h ----------------------------------------------------------===//
2//
Than McIntoshc84d4022018-06-20 08:50:57 -04003// Copyright 2018 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
Than McIntosha71e06e2018-04-23 18:22:53 -04006//
7//===----------------------------------------------------------------------===//
8//
9// Defines the Driver class (helper for driver functionality).
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef GOLLVM_DRIVER_DRIVER_H
14#define GOLLVM_DRIVER_DRIVER_H
15
16#include "llvm/ADT/Optional.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/OptTable.h"
23#include "llvm/Support/CodeGen.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/Target/TargetOptions.h"
26
27#include "Action.h"
28#include "Artifact.h"
29#include "GollvmOptions.h"
30
31#include <unordered_map>
32
33namespace gollvm {
34namespace driver {
35
36class Compilation;
Than McIntosh93bb72f2018-04-24 15:36:24 -040037class InternalTool;
Than McIntosha71e06e2018-04-23 18:22:53 -040038class ToolChain;
39
40// Driver class. Drives the process of translating a given command
41// line into a series of compilation actions, then into commands to
42// carry out those actions.
43
44class Driver {
45 public:
46 Driver(llvm::opt::InputArgList &args,
47 llvm::opt::OptTable *optTable,
48 const char *argv0);
49 ~Driver();
50
51 // Set up target and select toolchain. Returns nullptr on error.
52 ToolChain *setup();
53
54 // Create compilation object
55 std::unique_ptr<Compilation> buildCompilation(ToolChain &tc);
56
57 // Build actions for compilation. Returns false if error.
Than McIntosh0eaf1e32018-04-24 16:16:24 -040058 bool buildActions(Compilation &compilation);
Than McIntosha71e06e2018-04-23 18:22:53 -040059
60 // Process the action list. This means:
61 // - execute any non-dependent actions that don't require the
62 // invocation of an external tool, and
63 // - generate generate a list of commands for invoking external tools.
64 // Return is true for success, false for error;
65 bool processActions(Compilation &compilation);
66
67 // Locate an object file needed for linking (ex: crt0.o)
68 std::string getFilePath(llvm::StringRef name, ToolChain &toolchain);
69
70 // Locate an external tool (ex: "as"). Returns a full path if a hit
71 // is found, otherwise just returns its name argument.
72 std::string getProgramPath(llvm::StringRef name, ToolChain &toolchain);
73
74 // For constructing default output file with -c, -S, etc.
75 std::string firstFileBase();
76
77 // Getters
78 const llvm::Triple &triple() const { return triple_; }
79 llvm::opt::InputArgList &args() { return args_; }
80 llvm::opt::OptTable &opts() { return *opts_; }
81 const char *progname() const { return progname_; }
Than McIntosh3ab1e5c2018-04-26 12:34:48 -040082 const std::string &executablePath() const { return executablePath_; }
Than McIntosha71e06e2018-04-23 18:22:53 -040083
84 // Name of driver (program invoked)
85 std::string name();
86
87 // Sysroot (or empty string if not present)
88 std::string sysRoot() { return sysroot_; }
89
Cherry Zhang357c4502018-06-04 16:25:08 -040090 // gccToolchainDir (or empty string if not present)
91 std::string gccToolchainDir() { return gccToolchainDir_; }
92
Than McIntosh9a323cf2018-04-25 09:05:47 -040093 // Install directory of compiler binary.
94 std::string installDir() { return installDir_; }
95
Than McIntosh0bb733f2018-06-06 12:03:25 -040096 // Installed lib dir (binary dir above plus ../lib64)
97 std::string installedLibDir();
98
Than McIntosh3846a6f2018-06-05 15:34:39 -040099 // Prefix directories (supplied via -B args)
100 const std::vector<std::string> &prefixes() const { return prefixes_; }
101
Than McIntosha71e06e2018-04-23 18:22:53 -0400102 // Helpers related to command line options.
103 llvm::PICLevel::Level getPicLevel();
Than McIntosh7de8dd22018-05-29 10:56:09 -0400104 llvm::PIELevel::Level getPieLevel();
105 bool picIsPIE();
Than McIntosha71e06e2018-04-23 18:22:53 -0400106 bool isPIE();
107 template<typename IT>
108 llvm::Optional<IT> getLastArgAsInteger(gollvm::options::ID id,
109 IT defaultValue);
110 llvm::Optional<llvm::Reloc::Model> reconcileRelocModel();
111 bool reconcileOptionPair(gollvm::options::ID yesOption,
112 gollvm::options::ID noOption,
113 bool defaultVal);
114 llvm::Optional<llvm::FPOpFusion::FPOpFusionMode> getFPOpFusionMode();
115 typedef llvm::SmallVector<llvm::opt::Arg *, 8> inarglist;
Than McIntosh9a323cf2018-04-25 09:05:47 -0400116 void appendInputActions(const inarglist &infiles,
117 ActionList &result,
118 Compilation &compilation);
Than McIntosha71e06e2018-04-23 18:22:53 -0400119
120 private:
121 llvm::Triple triple_;
122 llvm::opt::InputArgList &args_;
123 llvm::opt::OptTable *opts_;
124 const char *progname_;
125 std::string sysroot_;
Cherry Zhang357c4502018-06-04 16:25:08 -0400126 std::string gccToolchainDir_;
Than McIntosh9a323cf2018-04-25 09:05:47 -0400127 std::string installDir_;
128 std::string executablePath_;
Than McIntosha71e06e2018-04-23 18:22:53 -0400129 // maps target to toolchain for that target
130 llvm::StringMap<std::unique_ptr<ToolChain>> toolchains_;
131 // Maps non-input actions to output artifacts.
132 std::unordered_map<Action *, Artifact*> artmap_;
Cherry Zhange5866e82018-06-04 16:03:46 -0400133 std::vector<std::string> prefixes_;
Than McIntosh93bb72f2018-04-24 15:36:24 -0400134
135 bool processAction(Action *act, Compilation &compilation, bool lastAct);
136 ArtifactList collectInputArtifacts(Action *act, InternalTool *it);
137 void emitVersion();
Than McIntosha71e06e2018-04-23 18:22:53 -0400138};
139
140template<typename IT>
141llvm::Optional<IT>
142Driver::getLastArgAsInteger(gollvm::options::ID id,
143 IT defaultValue)
144{
145 IT result = defaultValue;
146 llvm::opt::Arg *arg = args_.getLastArg(id);
147 if (arg != nullptr) {
148 if (llvm::StringRef(arg->getValue()).getAsInteger(10, result)) {
149 llvm::errs() << progname_ << ": invalid argument '"
150 << arg->getValue() << "' to '"
151 << arg->getAsString(args_) << "' option\n";
152 return llvm::None;
153 }
154 }
155 return result;
156}
157
158} // end namespace driver
159} // end namespace gollvm
160
161#endif // GOLLVM_DRIVER_DRIVER_H