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