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