blob: 882c783071ccba3a01702ecf2a5e8eaffb98d787 [file] [log] [blame]
Chandler Carruth1fc603e2011-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"
Jordan Rosea7d03842013-02-08 22:30:41 +000011#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "clang/Basic/Version.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000013#include "clang/Driver/Compilation.h"
14#include "clang/Driver/Driver.h"
Rafael Espindola79764462013-03-24 15:06:53 +000015#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000016#include "clang/Driver/Options.h"
Alp Tokerf1ffc842014-06-22 04:31:15 +000017#include "llvm/Config/llvm-config.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000018#include "llvm/Option/Arg.h"
19#include "llvm/Option/ArgList.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000020#include "llvm/Support/ErrorHandling.h"
Hans Wennborge6b994e2014-10-20 23:26:03 +000021#include "llvm/Support/Path.h"
Chandler Carruth1fc603e2011-12-17 23:10:01 +000022
23// Include the necessary headers to interface with the Windows registry and
24// environment.
Alp Tokerf1ffc842014-06-22 04:31:15 +000025#if defined(LLVM_ON_WIN32)
Alp Tokerfcce1832014-06-22 03:27:45 +000026#define USE_WIN32
27#endif
28
29#ifdef USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +000030 #define WIN32_LEAN_AND_MEAN
31 #define NOGDI
32 #define NOMINMAX
Logan Chien733e3c62014-06-24 16:18:10 +000033 #include <windows.h>
Chandler Carruth1fc603e2011-12-17 23:10:01 +000034#endif
35
36using namespace clang::driver;
37using namespace clang::driver::toolchains;
38using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000039using namespace llvm::opt;
Chandler Carruth1fc603e2011-12-17 23:10:01 +000040
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000041MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple,
42 const ArgList &Args)
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000043 : ToolChain(D, Triple, Args) {
44}
45
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000046Tool *MSVCToolChain::buildLinker() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000047 return new tools::visualstudio::Link(*this);
48}
49
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000050Tool *MSVCToolChain::buildAssembler() const {
Saleem Abdulrasool377066a2014-03-27 22:50:18 +000051 if (getTriple().isOSBinFormatMachO())
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000052 return new tools::darwin::Assemble(*this);
Alp Tokerc8d4f0f2013-11-22 08:27:46 +000053 getDriver().Diag(clang::diag::err_no_external_assembler);
Craig Topper92fc2df2014-05-17 16:56:41 +000054 return nullptr;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000055}
56
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000057bool MSVCToolChain::IsIntegratedAssemblerDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000058 return true;
59}
60
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000061bool MSVCToolChain::IsUnwindTablesDefault() const {
Reid Kleckner6b3a9402014-09-04 18:13:12 +000062 // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms
63 // such as ARM and PPC actually require unwind tables, but LLVM doesn't know
64 // how to generate them yet.
65 return getArch() == llvm::Triple::x86_64;
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000066}
67
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000068bool MSVCToolChain::isPICDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000069 return getArch() == llvm::Triple::x86_64;
70}
71
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000072bool MSVCToolChain::isPIEDefault() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000073 return false;
74}
75
Saleem Abdulrasool819f3912014-10-22 02:37:29 +000076bool MSVCToolChain::isPICDefaultForced() const {
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000077 return getArch() == llvm::Triple::x86_64;
78}
79
Chandler Carruth1fc603e2011-12-17 23:10:01 +000080/// \brief Read registry string.
81/// This also supports a means to look for high-versioned keys by use
82/// of a $VERSION placeholder in the key path.
83/// $VERSION in the key path is a placeholder for the version number,
84/// causing the highest value path to be searched for and used.
Hans Wennborge6b994e2014-10-20 23:26:03 +000085/// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
86/// There can be additional characters in the component. Only the numberic
87/// characters are compared.
Chandler Carruth1fc603e2011-12-17 23:10:01 +000088static bool getSystemRegistryString(const char *keyPath, const char *valueName,
Hans Wennborge6b994e2014-10-20 23:26:03 +000089 char *value, size_t maxLength) {
Alp Tokerfcce1832014-06-22 03:27:45 +000090#ifndef USE_WIN32
91 return false;
92#else
Hans Wennborge6b994e2014-10-20 23:26:03 +000093 HKEY hRootKey = NULL;
Chandler Carruth1fc603e2011-12-17 23:10:01 +000094 HKEY hKey = NULL;
Hans Wennborge6b994e2014-10-20 23:26:03 +000095 const char* subKey = NULL;
96 DWORD valueType;
97 DWORD valueSize = maxLength - 1;
Chandler Carruth1fc603e2011-12-17 23:10:01 +000098 long lResult;
99 bool returnValue = false;
100
Hans Wennborge6b994e2014-10-20 23:26:03 +0000101 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
102 hRootKey = HKEY_CLASSES_ROOT;
103 subKey = keyPath + 18;
104 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
105 hRootKey = HKEY_USERS;
106 subKey = keyPath + 11;
107 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
108 hRootKey = HKEY_LOCAL_MACHINE;
109 subKey = keyPath + 19;
110 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
111 hRootKey = HKEY_CURRENT_USER;
112 subKey = keyPath + 18;
113 } else {
114 return false;
115 }
116
117 const char *placeHolder = strstr(subKey, "$VERSION");
118 char bestName[256];
119 bestName[0] = '\0';
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000120 // If we have a $VERSION placeholder, do the highest-version search.
121 if (placeHolder) {
122 const char *keyEnd = placeHolder - 1;
123 const char *nextKey = placeHolder;
124 // Find end of previous key.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000125 while ((keyEnd > subKey) && (*keyEnd != '\\'))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000126 keyEnd--;
127 // Find end of key containing $VERSION.
128 while (*nextKey && (*nextKey != '\\'))
129 nextKey++;
Hans Wennborge6b994e2014-10-20 23:26:03 +0000130 size_t partialKeyLength = keyEnd - subKey;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000131 char partialKey[256];
132 if (partialKeyLength > sizeof(partialKey))
133 partialKeyLength = sizeof(partialKey);
Hans Wennborge6b994e2014-10-20 23:26:03 +0000134 strncpy(partialKey, subKey, partialKeyLength);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000135 partialKey[partialKeyLength] = '\0';
136 HKEY hTopKey = NULL;
Hans Wennborg935d01d2013-10-09 23:41:48 +0000137 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
138 &hTopKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000139 if (lResult == ERROR_SUCCESS) {
140 char keyName[256];
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000141 double bestValue = 0.0;
142 DWORD index, size = sizeof(keyName) - 1;
143 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
144 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
145 const char *sp = keyName;
Jordan Rosea7d03842013-02-08 22:30:41 +0000146 while (*sp && !isDigit(*sp))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000147 sp++;
148 if (!*sp)
149 continue;
150 const char *ep = sp + 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000151 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000152 ep++;
153 char numBuf[32];
154 strncpy(numBuf, sp, sizeof(numBuf) - 1);
155 numBuf[sizeof(numBuf) - 1] = '\0';
Hans Wennborgd2192312013-10-10 18:03:08 +0000156 double dvalue = strtod(numBuf, NULL);
157 if (dvalue > bestValue) {
158 // Test that InstallDir is indeed there before keeping this index.
159 // Open the chosen key path remainder.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000160 strcpy(bestName, keyName);
Hans Wennborgd2192312013-10-10 18:03:08 +0000161 // Append rest of key.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000162 strncat(bestName, nextKey, sizeof(bestName) - 1);
163 bestName[sizeof(bestName) - 1] = '\0';
164 lResult = RegOpenKeyEx(hTopKey, bestName, 0,
Hans Wennborgd2192312013-10-10 18:03:08 +0000165 KEY_READ | KEY_WOW64_32KEY, &hKey);
166 if (lResult == ERROR_SUCCESS) {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000167 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
168 (LPBYTE)value, &valueSize);
Hans Wennborgd2192312013-10-10 18:03:08 +0000169 if (lResult == ERROR_SUCCESS) {
Hans Wennborgd2192312013-10-10 18:03:08 +0000170 bestValue = dvalue;
171 returnValue = true;
172 }
173 RegCloseKey(hKey);
174 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000175 }
176 size = sizeof(keyName) - 1;
177 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000178 RegCloseKey(hTopKey);
179 }
180 } else {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000181 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY,
182 &hKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000183 if (lResult == ERROR_SUCCESS) {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000184 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
185 (LPBYTE)value, &valueSize);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000186 if (lResult == ERROR_SUCCESS)
187 returnValue = true;
188 RegCloseKey(hKey);
189 }
190 }
191 return returnValue;
Alp Tokerfcce1832014-06-22 03:27:45 +0000192#endif // USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000193}
194
195/// \brief Get Windows SDK installation directory.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000196static bool getWindowsSDKDir(std::string &path) {
197 char windowsSDKInstallDir[256];
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000198 // Try the Windows registry.
199 bool hasSDKDir = getSystemRegistryString(
Hans Wennborge6b994e2014-10-20 23:26:03 +0000200 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
201 "InstallationFolder",
202 windowsSDKInstallDir,
203 sizeof(windowsSDKInstallDir) - 1);
204 // If we have both vc80 and vc90, pick version we were compiled with.
205 if (hasSDKDir && windowsSDKInstallDir[0]) {
206 path = windowsSDKInstallDir;
207 return true;
208 }
209 return false;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000210}
211
Alp Tokerfcce1832014-06-22 03:27:45 +0000212// Get Visual Studio installation directory.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000213static bool getVisualStudioDir(std::string &path) {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000214 // First check the environment variables that vsvars32.bat sets.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000215 const char* vcinstalldir = getenv("VCINSTALLDIR");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000216 if (vcinstalldir) {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000217 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
218 if (p)
219 *p = '\0';
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000220 path = vcinstalldir;
221 return true;
222 }
223
Hans Wennborge6b994e2014-10-20 23:26:03 +0000224 char vsIDEInstallDir[256];
225 char vsExpressIDEInstallDir[256];
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000226 // Then try the windows registry.
Hans Wennborge6b994e2014-10-20 23:26:03 +0000227 bool hasVCDir = getSystemRegistryString(
228 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
229 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
230 bool hasVCExpressDir = getSystemRegistryString(
231 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
232 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
233 // If we have both vc80 and vc90, pick version we were compiled with.
234 if (hasVCDir && vsIDEInstallDir[0]) {
235 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
236 if (p)
237 *p = '\0';
238 path = vsIDEInstallDir;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000239 return true;
240 }
241
Hans Wennborge6b994e2014-10-20 23:26:03 +0000242 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
243 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
244 if (p)
245 *p = '\0';
246 path = vsExpressIDEInstallDir;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000247 return true;
248 }
249
250 // Try the environment.
251 const char *vs100comntools = getenv("VS100COMNTOOLS");
252 const char *vs90comntools = getenv("VS90COMNTOOLS");
253 const char *vs80comntools = getenv("VS80COMNTOOLS");
Alp Tokerfcce1832014-06-22 03:27:45 +0000254
255 const char *vscomntools = nullptr;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000256
Alp Tokera2074402014-06-22 03:27:52 +0000257 // Find any version we can
Hans Wennborge6b994e2014-10-20 23:26:03 +0000258 if (vs100comntools)
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000259 vscomntools = vs100comntools;
260 else if (vs90comntools)
261 vscomntools = vs90comntools;
262 else if (vs80comntools)
263 vscomntools = vs80comntools;
264
265 if (vscomntools && *vscomntools) {
266 const char *p = strstr(vscomntools, "\\Common7\\Tools");
267 path = p ? std::string(vscomntools, p) : vscomntools;
268 return true;
269 }
270 return false;
271}
272
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000273void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
274 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000275 if (DriverArgs.hasArg(options::OPT_nostdinc))
276 return;
277
278 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola76565f02013-06-26 03:39:10 +0000279 SmallString<128> P(getDriver().ResourceDir);
280 llvm::sys::path::append(P, "include");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000281 addSystemInclude(DriverArgs, CC1Args, P.str());
282 }
283
284 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
285 return;
286
Joao Matos792d7af2012-09-04 17:29:52 +0000287 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
288 if (const char *cl_include_dir = getenv("INCLUDE")) {
289 SmallVector<StringRef, 8> Dirs;
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000290 StringRef(cl_include_dir)
291 .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
292 for (StringRef Dir : Dirs)
293 addSystemInclude(DriverArgs, CC1Args, Dir);
294 if (!Dirs.empty())
295 return;
Joao Matos792d7af2012-09-04 17:29:52 +0000296 }
297
298 std::string VSDir;
Hans Wennborge6b994e2014-10-20 23:26:03 +0000299 std::string WindowsSDKDir;
Joao Matos792d7af2012-09-04 17:29:52 +0000300
301 // When built with access to the proper Windows APIs, try to actually find
302 // the correct include paths first.
303 if (getVisualStudioDir(VSDir)) {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000304 SmallString<128> P;
305 P = VSDir;
306 llvm::sys::path::append(P, "VC\\include");
307 addSystemInclude(DriverArgs, CC1Args, P.str());
308 if (getWindowsSDKDir(WindowsSDKDir)) {
309 P = WindowsSDKDir;
310 llvm::sys::path::append(P, "include");
311 addSystemInclude(DriverArgs, CC1Args, P.str());
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000312 } else {
Hans Wennborge6b994e2014-10-20 23:26:03 +0000313 P = VSDir;
314 llvm::sys::path::append(P, "VC\\PlatformSDK\\Include");
315 addSystemInclude(DriverArgs, CC1Args, P.str());
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000316 }
Joao Matos792d7af2012-09-04 17:29:52 +0000317 return;
318 }
Joao Matos792d7af2012-09-04 17:29:52 +0000319
320 // As a fallback, select default install paths.
Alp Tokerfcce1832014-06-22 03:27:45 +0000321 // FIXME: Don't guess drives and paths like this on Windows.
Joao Matos792d7af2012-09-04 17:29:52 +0000322 const StringRef Paths[] = {
323 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
324 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
325 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
326 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
327 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
328 };
329 addSystemIncludes(DriverArgs, CC1Args, Paths);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000330}
331
Saleem Abdulrasool819f3912014-10-22 02:37:29 +0000332void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
333 ArgStringList &CC1Args) const {
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000334 // FIXME: There should probably be logic here to find libc++ on Windows.
335}