Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 1 | //===-- Driver.h ----------------------------------------------------------===// |
| 2 | // |
Than McIntosh | c84d402 | 2018-06-20 08:50:57 -0400 | [diff] [blame] | 3 | // 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 McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 6 | // |
| 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 | |
| 33 | namespace gollvm { |
| 34 | namespace driver { |
| 35 | |
| 36 | class Compilation; |
Than McIntosh | 93bb72f | 2018-04-24 15:36:24 -0400 | [diff] [blame] | 37 | class InternalTool; |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 38 | class 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 | |
| 44 | class 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 McIntosh | 0eaf1e3 | 2018-04-24 16:16:24 -0400 | [diff] [blame] | 58 | bool buildActions(Compilation &compilation); |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 59 | |
| 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 McIntosh | 3ab1e5c | 2018-04-26 12:34:48 -0400 | [diff] [blame] | 82 | const std::string &executablePath() const { return executablePath_; } |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 83 | |
| 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 Zhang | 357c450 | 2018-06-04 16:25:08 -0400 | [diff] [blame] | 90 | // gccToolchainDir (or empty string if not present) |
| 91 | std::string gccToolchainDir() { return gccToolchainDir_; } |
| 92 | |
Than McIntosh | 9a323cf | 2018-04-25 09:05:47 -0400 | [diff] [blame] | 93 | // Install directory of compiler binary. |
| 94 | std::string installDir() { return installDir_; } |
| 95 | |
Than McIntosh | 0bb733f | 2018-06-06 12:03:25 -0400 | [diff] [blame] | 96 | // Installed lib dir (binary dir above plus ../lib64) |
| 97 | std::string installedLibDir(); |
| 98 | |
Than McIntosh | 3846a6f | 2018-06-05 15:34:39 -0400 | [diff] [blame] | 99 | // Prefix directories (supplied via -B args) |
| 100 | const std::vector<std::string> &prefixes() const { return prefixes_; } |
| 101 | |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 102 | // Helpers related to command line options. |
| 103 | llvm::PICLevel::Level getPicLevel(); |
Than McIntosh | 7de8dd2 | 2018-05-29 10:56:09 -0400 | [diff] [blame] | 104 | llvm::PIELevel::Level getPieLevel(); |
| 105 | bool picIsPIE(); |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 106 | 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 McIntosh | 9a323cf | 2018-04-25 09:05:47 -0400 | [diff] [blame] | 116 | void appendInputActions(const inarglist &infiles, |
| 117 | ActionList &result, |
| 118 | Compilation &compilation); |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 119 | |
| 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 Zhang | 357c450 | 2018-06-04 16:25:08 -0400 | [diff] [blame] | 126 | std::string gccToolchainDir_; |
Than McIntosh | 9a323cf | 2018-04-25 09:05:47 -0400 | [diff] [blame] | 127 | std::string installDir_; |
| 128 | std::string executablePath_; |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 129 | // 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 Zhang | e5866e8 | 2018-06-04 16:03:46 -0400 | [diff] [blame] | 133 | std::vector<std::string> prefixes_; |
Than McIntosh | 93bb72f | 2018-04-24 15:36:24 -0400 | [diff] [blame] | 134 | |
| 135 | bool processAction(Action *act, Compilation &compilation, bool lastAct); |
| 136 | ArtifactList collectInputArtifacts(Action *act, InternalTool *it); |
| 137 | void emitVersion(); |
Than McIntosh | a71e06e | 2018-04-23 18:22:53 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | template<typename IT> |
| 141 | llvm::Optional<IT> |
| 142 | Driver::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 |