clang  8.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  const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs();
80 
81  if (!Args.hasArg(options::OPT_shared)) {
82  std::string Dyld = D.DyldPrefix;
83  if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt())
84  Dyld += "asan/";
85  Dyld += "ld.so.1";
86  CmdArgs.push_back("-dynamic-linker");
87  CmdArgs.push_back(Args.MakeArgString(Dyld));
88  }
89 
90  CmdArgs.push_back("-o");
91  CmdArgs.push_back(Output.getFilename());
92 
93  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
94  if (!Args.hasArg(options::OPT_shared)) {
95  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
96  }
97  }
98 
99  Args.AddAllArgs(CmdArgs, options::OPT_L);
100  Args.AddAllArgs(CmdArgs, options::OPT_u);
101 
102  addSanitizerPathLibArgs(ToolChain, Args, CmdArgs);
103 
104  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
105 
106  if (D.isUsingLTO()) {
107  assert(!Inputs.empty() && "Must have at least one input.");
108  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
109  D.getLTOMode() == LTOK_Thin);
110  }
111 
112  addSanitizerRuntimes(ToolChain, Args, CmdArgs);
113 
114  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
115  ToolChain.addProfileRTLibs(Args, CmdArgs);
116 
117  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
118  if (Args.hasArg(options::OPT_static))
119  CmdArgs.push_back("-Bdynamic");
120 
121  if (D.CCCIsCXX()) {
122  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
123  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
124  !Args.hasArg(options::OPT_static);
125  CmdArgs.push_back("--push-state");
126  CmdArgs.push_back("--as-needed");
127  if (OnlyLibstdcxxStatic)
128  CmdArgs.push_back("-Bstatic");
129  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
130  if (OnlyLibstdcxxStatic)
131  CmdArgs.push_back("-Bdynamic");
132  CmdArgs.push_back("-lm");
133  CmdArgs.push_back("--pop-state");
134  }
135  }
136 
137  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
138 
139  if (Args.hasArg(options::OPT_pthread) ||
140  Args.hasArg(options::OPT_pthreads))
141  CmdArgs.push_back("-lpthread");
142 
143  if (Args.hasArg(options::OPT_fsplit_stack))
144  CmdArgs.push_back("--wrap=pthread_create");
145 
146  CmdArgs.push_back("-lc");
147  }
148 
149  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
150 }
151 
152 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
153 
154 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
155  const ArgList &Args)
156  : ToolChain(D, Triple, Args) {
157  getProgramPaths().push_back(getDriver().getInstalledDir());
158  if (getDriver().getInstalledDir() != D.Dir)
159  getProgramPaths().push_back(D.Dir);
160 
161  if (!D.SysRoot.empty()) {
163  llvm::sys::path::append(P, "lib");
164  getFilePaths().push_back(P.str());
165  }
166 }
167 
168 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
169  types::ID InputType) const {
170  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
171  return (Triple.getArchName() + "-" + Triple.getOSName()).str();
172 }
173 
175  return new tools::fuchsia::Linker(*this);
176 }
177 
179  const ArgList &Args) const {
180  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
181  StringRef Value = A->getValue();
182  if (Value != "compiler-rt")
183  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
184  << A->getAsString(Args);
185  }
186 
188 }
189 
191 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
192  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
193  StringRef Value = A->getValue();
194  if (Value != "libc++")
195  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
196  << A->getAsString(Args);
197  }
198 
199  return ToolChain::CST_Libcxx;
200 }
201 
202 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
203  ArgStringList &CC1Args,
204  Action::OffloadKind) const {
205  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
206  options::OPT_fno_use_init_array, true))
207  CC1Args.push_back("-fuse-init-array");
208 }
209 
210 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
211  ArgStringList &CC1Args) const {
212  const Driver &D = getDriver();
213 
214  if (DriverArgs.hasArg(options::OPT_nostdinc))
215  return;
216 
217  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
219  llvm::sys::path::append(P, "include");
220  addSystemInclude(DriverArgs, CC1Args, P);
221  }
222 
223  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
224  return;
225 
226  // Check for configure-time C include directories.
227  StringRef CIncludeDirs(C_INCLUDE_DIRS);
228  if (CIncludeDirs != "") {
230  CIncludeDirs.split(dirs, ":");
231  for (StringRef dir : dirs) {
232  StringRef Prefix =
233  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
234  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
235  }
236  return;
237  }
238 
239  if (!D.SysRoot.empty()) {
241  llvm::sys::path::append(P, "include");
242  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
243  }
244 }
245 
246 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
247  ArgStringList &CC1Args) const {
248  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
249  DriverArgs.hasArg(options::OPT_nostdincxx))
250  return;
251 
252  switch (GetCXXStdlibType(DriverArgs)) {
253  case ToolChain::CST_Libcxx: {
254  SmallString<128> P(getDriver().ResourceDir);
255  llvm::sys::path::append(P, "include", "c++", "v1");
256  addSystemInclude(DriverArgs, CC1Args, P.str());
257  break;
258  }
259 
260  default:
261  llvm_unreachable("invalid stdlib name");
262  }
263 }
264 
265 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
266  ArgStringList &CmdArgs) const {
267  switch (GetCXXStdlibType(Args)) {
269  CmdArgs.push_back("-lc++");
270  break;
271 
273  llvm_unreachable("invalid stdlib name");
274  }
275 }
276 
279  Res |= SanitizerKind::Address;
280  Res |= SanitizerKind::Fuzzer;
281  Res |= SanitizerKind::FuzzerNoLink;
282  Res |= SanitizerKind::SafeStack;
283  Res |= SanitizerKind::Scudo;
284  return Res;
285 }
286 
288  return SanitizerKind::SafeStack;
289 }
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:713
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:516
StringRef P
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: Fuchsia.cpp:287
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:151
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:109
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:246
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:653
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:435
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:225
void addSanitizerPathLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:120
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:427
path_list & getFilePaths()
Definition: ToolChain.h:222
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
return Out str()
Tool * buildLinker() const override
Definition: Fuchsia.cpp:174
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:178
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:265
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:277
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:774
const Driver & getDriver() const
Definition: ToolChain.h:184
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:210
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:173
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:698
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:751
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:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:116
uint64_t SanitizerMask
Definition: Sanitizers.h:26
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:814
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:168
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:202
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:191
Fuchsia(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
Definition: Fuchsia.cpp:154
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:87
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:132