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