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"
11 #include "clang/Basic/CharInfo.h"
12 #include "clang/Basic/Version.h"
13 #include "clang/Driver/Compilation.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/Options.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Path.h"
22 // Include the necessary headers to interface with the Windows registry and
25 #define WIN32_LEAN_AND_MEAN
31 using namespace clang::driver;
32 using namespace clang::driver::toolchains;
33 using namespace clang;
34 using namespace llvm::opt;
36 Windows::Windows(const Driver &D, const llvm::Triple& Triple,
38 : ToolChain(D, Triple, Args) {
41 Tool *Windows::buildLinker() const {
42 return new tools::visualstudio::Link(*this);
45 Tool *Windows::buildAssembler() const {
46 if (getTriple().getEnvironment() == llvm::Triple::MachO)
47 return new tools::darwin::Assemble(*this);
48 getDriver().Diag(clang::diag::err_no_external_assembler);
52 bool Windows::IsIntegratedAssemblerDefault() const {
56 bool Windows::IsUnwindTablesDefault() const {
57 return getArch() == llvm::Triple::x86_64;
60 bool Windows::isPICDefault() const {
61 return getArch() == llvm::Triple::x86_64;
64 bool Windows::isPIEDefault() const {
68 bool Windows::isPICDefaultForced() const {
69 return getArch() == llvm::Triple::x86_64;
72 // FIXME: This probably should goto to some platform utils place.
75 /// \brief Read registry string.
76 /// This also supports a means to look for high-versioned keys by use
77 /// of a $VERSION placeholder in the key path.
78 /// $VERSION in the key path is a placeholder for the version number,
79 /// causing the highest value path to be searched for and used.
80 /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
81 /// There can be additional characters in the component. Only the numberic
82 /// characters are compared.
83 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
84 char *value, size_t maxLength) {
87 const char* subKey = NULL;
89 DWORD valueSize = maxLength - 1;
91 bool returnValue = false;
93 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
94 hRootKey = HKEY_CLASSES_ROOT;
95 subKey = keyPath + 18;
96 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
97 hRootKey = HKEY_USERS;
98 subKey = keyPath + 11;
99 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
100 hRootKey = HKEY_LOCAL_MACHINE;
101 subKey = keyPath + 19;
102 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
103 hRootKey = HKEY_CURRENT_USER;
104 subKey = keyPath + 18;
109 const char *placeHolder = strstr(subKey, "$VERSION");
112 // If we have a $VERSION placeholder, do the highest-version search.
114 const char *keyEnd = placeHolder - 1;
115 const char *nextKey = placeHolder;
116 // Find end of previous key.
117 while ((keyEnd > subKey) && (*keyEnd != '\\'))
119 // Find end of key containing $VERSION.
120 while (*nextKey && (*nextKey != '\\'))
122 size_t partialKeyLength = keyEnd - subKey;
123 char partialKey[256];
124 if (partialKeyLength > sizeof(partialKey))
125 partialKeyLength = sizeof(partialKey);
126 strncpy(partialKey, subKey, partialKeyLength);
127 partialKey[partialKeyLength] = '\0';
129 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
131 if (lResult == ERROR_SUCCESS) {
134 double bestValue = 0.0;
135 DWORD index, size = sizeof(keyName) - 1;
136 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
137 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
138 const char *sp = keyName;
139 while (*sp && !isDigit(*sp))
143 const char *ep = sp + 1;
144 while (*ep && (isDigit(*ep) || (*ep == '.')))
147 strncpy(numBuf, sp, sizeof(numBuf) - 1);
148 numBuf[sizeof(numBuf) - 1] = '\0';
149 double dvalue = strtod(numBuf, NULL);
150 if (dvalue > bestValue) {
151 // Test that InstallDir is indeed there before keeping this index.
152 // Open the chosen key path remainder.
153 strcpy(bestName, keyName);
154 // Append rest of key.
155 strncat(bestName, nextKey, sizeof(bestName) - 1);
156 bestName[sizeof(bestName) - 1] = '\0';
157 lResult = RegOpenKeyEx(hTopKey, bestName, 0,
158 KEY_READ | KEY_WOW64_32KEY, &hKey);
159 if (lResult == ERROR_SUCCESS) {
160 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
161 (LPBYTE)value, &valueSize);
162 if (lResult == ERROR_SUCCESS) {
163 bestIndex = (int)index;
170 size = sizeof(keyName) - 1;
172 RegCloseKey(hTopKey);
175 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY,
177 if (lResult == ERROR_SUCCESS) {
178 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
179 (LPBYTE)value, &valueSize);
180 if (lResult == ERROR_SUCCESS)
188 /// \brief Get Windows SDK installation directory.
189 static bool getWindowsSDKDir(std::string &path) {
190 char windowsSDKInstallDir[256];
191 // Try the Windows registry.
192 bool hasSDKDir = getSystemRegistryString(
193 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
194 "InstallationFolder",
195 windowsSDKInstallDir,
196 sizeof(windowsSDKInstallDir) - 1);
197 // If we have both vc80 and vc90, pick version we were compiled with.
198 if (hasSDKDir && windowsSDKInstallDir[0]) {
199 path = windowsSDKInstallDir;
205 // Get Visual Studio installation directory.
206 static bool getVisualStudioDir(std::string &path) {
207 // First check the environment variables that vsvars32.bat sets.
208 const char* vcinstalldir = getenv("VCINSTALLDIR");
210 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
217 char vsIDEInstallDir[256];
218 char vsExpressIDEInstallDir[256];
219 // Then try the windows registry.
220 bool hasVCDir = getSystemRegistryString(
221 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
222 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
223 bool hasVCExpressDir = getSystemRegistryString(
224 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
225 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
226 // If we have both vc80 and vc90, pick version we were compiled with.
227 if (hasVCDir && vsIDEInstallDir[0]) {
228 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
231 path = vsIDEInstallDir;
235 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
236 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
239 path = vsExpressIDEInstallDir;
243 // Try the environment.
244 const char *vs100comntools = getenv("VS100COMNTOOLS");
245 const char *vs90comntools = getenv("VS90COMNTOOLS");
246 const char *vs80comntools = getenv("VS80COMNTOOLS");
247 const char *vscomntools = NULL;
249 // Try to find the version that we were compiled with
251 #if (_MSC_VER >= 1600) // VC100
252 else if(vs100comntools) {
253 vscomntools = vs100comntools;
255 #elif (_MSC_VER == 1500) // VC80
256 else if(vs90comntools) {
257 vscomntools = vs90comntools;
259 #elif (_MSC_VER == 1400) // VC80
260 else if(vs80comntools) {
261 vscomntools = vs80comntools;
264 // Otherwise find any version we can
265 else if (vs100comntools)
266 vscomntools = vs100comntools;
267 else if (vs90comntools)
268 vscomntools = vs90comntools;
269 else if (vs80comntools)
270 vscomntools = vs80comntools;
272 if (vscomntools && *vscomntools) {
273 const char *p = strstr(vscomntools, "\\Common7\\Tools");
274 path = p ? std::string(vscomntools, p) : vscomntools;
282 void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
283 ArgStringList &CC1Args) const {
284 if (DriverArgs.hasArg(options::OPT_nostdinc))
287 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
288 SmallString<128> P(getDriver().ResourceDir);
289 llvm::sys::path::append(P, "include");
290 addSystemInclude(DriverArgs, CC1Args, P.str());
293 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
297 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
298 if (const char *cl_include_dir = getenv("INCLUDE")) {
299 SmallVector<StringRef, 8> Dirs;
300 StringRef(cl_include_dir).split(Dirs, ";");
302 for (SmallVectorImpl<StringRef>::iterator I = Dirs.begin(), E = Dirs.end();
308 addSystemInclude(DriverArgs, CC1Args, d);
314 std::string WindowsSDKDir;
316 // When built with access to the proper Windows APIs, try to actually find
317 // the correct include paths first.
318 if (getVisualStudioDir(VSDir)) {
319 addSystemInclude(DriverArgs, CC1Args, VSDir + "\\VC\\include");
320 if (getWindowsSDKDir(WindowsSDKDir))
321 addSystemInclude(DriverArgs, CC1Args, WindowsSDKDir + "\\include");
323 addSystemInclude(DriverArgs, CC1Args,
324 VSDir + "\\VC\\PlatformSDK\\Include");
329 // As a fallback, select default install paths.
330 const StringRef Paths[] = {
331 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
332 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
333 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
334 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
335 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
337 addSystemIncludes(DriverArgs, CC1Args, Paths);
340 void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
341 ArgStringList &CC1Args) const {
342 // FIXME: There should probably be logic here to find libc++ on Windows.