clang  6.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::stem(Exec).equals_lower("lld")) {
48  CmdArgs.push_back("-flavor");
49  CmdArgs.push_back("gnu");
50 
51  CmdArgs.push_back("-z");
52  CmdArgs.push_back("rodynamic");
53  }
54 
55  if (!D.SysRoot.empty())
56  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
57 
58  if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r))
59  CmdArgs.push_back("-pie");
60 
61  if (Args.hasArg(options::OPT_rdynamic))
62  CmdArgs.push_back("-export-dynamic");
63 
64  if (Args.hasArg(options::OPT_s))
65  CmdArgs.push_back("-s");
66 
67  if (Args.hasArg(options::OPT_r)) {
68  CmdArgs.push_back("-r");
69  } else {
70  CmdArgs.push_back("--build-id");
71  CmdArgs.push_back("--hash-style=gnu");
72  }
73 
74  CmdArgs.push_back("--eh-frame-hdr");
75 
76  if (Args.hasArg(options::OPT_static))
77  CmdArgs.push_back("-Bstatic");
78  else if (Args.hasArg(options::OPT_shared))
79  CmdArgs.push_back("-shared");
80 
81  if (!Args.hasArg(options::OPT_shared)) {
82  std::string Dyld = D.DyldPrefix;
83  if (ToolChain.getSanitizerArgs().needsAsanRt() &&
84  ToolChain.getSanitizerArgs().needsSharedRt())
85  Dyld += "asan/";
86  Dyld += "ld.so.1";
87  CmdArgs.push_back("-dynamic-linker");
88  CmdArgs.push_back(Args.MakeArgString(Dyld));
89  }
90 
91  CmdArgs.push_back("-o");
92  CmdArgs.push_back(Output.getFilename());
93 
94  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
95  if (!Args.hasArg(options::OPT_shared)) {
96  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
97  }
98  }
99 
100  Args.AddAllArgs(CmdArgs, options::OPT_L);
101  Args.AddAllArgs(CmdArgs, options::OPT_u);
102 
103  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
104 
105  addSanitizerRuntimes(ToolChain, Args, CmdArgs);
106 
107  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
108 
109  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
110  if (Args.hasArg(options::OPT_static))
111  CmdArgs.push_back("-Bdynamic");
112 
113  if (D.CCCIsCXX()) {
114  if (ToolChain.ShouldLinkCXXStdlib(Args))
115  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
116  CmdArgs.push_back("-lm");
117  }
118 
119  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
120 
121  if (Args.hasArg(options::OPT_pthread) ||
122  Args.hasArg(options::OPT_pthreads))
123  CmdArgs.push_back("-lpthread");
124 
125  if (Args.hasArg(options::OPT_fsplit_stack))
126  CmdArgs.push_back("--wrap=pthread_create");
127 
128  CmdArgs.push_back("-lc");
129  }
130 
131  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
132 }
133 
134 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
135 
136 static std::string normalizeTriple(llvm::Triple Triple) {
138  T += Triple.getArchName();
139  T += "-";
140  T += Triple.getOSName();
141  return T.str();
142 }
143 
144 static std::string getTargetDir(const Driver &D,
145  llvm::Triple Triple) {
146  SmallString<128> P(llvm::sys::path::parent_path(D.Dir));
147  llvm::sys::path::append(P, "lib", normalizeTriple(Triple));
148  return P.str();
149 }
150 
151 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
152  const ArgList &Args)
153  : ToolChain(D, Triple, Args) {
154  getProgramPaths().push_back(getDriver().getInstalledDir());
155  if (getDriver().getInstalledDir() != D.Dir)
156  getProgramPaths().push_back(D.Dir);
157 
159  llvm::sys::path::append(P, "lib");
160  getFilePaths().push_back(P.str());
161 
162  if (!D.SysRoot.empty()) {
164  llvm::sys::path::append(P, "lib");
165  getFilePaths().push_back(P.str());
166  }
167 }
168 
169 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
170  types::ID InputType) const {
171  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
172  Triple.setTriple(normalizeTriple(Triple));
173  return Triple.getTriple();
174 }
175 
177  return new tools::fuchsia::Linker(*this);
178 }
179 
181  const ArgList &Args) const {
182  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
183  StringRef Value = A->getValue();
184  if (Value != "compiler-rt")
185  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
186  << A->getAsString(Args);
187  }
188 
190 }
191 
193 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
194  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
195  StringRef Value = A->getValue();
196  if (Value != "libc++")
197  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
198  << A->getAsString(Args);
199  }
200 
201  return ToolChain::CST_Libcxx;
202 }
203 
204 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
205  ArgStringList &CC1Args,
206  Action::OffloadKind) const {
207  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
208  options::OPT_fno_use_init_array, true))
209  CC1Args.push_back("-fuse-init-array");
210 }
211 
212 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
213  ArgStringList &CC1Args) const {
214  const Driver &D = getDriver();
215 
216  if (DriverArgs.hasArg(options::OPT_nostdinc))
217  return;
218 
219  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
221  llvm::sys::path::append(P, "include");
222  addSystemInclude(DriverArgs, CC1Args, P);
223  }
224 
225  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
226  return;
227 
228  // Check for configure-time C include directories.
229  StringRef CIncludeDirs(C_INCLUDE_DIRS);
230  if (CIncludeDirs != "") {
232  CIncludeDirs.split(dirs, ":");
233  for (StringRef dir : dirs) {
234  StringRef Prefix =
235  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
236  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
237  }
238  return;
239  }
240 
241  if (!D.SysRoot.empty()) {
243  llvm::sys::path::append(P, "include");
244  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
245  }
246 }
247 
248 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
249  ArgStringList &CC1Args) const {
250  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
251  DriverArgs.hasArg(options::OPT_nostdincxx))
252  return;
253 
254  switch (GetCXXStdlibType(DriverArgs)) {
255  case ToolChain::CST_Libcxx: {
257  llvm::sys::path::append(P, "include", "c++", "v1");
258  addSystemInclude(DriverArgs, CC1Args, P.str());
259  break;
260  }
261 
262  default:
263  llvm_unreachable("invalid stdlib name");
264  }
265 }
266 
267 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
268  ArgStringList &CmdArgs) const {
269  switch (GetCXXStdlibType(Args)) {
271  CmdArgs.push_back("-lc++");
272  CmdArgs.push_back("-lc++abi");
273  CmdArgs.push_back("-lunwind");
274  break;
275 
277  llvm_unreachable("invalid stdlib name");
278  }
279 }
280 
283  Res |= SanitizerKind::SafeStack;
284  Res |= SanitizerKind::Address;
285  return Res;
286 }
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:633
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:455
StringRef P
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)
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
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:248
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:379
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:197
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:127
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:371
path_list & getFilePaths()
Definition: ToolChain.h:194
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
Tool * buildLinker() const override
Definition: Fuchsia.cpp:176
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:180
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
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:267
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:281
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:694
const Driver & getDriver() const
Definition: ToolChain.h:164
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:212
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:618
static std::string getTargetDir(const Driver &D, llvm::Triple Triple)
Definition: Fuchsia.cpp:144
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:671
Dataflow Directional Tag Classes.
uint64_t SanitizerMask
Definition: Sanitizers.h:24
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:93
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:166
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:730
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:169
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:204
static std::string normalizeTriple(llvm::Triple Triple)
Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
Definition: Fuchsia.cpp:136
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:193
Fuchsia(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Fuchsia.cpp:151
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:72
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:139