11#include "clang/Config/config.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>
34 const char *LinkingOutput)
const {
36 ArgStringList CmdArgs;
39 CmdArgs.push_back(
"--32");
40 }
else if (
getToolChain().getArch() == llvm::Triple::x86_64) {
41 CmdArgs.push_back(
"--64");
44 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
46 CmdArgs.push_back(
"-o");
49 for (
const auto &II : Inputs)
50 CmdArgs.push_back(II.getFilename());
52 const char *Exec = Args.MakeArgString(
getToolChain().GetProgramPath(
"as"));
54 Exec, CmdArgs, Inputs, Output));
56 if (Args.hasArg(options::OPT_gsplit_dwarf))
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");
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();
76 CmdArgs.push_back(
"-lgcc");
77 CmdArgs.push_back(
"-lgcc_eh");
79 CmdArgs.push_back(
"-lgcc_s");
80 CmdArgs.push_back(
"-lgcc");
83 AddRunTimeLibs(getToolChain(), getToolChain().getDriver(), CmdArgs, Args);
86 CmdArgs.push_back(
"-lmoldname");
87 CmdArgs.push_back(
"-lmingwex");
88 for (
auto Lib : Args.getAllArgValues(options::OPT_l))
89 if (StringRef(Lib).starts_with(
"msvcr") ||
90 StringRef(Lib).starts_with(
"ucrt") ||
91 StringRef(Lib).starts_with(
"crtdll"))
93 CmdArgs.push_back(
"-lmsvcrt");
100 const char *LinkingOutput)
const {
105 ArgStringList CmdArgs;
108 Args.ClaimAllArgs(options::OPT_g_Group);
110 Args.ClaimAllArgs(options::OPT_emit_llvm);
113 Args.ClaimAllArgs(options::OPT_w);
115 if (!
D.SysRoot.empty())
116 CmdArgs.push_back(Args.MakeArgString(
"--sysroot=" +
D.SysRoot));
118 if (Args.hasArg(options::OPT_s))
119 CmdArgs.push_back(
"-s");
121 CmdArgs.push_back(
"-m");
123 case llvm::Triple::x86:
124 CmdArgs.push_back(
"i386pe");
126 case llvm::Triple::x86_64:
127 CmdArgs.push_back(
"i386pep");
129 case llvm::Triple::arm:
130 case llvm::Triple::thumb:
132 CmdArgs.push_back(
"thumb2pe");
134 case llvm::Triple::aarch64:
136 CmdArgs.push_back(
"arm64ecpe");
138 CmdArgs.push_back(
"arm64pe");
145 Args.getLastArg(options::OPT_mwindows, options::OPT_mconsole);
146 if (SubsysArg && SubsysArg->getOption().matches(options::OPT_mwindows)) {
147 CmdArgs.push_back(
"--subsystem");
148 CmdArgs.push_back(
"windows");
149 }
else if (SubsysArg &&
150 SubsysArg->getOption().matches(options::OPT_mconsole)) {
151 CmdArgs.push_back(
"--subsystem");
152 CmdArgs.push_back(
"console");
155 if (Args.hasArg(options::OPT_mdll))
156 CmdArgs.push_back(
"--dll");
157 else if (Args.hasArg(options::OPT_shared))
158 CmdArgs.push_back(
"--shared");
159 if (Args.hasArg(options::OPT_static))
160 CmdArgs.push_back(
"-Bstatic");
162 CmdArgs.push_back(
"-Bdynamic");
163 if (Args.hasArg(options::OPT_mdll) || Args.hasArg(options::OPT_shared)) {
164 CmdArgs.push_back(
"-e");
165 if (TC.
getArch() == llvm::Triple::x86)
166 CmdArgs.push_back(
"_DllMainCRTStartup@12");
168 CmdArgs.push_back(
"DllMainCRTStartup");
169 CmdArgs.push_back(
"--enable-auto-image-base");
172 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
173 CmdArgs.push_back(
"--no-demangle");
175 if (!Args.hasFlag(options::OPT_fauto_import, options::OPT_fno_auto_import,
177 CmdArgs.push_back(
"--disable-auto-import");
179 if (Arg *A = Args.getLastArg(options::OPT_mguard_EQ)) {
180 StringRef GuardArgs = A->getValue();
181 if (GuardArgs ==
"none")
182 CmdArgs.push_back(
"--no-guard-cf");
183 else if (GuardArgs ==
"cf" || GuardArgs ==
"cf-nochecks")
184 CmdArgs.push_back(
"--guard-cf");
186 D.Diag(diag::err_drv_unsupported_option_argument)
187 << A->getSpelling() << GuardArgs;
190 CmdArgs.push_back(
"-o");
196 if (!llvm::sys::path::has_extension(OutputFile)) {
197 CmdArgs.push_back(Args.MakeArgString(Twine(OutputFile) +
".exe"));
198 OutputFile = CmdArgs.back();
200 CmdArgs.push_back(OutputFile);
203 Args.AddLastArg(CmdArgs, options::OPT_r);
204 Args.AddLastArg(CmdArgs, options::OPT_s);
205 Args.AddLastArg(CmdArgs, options::OPT_t);
206 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
211 if (Sanitize.
needsAsanRt() && !Args.hasArg(options::OPT_nostdlib) &&
212 !Args.hasArg(options::OPT_nodefaultlibs)) {
218 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
219 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_mdll)) {
220 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"dllcrt2.o")));
222 if (Args.hasArg(options::OPT_municode))
223 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crt2u.o")));
225 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crt2.o")));
227 if (Args.hasArg(options::OPT_pg))
228 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"gcrt2.o")));
229 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crtbegin.o")));
232 Args.AddAllArgs(CmdArgs, options::OPT_L);
238 if (TC.
getVFS().exists(LibPath))
239 CmdArgs.push_back(Args.MakeArgString(
"-L" + LibPath));
242 if (TC.
getVFS().exists(CRTPath))
243 CmdArgs.push_back(Args.MakeArgString(
"-L" + CRTPath));
247 if (
D.isUsingLTO()) {
248 assert(!Inputs.empty() &&
"Must have at least one input.");
253 if (
C.getDriver().IsFlangMode()) {
261 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
262 !Args.hasArg(options::OPT_static);
263 if (OnlyLibstdcxxStatic)
264 CmdArgs.push_back(
"-Bstatic");
266 if (OnlyLibstdcxxStatic)
267 CmdArgs.push_back(
"-Bdynamic");
270 bool HasWindowsApp =
false;
271 for (
auto Lib : Args.getAllArgValues(options::OPT_l)) {
272 if (Lib ==
"windowsapp") {
273 HasWindowsApp =
true;
278 if (!Args.hasArg(options::OPT_nostdlib)) {
279 if (!Args.hasArg(options::OPT_nodefaultlibs)) {
280 if (Args.hasArg(options::OPT_static))
281 CmdArgs.push_back(
"--start-group");
283 if (Args.hasArg(options::OPT_fstack_protector) ||
284 Args.hasArg(options::OPT_fstack_protector_strong) ||
285 Args.hasArg(options::OPT_fstack_protector_all)) {
286 CmdArgs.push_back(
"-lssp_nonshared");
287 CmdArgs.push_back(
"-lssp");
290 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
291 options::OPT_fno_openmp,
false)) {
294 CmdArgs.push_back(
"-lomp");
297 CmdArgs.push_back(
"-liomp5md");
300 CmdArgs.push_back(
"-lgomp");
308 AddLibGCC(Args, CmdArgs);
310 if (Args.hasArg(options::OPT_pg))
311 CmdArgs.push_back(
"-lgmon");
313 if (Args.hasArg(options::OPT_pthread))
314 CmdArgs.push_back(
"-lpthread");
322 CmdArgs.push_back(
"--require-defined");
323 CmdArgs.push_back(TC.
getArch() == llvm::Triple::x86
324 ?
"___asan_seh_interceptor"
325 :
"__asan_seh_interceptor");
328 CmdArgs.push_back(
"--whole-archive");
331 CmdArgs.push_back(
"--no-whole-archive");
336 if (!HasWindowsApp) {
340 if (Args.hasArg(options::OPT_mwindows)) {
341 CmdArgs.push_back(
"-lgdi32");
342 CmdArgs.push_back(
"-lcomdlg32");
344 CmdArgs.push_back(
"-ladvapi32");
345 CmdArgs.push_back(
"-lshell32");
346 CmdArgs.push_back(
"-luser32");
347 CmdArgs.push_back(
"-lkernel32");
350 if (Args.hasArg(options::OPT_static)) {
351 CmdArgs.push_back(
"--end-group");
353 AddLibGCC(Args, CmdArgs);
355 CmdArgs.push_back(
"-lkernel32");
359 if (!Args.hasArg(options::OPT_nostartfiles)) {
363 CmdArgs.push_back(Args.MakeArgString(TC.
GetFilePath(
"crtend.o")));
367 C.addCommand(std::make_unique<Command>(JA, *
this,
369 Exec, CmdArgs, Inputs, Output));
373 llvm::Triple HostTriple(llvm::Triple::normalize(LLVM_HOST_TRIPLE));
374 if (HostTriple.getOS() != llvm::Triple::Win32)
376 if (RequireArchMatch && HostTriple.getArch() !=
T.getArch())
387 for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
388 LI = LI.increment(EC)) {
389 StringRef VersionText = llvm::sys::path::filename(LI->path());
390 auto CandidateVersion =
392 if (CandidateVersion.Major == -1)
394 if (CandidateVersion <= Version)
396 Version = CandidateVersion;
397 Ver = std::string(VersionText);
398 GccLibDir = LI->path();
404 llvm::Triple LiteralTriple(
D.getTargetTriple());
406 LiteralTriple.setArchName(
T.getArchName());
407 return LiteralTriple;
410void toolchains::MinGW::findGccLibDir(
const llvm::Triple &LiteralTriple) {
412 SubdirNames.emplace_back(LiteralTriple.str());
413 SubdirNames.emplace_back(getTriple().str());
414 SubdirNames.emplace_back(getTriple().getArchName());
415 SubdirNames.back() +=
"-w64-mingw32";
416 SubdirNames.emplace_back(getTriple().getArchName());
417 SubdirNames.back() +=
"-w64-mingw32ucrt";
418 SubdirNames.emplace_back(
"mingw32");
419 if (SubdirName.empty()) {
420 SubdirName = getTriple().getArchName();
421 SubdirName +=
"-w64-mingw32";
425 for (StringRef CandidateLib : {
"lib",
"lib64"}) {
426 for (StringRef CandidateSysroot : SubdirNames) {
428 llvm::sys::path::append(LibDir, CandidateLib,
"gcc", CandidateSysroot);
430 SubdirName = std::string(CandidateSysroot);
437static llvm::ErrorOr<std::string>
findGcc(
const llvm::Triple &LiteralTriple,
438 const llvm::Triple &
T) {
440 Gccs.emplace_back(LiteralTriple.str());
441 Gccs.back() +=
"-gcc";
442 Gccs.emplace_back(
T.str());
443 Gccs.back() +=
"-gcc";
444 Gccs.emplace_back(
T.getArchName());
445 Gccs.back() +=
"-w64-mingw32-gcc";
446 Gccs.emplace_back(
T.getArchName());
447 Gccs.back() +=
"-w64-mingw32ucrt-gcc";
448 Gccs.emplace_back(
"mingw32-gcc");
450 for (StringRef CandidateGcc : Gccs)
451 if (llvm::ErrorOr<std::string> GPPName = llvm::sys::findProgramByName(CandidateGcc))
456static llvm::ErrorOr<std::string>
458 const llvm::Triple &
T, std::string &SubdirName) {
460 Subdirs.emplace_back(LiteralTriple.str());
461 Subdirs.emplace_back(
T.str());
462 Subdirs.emplace_back(
T.getArchName());
463 Subdirs.back() +=
"-w64-mingw32";
464 Subdirs.emplace_back(
T.getArchName());
465 Subdirs.back() +=
"-w64-mingw32ucrt";
466 StringRef ClangRoot = llvm::sys::path::parent_path(
D.Dir);
467 StringRef Sep = llvm::sys::path::get_separator();
468 for (StringRef CandidateSubdir : Subdirs) {
469 if (llvm::sys::fs::is_directory(ClangRoot + Sep + CandidateSubdir)) {
470 SubdirName = std::string(CandidateSubdir);
471 return (ClangRoot + Sep + CandidateSubdir).str();
478 StringRef Sep = llvm::sys::path::get_separator();
479 if (!llvm::sys::fs::exists(Directory + Sep +
"include" + Sep +
"_mingw.h"))
481 if (!llvm::sys::fs::exists(Directory + Sep +
"lib" + Sep +
"libkernel32.a"))
488 :
ToolChain(
D, Triple, Args), CudaInstallation(
D, Triple, Args),
489 RocmInstallation(
D, Triple, Args) {
492 std::string InstallBase =
493 std::string(llvm::sys::path::parent_path(
getDriver().Dir));
503 Base = std::string(llvm::sys::path::parent_path(TargetSubdir.get()));
509 else if (llvm::ErrorOr<std::string> GPPName =
511 Base = std::string(llvm::sys::path::parent_path(
512 llvm::sys::path::parent_path(GPPName.get())));
516 Base += llvm::sys::path::get_separator();
517 findGccLibDir(LiteralTriple);
518 TripleDirName = SubdirName;
524 std::string CandidateSubdir = SubdirName +
"/sys-root/mingw";
526 SubdirName = CandidateSubdir;
529 (
Base + SubdirName + llvm::sys::path::get_separator() +
"lib").str());
533 (
Base + SubdirName + llvm::sys::path::get_separator() +
"mingw/lib").str());
544 Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER)
545 .equals_insensitive(
"lld");
557 return Compiler.get();
572 return NativeLLVMSupport;
577 Arg *ExceptionArg = Args.getLastArg(options::OPT_fsjlj_exceptions,
578 options::OPT_fseh_exceptions,
579 options::OPT_fdwarf_exceptions);
581 ExceptionArg->getOption().matches(options::OPT_fseh_exceptions))
582 return UnwindTableLevel::Asynchronous;
584 if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::arm ||
585 getArch() == llvm::Triple::thumb || getArch() == llvm::Triple::aarch64)
586 return UnwindTableLevel::Asynchronous;
587 return UnwindTableLevel::None;
591 return getArch() == llvm::Triple::x86_64 ||
592 getArch() == llvm::Triple::aarch64;
601llvm::ExceptionHandling
603 if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64 ||
604 getArch() == llvm::Triple::arm || getArch() == llvm::Triple::thumb)
605 return llvm::ExceptionHandling::WinEH;
606 return llvm::ExceptionHandling::DwarfCFI;
611 Res |= SanitizerKind::Address;
612 Res |= SanitizerKind::PointerCompare;
613 Res |= SanitizerKind::PointerSubtract;
614 Res |= SanitizerKind::Vptr;
619 ArgStringList &CC1Args)
const {
620 CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args);
624 ArgStringList &CC1Args)
const {
625 RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args);
629 CudaInstallation->print(OS);
630 RocmInstallation->print(OS);
680 ArgStringList &CC1Args)
const {
681 if (DriverArgs.hasArg(options::OPT_nostdinc))
684 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
686 llvm::sys::path::append(
P,
"include");
687 addSystemInclude(DriverArgs, CC1Args,
P.str());
690 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
693 addSystemInclude(DriverArgs, CC1Args,
694 Base + SubdirName + llvm::sys::path::get_separator() +
698 addSystemInclude(DriverArgs, CC1Args,
699 Base + SubdirName + llvm::sys::path::get_separator() +
"usr/include");
706 getDriver().SysRoot.size())
707 addSystemInclude(DriverArgs, CC1Args,
Base +
"include");
711 const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args,
713 if (Arg *A = DriverArgs.getLastArg(options::OPT_mguard_EQ)) {
714 StringRef GuardArgs = A->getValue();
715 if (GuardArgs ==
"none") {
717 }
else if (GuardArgs ==
"cf") {
719 CC1Args.push_back(
"-cfguard");
720 }
else if (GuardArgs ==
"cf-nochecks") {
722 CC1Args.push_back(
"-cfguard-no-checks");
724 getDriver().Diag(diag::err_drv_unsupported_option_argument)
725 << A->getSpelling() << GuardArgs;
749 if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
750 options::OPT_fno_sized_deallocation))
751 CC1Args.push_back(
"-fno-sized-deallocation");
753 CC1Args.push_back(
"-fno-use-init-array");
755 for (
auto Opt : {options::OPT_mthreads, options::OPT_mwindows,
756 options::OPT_mconsole, options::OPT_mdll}) {
757 if (Arg *A = DriverArgs.getLastArgNoClaim(Opt))
758 A->ignoreTargetSpecific();
763 const ArgList &DriverArgs, ArgStringList &CC1Args)
const {
764 if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
765 options::OPT_nostdincxx))
768 StringRef Slash = llvm::sys::path::get_separator();
770 switch (GetCXXStdlibType(DriverArgs)) {
772 std::string TargetDir = (
Base +
"include" + Slash + getTripleString() +
773 Slash +
"c++" + Slash +
"v1")
775 if (getDriver().getVFS().exists(TargetDir))
776 addSystemInclude(DriverArgs, CC1Args, TargetDir);
777 addSystemInclude(DriverArgs, CC1Args,
778 Base + SubdirName + Slash +
"include" + Slash +
"c++" +
780 addSystemInclude(DriverArgs, CC1Args,
781 Base +
"include" + Slash +
"c++" + Slash +
"v1");
787 CppIncludeBases.emplace_back(
Base);
788 llvm::sys::path::append(CppIncludeBases[0], SubdirName,
"include",
"c++");
789 CppIncludeBases.emplace_back(
Base);
790 llvm::sys::path::append(CppIncludeBases[1], SubdirName,
"include",
"c++",
792 CppIncludeBases.emplace_back(
Base);
793 llvm::sys::path::append(CppIncludeBases[2],
"include",
"c++", Ver);
794 CppIncludeBases.emplace_back(GccLibDir);
795 llvm::sys::path::append(CppIncludeBases[3],
"include",
"c++");
796 CppIncludeBases.emplace_back(GccLibDir);
797 llvm::sys::path::append(CppIncludeBases[4],
"include",
798 "g++-v" + GccVer.Text);
799 CppIncludeBases.emplace_back(GccLibDir);
800 llvm::sys::path::append(CppIncludeBases[5],
"include",
801 "g++-v" + GccVer.MajorStr +
"." + GccVer.MinorStr);
802 CppIncludeBases.emplace_back(GccLibDir);
803 llvm::sys::path::append(CppIncludeBases[6],
"include",
804 "g++-v" + GccVer.MajorStr);
805 for (
auto &CppIncludeBase : CppIncludeBases) {
806 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
807 CppIncludeBase += Slash;
808 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + TripleDirName);
809 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase +
"backward");
816 const ArgList &Args) {
819 std::string SubdirName;
820 if (
D.SysRoot.size())
823 std::string InstallBase = std::string(llvm::sys::path::parent_path(
D.Dir));
824 if (llvm::ErrorOr<std::string> TargetSubdir =
831 if (llvm::ErrorOr<std::string> GPPName =
findGcc(LiteralTriple, Triple))
839 const ArgList &Args) {
847 if (Triple.getArch() == llvm::Triple::x86) {
848 Archs.emplace_back(
"i386");
849 Archs.emplace_back(
"i586");
850 Archs.emplace_back(
"i686");
851 }
else if (Triple.getArch() == llvm::Triple::arm ||
852 Triple.getArch() == llvm::Triple::thumb) {
853 Archs.emplace_back(
"armv7");
855 for (
auto A : Archs) {
856 llvm::Triple TestTriple(Triple);
857 TestTriple.setArchName(A);
866 const ArgList &Args) {
867 if (Triple.getArch() == llvm::Triple::x86 ||
868 Triple.getArch() == llvm::Triple::arm ||
869 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()