18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Process.h"
29 #if defined(LLVM_ON_WIN32)
34 #define WIN32_LEAN_AND_MEAN
42 using namespace clang::driver;
43 using namespace clang::driver::toolchains;
44 using namespace clang;
45 using namespace llvm::opt;
74 return getArch() == llvm::Triple::x86_64;
78 return getArch() == llvm::Triple::x86_64;
86 return getArch() == llvm::Triple::x86_64;
90 static bool readFullStringValue(HKEY hkey,
const char *valueName,
101 result = RegQueryValueEx(hkey, valueName,
NULL, &type,
NULL, &valueSize);
102 if (result != ERROR_SUCCESS || type != REG_SZ)
104 std::vector<BYTE> buffer(valueSize);
105 result = RegQueryValueEx(hkey, valueName,
NULL,
NULL, &buffer[0], &valueSize);
106 if (result == ERROR_SUCCESS)
107 value.assign(reinterpret_cast<const char *>(buffer.data()));
121 std::string &value, std::string *phValue) {
125 HKEY hRootKey = HKEY_LOCAL_MACHINE;
128 bool returnValue =
false;
130 const char *placeHolder = strstr(keyPath,
"$VERSION");
131 std::string bestName;
134 const char *keyEnd = placeHolder - 1;
135 const char *nextKey = placeHolder;
137 while ((keyEnd > keyPath) && (*keyEnd !=
'\\'))
140 while (*nextKey && (*nextKey !=
'\\'))
142 size_t partialKeyLength = keyEnd - keyPath;
143 char partialKey[256];
144 if (partialKeyLength >
sizeof(partialKey))
145 partialKeyLength =
sizeof(partialKey);
146 strncpy(partialKey, keyPath, partialKeyLength);
147 partialKey[partialKeyLength] =
'\0';
149 lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
151 if (lResult == ERROR_SUCCESS) {
153 double bestValue = 0.0;
154 DWORD index, size =
sizeof(keyName) - 1;
155 for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size,
NULL,
157 const char *sp = keyName;
162 const char *ep = sp + 1;
163 while (*ep && (
isDigit(*ep) || (*ep ==
'.')))
166 strncpy(numBuf, sp,
sizeof(numBuf) - 1);
167 numBuf[
sizeof(numBuf) - 1] =
'\0';
168 double dvalue = strtod(numBuf,
NULL);
169 if (dvalue > bestValue) {
174 bestName.append(nextKey);
175 lResult = RegOpenKeyEx(hTopKey, bestName.c_str(), 0,
176 KEY_READ | KEY_WOW64_32KEY, &hKey);
177 if (lResult == ERROR_SUCCESS) {
178 lResult = readFullStringValue(hKey, valueName, value);
179 if (lResult == ERROR_SUCCESS) {
188 size =
sizeof(keyName) - 1;
190 RegCloseKey(hTopKey);
194 RegOpenKeyEx(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
195 if (lResult == ERROR_SUCCESS) {
196 lResult = readFullStringValue(hKey, valueName, value);
197 if (lResult == ERROR_SUCCESS)
211 std::string sdkVersion;
214 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
215 "InstallationFolder", path, &sdkVersion);
216 if (!sdkVersion.empty())
217 std::sscanf(sdkVersion.c_str(),
"v%d.%d", &major, &minor);
218 return hasSDKDir && !path.empty();
232 llvm::sys::path::append(libPath,
"Lib");
236 case llvm::Triple::x86:
238 case llvm::Triple::x86_64:
239 llvm::sys::path::append(libPath,
"x64");
241 case llvm::Triple::arm:
251 const char *tests[] = {
"winv6.3",
"win8",
"win7"};
253 for (
const char *test : tests) {
255 llvm::sys::path::append(testPath, test);
256 if (llvm::sys::fs::exists(testPath.c_str())) {
266 llvm::sys::path::append(libPath,
"um");
268 case llvm::Triple::x86:
269 llvm::sys::path::append(libPath,
"x86");
271 case llvm::Triple::x86_64:
272 llvm::sys::path::append(libPath,
"x64");
274 case llvm::Triple::arm:
275 llvm::sys::path::append(libPath,
"arm");
282 path = libPath.str();
290 std::string &path)
const {
297 llvm::sys::Process::GetEnv(
"VCINSTALLDIR");
298 if (VcInstallDir.hasValue()) {
299 BinDir = VcInstallDir.getValue();
300 llvm::sys::path::append(BinDir,
"bin");
305 if (OptPath.hasValue()) {
306 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator,
'\0'};
308 llvm::SplitString(OptPath.getValue(), PathSegments, EnvPathSeparatorStr);
310 for (StringRef PathSegment : PathSegments) {
311 if (PathSegment.empty())
315 llvm::sys::path::append(FilePath,
"cl.exe");
316 if (llvm::sys::fs::can_execute(FilePath.c_str()) &&
317 !llvm::sys::fs::equivalent(FilePath.c_str(), clangProgramPath)) {
326 std::string installDir;
338 llvm::sys::path::append(BinDir,
"VC",
"bin");
340 llvm::sys::path::append(ClPath,
"cl.exe");
342 if (!llvm::sys::fs::can_execute(ClPath.c_str()))
350 case llvm::Triple::x86:
352 case llvm::Triple::x86_64:
353 llvm::sys::path::append(BinDir,
"amd64");
355 case llvm::Triple::arm:
356 llvm::sys::path::append(BinDir,
"arm");
369 const char *vcinstalldir = getenv(
"VCINSTALLDIR");
372 path = path.substr(0, path.find(
"\\VC"));
376 std::string vsIDEInstallDir;
377 std::string vsExpressIDEInstallDir;
381 "InstallDir", vsIDEInstallDir,
nullptr);
382 if (hasVCDir && !vsIDEInstallDir.empty()) {
383 path = vsIDEInstallDir.substr(0, vsIDEInstallDir.find(
"\\Common7\\IDE"));
387 bool hasVCExpressDir =
389 "InstallDir", vsExpressIDEInstallDir,
nullptr);
390 if (hasVCExpressDir && !vsExpressIDEInstallDir.empty()) {
391 path = vsExpressIDEInstallDir.substr(
392 0, vsIDEInstallDir.find(
"\\Common7\\IDE"));
397 const char *vs120comntools = getenv(
"VS120COMNTOOLS");
398 const char *vs100comntools = getenv(
"VS100COMNTOOLS");
399 const char *vs90comntools = getenv(
"VS90COMNTOOLS");
400 const char *vs80comntools = getenv(
"VS80COMNTOOLS");
402 const char *vscomntools =
nullptr;
406 vscomntools = vs120comntools;
407 else if (vs100comntools)
408 vscomntools = vs100comntools;
409 else if (vs90comntools)
410 vscomntools = vs90comntools;
411 else if (vs80comntools)
412 vscomntools = vs80comntools;
414 if (vscomntools && *vscomntools) {
415 const char *
p = strstr(vscomntools,
"\\Common7\\Tools");
416 path = p ? std::string(vscomntools, p) : vscomntools;
423 ArgStringList &CC1Args,
424 const std::string &folder,
425 const char *subfolder)
const {
427 llvm::sys::path::append(path, subfolder);
432 ArgStringList &CC1Args)
const {
433 if (DriverArgs.hasArg(options::OPT_nostdinc))
436 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
438 llvm::sys::path::append(P,
"include");
442 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
446 if (
const char *cl_include_dir = getenv(
"INCLUDE")) {
448 StringRef(cl_include_dir)
449 .split(Dirs,
";", -1,
false);
450 for (StringRef Dir : Dirs)
463 std::string WindowsSDKDir;
485 const StringRef Paths[] = {
486 "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
487 "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
488 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
489 "C:/Program Files/Microsoft Visual Studio 8/VC/include",
490 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
496 ArgStringList &CC1Args)
const {
503 std::string TripleStr =
505 llvm::Triple Triple(TripleStr);
515 if (Triple.getEnvironment() == llvm::Triple::MSVC) {
516 StringRef ObjFmt = Triple.getEnvironmentName().split(
'-').second;
518 Triple.setEnvironmentName((Twine(
"msvc") + MSVT.
getAsString()).str());
520 Triple.setEnvironmentName(
521 (Twine(
"msvc") + MSVT.
getAsString() + Twine(
'-') + ObjFmt).str());
523 return Triple.getTriple();
528 Res |= SanitizerKind::Address;
static LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Represents a version number in the form major[.minor[.subminor[.build]]].
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
DiagnosticBuilder Diag(unsigned DiagID) const
std::string getAsString() const
Retrieve a string representation of the version number.
Defines version macros and version-related utility functions for Clang.
unsigned getMajor() const
Retrieve the major version number.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero)...
static __inline__ uint32_t volatile uint32_t * p
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.