David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 1 | //===--- Fuchsia.cpp - Fuchsia 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 "Fuchsia.h" |
| 11 | #include "CommonArgs.h" |
| 12 | #include "clang/Config/config.h" |
| 13 | #include "clang/Driver/Compilation.h" |
| 14 | #include "clang/Driver/Driver.h" |
| 15 | #include "clang/Driver/DriverDiagnostic.h" |
| 16 | #include "clang/Driver/Options.h" |
| 17 | #include "llvm/Option/ArgList.h" |
| 18 | #include "llvm/Support/Path.h" |
| 19 | |
| 20 | using namespace clang::driver; |
| 21 | using namespace clang::driver::toolchains; |
| 22 | using namespace clang::driver::tools; |
| 23 | using namespace clang; |
| 24 | using namespace llvm::opt; |
| 25 | |
| 26 | void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 27 | const InputInfo &Output, |
| 28 | const InputInfoList &Inputs, |
| 29 | const ArgList &Args, |
| 30 | const char *LinkingOutput) const { |
| 31 | const toolchains::Fuchsia &ToolChain = |
| 32 | static_cast<const toolchains::Fuchsia &>(getToolChain()); |
| 33 | const Driver &D = ToolChain.getDriver(); |
| 34 | |
| 35 | ArgStringList CmdArgs; |
| 36 | |
| 37 | // Silence warning for "clang -g foo.o -o foo" |
| 38 | Args.ClaimAllArgs(options::OPT_g_Group); |
| 39 | // and "clang -emit-llvm foo.o -o foo" |
| 40 | Args.ClaimAllArgs(options::OPT_emit_llvm); |
| 41 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 42 | // handled somewhere else. |
| 43 | Args.ClaimAllArgs(options::OPT_w); |
| 44 | |
| 45 | const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); |
| 46 | if (llvm::sys::path::stem(Exec).equals_lower("lld")) { |
| 47 | CmdArgs.push_back("-flavor"); |
| 48 | CmdArgs.push_back("gnu"); |
| 49 | } |
| 50 | |
| 51 | if (!D.SysRoot.empty()) |
| 52 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
| 53 | |
| 54 | if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r)) |
| 55 | CmdArgs.push_back("-pie"); |
| 56 | |
| 57 | if (Args.hasArg(options::OPT_rdynamic)) |
| 58 | CmdArgs.push_back("-export-dynamic"); |
| 59 | |
| 60 | if (Args.hasArg(options::OPT_s)) |
| 61 | CmdArgs.push_back("-s"); |
| 62 | |
| 63 | if (Args.hasArg(options::OPT_r)) |
| 64 | CmdArgs.push_back("-r"); |
| 65 | else |
| 66 | CmdArgs.push_back("--build-id"); |
| 67 | |
| 68 | if (!Args.hasArg(options::OPT_static)) |
| 69 | CmdArgs.push_back("--eh-frame-hdr"); |
| 70 | |
| 71 | if (Args.hasArg(options::OPT_static)) |
| 72 | CmdArgs.push_back("-Bstatic"); |
| 73 | else if (Args.hasArg(options::OPT_shared)) |
| 74 | CmdArgs.push_back("-shared"); |
| 75 | |
| 76 | if (!Args.hasArg(options::OPT_static)) { |
| 77 | if (Args.hasArg(options::OPT_rdynamic)) |
| 78 | CmdArgs.push_back("-export-dynamic"); |
| 79 | |
| 80 | if (!Args.hasArg(options::OPT_shared)) { |
| 81 | CmdArgs.push_back("-dynamic-linker"); |
| 82 | CmdArgs.push_back(Args.MakeArgString(D.DyldPrefix + "ld.so.1")); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | CmdArgs.push_back("-o"); |
| 87 | CmdArgs.push_back(Output.getFilename()); |
| 88 | |
| 89 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
| 90 | if (!Args.hasArg(options::OPT_shared)) { |
| 91 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o"))); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 96 | Args.AddAllArgs(CmdArgs, options::OPT_u); |
| 97 | |
| 98 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
| 99 | |
| 100 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
| 101 | |
| 102 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { |
| 103 | if (Args.hasArg(options::OPT_static)) |
| 104 | CmdArgs.push_back("-Bdynamic"); |
| 105 | |
| 106 | if (D.CCCIsCXX()) { |
| 107 | bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && |
| 108 | !Args.hasArg(options::OPT_static); |
| 109 | if (OnlyLibstdcxxStatic) |
| 110 | CmdArgs.push_back("-Bstatic"); |
| 111 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
| 112 | if (OnlyLibstdcxxStatic) |
| 113 | CmdArgs.push_back("-Bdynamic"); |
| 114 | CmdArgs.push_back("-lm"); |
| 115 | } |
| 116 | |
| 117 | AddRunTimeLibs(ToolChain, D, CmdArgs, Args); |
| 118 | |
| 119 | if (Args.hasArg(options::OPT_pthread) || |
| 120 | Args.hasArg(options::OPT_pthreads)) |
| 121 | CmdArgs.push_back("-lpthread"); |
| 122 | |
| 123 | if (Args.hasArg(options::OPT_fsplit_stack)) |
| 124 | CmdArgs.push_back("--wrap=pthread_create"); |
| 125 | |
| 126 | CmdArgs.push_back("-lc"); |
| 127 | } |
| 128 | |
| 129 | C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); |
| 130 | } |
| 131 | |
| 132 | /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly. |
| 133 | |
| 134 | Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, |
| 135 | const ArgList &Args) |
| 136 | : Generic_ELF(D, Triple, Args) { |
| 137 | |
| 138 | getFilePaths().push_back(D.SysRoot + "/lib"); |
| 139 | getFilePaths().push_back(D.ResourceDir + "/lib/fuchsia"); |
| 140 | } |
| 141 | |
| 142 | Tool *Fuchsia::buildAssembler() const { |
| 143 | return new tools::gnutools::Assembler(*this); |
| 144 | } |
| 145 | |
| 146 | Tool *Fuchsia::buildLinker() const { |
| 147 | return new tools::fuchsia::Linker(*this); |
| 148 | } |
| 149 | |
| 150 | ToolChain::RuntimeLibType Fuchsia::GetRuntimeLibType( |
| 151 | const ArgList &Args) const { |
| 152 | if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) { |
| 153 | StringRef Value = A->getValue(); |
| 154 | if (Value != "compiler-rt") |
| 155 | getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name) |
| 156 | << A->getAsString(Args); |
| 157 | } |
| 158 | |
| 159 | return ToolChain::RLT_CompilerRT; |
| 160 | } |
| 161 | |
| 162 | ToolChain::CXXStdlibType |
| 163 | Fuchsia::GetCXXStdlibType(const ArgList &Args) const { |
| 164 | if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { |
| 165 | StringRef Value = A->getValue(); |
| 166 | if (Value != "libc++") |
| 167 | getDriver().Diag(diag::err_drv_invalid_stdlib_name) |
| 168 | << A->getAsString(Args); |
| 169 | } |
| 170 | |
| 171 | return ToolChain::CST_Libcxx; |
| 172 | } |
| 173 | |
| 174 | void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs, |
Gheorghe-Teodor Bercea | f0f2960 | 2017-07-06 16:22:21 +0000 | [diff] [blame^] | 175 | ArgStringList &CC1Args, |
| 176 | Action::OffloadKind) const { |
David L. Jones | f561aba | 2017-03-08 01:02:16 +0000 | [diff] [blame] | 177 | if (DriverArgs.hasFlag(options::OPT_fuse_init_array, |
| 178 | options::OPT_fno_use_init_array, true)) |
| 179 | CC1Args.push_back("-fuse-init-array"); |
| 180 | } |
| 181 | |
| 182 | void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 183 | ArgStringList &CC1Args) const { |
| 184 | const Driver &D = getDriver(); |
| 185 | |
| 186 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
| 187 | return; |
| 188 | |
| 189 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
| 190 | SmallString<128> P(D.ResourceDir); |
| 191 | llvm::sys::path::append(P, "include"); |
| 192 | addSystemInclude(DriverArgs, CC1Args, P); |
| 193 | } |
| 194 | |
| 195 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 196 | return; |
| 197 | |
| 198 | // Check for configure-time C include directories. |
| 199 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
| 200 | if (CIncludeDirs != "") { |
| 201 | SmallVector<StringRef, 5> dirs; |
| 202 | CIncludeDirs.split(dirs, ":"); |
| 203 | for (StringRef dir : dirs) { |
| 204 | StringRef Prefix = |
| 205 | llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; |
| 206 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
| 207 | } |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/include"); |
| 212 | } |
| 213 | |
| 214 | std::string Fuchsia::findLibCxxIncludePath() const { |
| 215 | return getDriver().SysRoot + "/include/c++/v1"; |
| 216 | } |
| 217 | |
| 218 | void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args, |
| 219 | ArgStringList &CmdArgs) const { |
| 220 | (void) GetCXXStdlibType(Args); |
| 221 | CmdArgs.push_back("-lc++"); |
| 222 | CmdArgs.push_back("-lc++abi"); |
| 223 | CmdArgs.push_back("-lunwind"); |
| 224 | } |
| 225 | |
| 226 | SanitizerMask Fuchsia::getSupportedSanitizers() const { |
| 227 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 228 | Res |= SanitizerKind::SafeStack; |
| 229 | return Res; |
| 230 | } |