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