blob: ca4aa6780b5ce9bc02dac389fb8f37f534a86eeb [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"
21#include "llvm/Support/Path.h"
22
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
Hans Wennborg1cc6cce2013-08-30 09:42:06 +000041Windows::Windows(const Driver &D, const llvm::Triple& Triple,
42 const ArgList &Args)
43 : ToolChain(D, Triple, Args) {
44}
45
46Tool *Windows::buildLinker() const {
47 return new tools::visualstudio::Link(*this);
48}
49
50Tool *Windows::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
57bool Windows::IsIntegratedAssemblerDefault() const {
58 return true;
59}
60
61bool Windows::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
68bool Windows::isPICDefault() const {
69 return getArch() == llvm::Triple::x86_64;
70}
71
72bool Windows::isPIEDefault() const {
73 return false;
74}
75
76bool Windows::isPICDefaultForced() const {
77 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.
85/// 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.
88static bool getSystemRegistryString(const char *keyPath, const char *valueName,
89 char *value, size_t maxLength) {
Alp Tokerfcce1832014-06-22 03:27:45 +000090#ifndef USE_WIN32
91 return false;
92#else
Chandler Carruth1fc603e2011-12-17 23:10:01 +000093 HKEY hRootKey = NULL;
94 HKEY hKey = NULL;
95 const char* subKey = NULL;
96 DWORD valueType;
97 DWORD valueSize = maxLength - 1;
98 long lResult;
99 bool returnValue = false;
100
101 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';
120 // 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.
125 while ((keyEnd > subKey) && (*keyEnd != '\\'))
126 keyEnd--;
127 // Find end of key containing $VERSION.
128 while (*nextKey && (*nextKey != '\\'))
129 nextKey++;
130 size_t partialKeyLength = keyEnd - subKey;
131 char partialKey[256];
132 if (partialKeyLength > sizeof(partialKey))
133 partialKeyLength = sizeof(partialKey);
134 strncpy(partialKey, subKey, partialKeyLength);
135 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];
141 int bestIndex = -1;
142 double bestValue = 0.0;
143 DWORD index, size = sizeof(keyName) - 1;
144 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
145 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
146 const char *sp = keyName;
Jordan Rosea7d03842013-02-08 22:30:41 +0000147 while (*sp && !isDigit(*sp))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000148 sp++;
149 if (!*sp)
150 continue;
151 const char *ep = sp + 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000152 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000153 ep++;
154 char numBuf[32];
155 strncpy(numBuf, sp, sizeof(numBuf) - 1);
156 numBuf[sizeof(numBuf) - 1] = '\0';
Hans Wennborgd2192312013-10-10 18:03:08 +0000157 double dvalue = strtod(numBuf, NULL);
158 if (dvalue > bestValue) {
159 // Test that InstallDir is indeed there before keeping this index.
160 // Open the chosen key path remainder.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000161 strcpy(bestName, keyName);
Hans Wennborgd2192312013-10-10 18:03:08 +0000162 // Append rest of key.
163 strncat(bestName, nextKey, sizeof(bestName) - 1);
164 bestName[sizeof(bestName) - 1] = '\0';
165 lResult = RegOpenKeyEx(hTopKey, bestName, 0,
166 KEY_READ | KEY_WOW64_32KEY, &hKey);
167 if (lResult == ERROR_SUCCESS) {
168 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
169 (LPBYTE)value, &valueSize);
170 if (lResult == ERROR_SUCCESS) {
171 bestIndex = (int)index;
172 bestValue = dvalue;
173 returnValue = true;
174 }
175 RegCloseKey(hKey);
176 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000177 }
178 size = sizeof(keyName) - 1;
179 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000180 RegCloseKey(hTopKey);
181 }
182 } else {
Hans Wennborg935d01d2013-10-09 23:41:48 +0000183 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY,
184 &hKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000185 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 return returnValue;
Alp Tokerfcce1832014-06-22 03:27:45 +0000194#endif // USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000195}
196
197/// \brief Get Windows SDK installation directory.
198static bool getWindowsSDKDir(std::string &path) {
199 char windowsSDKInstallDir[256];
200 // Try the Windows registry.
201 bool hasSDKDir = getSystemRegistryString(
202 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
203 "InstallationFolder",
204 windowsSDKInstallDir,
205 sizeof(windowsSDKInstallDir) - 1);
206 // If we have both vc80 and vc90, pick version we were compiled with.
207 if (hasSDKDir && windowsSDKInstallDir[0]) {
208 path = windowsSDKInstallDir;
209 return true;
210 }
211 return false;
212}
213
Alp Tokerfcce1832014-06-22 03:27:45 +0000214// Get Visual Studio installation directory.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000215static bool getVisualStudioDir(std::string &path) {
216 // First check the environment variables that vsvars32.bat sets.
217 const char* vcinstalldir = getenv("VCINSTALLDIR");
218 if (vcinstalldir) {
219 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
220 if (p)
221 *p = '\0';
222 path = vcinstalldir;
223 return true;
224 }
225
226 char vsIDEInstallDir[256];
227 char vsExpressIDEInstallDir[256];
228 // Then try the windows registry.
229 bool hasVCDir = getSystemRegistryString(
230 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
231 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
232 bool hasVCExpressDir = getSystemRegistryString(
233 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
234 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
235 // If we have both vc80 and vc90, pick version we were compiled with.
236 if (hasVCDir && vsIDEInstallDir[0]) {
237 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
238 if (p)
239 *p = '\0';
240 path = vsIDEInstallDir;
241 return true;
242 }
243
244 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
245 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
246 if (p)
247 *p = '\0';
248 path = vsExpressIDEInstallDir;
249 return true;
250 }
251
252 // Try the environment.
253 const char *vs100comntools = getenv("VS100COMNTOOLS");
254 const char *vs90comntools = getenv("VS90COMNTOOLS");
255 const char *vs80comntools = getenv("VS80COMNTOOLS");
Alp Tokerfcce1832014-06-22 03:27:45 +0000256
257 const char *vscomntools = nullptr;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000258
Alp Tokera2074402014-06-22 03:27:52 +0000259 // Find any version we can
260 if (vs100comntools)
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000261 vscomntools = vs100comntools;
262 else if (vs90comntools)
263 vscomntools = vs90comntools;
264 else if (vs80comntools)
265 vscomntools = vs80comntools;
266
267 if (vscomntools && *vscomntools) {
268 const char *p = strstr(vscomntools, "\\Common7\\Tools");
269 path = p ? std::string(vscomntools, p) : vscomntools;
270 return true;
271 }
272 return false;
273}
274
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000275void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
276 ArgStringList &CC1Args) const {
277 if (DriverArgs.hasArg(options::OPT_nostdinc))
278 return;
279
280 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola76565f02013-06-26 03:39:10 +0000281 SmallString<128> P(getDriver().ResourceDir);
282 llvm::sys::path::append(P, "include");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000283 addSystemInclude(DriverArgs, CC1Args, P.str());
284 }
285
286 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
287 return;
288
Joao Matos792d7af2012-09-04 17:29:52 +0000289 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
290 if (const char *cl_include_dir = getenv("INCLUDE")) {
291 SmallVector<StringRef, 8> Dirs;
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000292 StringRef(cl_include_dir)
293 .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
294 for (StringRef Dir : Dirs)
295 addSystemInclude(DriverArgs, CC1Args, Dir);
296 if (!Dirs.empty())
297 return;
Joao Matos792d7af2012-09-04 17:29:52 +0000298 }
299
300 std::string VSDir;
301 std::string WindowsSDKDir;
302
303 // When built with access to the proper Windows APIs, try to actually find
304 // the correct include paths first.
305 if (getVisualStudioDir(VSDir)) {
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000306 SmallString<128> P;
307 P = VSDir;
308 llvm::sys::path::append(P, "VC\\include");
309 addSystemInclude(DriverArgs, CC1Args, P.str());
310 if (getWindowsSDKDir(WindowsSDKDir)) {
311 P = WindowsSDKDir;
312 llvm::sys::path::append(P, "include");
313 addSystemInclude(DriverArgs, CC1Args, P.str());
314 } else {
315 P = VSDir;
316 llvm::sys::path::append(P, "VC\\PlatformSDK\\Include");
317 addSystemInclude(DriverArgs, CC1Args, P.str());
318 }
Joao Matos792d7af2012-09-04 17:29:52 +0000319 return;
320 }
Joao Matos792d7af2012-09-04 17:29:52 +0000321
322 // As a fallback, select default install paths.
Alp Tokerfcce1832014-06-22 03:27:45 +0000323 // FIXME: Don't guess drives and paths like this on Windows.
Joao Matos792d7af2012-09-04 17:29:52 +0000324 const StringRef Paths[] = {
325 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
326 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
327 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
328 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
329 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
330 };
331 addSystemIncludes(DriverArgs, CC1Args, Paths);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000332}
333
334void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
335 ArgStringList &CC1Args) const {
336 // FIXME: There should probably be logic here to find libc++ on Windows.
337}