blob: de31e856491d21eb34dbb7dd942db07e450a7ddb [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
33 #include <Windows.h>
34#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 {
62 return getArch() == llvm::Triple::x86_64;
63}
64
65bool Windows::isPICDefault() const {
66 return getArch() == llvm::Triple::x86_64;
67}
68
69bool Windows::isPIEDefault() const {
70 return false;
71}
72
73bool Windows::isPICDefaultForced() const {
74 return getArch() == llvm::Triple::x86_64;
75}
76
Chandler Carruth1fc603e2011-12-17 23:10:01 +000077/// \brief Read registry string.
78/// This also supports a means to look for high-versioned keys by use
79/// of a $VERSION placeholder in the key path.
80/// $VERSION in the key path is a placeholder for the version number,
81/// causing the highest value path to be searched for and used.
82/// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
83/// There can be additional characters in the component. Only the numberic
84/// characters are compared.
85static bool getSystemRegistryString(const char *keyPath, const char *valueName,
86 char *value, size_t maxLength) {
Alp Tokerfcce1832014-06-22 03:27:45 +000087#ifndef USE_WIN32
88 return false;
89#else
Chandler Carruth1fc603e2011-12-17 23:10:01 +000090 HKEY hRootKey = NULL;
91 HKEY hKey = NULL;
92 const char* subKey = NULL;
93 DWORD valueType;
94 DWORD valueSize = maxLength - 1;
95 long lResult;
96 bool returnValue = false;
97
98 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
99 hRootKey = HKEY_CLASSES_ROOT;
100 subKey = keyPath + 18;
101 } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
102 hRootKey = HKEY_USERS;
103 subKey = keyPath + 11;
104 } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
105 hRootKey = HKEY_LOCAL_MACHINE;
106 subKey = keyPath + 19;
107 } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
108 hRootKey = HKEY_CURRENT_USER;
109 subKey = keyPath + 18;
110 } else {
111 return false;
112 }
113
114 const char *placeHolder = strstr(subKey, "$VERSION");
115 char bestName[256];
116 bestName[0] = '\0';
117 // If we have a $VERSION placeholder, do the highest-version search.
118 if (placeHolder) {
119 const char *keyEnd = placeHolder - 1;
120 const char *nextKey = placeHolder;
121 // Find end of previous key.
122 while ((keyEnd > subKey) && (*keyEnd != '\\'))
123 keyEnd--;
124 // Find end of key containing $VERSION.
125 while (*nextKey && (*nextKey != '\\'))
126 nextKey++;
127 size_t partialKeyLength = keyEnd - subKey;
128 char partialKey[256];
129 if (partialKeyLength > sizeof(partialKey))
130 partialKeyLength = sizeof(partialKey);
131 strncpy(partialKey, subKey, partialKeyLength);
132 partialKey[partialKeyLength] = '\0';
133 HKEY hTopKey = NULL;
Hans Wennborg935d01d2013-10-09 23:41:48 +0000134 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
135 &hTopKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000136 if (lResult == ERROR_SUCCESS) {
137 char keyName[256];
138 int bestIndex = -1;
139 double bestValue = 0.0;
140 DWORD index, size = sizeof(keyName) - 1;
141 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
142 NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
143 const char *sp = keyName;
Jordan Rosea7d03842013-02-08 22:30:41 +0000144 while (*sp && !isDigit(*sp))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000145 sp++;
146 if (!*sp)
147 continue;
148 const char *ep = sp + 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000149 while (*ep && (isDigit(*ep) || (*ep == '.')))
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000150 ep++;
151 char numBuf[32];
152 strncpy(numBuf, sp, sizeof(numBuf) - 1);
153 numBuf[sizeof(numBuf) - 1] = '\0';
Hans Wennborgd2192312013-10-10 18:03:08 +0000154 double dvalue = strtod(numBuf, NULL);
155 if (dvalue > bestValue) {
156 // Test that InstallDir is indeed there before keeping this index.
157 // Open the chosen key path remainder.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000158 strcpy(bestName, keyName);
Hans Wennborgd2192312013-10-10 18:03:08 +0000159 // Append rest of key.
160 strncat(bestName, nextKey, sizeof(bestName) - 1);
161 bestName[sizeof(bestName) - 1] = '\0';
162 lResult = RegOpenKeyEx(hTopKey, bestName, 0,
163 KEY_READ | KEY_WOW64_32KEY, &hKey);
164 if (lResult == ERROR_SUCCESS) {
165 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
166 (LPBYTE)value, &valueSize);
167 if (lResult == ERROR_SUCCESS) {
168 bestIndex = (int)index;
169 bestValue = dvalue;
170 returnValue = true;
171 }
172 RegCloseKey(hKey);
173 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000174 }
175 size = sizeof(keyName) - 1;
176 }
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000177 RegCloseKey(hTopKey);
178 }
179 } else {
Hans Wennborg935d01d2013-10-09 23:41:48 +0000180 lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY,
181 &hKey);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000182 if (lResult == ERROR_SUCCESS) {
183 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
184 (LPBYTE)value, &valueSize);
185 if (lResult == ERROR_SUCCESS)
186 returnValue = true;
187 RegCloseKey(hKey);
188 }
189 }
190 return returnValue;
Alp Tokerfcce1832014-06-22 03:27:45 +0000191#endif // USE_WIN32
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000192}
193
194/// \brief Get Windows SDK installation directory.
195static bool getWindowsSDKDir(std::string &path) {
196 char windowsSDKInstallDir[256];
197 // Try the Windows registry.
198 bool hasSDKDir = getSystemRegistryString(
199 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
200 "InstallationFolder",
201 windowsSDKInstallDir,
202 sizeof(windowsSDKInstallDir) - 1);
203 // If we have both vc80 and vc90, pick version we were compiled with.
204 if (hasSDKDir && windowsSDKInstallDir[0]) {
205 path = windowsSDKInstallDir;
206 return true;
207 }
208 return false;
209}
210
Alp Tokerfcce1832014-06-22 03:27:45 +0000211// Get Visual Studio installation directory.
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000212static bool getVisualStudioDir(std::string &path) {
213 // First check the environment variables that vsvars32.bat sets.
214 const char* vcinstalldir = getenv("VCINSTALLDIR");
215 if (vcinstalldir) {
216 char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
217 if (p)
218 *p = '\0';
219 path = vcinstalldir;
220 return true;
221 }
222
223 char vsIDEInstallDir[256];
224 char vsExpressIDEInstallDir[256];
225 // Then try the windows registry.
226 bool hasVCDir = getSystemRegistryString(
227 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
228 "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
229 bool hasVCExpressDir = getSystemRegistryString(
230 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
231 "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
232 // If we have both vc80 and vc90, pick version we were compiled with.
233 if (hasVCDir && vsIDEInstallDir[0]) {
234 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
235 if (p)
236 *p = '\0';
237 path = vsIDEInstallDir;
238 return true;
239 }
240
241 if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
242 char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
243 if (p)
244 *p = '\0';
245 path = vsExpressIDEInstallDir;
246 return true;
247 }
248
249 // Try the environment.
250 const char *vs100comntools = getenv("VS100COMNTOOLS");
251 const char *vs90comntools = getenv("VS90COMNTOOLS");
252 const char *vs80comntools = getenv("VS80COMNTOOLS");
Alp Tokerfcce1832014-06-22 03:27:45 +0000253
254 const char *vscomntools = nullptr;
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000255
Alp Tokera2074402014-06-22 03:27:52 +0000256 // Find any version we can
257 if (vs100comntools)
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000258 vscomntools = vs100comntools;
259 else if (vs90comntools)
260 vscomntools = vs90comntools;
261 else if (vs80comntools)
262 vscomntools = vs80comntools;
263
264 if (vscomntools && *vscomntools) {
265 const char *p = strstr(vscomntools, "\\Common7\\Tools");
266 path = p ? std::string(vscomntools, p) : vscomntools;
267 return true;
268 }
269 return false;
270}
271
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000272void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
273 ArgStringList &CC1Args) const {
274 if (DriverArgs.hasArg(options::OPT_nostdinc))
275 return;
276
277 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
Rafael Espindola76565f02013-06-26 03:39:10 +0000278 SmallString<128> P(getDriver().ResourceDir);
279 llvm::sys::path::append(P, "include");
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000280 addSystemInclude(DriverArgs, CC1Args, P.str());
281 }
282
283 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
284 return;
285
Joao Matos792d7af2012-09-04 17:29:52 +0000286 // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
287 if (const char *cl_include_dir = getenv("INCLUDE")) {
288 SmallVector<StringRef, 8> Dirs;
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000289 StringRef(cl_include_dir)
290 .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
291 for (StringRef Dir : Dirs)
292 addSystemInclude(DriverArgs, CC1Args, Dir);
293 if (!Dirs.empty())
294 return;
Joao Matos792d7af2012-09-04 17:29:52 +0000295 }
296
297 std::string VSDir;
298 std::string WindowsSDKDir;
299
300 // When built with access to the proper Windows APIs, try to actually find
301 // the correct include paths first.
302 if (getVisualStudioDir(VSDir)) {
Reid Kleckner77b45ba2014-04-23 00:15:01 +0000303 SmallString<128> P;
304 P = VSDir;
305 llvm::sys::path::append(P, "VC\\include");
306 addSystemInclude(DriverArgs, CC1Args, P.str());
307 if (getWindowsSDKDir(WindowsSDKDir)) {
308 P = WindowsSDKDir;
309 llvm::sys::path::append(P, "include");
310 addSystemInclude(DriverArgs, CC1Args, P.str());
311 } else {
312 P = VSDir;
313 llvm::sys::path::append(P, "VC\\PlatformSDK\\Include");
314 addSystemInclude(DriverArgs, CC1Args, P.str());
315 }
Joao Matos792d7af2012-09-04 17:29:52 +0000316 return;
317 }
Joao Matos792d7af2012-09-04 17:29:52 +0000318
319 // As a fallback, select default install paths.
Alp Tokerfcce1832014-06-22 03:27:45 +0000320 // FIXME: Don't guess drives and paths like this on Windows.
Joao Matos792d7af2012-09-04 17:29:52 +0000321 const StringRef Paths[] = {
322 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
323 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
324 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
325 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
326 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
327 };
328 addSystemIncludes(DriverArgs, CC1Args, Paths);
Chandler Carruth1fc603e2011-12-17 23:10:01 +0000329}
330
331void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
332 ArgStringList &CC1Args) const {
333 // FIXME: There should probably be logic here to find libc++ on Windows.
334}