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