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