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