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