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