blob: 938440b08f602fa43f13c9d92bb16d3d900ae0a5 [file] [log] [blame]
Yaron Kerenbfc481b2015-07-07 15:10:47 +00001//===--- MinGWToolChain.cpp - MinGWToolChain Implementation ---------------===//
Yaron Keren1c0070c2015-07-02 04:45:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ToolChains.h"
11#include "clang/Driver/Driver.h"
12#include "clang/Driver/Options.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/Path.h"
16
17using namespace clang::diag;
18using namespace clang::driver;
19using namespace clang::driver::toolchains;
20using namespace clang;
21using namespace llvm::opt;
22
Yaron Keren8cb250a2015-07-24 11:01:45 +000023namespace {
Yaron Kerene8969392015-07-24 20:18:27 +000024// Simplified from Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple.
Yaron Keren8cb250a2015-07-24 11:01:45 +000025bool findGccVersion(StringRef LibDir, std::string &GccLibDir,
26 std::string &Ver) {
Yaron Kerene8969392015-07-24 20:18:27 +000027 Generic_GCC::GCCVersion Version = Generic_GCC::GCCVersion::Parse("0.0.0");
Yaron Keren8cb250a2015-07-24 11:01:45 +000028 std::error_code EC;
Yaron Kerene8969392015-07-24 20:18:27 +000029 for (llvm::sys::fs::directory_iterator LI(LibDir, EC), LE; !EC && LI != LE;
30 LI = LI.increment(EC)) {
31 StringRef VersionText = llvm::sys::path::filename(LI->path());
32 Generic_GCC::GCCVersion CandidateVersion =
33 Generic_GCC::GCCVersion::Parse(VersionText);
34 if (CandidateVersion.Major == -1)
35 continue;
36 if (CandidateVersion <= Version)
37 continue;
38 Ver = VersionText;
39 GccLibDir = LI->path();
Yaron Keren8cb250a2015-07-24 11:01:45 +000040 }
Yaron Kerene8969392015-07-24 20:18:27 +000041 return Ver.size();
Yaron Keren8cb250a2015-07-24 11:01:45 +000042}
43}
44
Yaron Keren28596532015-07-21 11:01:00 +000045void MinGW::findGccLibDir() {
Yaron Keren8cb250a2015-07-24 11:01:45 +000046 llvm::SmallVector<llvm::SmallString<32>, 2> Archs;
47 Archs.emplace_back(getTriple().getArchName());
48 Archs[0] += "-w64-mingw32";
49 Archs.emplace_back("mingw32");
Martell Malone1bc12bb2015-08-13 15:41:04 +000050 Arch = Archs[0].str();
Yaron Keren658df8b2015-07-20 06:38:39 +000051 // lib: Arch Linux, Ubuntu, Windows
52 // lib64: openSUSE Linux
Yaron Kerene8969392015-07-24 20:18:27 +000053 for (StringRef CandidateLib : {"lib", "lib64"}) {
54 for (StringRef CandidateArch : Archs) {
Yaron Keren8cb250a2015-07-24 11:01:45 +000055 llvm::SmallString<1024> LibDir(Base);
Yaron Kerene8969392015-07-24 20:18:27 +000056 llvm::sys::path::append(LibDir, CandidateLib, "gcc", CandidateArch);
Yaron Keren8cb250a2015-07-24 11:01:45 +000057 if (findGccVersion(LibDir, GccLibDir, Ver)) {
Yaron Kerene8969392015-07-24 20:18:27 +000058 Arch = CandidateArch;
Yaron Keren8cb250a2015-07-24 11:01:45 +000059 return;
60 }
Yaron Keren658df8b2015-07-20 06:38:39 +000061 }
Yaron Keren1c0070c2015-07-02 04:45:27 +000062 }
Yaron Keren28596532015-07-21 11:01:00 +000063}
Yaron Keren658df8b2015-07-20 06:38:39 +000064
Yaron Keren28596532015-07-21 11:01:00 +000065MinGW::MinGW(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
66 : ToolChain(D, Triple, Args) {
67 getProgramPaths().push_back(getDriver().getInstalledDir());
68
Ismail Donmez5797c552016-01-12 10:41:20 +000069// In Windows there aren't any standard install locations, we search
70// for gcc on the PATH. In Linux the base is always /usr.
Yaron Keren28596532015-07-21 11:01:00 +000071#ifdef LLVM_ON_WIN32
Ismail Donmez5797c552016-01-12 10:41:20 +000072 if (getDriver().SysRoot.size())
73 Base = getDriver().SysRoot;
Martell Malonea9692532015-11-23 19:05:19 +000074 else if (llvm::ErrorOr<std::string> GPPName =
75 llvm::sys::findProgramByName("gcc"))
76 Base = llvm::sys::path::parent_path(
77 llvm::sys::path::parent_path(GPPName.get()));
Ismail Donmez5797c552016-01-12 10:41:20 +000078 else
Martell Malonea9692532015-11-23 19:05:19 +000079 Base = llvm::sys::path::parent_path(getDriver().getInstalledDir());
Ismail Donmez5797c552016-01-12 10:41:20 +000080#else
81 if (getDriver().SysRoot.size())
82 Base = getDriver().SysRoot;
83 else
84 Base = "/usr";
85#endif
Martell Malonef6301fa2015-11-23 18:59:48 +000086
Yaron Keren28596532015-07-21 11:01:00 +000087 Base += llvm::sys::path::get_separator();
Yaron Keren327675b2015-07-24 08:50:15 +000088 findGccLibDir();
Yaron Keren1c0070c2015-07-02 04:45:27 +000089 // GccLibDir must precede Base/lib so that the
90 // correct crtbegin.o ,cetend.o would be found.
91 getFilePaths().push_back(GccLibDir);
Yaron Keren327675b2015-07-24 08:50:15 +000092 getFilePaths().push_back(
93 (Base + Arch + llvm::sys::path::get_separator() + "lib").str());
Yaron Keren658df8b2015-07-20 06:38:39 +000094 getFilePaths().push_back(Base + "lib");
Yaron Keren658df8b2015-07-20 06:38:39 +000095 // openSUSE
Yaron Keren327675b2015-07-24 08:50:15 +000096 getFilePaths().push_back(Base + Arch + "/sys-root/mingw/lib");
Yaron Keren1c0070c2015-07-02 04:45:27 +000097}
98
99bool MinGW::IsIntegratedAssemblerDefault() const { return true; }
100
101Tool *MinGW::getTool(Action::ActionClass AC) const {
102 switch (AC) {
103 case Action::PreprocessJobClass:
104 if (!Preprocessor)
105 Preprocessor.reset(new tools::gcc::Preprocessor(*this));
106 return Preprocessor.get();
107 case Action::CompileJobClass:
108 if (!Compiler)
109 Compiler.reset(new tools::gcc::Compiler(*this));
110 return Compiler.get();
111 default:
112 return ToolChain::getTool(AC);
113 }
114}
115
116Tool *MinGW::buildAssembler() const {
117 return new tools::MinGW::Assembler(*this);
118}
119
120Tool *MinGW::buildLinker() const { return new tools::MinGW::Linker(*this); }
121
122bool MinGW::IsUnwindTablesDefault() const {
123 return getArch() == llvm::Triple::x86_64;
124}
125
126bool MinGW::isPICDefault() const { return getArch() == llvm::Triple::x86_64; }
127
128bool MinGW::isPIEDefault() const { return false; }
129
130bool MinGW::isPICDefaultForced() const {
131 return getArch() == llvm::Triple::x86_64;
132}
133
134bool MinGW::UseSEHExceptions() const {
135 return getArch() == llvm::Triple::x86_64;
136}
137
Yaron Keren28596532015-07-21 11:01:00 +0000138// Include directories for various hosts:
139
140// Windows, mingw.org
Yaron Keren327675b2015-07-24 08:50:15 +0000141// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++
142// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32
143// c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward
144// c:\mingw\lib\gcc\mingw32\4.8.1\include
145// c:\mingw\include
146// c:\mingw\lib\gcc\mingw32\4.8.1\include-fixed
Yaron Keren28596532015-07-21 11:01:00 +0000147// c:\mingw\mingw32\include
148
149// Windows, mingw-w64 mingw-builds
Yaron Keren327675b2015-07-24 08:50:15 +0000150// c:\mingw32\lib\gcc\i686-w64-mingw32\4.9.1\include
151// c:\mingw32\lib\gcc\i686-w64-mingw32\4.9.1\include-fixed
152// c:\mingw32\i686-w64-mingw32\include
153// c:\mingw32\i686-w64-mingw32\include\c++
154// c:\mingw32\i686-w64-mingw32\include\c++\i686-w64-mingw32
155// c:\mingw32\i686-w64-mingw32\include\c++\backward
Yaron Keren28596532015-07-21 11:01:00 +0000156
157// Windows, mingw-w64 msys2
Yaron Keren327675b2015-07-24 08:50:15 +0000158// c:\msys64\mingw32\lib\gcc\i686-w64-mingw32\4.9.2\include
159// c:\msys64\mingw32\include
160// c:\msys64\mingw32\lib\gcc\i686-w64-mingw32\4.9.2\include-fixed
161// c:\msys64\mingw32\i686-w64-mingw32\include
162// c:\msys64\mingw32\include\c++\4.9.2
163// c:\msys64\mingw32\include\c++\4.9.2\i686-w64-mingw32
Yaron Keren28596532015-07-21 11:01:00 +0000164// c:\msys64\mingw32\include\c++\4.9.2\backward
165
166// openSUSE
167// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++
168// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32
169// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include/c++/backward
170// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include
171// /usr/lib64/gcc/x86_64-w64-mingw32/5.1.0/include-fixed
172// /usr/x86_64-w64-mingw32/sys-root/mingw/include
173
174// Arch Linux
Yaron Keren327675b2015-07-24 08:50:15 +0000175// /usr/i686-w64-mingw32/include/c++/5.1.0
176// /usr/i686-w64-mingw32/include/c++/5.1.0/i686-w64-mingw32
177// /usr/i686-w64-mingw32/include/c++/5.1.0/backward
178// /usr/lib/gcc/i686-w64-mingw32/5.1.0/include
179// /usr/lib/gcc/i686-w64-mingw32/5.1.0/include-fixed
Yaron Keren28596532015-07-21 11:01:00 +0000180// /usr/i686-w64-mingw32/include
181
182// Ubuntu
Yaron Keren327675b2015-07-24 08:50:15 +0000183// /usr/include/c++/4.8
184// /usr/include/c++/4.8/x86_64-w64-mingw32
185// /usr/include/c++/4.8/backward
186// /usr/lib/gcc/x86_64-w64-mingw32/4.8/include
187// /usr/lib/gcc/x86_64-w64-mingw32/4.8/include-fixed
188// /usr/x86_64-w64-mingw32/include
Yaron Keren28596532015-07-21 11:01:00 +0000189
Yaron Keren1c0070c2015-07-02 04:45:27 +0000190void MinGW::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
191 ArgStringList &CC1Args) const {
192 if (DriverArgs.hasArg(options::OPT_nostdinc))
193 return;
194
195 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
196 SmallString<1024> P(getDriver().ResourceDir);
197 llvm::sys::path::append(P, "include");
198 addSystemInclude(DriverArgs, CC1Args, P.str());
199 }
200
201 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
202 return;
203
Reid Kleckner0213a472015-07-22 16:01:38 +0000204 if (GetRuntimeLibType(DriverArgs) == ToolChain::RLT_Libgcc) {
205 llvm::SmallString<1024> IncludeDir(GccLibDir);
206 llvm::sys::path::append(IncludeDir, "include");
207 addSystemInclude(DriverArgs, CC1Args, IncludeDir.c_str());
208 IncludeDir += "-fixed";
Reid Kleckner0213a472015-07-22 16:01:38 +0000209 // openSUSE
210 addSystemInclude(DriverArgs, CC1Args,
Yaron Kerena749b1b2015-07-24 19:18:17 +0000211 Base + Arch + "/sys-root/mingw/include");
Reid Kleckner0213a472015-07-22 16:01:38 +0000212 addSystemInclude(DriverArgs, CC1Args, IncludeDir.c_str());
213 }
Yaron Keren327675b2015-07-24 08:50:15 +0000214 addSystemInclude(DriverArgs, CC1Args,
215 Base + Arch + llvm::sys::path::get_separator() + "include");
Yaron Keren1c0070c2015-07-02 04:45:27 +0000216 addSystemInclude(DriverArgs, CC1Args, Base + "include");
217}
218
219void MinGW::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
220 ArgStringList &CC1Args) const {
221 if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
222 DriverArgs.hasArg(options::OPT_nostdincxx))
223 return;
224
Reid Kleckner0213a472015-07-22 16:01:38 +0000225 switch (GetCXXStdlibType(DriverArgs)) {
226 case ToolChain::CST_Libcxx:
Yaron Keren327675b2015-07-24 08:50:15 +0000227 addSystemInclude(DriverArgs, CC1Args,
228 Base + "include" + llvm::sys::path::get_separator() +
229 "c++" + llvm::sys::path::get_separator() + "v1");
Reid Kleckner0213a472015-07-22 16:01:38 +0000230 break;
231
232 case ToolChain::CST_Libstdcxx:
233 llvm::SmallVector<llvm::SmallString<1024>, 4> CppIncludeBases;
234 CppIncludeBases.emplace_back(Base);
235 llvm::sys::path::append(CppIncludeBases[0], Arch, "include", "c++");
236 CppIncludeBases.emplace_back(Base);
237 llvm::sys::path::append(CppIncludeBases[1], Arch, "include", "c++", Ver);
238 CppIncludeBases.emplace_back(Base);
239 llvm::sys::path::append(CppIncludeBases[2], "include", "c++", Ver);
240 CppIncludeBases.emplace_back(GccLibDir);
241 llvm::sys::path::append(CppIncludeBases[3], "include", "c++");
242 for (auto &CppIncludeBase : CppIncludeBases) {
Reid Kleckner0213a472015-07-22 16:01:38 +0000243 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
Yaron Keren327675b2015-07-24 08:50:15 +0000244 CppIncludeBase += llvm::sys::path::get_separator();
Reid Kleckner0213a472015-07-22 16:01:38 +0000245 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + Arch);
246 addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward");
247 }
248 break;
Yaron Keren1c0070c2015-07-02 04:45:27 +0000249 }
250}