blob: da914befa7c8524ca6f0afcf23a2bb3d3830ca4e [file] [log] [blame]
Chandler Carruth79cbbdc2011-12-17 23:10:01 +00001//===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
2//
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"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000011#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000012#include "clang/Basic/Version.h"
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000013#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Driver.h"
Rafael Espindola29511872013-03-24 15:06:53 +000015#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000016#include "clang/Driver/Options.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000017#include "llvm/Option/Arg.h"
18#include "llvm/Option/ArgList.h"
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/Path.h"
Rafael Espindolaa372f402013-06-17 17:23:47 +000021#include "llvm/Support/PathV1.h"
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000022
23// Include the necessary headers to interface with the Windows registry and
24// environment.
25#ifdef _MSC_VER
26 #define WIN32_LEAN_AND_MEAN
27 #define NOGDI
28 #define NOMINMAX
29 #include <Windows.h>
30#endif
31
32using namespace clang::driver;
33using namespace clang::driver::toolchains;
34using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000035using namespace llvm::opt;
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000036
Rafael Espindolaaf370e62013-03-18 18:10:27 +000037Windows::Windows(const Driver &D, const llvm::Triple& Triple,
38 const ArgList &Args)
39 : ToolChain(D, Triple, Args) {
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000040}
41
Rafael Espindolaf48b93c2013-03-20 03:05:54 +000042Tool *Windows::buildLinker() const {
43 return new tools::visualstudio::Link(*this);
44}
45
46Tool *Windows::buildAssembler() const {
47 if (getTriple().getEnvironment() == llvm::Triple::MachO)
48 return new tools::darwin::Assemble(*this);
Rafael Espindola29511872013-03-24 15:06:53 +000049 getDriver().Diag(clang::diag::err_no_external_windows_assembler);
50 return NULL;
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000051}
52
53bool Windows::IsIntegratedAssemblerDefault() const {
54 return true;
55}
56
57bool Windows::IsUnwindTablesDefault() const {
Rafael Espindolaa4a809e2012-10-07 03:23:40 +000058 return getArch() == llvm::Triple::x86_64;
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000059}
60
Chandler Carruth7ce816a2012-11-19 03:52:03 +000061bool Windows::isPICDefault() const {
62 return getArch() == llvm::Triple::x86_64;
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000063}
64
Peter Collingbourne52ca70d2013-04-09 04:35:11 +000065bool Windows::isPIEDefault() const {
66 return false;
67}
68
Chandler Carruth7ce816a2012-11-19 03:52:03 +000069bool Windows::isPICDefaultForced() const {
70 return getArch() == llvm::Triple::x86_64;
Chandler Carruth79cbbdc2011-12-17 23:10:01 +000071}
72
73// FIXME: This probably should goto to some platform utils place.
74#ifdef _MSC_VER
75
76/// \brief Read registry string.
77/// This also supports a means to look for high-versioned keys by use
78/// of a $VERSION placeholder in the key path.
79/// $VERSION in the key path is a placeholder for the version number,
80/// causing the highest value path to be searched for and used.
81/// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
82/// There can be additional characters in the component. Only the numberic
83/// characters are compared.
84static bool getSystemRegistryString(const char *keyPath, const char *valueName,
85 char *value, size_t maxLength) {
86 HKEY hRootKey = NULL;
87 HKEY hKey = NULL;
88 const char* subKey = NULL;
89 DWORD valueType;
90 DWORD valueSize = maxLength - 1;
91 long lResult;
92 bool returnValue = false;
93
94 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
95 hRootKey = HKEY_CLASSES_ROOT;
96 subKey = keyPath + 18;
97 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
98 hRootKey = HKEY_USERS;
99 subKey = keyPath + 11;
100 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
101 hRootKey = HKEY_LOCAL_MACHINE;
102 subKey = keyPath + 19;
103 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
104 hRootKey = HKEY_CURRENT_USER;
105 subKey = keyPath + 18;
106 } else {
107 return false;
108 }
109
110 const char *placeHolder = strstr(subKey, "$VERSION");
111 char bestName[256];
112 bestName[0] = '\0';
113 // If we have a $VERSION placeholder, do the highest-version search.
114 if (placeHolder) {
115 const char *keyEnd = placeHolder - 1;
116 const char *nextKey = placeHolder;
117 // Find end of previous key.
118 while ((keyEnd > subKey) && (*keyEnd != '\\'))
119 keyEnd--;
120 // Find end of key containing $VERSION.
121 while (*nextKey && (*nextKey != '\\'))
122 nextKey++;
123 size_t partialKeyLength = keyEnd - subKey;
124 char partialKey[256];
125 if (partialKeyLength > sizeof(partialKey))
126 partialKeyLength = sizeof(partialKey);
127 strncpy(partialKey, subKey, partialKeyLength);
128 partialKey[partialKeyLength] = '\0';
129 HKEY hTopKey = NULL;
130 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
131 if (lResult == ERROR_SUCCESS) {
132 char keyName[256];
133 int bestIndex = -1;
134 double bestValue = 0.0;
135 DWORD index, size = sizeof(keyName) - 1;
136 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
137 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
138 const char *sp = keyName;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000139 while (*sp && !isDigit(*sp))
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000140 sp++;
141 if (!*sp)
142 continue;
143 const char *ep = sp + 1;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000144 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000145 ep++;
146 char numBuf[32];
147 strncpy(numBuf, sp, sizeof(numBuf) - 1);
148 numBuf[sizeof(numBuf) - 1] = '\0';
149 double value = strtod(numBuf, NULL);
150 if (value > bestValue) {
151 bestIndex = (int)index;
152 bestValue = value;
153 strcpy(bestName, keyName);
154 }
155 size = sizeof(keyName) - 1;
156 }
157 // If we found the highest versioned key, open the key and get the value.
158 if (bestIndex != -1) {
159 // Append rest of key.
160 strncat(bestName, nextKey, sizeof(bestName) - 1);
161 bestName[sizeof(bestName) - 1] = '\0';
162 // Open the chosen key path remainder.
163 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
164 if (lResult == ERROR_SUCCESS) {
165 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
166 (LPBYTE)value, &valueSize);
167 if (lResult == ERROR_SUCCESS)
168 returnValue = true;
169 RegCloseKey(hKey);
170 }
171 }
172 RegCloseKey(hTopKey);
173 }
174 } else {
175 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
176 if (lResult == ERROR_SUCCESS) {
177 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
178 (LPBYTE)value, &valueSize);
179 if (lResult == ERROR_SUCCESS)
180 returnValue = true;
181 RegCloseKey(hKey);
182 }
183 }
184 return returnValue;
185}
186
187/// \brief Get Windows SDK installation directory.
188static bool getWindowsSDKDir(std::string &path) {
189 char windowsSDKInstallDir[256];
190 // Try the Windows registry.
191 bool hasSDKDir = getSystemRegistryString(
192 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
193 "InstallationFolder",
194 windowsSDKInstallDir,
195 sizeof(windowsSDKInstallDir) - 1);
196 // If we have both vc80 and vc90, pick version we were compiled with.
197 if (hasSDKDir && windowsSDKInstallDir[0]) {
198 path = windowsSDKInstallDir;
199 return true;
200 }
201 return false;
202}
203
204 // Get Visual Studio installation directory.
205static bool getVisualStudioDir(std::string &path) {
206 // First check the environment variables that vsvars32.bat sets.
207 const char* vcinstalldir = getenv("VCINSTALLDIR");
208 if (vcinstalldir) {
209 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
210 if (p)
211 *p = '\0';
212 path = vcinstalldir;
213 return true;
214 }
215
216 char vsIDEInstallDir[256];
217 char vsExpressIDEInstallDir[256];
218 // Then try the windows registry.
219 bool hasVCDir = getSystemRegistryString(
220 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
221 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
222 bool hasVCExpressDir = getSystemRegistryString(
223 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
224 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
225 // If we have both vc80 and vc90, pick version we were compiled with.
226 if (hasVCDir && vsIDEInstallDir[0]) {
227 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
228 if (p)
229 *p = '\0';
230 path = vsIDEInstallDir;
231 return true;
232 }
233
234 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
235 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
236 if (p)
237 *p = '\0';
238 path = vsExpressIDEInstallDir;
239 return true;
240 }
241
242 // Try the environment.
243 const char *vs100comntools = getenv("VS100COMNTOOLS");
244 const char *vs90comntools = getenv("VS90COMNTOOLS");
245 const char *vs80comntools = getenv("VS80COMNTOOLS");
246 const char *vscomntools = NULL;
247
248 // Try to find the version that we were compiled with
249 if(false) {}
250 #if (_MSC_VER >= 1600) // VC100
251 else if(vs100comntools) {
252 vscomntools = vs100comntools;
253 }
254 #elif (_MSC_VER == 1500) // VC80
255 else if(vs90comntools) {
256 vscomntools = vs90comntools;
257 }
258 #elif (_MSC_VER == 1400) // VC80
259 else if(vs80comntools) {
260 vscomntools = vs80comntools;
261 }
262 #endif
263 // Otherwise find any version we can
264 else if (vs100comntools)
265 vscomntools = vs100comntools;
266 else if (vs90comntools)
267 vscomntools = vs90comntools;
268 else if (vs80comntools)
269 vscomntools = vs80comntools;
270
271 if (vscomntools && *vscomntools) {
272 const char *p = strstr(vscomntools, "\\Common7\\Tools");
273 path = p ? std::string(vscomntools, p) : vscomntools;
274 return true;
275 }
276 return false;
277}
278
279#endif // _MSC_VER
280
281void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
282 ArgStringList &CC1Args) const {
283 if (DriverArgs.hasArg(options::OPT_nostdinc))
284 return;
285
286 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
287 llvm::sys::Path P(getDriver().ResourceDir);
288 P.appendComponent("include");
289 addSystemInclude(DriverArgs, CC1Args, P.str());
290 }
291
292 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
293 return;
294
Joao Matos5d9cb1b2012-09-04 17:29:52 +0000295#ifdef _MSC_VER
296 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
297 if (const char *cl_include_dir = getenv("INCLUDE")) {
298 SmallVector<StringRef, 8> Dirs;
299 StringRef(cl_include_dir).split(Dirs, ";");
300 int n = 0;
301 for (SmallVectorImpl<StringRef>::iterator I = Dirs.begin(), E = Dirs.end();
302 I != E; ++I) {
303 StringRef d = *I;
304 if (d.size() == 0)
305 continue;
306 ++n;
307 addSystemInclude(DriverArgs, CC1Args, d);
308 }
309 if (n) return;
310 }
311
312 std::string VSDir;
313 std::string WindowsSDKDir;
314
315 // When built with access to the proper Windows APIs, try to actually find
316 // the correct include paths first.
317 if (getVisualStudioDir(VSDir)) {
318 addSystemInclude(DriverArgs, CC1Args, VSDir + "\\VC\\include");
319 if (getWindowsSDKDir(WindowsSDKDir))
320 addSystemInclude(DriverArgs, CC1Args, WindowsSDKDir + "\\include");
321 else
322 addSystemInclude(DriverArgs, CC1Args,
323 VSDir + "\\VC\\PlatformSDK\\Include");
324 return;
325 }
326#endif // _MSC_VER
327
328 // As a fallback, select default install paths.
329 const StringRef Paths[] = {
330 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
331 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
332 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
333 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
334 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
335 };
336 addSystemIncludes(DriverArgs, CC1Args, Paths);
Chandler Carruth79cbbdc2011-12-17 23:10:01 +0000337}
338
339void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
340 ArgStringList &CC1Args) const {
341 // FIXME: There should probably be logic here to find libc++ on Windows.
342}