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