11#include "clang/Config/config.h"
18#include "llvm/Config/llvm-config.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/VirtualFileSystem.h"
23#include <system_error>
35 const char *LinkingOutput)
const {
37 ArgStringList CmdArgs;
40 CmdArgs.push_back(
"--32");
41 }
else if (
getToolChain().getArch() == llvm::Triple::x86_64) {
42 CmdArgs.push_back(
"--64");
45 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
47 CmdArgs.push_back(
"-o");
50 for (
const auto &II : Inputs)
51 CmdArgs.push_back(II.getFilename());
53 const char *Exec = Args.MakeArgString(
getToolChain().GetProgramPath(
"as"));
55 Exec, CmdArgs, Inputs, Output));
57 if (Args.hasArg(options::OPT_gsplit_dwarf))
62void tools::MinGW::Linker::AddLibGCC(
const ArgList &Args,
63 ArgStringList &CmdArgs)
const {
64 if (Args.hasArg(options::OPT_mthreads))
65 CmdArgs.push_back(
"-lmingwthrd");
66 CmdArgs.push_back(
"-lmingw32");
71 bool Static = Args.hasArg(options::OPT_static_libgcc) ||
72 Args.hasArg(options::OPT_static);
73 bool Shared = Args.hasArg(options::OPT_shared);
74 bool CXX = getToolChain().getDriver().CCCIsCXX();
77 CmdArgs.push_back(
"-lgcc");
78 CmdArgs.push_back(
"-lgcc_eh");
80 CmdArgs.push_back(
"-lgcc_s");
81 CmdArgs.push_back(
"-lgcc");
84 AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args);
87 CmdArgs.push_back(
"-lmoldname");
88 CmdArgs.push_back(
"-lmingwex");
89 for (
auto Lib : Args.getAllArgValues(options::OPT_l))
90 if (StringRef(Lib).starts_with(
"msvcr") ||
91 StringRef(Lib).starts_with(
"ucrt") ||
92 StringRef(Lib).starts_with(
"crtdll"))
94 CmdArgs.push_back(
"-lmsvcrt");
101 const char *LinkingOutput)
const {
106 ArgStringList CmdArgs;
109 Args.ClaimAllArgs(options::OPT_g_Group);
111 Args.ClaimAllArgs(options::OPT_emit_llvm);
114 Args.ClaimAllArgs(options::OPT_w);
116 if (!
D.SysRoot.empty())
117 CmdArgs.push_back(Args.MakeArgString(
"--sysroot=" +
D.SysRoot));
119 if (Args.hasArg(options::OPT_s))
120 CmdArgs.push_back(
"-s");
122 CmdArgs.push_back(
"-m");
124 case llvm::Triple::x86:
125 CmdArgs.push_back(
"i386pe");
127 case llvm::Triple::x86_64:
128 CmdArgs.push_back(
"i386pep");
130 case llvm::Triple::arm:
131 case llvm::Triple::thumb:
133 CmdArgs.push_back(
"thumb2pe");
135 case llvm::Triple::aarch64:
137 CmdArgs.push_back(
"arm64ecpe");
139 CmdArgs.push_back(
"arm64pe");
146 Args.getLastArg(options::OPT_mwindows, options::OPT_mconsole);
147 if (SubsysArg && SubsysArg->getOption().matches(options::OPT_mwindows)) {
148 CmdArgs.push_back(
"--subsystem");
149 CmdArgs.push_back(
"windows");
150 }
else if (SubsysArg &&
151 SubsysArg->getOption().matches(options::OPT_mconsole)) {
152 CmdArgs.push_back(
"--subsystem");
153 CmdArgs.push_back(
"console");
156 if (Args.hasArg(options::OPT_mdll))
157 CmdArgs.push_back(
"--dll");
158 else if (Args.hasArg(options::OPT_shared))
159 CmdArgs.push_back(
"--shared");
160 if (Args.hasArg(options::OPT_static))
161 CmdArgs.push_back(
"-Bstatic");
163 CmdArgs.push_back(
"-Bdynamic");
164 if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) {
165 CmdArgs.push_back(
"-e");
166 if (TC.
getArch() == llvm::Triple::x86)
167 CmdArgs.push_back(
"_DllMainCRTStartup@12");
169 CmdArgs.push_back(
"DllMainCRTStartup");
170 CmdArgs.push_back(
"--enable-auto-image-base");
173 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
174 CmdArgs.push_back(
"--no-demangle");
176 if (!Args.hasFlag(options::OPT_fauto_import, options::OPT_fno_auto_import,
178 CmdArgs.push_back(
"--disable-auto-import");
180 if (Arg *A = Args.getLastArg(options::OPT_mguard_EQ)) {
181 StringRef GuardArgs = A->getValue();
182 if (GuardArgs ==
"none")
183 CmdArgs.push_back(
"--no-guard-cf");
184 else if (GuardArgs ==
"cf" || GuardArgs ==
"cf-nochecks")
185 CmdArgs.push_back(
"--guard-cf");
187 D.Diag(diag::err_drv_unsupported_option_argument)
188 << A->getSpelling() << GuardArgs;
191 if (Args.hasArg(options::OPT_fms_hotpatch))
192 CmdArgs.push_back(
"--functionpadmin");
194 CmdArgs.push_back(
"-o");
200 if (!llvm::sys::path::has_extension(OutputFile)) {
201 CmdArgs.push_back(Args.MakeArgString(Twine(OutputFile) +
".exe"));
202 OutputFile = CmdArgs.back();
204 CmdArgs.push_back(OutputFile);
207 Args.AddLastArg(CmdArgs, options::OPT_r);
208 Args.AddLastArg(CmdArgs, options::OPT_s);
209 Args.AddLastArg(CmdArgs, options::OPT_t);
210 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
215 if (Sanitize.
needsAsanRt() && !Args.hasArg(options::OPT_nostdlib) &&
216 !Args.hasArg(options::OPT_nodefaultlibs)) {
222 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
223 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
224 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"dllcrt2.o")));
226 if (Args.hasArg(options::OPT_municode))
227 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crt2u.o")));
229 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crt2.o")));
231 if (Args.hasArg(options::OPT_pg))
232 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"gcrt2.o")));
233 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crtbegin.o")));
236 Args.AddAllArgs(CmdArgs, options::OPT_L);
242 if (TC.
getVFS().exists(LibPath))
243 CmdArgs.push_back(Args.MakeArgString(
"-L" + LibPath));
246 if (TC.
getVFS().exists(CRTPath))
247 CmdArgs.push_back(Args.MakeArgString(
"-L" + CRTPath));
251 if (
D.isUsingLTO()) {
252 assert(!Inputs.empty() &&
"Must have at least one input.");
257 if (
C.getDriver().IsFlangMode() &&
258 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
266 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
267 !Args.hasArg(options::OPT_static);
268 if (OnlyLibstdcxxStatic)
269 CmdArgs.push_back(
"-Bstatic");
271 if (OnlyLibstdcxxStatic)
272 CmdArgs.push_back(
"-Bdynamic");
275 bool HasWindowsApp =
false;
276 for (
auto Lib : Args.getAllArgValues(options::OPT_l)) {
277 if (Lib ==
"windowsapp") {
278 HasWindowsApp =
true;
283 if (!Args.hasArg(options::OPT_nostdlib)) {
284 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
285 if (Args.hasArg(options::OPT_static))
286 CmdArgs.push_back(
"--start-group");
288 if (Args.hasArg(options::OPT_fstack_protector) ||
289 Args.hasArg(options::OPT_fstack_protector_strong) ||
290 Args.hasArg(options::OPT_fstack_protector_all)) {
291 CmdArgs.push_back(
"-lssp_nonshared");
292 CmdArgs.push_back(
"-lssp");
295 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
296 options::OPT_fno_openmp,
false)) {
299 CmdArgs.push_back(
"-lomp");
302 CmdArgs.push_back(
"-liomp5md");
305 CmdArgs.push_back(
"-lgomp");
313 AddLibGCC(Args, CmdArgs);
315 if (Args.hasArg(options::OPT_pg))
316 CmdArgs.push_back(
"-lgmon");
318 if (Args.hasArg(options::OPT_pthread))
319 CmdArgs.push_back(
"-lpthread");
327 CmdArgs.push_back(
"--require-defined");
328 CmdArgs.push_back(TC.
getArch() == llvm::Triple::x86
329 ?
"___asan_seh_interceptor"
330 :
"__asan_seh_interceptor");
333 CmdArgs.push_back(
"--whole-archive");
336 CmdArgs.push_back(
"--no-whole-archive");
341 if (!HasWindowsApp) {
345 if (Args.hasArg(options::OPT_mwindows)) {
346 CmdArgs.push_back(
"-lgdi32");
347 CmdArgs.push_back(
"-lcomdlg32");
349 CmdArgs.push_back(
"-ladvapi32");
350 CmdArgs.push_back(
"-lshell32");
351 CmdArgs.push_back(
"-luser32");
352 CmdArgs.push_back(
"-lkernel32");
355 if (Args.hasArg(options::OPT_static)) {
356 CmdArgs.push_back(
"--end-group");
358 AddLibGCC(Args, CmdArgs);
360 CmdArgs.push_back(
"-lkernel32");
364 if (!Args.hasArg(options::OPT_nostartfiles)) {
368 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crtend.o")));
372 C.addCommand(std::make_unique<Command>(JA, *
this,
374 Exec, CmdArgs, Inputs, Output));
378 llvm::Triple HostTriple(llvm::Triple::normalize(LLVM_HOST_TRIPLE));
379 if (HostTriple.getOS() != llvm::Triple::Win32)
381 if (RequireArchMatch && HostTriple.getArch() !=
T.getArch())
392 for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
393 LI = LI.increment(EC)) {
394 StringRef VersionText = llvm::sys::path::filename(LI->path());
395 auto CandidateVersion =
397 if (CandidateVersion.Major == -1)
399 if (CandidateVersion <= Version)
401 Version = CandidateVersion;
402 Ver = std::string(VersionText);
403 GccLibDir = LI->path();
409 llvm::Triple LiteralTriple(
D.getTargetTriple());
411 LiteralTriple.setArchName(
T.getArchName());
412 return LiteralTriple;
415void toolchains::MinGW::findGccLibDir(
const llvm::Triple &LiteralTriple) {
417 SubdirNames.emplace_back(LiteralTriple.str());
418 SubdirNames.emplace_back(getTriple().str());
419 SubdirNames.emplace_back(getTriple().getArchName());
420 SubdirNames.back() +=
"-w64-mingw32";
421 SubdirNames.emplace_back(getTriple().getArchName());
422 SubdirNames.back() +=
"-w64-mingw32ucrt";
423 SubdirNames.emplace_back(
"mingw32");
424 if (SubdirName.empty()) {
425 SubdirName = getTriple().getArchName();
426 SubdirName +=
"-w64-mingw32";
430 for (StringRef CandidateLib : {
"lib",
"lib64"}) {
431 for (StringRef CandidateSysroot : SubdirNames) {
433 llvm::sys::path::append(LibDir, CandidateLib,
"gcc", CandidateSysroot);
435 SubdirName = std::string(CandidateSysroot);
442static llvm::ErrorOr<std::string>
findGcc(
const llvm::Triple &LiteralTriple,
443 const llvm::Triple &
T) {
445 Gccs.emplace_back(LiteralTriple.str());
446 Gccs.back() +=
"-gcc";
447 Gccs.emplace_back(
T.str());
448 Gccs.back() +=
"-gcc";
449 Gccs.emplace_back(
T.getArchName());
450 Gccs.back() +=
"-w64-mingw32-gcc";
451 Gccs.emplace_back(
T.getArchName());
452 Gccs.back() +=
"-w64-mingw32ucrt-gcc";
453 Gccs.emplace_back(
"mingw32-gcc");
455 for (StringRef CandidateGcc : Gccs)
456 if (llvm::ErrorOr<std::string> GPPName = llvm::sys::findProgramByName(CandidateGcc))
461static llvm::ErrorOr<std::string>
463 const llvm::Triple &
T, std::string &SubdirName) {
465 Subdirs.emplace_back(LiteralTriple.str());
466 Subdirs.emplace_back(
T.str());
467 Subdirs.emplace_back(
T.getArchName());
468 Subdirs.back() +=
"-w64-mingw32";
469 Subdirs.emplace_back(
T.getArchName());
470 Subdirs.back() +=
"-w64-mingw32ucrt";
471 StringRef ClangRoot = llvm::sys::path::parent_path(
D.Dir);
472 StringRef Sep = llvm::sys::path::get_separator();
473 for (StringRef CandidateSubdir : Subdirs) {
474 if (llvm::sys::fs::is_directory(ClangRoot + Sep + CandidateSubdir)) {
475 SubdirName = std::string(CandidateSubdir);
476 return (ClangRoot + Sep + CandidateSubdir).str();
483 StringRef Sep = llvm::sys::path::get_separator();
484 if (!llvm::sys::fs::exists(Directory + Sep +
"include" + Sep +
"_mingw.h"))
486 if (!llvm::sys::fs::exists(Directory + Sep +
"lib" + Sep +
"libkernel32.a"))
493 :
ToolChain(
D, Triple, Args), CudaInstallation(
D, Triple, Args),
494 RocmInstallation(
D, Triple, Args) {
497 std::string InstallBase =
498 std::string(llvm::sys::path::parent_path(
getDriver().Dir));
508 Base = std::string(llvm::sys::path::parent_path(TargetSubdir.get()));
514 else if (llvm::ErrorOr<std::string> GPPName =
516 Base = std::string(llvm::sys::path::parent_path(
517 llvm::sys::path::parent_path(GPPName.get())));
521 Base += llvm::sys::path::get_separator();
522 findGccLibDir(LiteralTriple);
523 TripleDirName = SubdirName;
529 std::string CandidateSubdir = SubdirName +
"/sys-root/mingw";
531 SubdirName = CandidateSubdir;
534 (
Base + SubdirName + llvm::sys::path::get_separator() +
"lib").str());
538 (
Base + SubdirName + llvm::sys::path::get_separator() +
"mingw/lib").str());
549 Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER)
550 .equals_insensitive(
"lld");
562 return Compiler.get();
577 return NativeLLVMSupport;
582 Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions,
583 options::OPT_fseh_exceptions,
584 options::OPT_fdwarf_exceptions);
586 ExceptionArg->getOption().matches(options::OPT_fseh_exceptions))
587 return UnwindTableLevel::Asynchronous;
589 if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
590 getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64)
591 return UnwindTableLevel::Asynchronous;
592 return UnwindTableLevel::None;
596 return getArch() == llvm::Triple::x86_64 ||
597 getArch() == llvm::Triple::aarch64;
606llvm::ExceptionHandling
608 if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64 ||
609 getArch() == llvm::Triple::arm || getArch() == llvm::Triple::thumb)
610 return llvm::ExceptionHandling::WinEH;
611 return llvm::ExceptionHandling::DwarfCFI;
616 Res |= SanitizerKind::Address;
617 Res |= SanitizerKind::PointerCompare;
618 Res |= SanitizerKind::PointerSubtract;
619 Res |= SanitizerKind::Vptr;
624 ArgStringList &CC1Args)
const {
625 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
629 ArgStringList &CC1Args)
const {
630 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
634 CudaInstallation->print(OS);
635 RocmInstallation->print(OS);
685 ArgStringList &CC1Args)
const {
686 if (DriverArgs.hasArg(options::OPT_nostdinc))
689 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
691 llvm::sys::path::append(
P,
"include");
692 addSystemInclude(DriverArgs, CC1Args,
P.str());
695 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
698 addSystemInclude(DriverArgs, CC1Args,
699 Base + SubdirName + llvm::sys::path::get_separator() +
703 addSystemInclude(DriverArgs, CC1Args,
704 Base + SubdirName + llvm::sys::path::get_separator() +
"usr/include");
711 getDriver().SysRoot.size())
712 addSystemInclude(DriverArgs, CC1Args,
Base +
"include");
716 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
718 if (Arg *A = DriverArgs.getLastArg(options::OPT_mguard_EQ)) {
719 StringRef GuardArgs = A->getValue();
720 if (GuardArgs ==
"none") {
722 }
else if (GuardArgs ==
"cf") {
724 CC1Args.push_back(
"-cfguard");
725 }
else if (GuardArgs ==
"cf-nochecks") {
727 CC1Args.push_back(
"-cfguard-no-checks");
729 getDriver().Diag(diag::err_drv_unsupported_option_argument)
730 << A->getSpelling() << GuardArgs;
754 if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
755 options::OPT_fno_sized_deallocation))
756 CC1Args.push_back(
"-fno-sized-deallocation");
758 CC1Args.push_back(
"-fno-use-init-array");
760 for (
auto Opt : {options::OPT_mthreads, options::OPT_mwindows,
761 options::OPT_mconsole, options::OPT_mdll}) {
762 if (Arg *A = DriverArgs.getLastArgNoClaim(Opt))
763 A->ignoreTargetSpecific();
768 const ArgList &DriverArgs, ArgStringList &CC1Args)
const {
769 if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
770 options::OPT_nostdincxx))
773 StringRef Slash = llvm::sys::path::get_separator();
775 switch (GetCXXStdlibType(DriverArgs)) {
777 std::string TargetDir = (
Base +
"include" + Slash + getTripleString() +
778 Slash +
"c++" + Slash +
"v1")
780 if (getDriver().getVFS().exists(TargetDir))
781 addSystemInclude(DriverArgs, CC1Args, TargetDir);
782 addSystemInclude(DriverArgs, CC1Args,
783 Base + SubdirName + Slash +
"include" + Slash +
"c++" +
785 addSystemInclude(DriverArgs, CC1Args,
786 Base +
"include" + Slash +
"c++" + Slash +
"v1");
792 CppIncludeBases.emplace_back(
Base);
793 llvm::sys::path::append(CppIncludeBases[0], SubdirName,
"include",
"c++");
794 CppIncludeBases.emplace_back(
Base);
795 llvm::sys::path::append(CppIncludeBases[1], SubdirName,
"include",
"c++",
797 CppIncludeBases.emplace_back(
Base);
798 llvm::sys::path::append(CppIncludeBases[2],
"include",
"c++", Ver);
799 CppIncludeBases.emplace_back(GccLibDir);
800 llvm::sys::path::append(CppIncludeBases[3],
"include",
"c++");
801 CppIncludeBases.emplace_back(GccLibDir);
802 llvm::sys::path::append(CppIncludeBases[4],
"include",
803 "g++-v" + GccVer.Text);
804 CppIncludeBases.emplace_back(GccLibDir);
805 llvm::sys::path::append(CppIncludeBases[5],
"include",
806 "g++-v" + GccVer.MajorStr +
"." + GccVer.MinorStr);
807 CppIncludeBases.emplace_back(GccLibDir);
808 llvm::sys::path::append(CppIncludeBases[6],
"include",
809 "g++-v" + GccVer.MajorStr);
810 for (
auto &CppIncludeBase : CppIncludeBases) {
811 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
812 CppIncludeBase += Slash;
813 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + TripleDirName);
814 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase +
"backward");
821 const ArgList &Args) {
824 std::string SubdirName;
825 if (
D.SysRoot.size())
828 std::string InstallBase = std::string(llvm::sys::path::parent_path(
D.Dir));
829 if (llvm::ErrorOr<std::string> TargetSubdir =
836 if (llvm::ErrorOr<std::string> GPPName =
findGcc(LiteralTriple, Triple))
844 const ArgList &Args) {
852 if (Triple.getArch() == llvm::Triple::x86) {
853 Archs.emplace_back(
"i386");
854 Archs.emplace_back(
"i586");
855 Archs.emplace_back(
"i686");
856 }
else if (Triple.getArch() == llvm::Triple::arm ||
857 Triple.getArch() == llvm::Triple::thumb) {
858 Archs.emplace_back(
"armv7");
860 for (
auto A : Archs) {
861 llvm::Triple TestTriple(Triple);
862 TestTriple.setArchName(A);
871 const ArgList &Args) {
872 if (Triple.getArch() == llvm::Triple::x86 ||
873 Triple.getArch() == llvm::Triple::arm ||
874 Triple.getArch() == llvm::Triple::thumb)
static llvm::Triple getLiteralTriple(const Driver &D, const llvm::Triple &T)
static bool looksLikeMinGWSysroot(const std::string &Directory)
static bool findGccVersion(StringRef LibDir, std::string &GccLibDir, std::string &Ver, toolchains::Generic_GCC::GCCVersion &Version)
static bool testTriple(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
static llvm::Triple adjustTriple(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
static llvm::ErrorOr< std::string > findGcc(const llvm::Triple &LiteralTriple, const llvm::Triple &T)
static llvm::ErrorOr< std::string > findClangRelativeSysroot(const Driver &D, const llvm::Triple &LiteralTriple, const llvm::Triple &T, std::string &SubdirName)
static bool isCrossCompiling(const llvm::Triple &T, bool RequireArchMatch)
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Compilation - A set of tasks to perform for a single driver invocation.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
std::string SysRoot
sysroot, if present
OpenMPRuntimeKind getOpenMPRuntime(const llvm::opt::ArgList &Args) const
Compute the desired OpenMP runtime from the flags provided.
@ OMPRT_IOMP5
The legacy name for the LLVM OpenMP runtime from when it was the Intel OpenMP runtime.
@ OMPRT_OMP
The LLVM OpenMP runtime.
@ OMPRT_Unknown
An unknown OpenMP runtime.
@ OMPRT_GOMP
The GNU OpenMP runtime.
The JSON file list parser is used to communicate input to InstallAPI.
std::error_code make_error_code(BuildPreambleError Error)
const FunctionProtoType * T
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
static constexpr ResponseFileSupport AtFileUTF8()