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