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