1 //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ToolChains.h"
12 #include "clang/Driver/Arg.h"
13 #include "clang/Driver/ArgList.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/Options.h"
17 #include "clang/Basic/Version.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/Path.h"
21 // Include the necessary headers to interface with the Windows registry and
24 #define WIN32_LEAN_AND_MEAN
30 using namespace clang::driver;
31 using namespace clang::driver::toolchains;
32 using namespace clang;
34 Windows::Windows(const Driver &D, const llvm::Triple& Triple)
35 : ToolChain(D, Triple) {
38 Tool &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;
46 bool UseIntegratedAs = C.getArgs().hasFlag(options::OPT_integrated_as,
47 options::OPT_no_integrated_as,
48 IsIntegratedAssemblerDefault());
50 Tool *&T = Tools[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::MigrateJobClass:
63 case Action::CompileJobClass:
64 T = new tools::Clang(*this); break;
65 case Action::AssembleJobClass:
66 if (!UseIntegratedAs && getTriple().getEnvironment() == llvm::Triple::MachO)
67 T = new tools::darwin::Assemble(*this);
69 T = new tools::ClangAs(*this);
71 case Action::LinkJobClass:
72 T = new tools::visualstudio::Link(*this); break;
79 bool Windows::IsIntegratedAssemblerDefault() const {
83 bool Windows::IsUnwindTablesDefault() const {
84 return getArch() == llvm::Triple::x86_64;
87 bool Windows::isPICDefault() const {
88 return getArch() == llvm::Triple::x86_64;
91 bool Windows::isPICDefaultForced() const {
92 return getArch() == llvm::Triple::x86_64;
95 // FIXME: This probably should goto to some platform utils place.
98 /// \brief Read registry string.
99 /// This also supports a means to look for high-versioned keys by use
100 /// of a $VERSION placeholder in the key path.
101 /// $VERSION in the key path is a placeholder for the version number,
102 /// causing the highest value path to be searched for and used.
103 /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
104 /// There can be additional characters in the component. Only the numberic
105 /// characters are compared.
106 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
107 char *value, size_t maxLength) {
108 HKEY hRootKey = NULL;
110 const char* subKey = NULL;
112 DWORD valueSize = maxLength - 1;
114 bool returnValue = false;
116 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
117 hRootKey = HKEY_CLASSES_ROOT;
118 subKey = keyPath + 18;
119 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
120 hRootKey = HKEY_USERS;
121 subKey = keyPath + 11;
122 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
123 hRootKey = HKEY_LOCAL_MACHINE;
124 subKey = keyPath + 19;
125 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
126 hRootKey = HKEY_CURRENT_USER;
127 subKey = keyPath + 18;
132 const char *placeHolder = strstr(subKey, "$VERSION");
135 // If we have a $VERSION placeholder, do the highest-version search.
137 const char *keyEnd = placeHolder - 1;
138 const char *nextKey = placeHolder;
139 // Find end of previous key.
140 while ((keyEnd > subKey) && (*keyEnd != '\\'))
142 // Find end of key containing $VERSION.
143 while (*nextKey && (*nextKey != '\\'))
145 size_t partialKeyLength = keyEnd - subKey;
146 char partialKey[256];
147 if (partialKeyLength > sizeof(partialKey))
148 partialKeyLength = sizeof(partialKey);
149 strncpy(partialKey, subKey, partialKeyLength);
150 partialKey[partialKeyLength] = '\0';
152 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
153 if (lResult == ERROR_SUCCESS) {
156 double bestValue = 0.0;
157 DWORD index, size = sizeof(keyName) - 1;
158 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
159 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
160 const char *sp = keyName;
161 while (*sp && !isdigit(*sp))
165 const char *ep = sp + 1;
166 while (*ep && (isdigit(*ep) || (*ep == '.')))
169 strncpy(numBuf, sp, sizeof(numBuf) - 1);
170 numBuf[sizeof(numBuf) - 1] = '\0';
171 double value = strtod(numBuf, NULL);
172 if (value > bestValue) {
173 bestIndex = (int)index;
175 strcpy(bestName, keyName);
177 size = sizeof(keyName) - 1;
179 // If we found the highest versioned key, open the key and get the value.
180 if (bestIndex != -1) {
181 // Append rest of key.
182 strncat(bestName, nextKey, sizeof(bestName) - 1);
183 bestName[sizeof(bestName) - 1] = '\0';
184 // Open the chosen key path remainder.
185 lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
186 if (lResult == ERROR_SUCCESS) {
187 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
188 (LPBYTE)value, &valueSize);
189 if (lResult == ERROR_SUCCESS)
194 RegCloseKey(hTopKey);
197 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
198 if (lResult == ERROR_SUCCESS) {
199 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
200 (LPBYTE)value, &valueSize);
201 if (lResult == ERROR_SUCCESS)
209 /// \brief Get Windows SDK installation directory.
210 static bool getWindowsSDKDir(std::string &path) {
211 char windowsSDKInstallDir[256];
212 // Try the Windows registry.
213 bool hasSDKDir = getSystemRegistryString(
214 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
215 "InstallationFolder",
216 windowsSDKInstallDir,
217 sizeof(windowsSDKInstallDir) - 1);
218 // If we have both vc80 and vc90, pick version we were compiled with.
219 if (hasSDKDir && windowsSDKInstallDir[0]) {
220 path = windowsSDKInstallDir;
226 // Get Visual Studio installation directory.
227 static bool getVisualStudioDir(std::string &path) {
228 // First check the environment variables that vsvars32.bat sets.
229 const char* vcinstalldir = getenv("VCINSTALLDIR");
231 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
238 char vsIDEInstallDir[256];
239 char vsExpressIDEInstallDir[256];
240 // Then try the windows registry.
241 bool hasVCDir = getSystemRegistryString(
242 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
243 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
244 bool hasVCExpressDir = getSystemRegistryString(
245 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
246 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
247 // If we have both vc80 and vc90, pick version we were compiled with.
248 if (hasVCDir && vsIDEInstallDir[0]) {
249 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
252 path = vsIDEInstallDir;
256 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
257 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
260 path = vsExpressIDEInstallDir;
264 // Try the environment.
265 const char *vs100comntools = getenv("VS100COMNTOOLS");
266 const char *vs90comntools = getenv("VS90COMNTOOLS");
267 const char *vs80comntools = getenv("VS80COMNTOOLS");
268 const char *vscomntools = NULL;
270 // Try to find the version that we were compiled with
272 #if (_MSC_VER >= 1600) // VC100
273 else if(vs100comntools) {
274 vscomntools = vs100comntools;
276 #elif (_MSC_VER == 1500) // VC80
277 else if(vs90comntools) {
278 vscomntools = vs90comntools;
280 #elif (_MSC_VER == 1400) // VC80
281 else if(vs80comntools) {
282 vscomntools = vs80comntools;
285 // Otherwise find any version we can
286 else if (vs100comntools)
287 vscomntools = vs100comntools;
288 else if (vs90comntools)
289 vscomntools = vs90comntools;
290 else if (vs80comntools)
291 vscomntools = vs80comntools;
293 if (vscomntools && *vscomntools) {
294 const char *p = strstr(vscomntools, "\\Common7\\Tools");
295 path = p ? std::string(vscomntools, p) : vscomntools;
303 void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
304 ArgStringList &CC1Args) const {
305 if (DriverArgs.hasArg(options::OPT_nostdinc))
308 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
309 llvm::sys::Path P(getDriver().ResourceDir);
310 P.appendComponent("include");
311 addSystemInclude(DriverArgs, CC1Args, P.str());
314 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
318 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
319 if (const char *cl_include_dir = getenv("INCLUDE")) {
320 SmallVector<StringRef, 8> Dirs;
321 StringRef(cl_include_dir).split(Dirs, ";");
323 for (SmallVectorImpl<StringRef>::iterator I = Dirs.begin(), E = Dirs.end();
329 addSystemInclude(DriverArgs, CC1Args, d);
335 std::string WindowsSDKDir;
337 // When built with access to the proper Windows APIs, try to actually find
338 // the correct include paths first.
339 if (getVisualStudioDir(VSDir)) {
340 addSystemInclude(DriverArgs, CC1Args, VSDir + "\\VC\\include");
341 if (getWindowsSDKDir(WindowsSDKDir))
342 addSystemInclude(DriverArgs, CC1Args, WindowsSDKDir + "\\include");
344 addSystemInclude(DriverArgs, CC1Args,
345 VSDir + "\\VC\\PlatformSDK\\Include");
350 // As a fallback, select default install paths.
351 const StringRef Paths[] = {
352 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
353 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
354 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
355 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
356 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
358 addSystemIncludes(DriverArgs, CC1Args, Paths);
361 void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
362 ArgStringList &CC1Args) const {
363 // FIXME: There should probably be logic here to find libc++ on Windows.