clang  7.0.0svn
CrossWindows.cpp
Go to the documentation of this file.
1 //===--- CrossWindowsToolChain.cpp - Cross Windows Tool Chain -------------===//
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 "CrossWindows.h"
11 #include "CommonArgs.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/Path.h"
18 
19 using namespace clang::driver;
20 using namespace clang::driver::toolchains;
21 
22 using llvm::opt::ArgList;
23 
25  Compilation &C, const JobAction &JA, const InputInfo &Output,
26  const InputInfoList &Inputs, const ArgList &Args,
27  const char *LinkingOutput) const {
28  claimNoWarnArgs(Args);
29  const auto &TC =
30  static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
31  ArgStringList CmdArgs;
32  const char *Exec;
33 
34  switch (TC.getArch()) {
35  default:
36  llvm_unreachable("unsupported architecture");
37  case llvm::Triple::arm:
38  case llvm::Triple::thumb:
39  case llvm::Triple::aarch64:
40  break;
41  case llvm::Triple::x86:
42  CmdArgs.push_back("--32");
43  break;
44  case llvm::Triple::x86_64:
45  CmdArgs.push_back("--64");
46  break;
47  }
48 
49  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
50 
51  CmdArgs.push_back("-o");
52  CmdArgs.push_back(Output.getFilename());
53 
54  for (const auto &Input : Inputs)
55  CmdArgs.push_back(Input.getFilename());
56 
57  const std::string Assembler = TC.GetProgramPath("as");
58  Exec = Args.MakeArgString(Assembler);
59 
60  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
61 }
62 
64  Compilation &C, const JobAction &JA, const InputInfo &Output,
65  const InputInfoList &Inputs, const ArgList &Args,
66  const char *LinkingOutput) const {
67  const auto &TC =
68  static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
69  const llvm::Triple &T = TC.getTriple();
70  const Driver &D = TC.getDriver();
71  SmallString<128> EntryPoint;
72  ArgStringList CmdArgs;
73  const char *Exec;
74 
75  // Silence warning for "clang -g foo.o -o foo"
76  Args.ClaimAllArgs(options::OPT_g_Group);
77  // and "clang -emit-llvm foo.o -o foo"
78  Args.ClaimAllArgs(options::OPT_emit_llvm);
79  // and for "clang -w foo.o -o foo"
80  Args.ClaimAllArgs(options::OPT_w);
81  // Other warning options are already handled somewhere else.
82 
83  if (!D.SysRoot.empty())
84  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
85 
86  if (Args.hasArg(options::OPT_pie))
87  CmdArgs.push_back("-pie");
88  if (Args.hasArg(options::OPT_rdynamic))
89  CmdArgs.push_back("-export-dynamic");
90  if (Args.hasArg(options::OPT_s))
91  CmdArgs.push_back("--strip-all");
92 
93  CmdArgs.push_back("-m");
94  switch (TC.getArch()) {
95  default:
96  llvm_unreachable("unsupported architecture");
97  case llvm::Triple::arm:
98  case llvm::Triple::thumb:
99  // FIXME: this is incorrect for WinCE
100  CmdArgs.push_back("thumb2pe");
101  break;
102  case llvm::Triple::aarch64:
103  CmdArgs.push_back("arm64pe");
104  break;
105  case llvm::Triple::x86:
106  CmdArgs.push_back("i386pe");
107  EntryPoint.append("_");
108  break;
109  case llvm::Triple::x86_64:
110  CmdArgs.push_back("i386pep");
111  break;
112  }
113 
114  if (Args.hasArg(options::OPT_shared)) {
115  switch (T.getArch()) {
116  default:
117  llvm_unreachable("unsupported architecture");
118  case llvm::Triple::aarch64:
119  case llvm::Triple::arm:
120  case llvm::Triple::thumb:
121  case llvm::Triple::x86_64:
122  EntryPoint.append("_DllMainCRTStartup");
123  break;
124  case llvm::Triple::x86:
125  EntryPoint.append("_DllMainCRTStartup@12");
126  break;
127  }
128 
129  CmdArgs.push_back("-shared");
130  CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
131  : "-Bdynamic");
132 
133  CmdArgs.push_back("--enable-auto-image-base");
134 
135  CmdArgs.push_back("--entry");
136  CmdArgs.push_back(Args.MakeArgString(EntryPoint));
137  } else {
138  EntryPoint.append("mainCRTStartup");
139 
140  CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
141  : "-Bdynamic");
142 
143  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
144  CmdArgs.push_back("--entry");
145  CmdArgs.push_back(Args.MakeArgString(EntryPoint));
146  }
147 
148  // FIXME: handle subsystem
149  }
150 
151  // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
152  CmdArgs.push_back("--allow-multiple-definition");
153 
154  CmdArgs.push_back("-o");
155  CmdArgs.push_back(Output.getFilename());
156 
157  if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_rdynamic)) {
158  SmallString<261> ImpLib(Output.getFilename());
159  llvm::sys::path::replace_extension(ImpLib, ".lib");
160 
161  CmdArgs.push_back("--out-implib");
162  CmdArgs.push_back(Args.MakeArgString(ImpLib));
163  }
164 
165  Args.AddAllArgs(CmdArgs, options::OPT_L);
166  TC.AddFilePathLibArgs(Args, CmdArgs);
167  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
168 
169  if (TC.ShouldLinkCXXStdlib(Args)) {
170  bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
171  !Args.hasArg(options::OPT_static);
172  if (StaticCXX)
173  CmdArgs.push_back("-Bstatic");
174  TC.AddCXXStdlibLibArgs(Args, CmdArgs);
175  if (StaticCXX)
176  CmdArgs.push_back("-Bdynamic");
177  }
178 
179  if (!Args.hasArg(options::OPT_nostdlib)) {
180  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
181  // TODO handle /MT[d] /MD[d]
182  CmdArgs.push_back("-lmsvcrt");
183  AddRunTimeLibs(TC, D, CmdArgs, Args);
184  }
185  }
186 
187  if (TC.getSanitizerArgs().needsAsanRt()) {
188  // TODO handle /MT[d] /MD[d]
189  if (Args.hasArg(options::OPT_shared)) {
190  CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
191  } else {
192  for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
193  CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
194  // Make sure the dynamic runtime thunk is not optimized out at link time
195  // to ensure proper SEH handling.
196  CmdArgs.push_back(Args.MakeArgString("--undefined"));
197  CmdArgs.push_back(Args.MakeArgString(TC.getArch() == llvm::Triple::x86
198  ? "___asan_seh_interceptor"
199  : "__asan_seh_interceptor"));
200  }
201  }
202 
203  Exec = Args.MakeArgString(TC.GetLinkerPath());
204 
205  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
206 }
207 
209  const llvm::Triple &T,
210  const llvm::opt::ArgList &Args)
211  : Generic_GCC(D, T, Args) {}
212 
213 bool CrossWindowsToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
214  // FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
215  // not know how to emit them.
216  return getArch() == llvm::Triple::x86_64;
217 }
218 
220  return getArch() == llvm::Triple::x86_64;
221 }
222 
224  return getArch() == llvm::Triple::x86_64;
225 }
226 
228  return getArch() == llvm::Triple::x86_64;
229 }
230 
232 AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
233  llvm::opt::ArgStringList &CC1Args) const {
234  const Driver &D = getDriver();
235  const std::string &SysRoot = D.SysRoot;
236 
237  auto AddSystemAfterIncludes = [&]() {
238  for (const auto &P : DriverArgs.getAllArgValues(options::OPT_isystem_after))
239  addSystemInclude(DriverArgs, CC1Args, P);
240  };
241 
242  if (DriverArgs.hasArg(options::OPT_nostdinc)) {
243  AddSystemAfterIncludes();
244  return;
245  }
246 
247  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
248  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
249  SmallString<128> ResourceDir(D.ResourceDir);
250  llvm::sys::path::append(ResourceDir, "include");
251  addSystemInclude(DriverArgs, CC1Args, ResourceDir);
252  }
253  AddSystemAfterIncludes();
254  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
255 }
256 
258 AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
259  llvm::opt::ArgStringList &CC1Args) const {
260  const std::string &SysRoot = getDriver().SysRoot;
261 
262  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
263  DriverArgs.hasArg(options::OPT_nostdincxx))
264  return;
265 
266  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
267  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/c++/v1");
268 }
269 
271 AddCXXStdlibLibArgs(const llvm::opt::ArgList &DriverArgs,
272  llvm::opt::ArgStringList &CC1Args) const {
273  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
274  CC1Args.push_back("-lc++");
275 }
276 
279  Res |= SanitizerKind::Address;
280  return Res;
281 }
282 
284  return new tools::CrossWindows::Linker(*this);
285 }
286 
288  return new tools::CrossWindows::Assembler(*this);
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:689
Generic_GCC - A tool chain using the &#39;gcc&#39; command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.h:140
StringRef P
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
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...
const char * getFilename() const
Definition: InputInfo.h:84
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
CrossWindowsToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
const FunctionProtoType * T
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:197
const Driver & getDriver() const
Definition: ToolChain.h:181
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
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
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
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
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:786
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:655
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
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...
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:133