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