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