clang  7.0.0svn
Fuchsia.cpp
Go to the documentation of this file.
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"
14 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/Options.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/Path.h"
20 
21 using namespace clang::driver;
22 using namespace clang::driver::toolchains;
23 using namespace clang::driver::tools;
24 using namespace clang;
25 using namespace llvm::opt;
26 
27 void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
28  const InputInfo &Output,
29  const InputInfoList &Inputs,
30  const ArgList &Args,
31  const char *LinkingOutput) const {
33  static_cast<const toolchains::Fuchsia &>(getToolChain());
34  const Driver &D = ToolChain.getDriver();
35 
36  ArgStringList CmdArgs;
37 
38  // Silence warning for "clang -g foo.o -o foo"
39  Args.ClaimAllArgs(options::OPT_g_Group);
40  // and "clang -emit-llvm foo.o -o foo"
41  Args.ClaimAllArgs(options::OPT_emit_llvm);
42  // and for "clang -w foo.o -o foo". Other warning options are already
43  // handled somewhere else.
44  Args.ClaimAllArgs(options::OPT_w);
45 
46  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
47  if (llvm::sys::path::filename(Exec).equals_lower("ld.lld") ||
48  llvm::sys::path::stem(Exec).equals_lower("ld.lld")) {
49  CmdArgs.push_back("-z");
50  CmdArgs.push_back("rodynamic");
51  }
52 
53  if (!D.SysRoot.empty())
54  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
55 
56  if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r))
57  CmdArgs.push_back("-pie");
58 
59  if (Args.hasArg(options::OPT_rdynamic))
60  CmdArgs.push_back("-export-dynamic");
61 
62  if (Args.hasArg(options::OPT_s))
63  CmdArgs.push_back("-s");
64 
65  if (Args.hasArg(options::OPT_r)) {
66  CmdArgs.push_back("-r");
67  } else {
68  CmdArgs.push_back("--build-id");
69  CmdArgs.push_back("--hash-style=gnu");
70  }
71 
72  CmdArgs.push_back("--eh-frame-hdr");
73 
74  if (Args.hasArg(options::OPT_static))
75  CmdArgs.push_back("-Bstatic");
76  else if (Args.hasArg(options::OPT_shared))
77  CmdArgs.push_back("-shared");
78 
79  if (!Args.hasArg(options::OPT_shared)) {
80  std::string Dyld = D.DyldPrefix;
81  if (ToolChain.getSanitizerArgs().needsAsanRt() &&
82  ToolChain.getSanitizerArgs().needsSharedRt())
83  Dyld += "asan/";
84  Dyld += "ld.so.1";
85  CmdArgs.push_back("-dynamic-linker");
86  CmdArgs.push_back(Args.MakeArgString(Dyld));
87  }
88 
89  CmdArgs.push_back("-o");
90  CmdArgs.push_back(Output.getFilename());
91 
92  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
93  if (!Args.hasArg(options::OPT_shared)) {
94  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
95  }
96  }
97 
98  Args.AddAllArgs(CmdArgs, options::OPT_L);
99  Args.AddAllArgs(CmdArgs, options::OPT_u);
100 
101  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
102 
103  if (D.isUsingLTO()) {
104  assert(!Inputs.empty() && "Must have at least one input.");
105  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
106  D.getLTOMode() == LTOK_Thin);
107  }
108 
109  addSanitizerRuntimes(ToolChain, Args, CmdArgs);
110 
111  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
112 
113  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
114  if (Args.hasArg(options::OPT_static))
115  CmdArgs.push_back("-Bdynamic");
116 
117  if (D.CCCIsCXX()) {
118  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
119  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
120  !Args.hasArg(options::OPT_static);
121  if (OnlyLibstdcxxStatic)
122  CmdArgs.push_back("-Bstatic");
123  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
124  if (OnlyLibstdcxxStatic)
125  CmdArgs.push_back("-Bdynamic");
126  }
127  CmdArgs.push_back("-lm");
128  }
129 
130  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
131 
132  if (Args.hasArg(options::OPT_pthread) ||
133  Args.hasArg(options::OPT_pthreads))
134  CmdArgs.push_back("-lpthread");
135 
136  if (Args.hasArg(options::OPT_fsplit_stack))
137  CmdArgs.push_back("--wrap=pthread_create");
138 
139  CmdArgs.push_back("-lc");
140  }
141 
142  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
143 }
144 
145 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
146 
147 static std::string normalizeTriple(llvm::Triple Triple) {
149  T += Triple.getArchName();
150  T += "-";
151  T += Triple.getOSName();
152  return T.str();
153 }
154 
155 static std::string getTargetDir(const Driver &D,
156  llvm::Triple Triple) {
157  SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
158  llvm::sys::path::append(P, "lib", normalizeTriple(Triple));
159  return P.str();
160 }
161 
162 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
163  const ArgList &Args)
164  : ToolChain(D, Triple, Args) {
165  getProgramPaths().push_back(getDriver().getInstalledDir());
166  if (getDriver().getInstalledDir() != D.Dir)
167  getProgramPaths().push_back(D.Dir);
168 
170  llvm::sys::path::append(P, "lib");
171  getFilePaths().push_back(P.str());
172 
173  if (!D.SysRoot.empty()) {
175  llvm::sys::path::append(P, "lib");
176  getFilePaths().push_back(P.str());
177  }
178 }
179 
180 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
181  types::ID InputType) const {
182  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
183  Triple.setTriple(normalizeTriple(Triple));
184  return Triple.getTriple();
185 }
186 
188  return new tools::fuchsia::Linker(*this);
189 }
190 
192  const ArgList &Args) const {
193  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
194  StringRef Value = A->getValue();
195  if (Value != "compiler-rt")
196  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
197  << A->getAsString(Args);
198  }
199 
201 }
202 
204 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
205  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
206  StringRef Value = A->getValue();
207  if (Value != "libc++")
208  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
209  << A->getAsString(Args);
210  }
211 
212  return ToolChain::CST_Libcxx;
213 }
214 
215 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
216  ArgStringList &CC1Args,
217  Action::OffloadKind) const {
218  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
219  options::OPT_fno_use_init_array, true))
220  CC1Args.push_back("-fuse-init-array");
221 }
222 
223 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
224  ArgStringList &CC1Args) const {
225  const Driver &D = getDriver();
226 
227  if (DriverArgs.hasArg(options::OPT_nostdinc))
228  return;
229 
230  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
232  llvm::sys::path::append(P, "include");
233  addSystemInclude(DriverArgs, CC1Args, P);
234  }
235 
236  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
237  return;
238 
239  // Check for configure-time C include directories.
240  StringRef CIncludeDirs(C_INCLUDE_DIRS);
241  if (CIncludeDirs != "") {
243  CIncludeDirs.split(dirs, ":");
244  for (StringRef dir : dirs) {
245  StringRef Prefix =
246  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
247  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
248  }
249  return;
250  }
251 
252  if (!D.SysRoot.empty()) {
254  llvm::sys::path::append(P, "include");
255  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
256  }
257 }
258 
259 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
260  ArgStringList &CC1Args) const {
261  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
262  DriverArgs.hasArg(options::OPT_nostdincxx))
263  return;
264 
265  switch (GetCXXStdlibType(DriverArgs)) {
266  case ToolChain::CST_Libcxx: {
268  llvm::sys::path::append(P, "include", "c++", "v1");
269  addSystemInclude(DriverArgs, CC1Args, P.str());
270  break;
271  }
272 
273  default:
274  llvm_unreachable("invalid stdlib name");
275  }
276 }
277 
278 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
279  ArgStringList &CmdArgs) const {
280  switch (GetCXXStdlibType(Args)) {
282  CmdArgs.push_back("-lc++");
283  break;
284 
286  llvm_unreachable("invalid stdlib name");
287  }
288 }
289 
292  Res |= SanitizerKind::Address;
293  Res |= SanitizerKind::Fuzzer;
294  Res |= SanitizerKind::FuzzerNoLink;
295  Res |= SanitizerKind::SafeStack;
296  Res |= SanitizerKind::Scudo;
297  return Res;
298 }
299 
301  return SanitizerKind::SafeStack;
302 }
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:689
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
ComputeLLVMTriple - Return the LLVM target triple to use, after taking command line arguments into ac...
Definition: ToolChain.cpp:492
StringRef P
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: Fuchsia.cpp:300
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:519
std::string DyldPrefix
Dynamic loader prefix, if present.
Definition: Driver.h:152
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:522
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:110
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: Fuchsia.cpp:259
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:411
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:219
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:121
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:403
path_list & getFilePaths()
Definition: ToolChain.h:216
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
Tool * buildLinker() const override
Definition: Fuchsia.cpp:187
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:191
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: Fuchsia.cpp:278
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:290
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:750
const Driver & getDriver() const
Definition: ToolChain.h:181
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Fuchsia.cpp:223
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:674
static std::string getTargetDir(const Driver &D, llvm::Triple Triple)
Definition: Fuchsia.cpp:155
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:727
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:102
uint64_t SanitizerMask
Definition: Sanitizers.h:26
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
const llvm::Triple & getTriple() const
Definition: ToolChain.h:183
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:786
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, types::ID InputType) const override
ComputeEffectiveClangTriple - Return the Clang triple to use for this target, which may take into acc...
Definition: Fuchsia.cpp:180
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Fuchsia.cpp:215
static std::string normalizeTriple(llvm::Triple Triple)
Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
Definition: Fuchsia.cpp:147
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:204
Fuchsia(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Fuchsia.cpp:162
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:133