/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/MinGW.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- MinGW.cpp - MinGWToolChain Implementation ------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "MinGW.h" |
10 | | #include "CommonArgs.h" |
11 | | #include "clang/Config/config.h" |
12 | | #include "clang/Driver/Compilation.h" |
13 | | #include "clang/Driver/Driver.h" |
14 | | #include "clang/Driver/DriverDiagnostic.h" |
15 | | #include "clang/Driver/InputInfo.h" |
16 | | #include "clang/Driver/Options.h" |
17 | | #include "clang/Driver/SanitizerArgs.h" |
18 | | #include "llvm/Option/ArgList.h" |
19 | | #include "llvm/Support/FileSystem.h" |
20 | | #include "llvm/Support/Path.h" |
21 | | #include "llvm/Support/VirtualFileSystem.h" |
22 | | #include <system_error> |
23 | | |
24 | | using namespace clang::diag; |
25 | | using namespace clang::driver; |
26 | | using namespace clang; |
27 | | using namespace llvm::opt; |
28 | | |
29 | | /// MinGW Tools |
30 | | void tools::MinGW::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
31 | | const InputInfo &Output, |
32 | | const InputInfoList &Inputs, |
33 | | const ArgList &Args, |
34 | 1 | const char *LinkingOutput) const { |
35 | 1 | claimNoWarnArgs(Args); |
36 | 1 | ArgStringList CmdArgs; |
37 | | |
38 | 1 | if (getToolChain().getArch() == llvm::Triple::x86) { |
39 | 1 | CmdArgs.push_back("--32"); |
40 | 1 | } else if (0 getToolChain().getArch() == llvm::Triple::x86_640 ) { |
41 | 0 | CmdArgs.push_back("--64"); |
42 | 0 | } |
43 | | |
44 | 1 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); |
45 | | |
46 | 1 | CmdArgs.push_back("-o"); |
47 | 1 | CmdArgs.push_back(Output.getFilename()); |
48 | | |
49 | 1 | for (const auto &II : Inputs) |
50 | 1 | CmdArgs.push_back(II.getFilename()); |
51 | | |
52 | 1 | const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); |
53 | 1 | C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), |
54 | 1 | Exec, CmdArgs, Inputs, Output)); |
55 | | |
56 | 1 | if (Args.hasArg(options::OPT_gsplit_dwarf)) |
57 | 0 | SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, |
58 | 0 | SplitDebugName(JA, Args, Inputs[0], Output)); |
59 | 1 | } |
60 | | |
61 | | void tools::MinGW::Linker::AddLibGCC(const ArgList &Args, |
62 | 104 | ArgStringList &CmdArgs) const { |
63 | 104 | if (Args.hasArg(options::OPT_mthreads)) |
64 | 0 | CmdArgs.push_back("-lmingwthrd"); |
65 | 104 | CmdArgs.push_back("-lmingw32"); |
66 | | |
67 | | // Make use of compiler-rt if --rtlib option is used |
68 | 104 | ToolChain::RuntimeLibType RLT = getToolChain().GetRuntimeLibType(Args); |
69 | 104 | if (RLT == ToolChain::RLT_Libgcc) { |
70 | 96 | bool Static = Args.hasArg(options::OPT_static_libgcc) || |
71 | 96 | Args.hasArg(options::OPT_static)88 ; |
72 | 96 | bool Shared = Args.hasArg(options::OPT_shared); |
73 | 96 | bool CXX = getToolChain().getDriver().CCCIsCXX(); |
74 | | |
75 | 96 | if (Static || (84 !CXX84 && !Shared80 )) { |
76 | 90 | CmdArgs.push_back("-lgcc"); |
77 | 90 | CmdArgs.push_back("-lgcc_eh"); |
78 | 90 | } else { |
79 | 6 | CmdArgs.push_back("-lgcc_s"); |
80 | 6 | CmdArgs.push_back("-lgcc"); |
81 | 6 | } |
82 | 96 | } else { |
83 | 8 | AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args); |
84 | 8 | } |
85 | | |
86 | 104 | CmdArgs.push_back("-lmoldname"); |
87 | 104 | CmdArgs.push_back("-lmingwex"); |
88 | 104 | for (auto Lib : Args.getAllArgValues(options::OPT_l)) |
89 | 14 | if (StringRef(Lib).startswith("msvcr") || |
90 | 14 | StringRef(Lib).startswith("ucrt")12 || |
91 | 14 | StringRef(Lib).startswith("crtdll")8 ) |
92 | 8 | return; |
93 | 96 | CmdArgs.push_back("-lmsvcrt"); |
94 | 96 | } |
95 | | |
96 | | void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
97 | | const InputInfo &Output, |
98 | | const InputInfoList &Inputs, |
99 | | const ArgList &Args, |
100 | 57 | const char *LinkingOutput) const { |
101 | 57 | const ToolChain &TC = getToolChain(); |
102 | 57 | const Driver &D = TC.getDriver(); |
103 | 57 | const SanitizerArgs &Sanitize = TC.getSanitizerArgs(Args); |
104 | | |
105 | 57 | ArgStringList CmdArgs; |
106 | | |
107 | | // Silence warning for "clang -g foo.o -o foo" |
108 | 57 | Args.ClaimAllArgs(options::OPT_g_Group); |
109 | | // and "clang -emit-llvm foo.o -o foo" |
110 | 57 | Args.ClaimAllArgs(options::OPT_emit_llvm); |
111 | | // and for "clang -w foo.o -o foo". Other warning options are already |
112 | | // handled somewhere else. |
113 | 57 | Args.ClaimAllArgs(options::OPT_w); |
114 | | |
115 | 57 | if (!D.SysRoot.empty()) |
116 | 3 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
117 | | |
118 | 57 | if (Args.hasArg(options::OPT_s)) |
119 | 0 | CmdArgs.push_back("-s"); |
120 | | |
121 | 57 | CmdArgs.push_back("-m"); |
122 | 57 | switch (TC.getArch()) { |
123 | 30 | case llvm::Triple::x86: |
124 | 30 | CmdArgs.push_back("i386pe"); |
125 | 30 | break; |
126 | 26 | case llvm::Triple::x86_64: |
127 | 26 | CmdArgs.push_back("i386pep"); |
128 | 26 | break; |
129 | 0 | case llvm::Triple::arm: |
130 | 0 | case llvm::Triple::thumb: |
131 | | // FIXME: this is incorrect for WinCE |
132 | 0 | CmdArgs.push_back("thumb2pe"); |
133 | 0 | break; |
134 | 0 | case llvm::Triple::aarch64: |
135 | 0 | CmdArgs.push_back("arm64pe"); |
136 | 0 | break; |
137 | 1 | default: |
138 | 1 | D.Diag(diag::err_target_unknown_triple) << TC.getEffectiveTriple().str(); |
139 | 57 | } |
140 | | |
141 | 57 | Arg *SubsysArg = |
142 | 57 | Args.getLastArg(options::OPT_mwindows, options::OPT_mconsole); |
143 | 57 | if (SubsysArg && SubsysArg->getOption().matches(options::OPT_mwindows)2 ) { |
144 | 1 | CmdArgs.push_back("--subsystem"); |
145 | 1 | CmdArgs.push_back("windows"); |
146 | 56 | } else if (SubsysArg && |
147 | 56 | SubsysArg->getOption().matches(options::OPT_mconsole)1 ) { |
148 | 1 | CmdArgs.push_back("--subsystem"); |
149 | 1 | CmdArgs.push_back("console"); |
150 | 1 | } |
151 | | |
152 | 57 | if (Args.hasArg(options::OPT_mdll)) |
153 | 0 | CmdArgs.push_back("--dll"); |
154 | 57 | else if (Args.hasArg(options::OPT_shared)) |
155 | 6 | CmdArgs.push_back("--shared"); |
156 | 57 | if (Args.hasArg(options::OPT_static)) |
157 | 4 | CmdArgs.push_back("-Bstatic"); |
158 | 53 | else |
159 | 53 | CmdArgs.push_back("-Bdynamic"); |
160 | 57 | if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) { |
161 | 6 | CmdArgs.push_back("-e"); |
162 | 6 | if (TC.getArch() == llvm::Triple::x86) |
163 | 6 | CmdArgs.push_back("_DllMainCRTStartup@12"); |
164 | 0 | else |
165 | 0 | CmdArgs.push_back("DllMainCRTStartup"); |
166 | 6 | CmdArgs.push_back("--enable-auto-image-base"); |
167 | 6 | } |
168 | | |
169 | 57 | if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) |
170 | 1 | CmdArgs.push_back("--no-demangle"); |
171 | | |
172 | 57 | if (!Args.hasFlag(options::OPT_fauto_import, options::OPT_fno_auto_import, |
173 | 57 | true)) |
174 | 1 | CmdArgs.push_back("--disable-auto-import"); |
175 | | |
176 | 57 | if (Arg *A = Args.getLastArg(options::OPT_mguard_EQ)) { |
177 | 4 | StringRef GuardArgs = A->getValue(); |
178 | 4 | if (GuardArgs == "none") |
179 | 1 | CmdArgs.push_back("--no-guard-cf"); |
180 | 3 | else if (GuardArgs == "cf" || GuardArgs == "cf-nochecks"2 ) |
181 | 2 | CmdArgs.push_back("--guard-cf"); |
182 | 1 | else |
183 | 1 | D.Diag(diag::err_drv_unsupported_option_argument) |
184 | 1 | << A->getSpelling() << GuardArgs; |
185 | 4 | } |
186 | | |
187 | 57 | CmdArgs.push_back("-o"); |
188 | 57 | const char *OutputFile = Output.getFilename(); |
189 | | // GCC implicitly adds an .exe extension if it is given an output file name |
190 | | // that lacks an extension. |
191 | | // GCC used to do this only when the compiler itself runs on windows, but |
192 | | // since GCC 8 it does the same when cross compiling as well. |
193 | 57 | if (!llvm::sys::path::has_extension(OutputFile)) { |
194 | 1 | CmdArgs.push_back(Args.MakeArgString(Twine(OutputFile) + ".exe")); |
195 | 1 | OutputFile = CmdArgs.back(); |
196 | 1 | } else |
197 | 56 | CmdArgs.push_back(OutputFile); |
198 | | |
199 | | // FIXME: add -N, -n flags |
200 | 57 | Args.AddLastArg(CmdArgs, options::OPT_r); |
201 | 57 | Args.AddLastArg(CmdArgs, options::OPT_s); |
202 | 57 | Args.AddLastArg(CmdArgs, options::OPT_t); |
203 | 57 | Args.AddAllArgs(CmdArgs, options::OPT_u_Group); |
204 | 57 | Args.AddLastArg(CmdArgs, options::OPT_Z_Flag); |
205 | | |
206 | | // Add asan_dynamic as the first import lib before other libs. This allows |
207 | | // asan to be initialized as early as possible to increase its instrumentation |
208 | | // coverage to include other user DLLs which has not been built with asan. |
209 | 57 | if (Sanitize.needsAsanRt() && !Args.hasArg(options::OPT_nostdlib)2 && |
210 | 57 | !Args.hasArg(options::OPT_nodefaultlibs)2 ) { |
211 | | // MinGW always links against a shared MSVCRT. |
212 | 2 | CmdArgs.push_back( |
213 | 2 | TC.getCompilerRTArgString(Args, "asan_dynamic", ToolChain::FT_Shared)); |
214 | 2 | } |
215 | | |
216 | 57 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
217 | 54 | if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)48 ) { |
218 | 6 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("dllcrt2.o"))); |
219 | 48 | } else { |
220 | 48 | if (Args.hasArg(options::OPT_municode)) |
221 | 0 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2u.o"))); |
222 | 48 | else |
223 | 48 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt2.o"))); |
224 | 48 | } |
225 | 54 | if (Args.hasArg(options::OPT_pg)) |
226 | 0 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("gcrt2.o"))); |
227 | 54 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o"))); |
228 | 54 | } |
229 | | |
230 | 57 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
231 | 57 | TC.AddFilePathLibArgs(Args, CmdArgs); |
232 | | |
233 | | // Add the compiler-rt library directories if they exist to help |
234 | | // the linker find the various sanitizer, builtin, and profiling runtimes. |
235 | 57 | for (const auto &LibPath : TC.getLibraryPaths()) { |
236 | 0 | if (TC.getVFS().exists(LibPath)) |
237 | 0 | CmdArgs.push_back(Args.MakeArgString("-L" + LibPath)); |
238 | 0 | } |
239 | 57 | auto CRTPath = TC.getCompilerRTPath(); |
240 | 57 | if (TC.getVFS().exists(CRTPath)) |
241 | 1 | CmdArgs.push_back(Args.MakeArgString("-L" + CRTPath)); |
242 | | |
243 | 57 | AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
244 | | |
245 | 57 | if (D.isUsingLTO()) { |
246 | 2 | assert(!Inputs.empty() && "Must have at least one input."); |
247 | 2 | addLTOOptions(TC, Args, CmdArgs, Output, Inputs[0], |
248 | 2 | D.getLTOMode() == LTOK_Thin); |
249 | 2 | } |
250 | | |
251 | 57 | if (C.getDriver().IsFlangMode()) { |
252 | 0 | addFortranRuntimeLibraryPath(TC, Args, CmdArgs); |
253 | 0 | addFortranRuntimeLibs(TC, CmdArgs); |
254 | 0 | } |
255 | | |
256 | | // TODO: Add profile stuff here |
257 | | |
258 | 57 | if (TC.ShouldLinkCXXStdlib(Args)) { |
259 | 7 | bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && |
260 | 7 | !Args.hasArg(options::OPT_static)0 ; |
261 | 7 | if (OnlyLibstdcxxStatic) |
262 | 0 | CmdArgs.push_back("-Bstatic"); |
263 | 7 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
264 | 7 | if (OnlyLibstdcxxStatic) |
265 | 0 | CmdArgs.push_back("-Bdynamic"); |
266 | 7 | } |
267 | | |
268 | 57 | bool HasWindowsApp = false; |
269 | 57 | for (auto Lib : Args.getAllArgValues(options::OPT_l)) { |
270 | 7 | if (Lib == "windowsapp") { |
271 | 1 | HasWindowsApp = true; |
272 | 1 | break; |
273 | 1 | } |
274 | 7 | } |
275 | | |
276 | 57 | if (!Args.hasArg(options::OPT_nostdlib)) { |
277 | 54 | if (!Args.hasArg(options::OPT_nodefaultlibs)) { |
278 | 54 | if (Args.hasArg(options::OPT_static)) |
279 | 4 | CmdArgs.push_back("--start-group"); |
280 | | |
281 | 54 | if (Args.hasArg(options::OPT_fstack_protector) || |
282 | 54 | Args.hasArg(options::OPT_fstack_protector_strong) || |
283 | 54 | Args.hasArg(options::OPT_fstack_protector_all)) { |
284 | 0 | CmdArgs.push_back("-lssp_nonshared"); |
285 | 0 | CmdArgs.push_back("-lssp"); |
286 | 0 | } |
287 | | |
288 | 54 | if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
289 | 54 | options::OPT_fno_openmp, false)) { |
290 | 4 | switch (TC.getDriver().getOpenMPRuntime(Args)) { |
291 | 2 | case Driver::OMPRT_OMP: |
292 | 2 | CmdArgs.push_back("-lomp"); |
293 | 2 | break; |
294 | 1 | case Driver::OMPRT_IOMP5: |
295 | 1 | CmdArgs.push_back("-liomp5md"); |
296 | 1 | break; |
297 | 1 | case Driver::OMPRT_GOMP: |
298 | 1 | CmdArgs.push_back("-lgomp"); |
299 | 1 | break; |
300 | 0 | case Driver::OMPRT_Unknown: |
301 | | // Already diagnosed. |
302 | 0 | break; |
303 | 4 | } |
304 | 4 | } |
305 | | |
306 | 54 | AddLibGCC(Args, CmdArgs); |
307 | | |
308 | 54 | if (Args.hasArg(options::OPT_pg)) |
309 | 0 | CmdArgs.push_back("-lgmon"); |
310 | | |
311 | 54 | if (Args.hasArg(options::OPT_pthread)) |
312 | 0 | CmdArgs.push_back("-lpthread"); |
313 | | |
314 | 54 | if (Sanitize.needsAsanRt()) { |
315 | | // MinGW always links against a shared MSVCRT. |
316 | 2 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dynamic", |
317 | 2 | ToolChain::FT_Shared)); |
318 | 2 | CmdArgs.push_back( |
319 | 2 | TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk")); |
320 | 2 | CmdArgs.push_back("--require-defined"); |
321 | 2 | CmdArgs.push_back(TC.getArch() == llvm::Triple::x86 |
322 | 2 | ? "___asan_seh_interceptor"1 |
323 | 2 | : "__asan_seh_interceptor"1 ); |
324 | | // Make sure the linker consider all object files from the dynamic |
325 | | // runtime thunk. |
326 | 2 | CmdArgs.push_back("--whole-archive"); |
327 | 2 | CmdArgs.push_back( |
328 | 2 | TC.getCompilerRTArgString(Args, "asan_dynamic_runtime_thunk")); |
329 | 2 | CmdArgs.push_back("--no-whole-archive"); |
330 | 2 | } |
331 | | |
332 | 54 | TC.addProfileRTLibs(Args, CmdArgs); |
333 | | |
334 | 54 | if (!HasWindowsApp) { |
335 | | // Add system libraries. If linking to libwindowsapp.a, that import |
336 | | // library replaces all these and we shouldn't accidentally try to |
337 | | // link to the normal desktop mode dlls. |
338 | 53 | if (Args.hasArg(options::OPT_mwindows)) { |
339 | 2 | CmdArgs.push_back("-lgdi32"); |
340 | 2 | CmdArgs.push_back("-lcomdlg32"); |
341 | 2 | } |
342 | 53 | CmdArgs.push_back("-ladvapi32"); |
343 | 53 | CmdArgs.push_back("-lshell32"); |
344 | 53 | CmdArgs.push_back("-luser32"); |
345 | 53 | CmdArgs.push_back("-lkernel32"); |
346 | 53 | } |
347 | | |
348 | 54 | if (Args.hasArg(options::OPT_static)) { |
349 | 4 | CmdArgs.push_back("--end-group"); |
350 | 50 | } else { |
351 | 50 | AddLibGCC(Args, CmdArgs); |
352 | 50 | if (!HasWindowsApp) |
353 | 49 | CmdArgs.push_back("-lkernel32"); |
354 | 50 | } |
355 | 54 | } |
356 | | |
357 | 54 | if (!Args.hasArg(options::OPT_nostartfiles)) { |
358 | | // Add crtfastmath.o if available and fast math is enabled. |
359 | 54 | TC.addFastMathRuntimeIfAvailable(Args, CmdArgs); |
360 | | |
361 | 54 | CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o"))); |
362 | 54 | } |
363 | 54 | } |
364 | 57 | const char *Exec = Args.MakeArgString(TC.GetLinkerPath()); |
365 | 57 | C.addCommand(std::make_unique<Command>(JA, *this, |
366 | 57 | ResponseFileSupport::AtFileUTF8(), |
367 | 57 | Exec, CmdArgs, Inputs, Output)); |
368 | 57 | } |
369 | | |
370 | 228 | static bool isCrossCompiling(const llvm::Triple &T, bool RequireArchMatch) { |
371 | 228 | llvm::Triple HostTriple(llvm::Triple::normalize(LLVM_HOST_TRIPLE)); |
372 | 228 | if (HostTriple.getOS() != llvm::Triple::Win32) |
373 | 228 | return true; |
374 | 0 | if (RequireArchMatch && HostTriple.getArch() != T.getArch()) |
375 | 0 | return true; |
376 | 0 | return false; |
377 | 0 | } |
378 | | |
379 | | // Simplified from Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple. |
380 | | static bool findGccVersion(StringRef LibDir, std::string &GccLibDir, |
381 | | std::string &Ver, |
382 | 1.06k | toolchains::Generic_GCC::GCCVersion &Version) { |
383 | 1.06k | Version = toolchains::Generic_GCC::GCCVersion::Parse("0.0.0"); |
384 | 1.06k | std::error_code EC; |
385 | 1.08k | for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE26 ; |
386 | 1.06k | LI = LI.increment(EC)15 ) { |
387 | 15 | StringRef VersionText = llvm::sys::path::filename(LI->path()); |
388 | 15 | auto CandidateVersion = |
389 | 15 | toolchains::Generic_GCC::GCCVersion::Parse(VersionText); |
390 | 15 | if (CandidateVersion.Major == -1) |
391 | 0 | continue; |
392 | 15 | if (CandidateVersion <= Version) |
393 | 0 | continue; |
394 | 15 | Version = CandidateVersion; |
395 | 15 | Ver = std::string(VersionText); |
396 | 15 | GccLibDir = LI->path(); |
397 | 15 | } |
398 | 1.06k | return Ver.size(); |
399 | 1.06k | } |
400 | | |
401 | 118 | static llvm::Triple getLiteralTriple(const Driver &D, const llvm::Triple &T) { |
402 | 118 | llvm::Triple LiteralTriple(D.getTargetTriple()); |
403 | | // The arch portion of the triple may be overridden by -m32/-m64. |
404 | 118 | LiteralTriple.setArchName(T.getArchName()); |
405 | 118 | return LiteralTriple; |
406 | 118 | } |
407 | | |
408 | 114 | void toolchains::MinGW::findGccLibDir(const llvm::Triple &LiteralTriple) { |
409 | 114 | llvm::SmallVector<llvm::SmallString<32>, 5> SubdirNames; |
410 | 114 | SubdirNames.emplace_back(LiteralTriple.str()); |
411 | 114 | SubdirNames.emplace_back(getTriple().str()); |
412 | 114 | SubdirNames.emplace_back(getTriple().getArchName()); |
413 | 114 | SubdirNames.back() += "-w64-mingw32"; |
414 | 114 | SubdirNames.emplace_back(getTriple().getArchName()); |
415 | 114 | SubdirNames.back() += "-w64-mingw32ucrt"; |
416 | 114 | SubdirNames.emplace_back("mingw32"); |
417 | 114 | if (SubdirName.empty()) { |
418 | 110 | SubdirName = getTriple().getArchName(); |
419 | 110 | SubdirName += "-w64-mingw32"; |
420 | 110 | } |
421 | | // lib: Arch Linux, Ubuntu, Windows |
422 | | // lib64: openSUSE Linux |
423 | 218 | for (StringRef CandidateLib : {"lib", "lib64"}) { |
424 | 1.06k | for (StringRef CandidateSysroot : SubdirNames) { |
425 | 1.06k | llvm::SmallString<1024> LibDir(Base); |
426 | 1.06k | llvm::sys::path::append(LibDir, CandidateLib, "gcc", CandidateSysroot); |
427 | 1.06k | if (findGccVersion(LibDir, GccLibDir, Ver, GccVer)) { |
428 | 11 | SubdirName = std::string(CandidateSysroot); |
429 | 11 | return; |
430 | 11 | } |
431 | 1.06k | } |
432 | 218 | } |
433 | 114 | } |
434 | | |
435 | | static llvm::ErrorOr<std::string> findGcc(const llvm::Triple &LiteralTriple, |
436 | 99 | const llvm::Triple &T) { |
437 | 99 | llvm::SmallVector<llvm::SmallString<32>, 5> Gccs; |
438 | 99 | Gccs.emplace_back(LiteralTriple.str()); |
439 | 99 | Gccs.back() += "-gcc"; |
440 | 99 | Gccs.emplace_back(T.str()); |
441 | 99 | Gccs.back() += "-gcc"; |
442 | 99 | Gccs.emplace_back(T.getArchName()); |
443 | 99 | Gccs.back() += "-w64-mingw32-gcc"; |
444 | 99 | Gccs.emplace_back(T.getArchName()); |
445 | 99 | Gccs.back() += "-w64-mingw32ucrt-gcc"; |
446 | 99 | Gccs.emplace_back("mingw32-gcc"); |
447 | | // Please do not add "gcc" here |
448 | 99 | for (StringRef CandidateGcc : Gccs) |
449 | 491 | if (llvm::ErrorOr<std::string> GPPName = llvm::sys::findProgramByName(CandidateGcc)) |
450 | 1 | return GPPName; |
451 | 98 | return make_error_code(std::errc::no_such_file_or_directory); |
452 | 99 | } |
453 | | |
454 | | static llvm::ErrorOr<std::string> |
455 | | findClangRelativeSysroot(const Driver &D, const llvm::Triple &LiteralTriple, |
456 | 104 | const llvm::Triple &T, std::string &SubdirName) { |
457 | 104 | llvm::SmallVector<llvm::SmallString<32>, 4> Subdirs; |
458 | 104 | Subdirs.emplace_back(LiteralTriple.str()); |
459 | 104 | Subdirs.emplace_back(T.str()); |
460 | 104 | Subdirs.emplace_back(T.getArchName()); |
461 | 104 | Subdirs.back() += "-w64-mingw32"; |
462 | 104 | Subdirs.emplace_back(T.getArchName()); |
463 | 104 | Subdirs.back() += "-w64-mingw32ucrt"; |
464 | 104 | StringRef ClangRoot = llvm::sys::path::parent_path(D.getInstalledDir()); |
465 | 104 | StringRef Sep = llvm::sys::path::get_separator(); |
466 | 401 | for (StringRef CandidateSubdir : Subdirs) { |
467 | 401 | if (llvm::sys::fs::is_directory(ClangRoot + Sep + CandidateSubdir)) { |
468 | 5 | SubdirName = std::string(CandidateSubdir); |
469 | 5 | return (ClangRoot + Sep + CandidateSubdir).str(); |
470 | 5 | } |
471 | 401 | } |
472 | 99 | return make_error_code(std::errc::no_such_file_or_directory); |
473 | 104 | } |
474 | | |
475 | | toolchains::MinGW::MinGW(const Driver &D, const llvm::Triple &Triple, |
476 | | const ArgList &Args) |
477 | | : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args), |
478 | 114 | RocmInstallation(D, Triple, Args) { |
479 | 114 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
480 | | |
481 | | // The sequence for detecting a sysroot here should be kept in sync with |
482 | | // the testTriple function below. |
483 | 114 | llvm::Triple LiteralTriple = getLiteralTriple(D, getTriple()); |
484 | 114 | if (getDriver().SysRoot.size()) |
485 | 14 | Base = getDriver().SysRoot; |
486 | | // Look for <clang-bin>/../<triplet>; if found, use <clang-bin>/.. as the |
487 | | // base as it could still be a base for a gcc setup with libgcc. |
488 | 100 | else if (llvm::ErrorOr<std::string> TargetSubdir = findClangRelativeSysroot( |
489 | 100 | getDriver(), LiteralTriple, getTriple(), SubdirName)) |
490 | 4 | Base = std::string(llvm::sys::path::parent_path(TargetSubdir.get())); |
491 | 96 | else if (llvm::ErrorOr<std::string> GPPName = |
492 | 96 | findGcc(LiteralTriple, getTriple())) |
493 | 1 | Base = std::string(llvm::sys::path::parent_path( |
494 | 1 | llvm::sys::path::parent_path(GPPName.get()))); |
495 | 95 | else |
496 | 95 | Base = std::string( |
497 | 95 | llvm::sys::path::parent_path(getDriver().getInstalledDir())); |
498 | | |
499 | 114 | Base += llvm::sys::path::get_separator(); |
500 | 114 | findGccLibDir(LiteralTriple); |
501 | 114 | TripleDirName = SubdirName; |
502 | | // GccLibDir must precede Base/lib so that the |
503 | | // correct crtbegin.o ,cetend.o would be found. |
504 | 114 | getFilePaths().push_back(GccLibDir); |
505 | | |
506 | | // openSUSE/Fedora |
507 | 114 | std::string CandidateSubdir = SubdirName + "/sys-root/mingw"; |
508 | 114 | if (getDriver().getVFS().exists(Base + CandidateSubdir)) |
509 | 2 | SubdirName = CandidateSubdir; |
510 | | |
511 | 114 | getFilePaths().push_back( |
512 | 114 | (Base + SubdirName + llvm::sys::path::get_separator() + "lib").str()); |
513 | | |
514 | | // Gentoo |
515 | 114 | getFilePaths().push_back( |
516 | 114 | (Base + SubdirName + llvm::sys::path::get_separator() + "mingw/lib").str()); |
517 | | |
518 | | // Only include <base>/lib if we're not cross compiling (not even for |
519 | | // windows->windows to a different arch), or if the sysroot has been set |
520 | | // (where we presume the user has pointed it at an arch specific |
521 | | // subdirectory). |
522 | 114 | if (!::isCrossCompiling(getTriple(), /*RequireArchMatch=*/true) || |
523 | 114 | getDriver().SysRoot.size()) |
524 | 14 | getFilePaths().push_back(Base + "lib"); |
525 | | |
526 | 114 | NativeLLVMSupport = |
527 | 114 | Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER) |
528 | 114 | .equals_insensitive("lld"); |
529 | 114 | } |
530 | | |
531 | 58 | Tool *toolchains::MinGW::getTool(Action::ActionClass AC) const { |
532 | 58 | switch (AC) { |
533 | 0 | case Action::PreprocessJobClass: |
534 | 0 | if (!Preprocessor) |
535 | 0 | Preprocessor.reset(new tools::gcc::Preprocessor(*this)); |
536 | 0 | return Preprocessor.get(); |
537 | 0 | case Action::CompileJobClass: |
538 | 0 | if (!Compiler) |
539 | 0 | Compiler.reset(new tools::gcc::Compiler(*this)); |
540 | 0 | return Compiler.get(); |
541 | 58 | default: |
542 | 58 | return ToolChain::getTool(AC); |
543 | 58 | } |
544 | 58 | } |
545 | | |
546 | 1 | Tool *toolchains::MinGW::buildAssembler() const { |
547 | 1 | return new tools::MinGW::Assembler(*this); |
548 | 1 | } |
549 | | |
550 | 57 | Tool *toolchains::MinGW::buildLinker() const { |
551 | 57 | return new tools::MinGW::Linker(*this); |
552 | 57 | } |
553 | | |
554 | 66 | bool toolchains::MinGW::HasNativeLLVMSupport() const { |
555 | 66 | return NativeLLVMSupport; |
556 | 66 | } |
557 | | |
558 | | ToolChain::UnwindTableLevel |
559 | 228 | toolchains::MinGW::getDefaultUnwindTableLevel(const ArgList &Args) const { |
560 | 228 | Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions, |
561 | 228 | options::OPT_fseh_exceptions, |
562 | 228 | options::OPT_fdwarf_exceptions); |
563 | 228 | if (ExceptionArg && |
564 | 228 | ExceptionArg->getOption().matches(options::OPT_fseh_exceptions)16 ) |
565 | 2 | return UnwindTableLevel::Asynchronous; |
566 | | |
567 | 226 | if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm122 || |
568 | 226 | getArch() == llvm::Triple::thumb110 || getArch() == llvm::Triple::aarch64110 ) |
569 | 128 | return UnwindTableLevel::Asynchronous; |
570 | 98 | return UnwindTableLevel::None; |
571 | 226 | } |
572 | | |
573 | 114 | bool toolchains::MinGW::isPICDefault() const { |
574 | 114 | return getArch() == llvm::Triple::x86_64 || |
575 | 114 | getArch() == llvm::Triple::aarch6461 ; |
576 | 114 | } |
577 | | |
578 | 114 | bool toolchains::MinGW::isPIEDefault(const llvm::opt::ArgList &Args) const { |
579 | 114 | return false; |
580 | 114 | } |
581 | | |
582 | 114 | bool toolchains::MinGW::isPICDefaultForced() const { return true; } |
583 | | |
584 | | llvm::ExceptionHandling |
585 | 106 | toolchains::MinGW::GetExceptionModel(const ArgList &Args) const { |
586 | 106 | if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch6458 || |
587 | 106 | getArch() == llvm::Triple::arm53 || getArch() == llvm::Triple::thumb48 ) |
588 | 58 | return llvm::ExceptionHandling::WinEH; |
589 | 48 | return llvm::ExceptionHandling::DwarfCFI; |
590 | 106 | } |
591 | | |
592 | 171 | SanitizerMask toolchains::MinGW::getSupportedSanitizers() const { |
593 | 171 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
594 | 171 | Res |= SanitizerKind::Address; |
595 | 171 | Res |= SanitizerKind::PointerCompare; |
596 | 171 | Res |= SanitizerKind::PointerSubtract; |
597 | 171 | Res |= SanitizerKind::Vptr; |
598 | 171 | return Res; |
599 | 171 | } |
600 | | |
601 | | void toolchains::MinGW::AddCudaIncludeArgs(const ArgList &DriverArgs, |
602 | 0 | ArgStringList &CC1Args) const { |
603 | 0 | CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); |
604 | 0 | } |
605 | | |
606 | | void toolchains::MinGW::AddHIPIncludeArgs(const ArgList &DriverArgs, |
607 | 0 | ArgStringList &CC1Args) const { |
608 | 0 | RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args); |
609 | 0 | } |
610 | | |
611 | 19 | void toolchains::MinGW::printVerboseInfo(raw_ostream &OS) const { |
612 | 19 | CudaInstallation.print(OS); |
613 | 19 | RocmInstallation.print(OS); |
614 | 19 | } |
615 | | |
616 | | // Include directories for various hosts: |
617 | | |
618 | | // Windows, mingw.org |
619 | | // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++ |
620 | | // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32 |
621 | | // c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward |
622 | | // c:\mingw\include |
623 | | // c:\mingw\mingw32\include |
624 | | |
625 | | // Windows, mingw-w64 mingw-builds |
626 | | // c:\mingw32\i686-w64-mingw32\include |
627 | | // c:\mingw32\i686-w64-mingw32\include\c++ |
628 | | // c:\mingw32\i686-w64-mingw32\include\c++\i686-w64-mingw32 |
629 | | // c:\mingw32\i686-w64-mingw32\include\c++\backward |
630 | | |
631 | | // Windows, mingw-w64 msys2 |
632 | | // c:\msys64\mingw32\include |
633 | | // c:\msys64\mingw32\i686-w64-mingw32\include |
634 | | // c:\msys64\mingw32\include\c++\4.9.2 |
635 | | // c:\msys64\mingw32\include\c++\4.9.2\i686-w64-mingw32 |
636 | | // c:\msys64\mingw32\include\c++\4.9.2\backward |
637 | | |
638 | | // openSUSE |
639 | | // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++ |
640 | | // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32 |
641 | | // /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward |
642 | | // /usr/x86_64-w64-mingw32/sys-root/mingw/include |
643 | | |
644 | | // Arch Linux |
645 | | // /usr/i686-w64-mingw32/include/c++/5.1.0 |
646 | | // /usr/i686-w64-mingw32/include/c++/5.1.0/i686-w64-mingw32 |
647 | | // /usr/i686-w64-mingw32/include/c++/5.1.0/backward |
648 | | // /usr/i686-w64-mingw32/include |
649 | | |
650 | | // Ubuntu |
651 | | // /usr/include/c++/4.8 |
652 | | // /usr/include/c++/4.8/x86_64-w64-mingw32 |
653 | | // /usr/include/c++/4.8/backward |
654 | | // /usr/x86_64-w64-mingw32/include |
655 | | |
656 | | // Fedora |
657 | | // /usr/x86_64-w64-mingw32ucrt/sys-root/mingw/include/c++/x86_64-w64-mingw32ucrt |
658 | | // /usr/x86_64-w64-mingw32ucrt/sys-root/mingw/include/c++/backward |
659 | | // /usr/x86_64-w64-mingw32ucrt/sys-root/mingw/include |
660 | | // /usr/lib/gcc/x86_64-w64-mingw32ucrt/12.2.1/include-fixed |
661 | | |
662 | | void toolchains::MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
663 | 114 | ArgStringList &CC1Args) const { |
664 | 114 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
665 | 0 | return; |
666 | | |
667 | 114 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
668 | 114 | SmallString<1024> P(getDriver().ResourceDir); |
669 | 114 | llvm::sys::path::append(P, "include"); |
670 | 114 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
671 | 114 | } |
672 | | |
673 | 114 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
674 | 0 | return; |
675 | | |
676 | 114 | addSystemInclude(DriverArgs, CC1Args, |
677 | 114 | Base + SubdirName + llvm::sys::path::get_separator() + |
678 | 114 | "include"); |
679 | | |
680 | | // Gentoo |
681 | 114 | addSystemInclude(DriverArgs, CC1Args, |
682 | 114 | Base + SubdirName + llvm::sys::path::get_separator() + "usr/include"); |
683 | | |
684 | | // Only include <base>/include if we're not cross compiling (but do allow it |
685 | | // if we're on Windows and building for Windows on another architecture), |
686 | | // or if the sysroot has been set (where we presume the user has pointed it |
687 | | // at an arch specific subdirectory). |
688 | 114 | if (!::isCrossCompiling(getTriple(), /*RequireArchMatch=*/false) || |
689 | 114 | getDriver().SysRoot.size()) |
690 | 14 | addSystemInclude(DriverArgs, CC1Args, Base + "include"); |
691 | 114 | } |
692 | | |
693 | | void toolchains::MinGW::addClangTargetOptions( |
694 | | const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, |
695 | 114 | Action::OffloadKind DeviceOffloadKind) const { |
696 | 114 | if (Arg *A = DriverArgs.getLastArg(options::OPT_mguard_EQ)) { |
697 | 4 | StringRef GuardArgs = A->getValue(); |
698 | 4 | if (GuardArgs == "none") { |
699 | | // Do nothing. |
700 | 3 | } else if (GuardArgs == "cf") { |
701 | | // Emit CFG instrumentation and the table of address-taken functions. |
702 | 1 | CC1Args.push_back("-cfguard"); |
703 | 2 | } else if (GuardArgs == "cf-nochecks") { |
704 | | // Emit only the table of address-taken functions. |
705 | 1 | CC1Args.push_back("-cfguard-no-checks"); |
706 | 1 | } else { |
707 | 1 | getDriver().Diag(diag::err_drv_unsupported_option_argument) |
708 | 1 | << A->getSpelling() << GuardArgs; |
709 | 1 | } |
710 | 4 | } |
711 | | |
712 | 114 | if (Arg *A = DriverArgs.getLastArgNoClaim(options::OPT_mthreads)) |
713 | 0 | A->ignoreTargetSpecific(); |
714 | 114 | } |
715 | | |
716 | | void toolchains::MinGW::AddClangCXXStdlibIncludeArgs( |
717 | 39 | const ArgList &DriverArgs, ArgStringList &CC1Args) const { |
718 | 39 | if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc, |
719 | 39 | options::OPT_nostdincxx)) |
720 | 0 | return; |
721 | | |
722 | 39 | StringRef Slash = llvm::sys::path::get_separator(); |
723 | | |
724 | 39 | switch (GetCXXStdlibType(DriverArgs)) { |
725 | 1 | case ToolChain::CST_Libcxx: { |
726 | 1 | std::string TargetDir = (Base + "include" + Slash + getTripleString() + |
727 | 1 | Slash + "c++" + Slash + "v1") |
728 | 1 | .str(); |
729 | 1 | if (getDriver().getVFS().exists(TargetDir)) |
730 | 1 | addSystemInclude(DriverArgs, CC1Args, TargetDir); |
731 | 1 | addSystemInclude(DriverArgs, CC1Args, |
732 | 1 | Base + SubdirName + Slash + "include" + Slash + "c++" + |
733 | 1 | Slash + "v1"); |
734 | 1 | addSystemInclude(DriverArgs, CC1Args, |
735 | 1 | Base + "include" + Slash + "c++" + Slash + "v1"); |
736 | 1 | break; |
737 | 0 | } |
738 | | |
739 | 38 | case ToolChain::CST_Libstdcxx: |
740 | 38 | llvm::SmallVector<llvm::SmallString<1024>, 7> CppIncludeBases; |
741 | 38 | CppIncludeBases.emplace_back(Base); |
742 | 38 | llvm::sys::path::append(CppIncludeBases[0], SubdirName, "include", "c++"); |
743 | 38 | CppIncludeBases.emplace_back(Base); |
744 | 38 | llvm::sys::path::append(CppIncludeBases[1], SubdirName, "include", "c++", |
745 | 38 | Ver); |
746 | 38 | CppIncludeBases.emplace_back(Base); |
747 | 38 | llvm::sys::path::append(CppIncludeBases[2], "include", "c++", Ver); |
748 | 38 | CppIncludeBases.emplace_back(GccLibDir); |
749 | 38 | llvm::sys::path::append(CppIncludeBases[3], "include", "c++"); |
750 | 38 | CppIncludeBases.emplace_back(GccLibDir); |
751 | 38 | llvm::sys::path::append(CppIncludeBases[4], "include", |
752 | 38 | "g++-v" + GccVer.Text); |
753 | 38 | CppIncludeBases.emplace_back(GccLibDir); |
754 | 38 | llvm::sys::path::append(CppIncludeBases[5], "include", |
755 | 38 | "g++-v" + GccVer.MajorStr + "." + GccVer.MinorStr); |
756 | 38 | CppIncludeBases.emplace_back(GccLibDir); |
757 | 38 | llvm::sys::path::append(CppIncludeBases[6], "include", |
758 | 38 | "g++-v" + GccVer.MajorStr); |
759 | 266 | for (auto &CppIncludeBase : CppIncludeBases) { |
760 | 266 | addSystemInclude(DriverArgs, CC1Args, CppIncludeBase); |
761 | 266 | CppIncludeBase += Slash; |
762 | 266 | addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + TripleDirName); |
763 | 266 | addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward"); |
764 | 266 | } |
765 | 38 | break; |
766 | 39 | } |
767 | 39 | } |
768 | | |
769 | | static bool testTriple(const Driver &D, const llvm::Triple &Triple, |
770 | 4 | const ArgList &Args) { |
771 | | // If an explicit sysroot is set, that will be used and we shouldn't try to |
772 | | // detect anything else. |
773 | 4 | std::string SubdirName; |
774 | 4 | if (D.SysRoot.size()) |
775 | 0 | return true; |
776 | 4 | llvm::Triple LiteralTriple = getLiteralTriple(D, Triple); |
777 | 4 | if (llvm::ErrorOr<std::string> TargetSubdir = |
778 | 4 | findClangRelativeSysroot(D, LiteralTriple, Triple, SubdirName)) |
779 | 1 | return true; |
780 | 3 | if (llvm::ErrorOr<std::string> GPPName = findGcc(LiteralTriple, Triple)) |
781 | 0 | return true; |
782 | | // If we neither found a colocated sysroot or a matching gcc executable, |
783 | | // conclude that we can't know if this is the correct spelling of the triple. |
784 | 3 | return false; |
785 | 3 | } |
786 | | |
787 | | static llvm::Triple adjustTriple(const Driver &D, const llvm::Triple &Triple, |
788 | 1 | const ArgList &Args) { |
789 | | // First test if the original triple can find a sysroot with the triple |
790 | | // name. |
791 | 1 | if (testTriple(D, Triple, Args)) |
792 | 0 | return Triple; |
793 | 1 | llvm::SmallVector<llvm::StringRef, 3> Archs; |
794 | | // If not, test a couple other possible arch names that might be what was |
795 | | // intended. |
796 | 1 | if (Triple.getArch() == llvm::Triple::x86) { |
797 | 1 | Archs.emplace_back("i386"); |
798 | 1 | Archs.emplace_back("i586"); |
799 | 1 | Archs.emplace_back("i686"); |
800 | 1 | } else if (0 Triple.getArch() == llvm::Triple::arm0 || |
801 | 0 | Triple.getArch() == llvm::Triple::thumb) { |
802 | 0 | Archs.emplace_back("armv7"); |
803 | 0 | } |
804 | 3 | for (auto A : Archs) { |
805 | 3 | llvm::Triple TestTriple(Triple); |
806 | 3 | TestTriple.setArchName(A); |
807 | 3 | if (testTriple(D, TestTriple, Args)) |
808 | 1 | return TestTriple; |
809 | 3 | } |
810 | | // If none was found, just proceed with the original value. |
811 | 0 | return Triple; |
812 | 1 | } |
813 | | |
814 | | void toolchains::MinGW::fixTripleArch(const Driver &D, llvm::Triple &Triple, |
815 | 1 | const ArgList &Args) { |
816 | 1 | if (Triple.getArch() == llvm::Triple::x86 || |
817 | 1 | Triple.getArch() == llvm::Triple::arm0 || |
818 | 1 | Triple.getArch() == llvm::Triple::thumb0 ) |
819 | 1 | Triple = adjustTriple(D, Triple, Args); |
820 | 1 | } |