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