Ed Schouten | 4dabea2 | 2017-06-25 08:29:09 +0000 | [diff] [blame^] | 1 | //===--- Ananas.cpp - Ananas ToolChain Implementations ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Ananas.h" |
| 11 | #include "InputInfo.h" |
| 12 | #include "CommonArgs.h" |
| 13 | #include "clang/Config/config.h" |
| 14 | #include "clang/Driver/Compilation.h" |
| 15 | #include "clang/Driver/Driver.h" |
| 16 | #include "clang/Driver/Options.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
| 18 | #include "llvm/Option/ArgList.h" |
| 19 | #include "llvm/Support/Path.h" |
| 20 | |
| 21 | using namespace clang::driver; |
| 22 | using namespace clang::driver::tools; |
| 23 | using namespace clang::driver::toolchains; |
| 24 | using namespace clang; |
| 25 | using namespace llvm::opt; |
| 26 | |
| 27 | void ananas::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
| 28 | const InputInfo &Output, |
| 29 | const InputInfoList &Inputs, |
| 30 | const ArgList &Args, |
| 31 | const char *LinkingOutput) const { |
| 32 | claimNoWarnArgs(Args); |
| 33 | ArgStringList CmdArgs; |
| 34 | |
| 35 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); |
| 36 | |
| 37 | CmdArgs.push_back("-o"); |
| 38 | CmdArgs.push_back(Output.getFilename()); |
| 39 | |
| 40 | for (const auto &II : Inputs) |
| 41 | CmdArgs.push_back(II.getFilename()); |
| 42 | |
| 43 | const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); |
| 44 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
| 45 | } |
| 46 | |
| 47 | void ananas::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 48 | const InputInfo &Output, |
| 49 | const InputInfoList &Inputs, |
| 50 | const ArgList &Args, |
| 51 | const char *LinkingOutput) const { |
| 52 | const ToolChain &ToolChain = getToolChain(); |
| 53 | const Driver &D = ToolChain.getDriver(); |
| 54 | ArgStringList CmdArgs; |
| 55 | |
| 56 | // Silence warning for "clang -g foo.o -o foo" |
| 57 | Args.ClaimAllArgs(options::OPT_g_Group); |
| 58 | // and "clang -emit-llvm foo.o -o foo" |
| 59 | Args.ClaimAllArgs(options::OPT_emit_llvm); |
| 60 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 61 | // handled somewhere else. |
| 62 | Args.ClaimAllArgs(options::OPT_w); |
| 63 | |
| 64 | if (!D.SysRoot.empty()) |
| 65 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
| 66 | |
| 67 | // Ananas only supports static linkage for now. |
| 68 | CmdArgs.push_back("-Bstatic"); |
| 69 | |
| 70 | if (Output.isFilename()) { |
| 71 | CmdArgs.push_back("-o"); |
| 72 | CmdArgs.push_back(Output.getFilename()); |
| 73 | } else { |
| 74 | assert(Output.isNothing() && "Invalid output."); |
| 75 | } |
| 76 | |
| 77 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
| 78 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); |
| 79 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); |
| 80 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o"))); |
| 81 | } |
| 82 | |
| 83 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 84 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
| 85 | Args.AddAllArgs(CmdArgs, |
| 86 | {options::OPT_T_Group, options::OPT_e, options::OPT_s, |
| 87 | options::OPT_t, options::OPT_Z_Flag, options::OPT_r}); |
| 88 | |
| 89 | if (D.isUsingLTO()) |
| 90 | AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D); |
| 91 | |
| 92 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
| 93 | |
| 94 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
| 95 | if (D.CCCIsCXX()) |
| 96 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 97 | CmdArgs.push_back("-lc"); |
| 98 | } |
| 99 | |
| 100 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
| 101 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); |
| 102 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); |
| 103 | } |
| 104 | |
| 105 | const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); |
| 106 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
| 107 | } |
| 108 | |
| 109 | // Ananas - Ananas tool chain which can call as(1) and ld(1) directly. |
| 110 | |
| 111 | Ananas::Ananas(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 112 | : Generic_ELF(D, Triple, Args) { |
| 113 | getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); |
| 114 | } |
| 115 | |
| 116 | Tool *Ananas::buildAssembler() const { |
| 117 | return new tools::ananas::Assembler(*this); |
| 118 | } |
| 119 | |
| 120 | Tool *Ananas::buildLinker() const { return new tools::ananas::Linker(*this); } |