Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 1 | //===--- BaremMetal.cpp - Bare Metal ToolChain ------------------*- 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 "BareMetal.h" |
| 11 | |
| 12 | #include "CommonArgs.h" |
| 13 | #include "InputInfo.h" |
| 14 | #include "Gnu.h" |
| 15 | |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 16 | #include "clang/Driver/Compilation.h" |
| 17 | #include "clang/Driver/Driver.h" |
| 18 | #include "clang/Driver/DriverDiagnostic.h" |
| 19 | #include "clang/Driver/Options.h" |
| 20 | #include "llvm/Option/ArgList.h" |
| 21 | #include "llvm/Support/Path.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 22 | #include "llvm/Support/VirtualFileSystem.h" |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | |
| 25 | using namespace llvm::opt; |
| 26 | using namespace clang; |
| 27 | using namespace clang::driver; |
| 28 | using namespace clang::driver::tools; |
| 29 | using namespace clang::driver::toolchains; |
| 30 | |
| 31 | BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple, |
| 32 | const ArgList &Args) |
| 33 | : ToolChain(D, Triple, Args) { |
| 34 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
| 35 | if (getDriver().getInstalledDir() != getDriver().Dir) |
| 36 | getProgramPaths().push_back(getDriver().Dir); |
| 37 | } |
| 38 | |
| 39 | BareMetal::~BareMetal() {} |
| 40 | |
| 41 | /// Is the triple {arm,thumb}-none-none-{eabi,eabihf} ? |
| 42 | static bool isARMBareMetal(const llvm::Triple &Triple) { |
| 43 | if (Triple.getArch() != llvm::Triple::arm && |
| 44 | Triple.getArch() != llvm::Triple::thumb) |
| 45 | return false; |
| 46 | |
| 47 | if (Triple.getVendor() != llvm::Triple::UnknownVendor) |
| 48 | return false; |
| 49 | |
| 50 | if (Triple.getOS() != llvm::Triple::UnknownOS) |
| 51 | return false; |
| 52 | |
| 53 | if (Triple.getEnvironment() != llvm::Triple::EABI && |
| 54 | Triple.getEnvironment() != llvm::Triple::EABIHF) |
| 55 | return false; |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool BareMetal::handlesTarget(const llvm::Triple &Triple) { |
| 61 | return isARMBareMetal(Triple); |
| 62 | } |
| 63 | |
| 64 | Tool *BareMetal::buildLinker() const { |
| 65 | return new tools::baremetal::Linker(*this); |
| 66 | } |
| 67 | |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 68 | std::string BareMetal::getRuntimesDir() const { |
| 69 | SmallString<128> Dir(getDriver().ResourceDir); |
| 70 | llvm::sys::path::append(Dir, "lib", "baremetal"); |
| 71 | return Dir.str(); |
| 72 | } |
| 73 | |
| 74 | void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 75 | ArgStringList &CC1Args) const { |
| 76 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
| 77 | return; |
| 78 | |
| 79 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
| 80 | SmallString<128> Dir(getDriver().ResourceDir); |
| 81 | llvm::sys::path::append(Dir, "include"); |
| 82 | addSystemInclude(DriverArgs, CC1Args, Dir.str()); |
| 83 | } |
| 84 | |
| 85 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) { |
| 86 | SmallString<128> Dir(getDriver().SysRoot); |
| 87 | llvm::sys::path::append(Dir, "include"); |
| 88 | addSystemInclude(DriverArgs, CC1Args, Dir.str()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void BareMetal::addClangTargetOptions(const ArgList &DriverArgs, |
Gheorghe-Teodor Bercea | f0f2960 | 2017-07-06 16:22:21 +0000 | [diff] [blame] | 93 | ArgStringList &CC1Args, |
| 94 | Action::OffloadKind) const { |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 95 | CC1Args.push_back("-nostdsysteminc"); |
| 96 | } |
| 97 | |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 98 | void BareMetal::AddClangCXXStdlibIncludeArgs( |
| 99 | const ArgList &DriverArgs, ArgStringList &CC1Args) const { |
| 100 | if (DriverArgs.hasArg(options::OPT_nostdinc) || |
| 101 | DriverArgs.hasArg(options::OPT_nostdlibinc) || |
| 102 | DriverArgs.hasArg(options::OPT_nostdincxx)) |
| 103 | return; |
| 104 | |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 105 | StringRef SysRoot = getDriver().SysRoot; |
| 106 | if (SysRoot.empty()) |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 107 | return; |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 108 | |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 109 | switch (GetCXXStdlibType(DriverArgs)) { |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 110 | case ToolChain::CST_Libcxx: { |
| 111 | SmallString<128> Dir(SysRoot); |
| 112 | llvm::sys::path::append(Dir, "include", "c++", "v1"); |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 113 | addSystemInclude(DriverArgs, CC1Args, Dir.str()); |
| 114 | break; |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 115 | } |
| 116 | case ToolChain::CST_Libstdcxx: { |
| 117 | SmallString<128> Dir(SysRoot); |
| 118 | llvm::sys::path::append(Dir, "include", "c++"); |
| 119 | std::error_code EC; |
| 120 | Generic_GCC::GCCVersion Version = {"", -1, -1, -1, "", "", ""}; |
| 121 | // Walk the subdirs, and find the one with the newest gcc version: |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 122 | for (llvm::vfs::directory_iterator |
| 123 | LI = getDriver().getVFS().dir_begin(Dir.str(), EC), |
| 124 | LE; |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 125 | !EC && LI != LE; LI = LI.increment(EC)) { |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 126 | StringRef VersionText = llvm::sys::path::filename(LI->path()); |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 127 | auto CandidateVersion = Generic_GCC::GCCVersion::Parse(VersionText); |
| 128 | if (CandidateVersion.Major == -1) |
| 129 | continue; |
| 130 | if (CandidateVersion <= Version) |
| 131 | continue; |
| 132 | Version = CandidateVersion; |
| 133 | } |
| 134 | if (Version.Major == -1) |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 135 | return; |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 136 | llvm::sys::path::append(Dir, Version.Text); |
Petr Hosek | 8d61214 | 2018-04-10 19:55:55 +0000 | [diff] [blame] | 137 | addSystemInclude(DriverArgs, CC1Args, Dir.str()); |
| 138 | break; |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void BareMetal::AddCXXStdlibLibArgs(const ArgList &Args, |
| 144 | ArgStringList &CmdArgs) const { |
| 145 | switch (GetCXXStdlibType(Args)) { |
| 146 | case ToolChain::CST_Libcxx: |
| 147 | CmdArgs.push_back("-lc++"); |
| 148 | CmdArgs.push_back("-lc++abi"); |
| 149 | break; |
| 150 | case ToolChain::CST_Libstdcxx: |
| 151 | CmdArgs.push_back("-lstdc++"); |
| 152 | CmdArgs.push_back("-lsupc++"); |
| 153 | break; |
| 154 | } |
| 155 | CmdArgs.push_back("-lunwind"); |
| 156 | } |
| 157 | |
| 158 | void BareMetal::AddLinkRuntimeLib(const ArgList &Args, |
| 159 | ArgStringList &CmdArgs) const { |
| 160 | CmdArgs.push_back(Args.MakeArgString("-lclang_rt.builtins-" + |
| 161 | getTriple().getArchName() + ".a")); |
| 162 | } |
| 163 | |
| 164 | void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 165 | const InputInfo &Output, |
| 166 | const InputInfoList &Inputs, |
| 167 | const ArgList &Args, |
| 168 | const char *LinkingOutput) const { |
| 169 | ArgStringList CmdArgs; |
| 170 | |
| 171 | auto &TC = static_cast<const toolchains::BareMetal&>(getToolChain()); |
| 172 | |
| 173 | AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
| 174 | |
| 175 | CmdArgs.push_back("-Bstatic"); |
| 176 | |
| 177 | CmdArgs.push_back(Args.MakeArgString("-L" + TC.getRuntimesDir())); |
| 178 | |
| 179 | Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group, |
| 180 | options::OPT_e, options::OPT_s, options::OPT_t, |
| 181 | options::OPT_Z_Flag, options::OPT_r}); |
| 182 | |
Nico Weber | 0ee47d9 | 2017-07-25 18:02:57 +0000 | [diff] [blame] | 183 | if (TC.ShouldLinkCXXStdlib(Args)) |
| 184 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 185 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
Jonathan Roelofs | 901c776 | 2017-05-25 15:42:13 +0000 | [diff] [blame] | 186 | CmdArgs.push_back("-lc"); |
| 187 | CmdArgs.push_back("-lm"); |
| 188 | |
| 189 | TC.AddLinkRuntimeLib(Args, CmdArgs); |
| 190 | } |
| 191 | |
| 192 | CmdArgs.push_back("-o"); |
| 193 | CmdArgs.push_back(Output.getFilename()); |
| 194 | |
| 195 | C.addCommand(llvm::make_unique<Command>(JA, *this, |
| 196 | Args.MakeArgString(TC.GetLinkerPath()), |
| 197 | CmdArgs, Inputs)); |
| 198 | } |