Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 1 | //===--- 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" |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 11 | #include "Tools.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 12 | #include "clang/Basic/CharInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Version.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Compilation.h" |
| 15 | #include "clang/Driver/Driver.h" |
Rafael Espindola | 7976446 | 2013-03-24 15:06:53 +0000 | [diff] [blame] | 16 | #include "clang/Driver/DriverDiagnostic.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Options.h" |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringSwitch.h" |
Alp Toker | f1ffc84 | 2014-06-22 04:31:15 +0000 | [diff] [blame] | 20 | #include "llvm/Config/llvm-config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 21 | #include "llvm/Option/Arg.h" |
| 22 | #include "llvm/Option/ArgList.h" |
Adrian McCarthy | e4b26fc | 2016-05-13 23:20:11 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ConvertUTF.h" |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/Host.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Process.h" |
Reid Kleckner | 6b7156b | 2015-01-23 19:16:25 +0000 | [diff] [blame] | 29 | #include <cstdio> |
| 30 | |
Alp Toker | f1ffc84 | 2014-06-22 04:31:15 +0000 | [diff] [blame] | 31 | #if defined(LLVM_ON_WIN32) |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 32 | #define USE_WIN32 |
| 33 | |
| 34 | // FIXME: Make this configurable with cmake when the final version of the API |
| 35 | // has been released. |
| 36 | #if 0 |
| 37 | #define USE_VS_SETUP_CONFIG |
| 38 | #endif |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 41 | // Include the necessary headers to interface with the Windows registry and |
| 42 | // environment. |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 43 | #ifdef USE_WIN32 |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 44 | #define WIN32_LEAN_AND_MEAN |
| 45 | #define NOGDI |
Yaron Keren | 7fc6f1e | 2014-12-04 21:46:50 +0000 | [diff] [blame] | 46 | #ifndef NOMINMAX |
| 47 | #define NOMINMAX |
| 48 | #endif |
Logan Chien | 733e3c6 | 2014-06-24 16:18:10 +0000 | [diff] [blame] | 49 | #include <windows.h> |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 52 | // Include the headers needed for the setup config COM stuff and define |
| 53 | // smart pointers for the interfaces we need. |
| 54 | #ifdef USE_VS_SETUP_CONFIG |
| 55 | #include "clang/Basic/VirtualFileSystem.h" |
| 56 | #include "llvm/Support/COM.h" |
| 57 | #include <comdef.h> |
| 58 | #include <Setup.Configuration.h> |
| 59 | _COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration)); |
| 60 | _COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2)); |
| 61 | _COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper)); |
| 62 | _COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances)); |
| 63 | _COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance)); |
| 64 | _COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2)); |
| 65 | #endif |
| 66 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 67 | using namespace clang::driver; |
| 68 | using namespace clang::driver::toolchains; |
| 69 | using namespace clang; |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 70 | using namespace llvm::opt; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 71 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 72 | // Defined below. |
| 73 | // Forward declare this so there aren't too many things above the constructor. |
| 74 | static bool getSystemRegistryString(const char *keyPath, const char *valueName, |
| 75 | std::string &value, std::string *phValue); |
| 76 | |
| 77 | // Check various environment variables to try and find a toolchain. |
| 78 | static bool findVCToolChainViaEnvironment(std::string &Path, |
| 79 | bool &IsVS2017OrNewer) { |
| 80 | // These variables are typically set by vcvarsall.bat |
| 81 | // when launching a developer command prompt. |
| 82 | if (llvm::Optional<std::string> VCToolsInstallDir = |
| 83 | llvm::sys::Process::GetEnv("VCToolsInstallDir")) { |
| 84 | // This is only set by newer Visual Studios, and it leads straight to |
| 85 | // the toolchain directory. |
| 86 | Path = std::move(*VCToolsInstallDir); |
| 87 | IsVS2017OrNewer = true; |
| 88 | return true; |
| 89 | } |
| 90 | if (llvm::Optional<std::string> VCInstallDir = |
| 91 | llvm::sys::Process::GetEnv("VCINSTALLDIR")) { |
| 92 | // If the previous variable isn't set but this one is, then we've found |
| 93 | // an older Visual Studio. This variable is set by newer Visual Studios too, |
| 94 | // so this check has to appear second. |
| 95 | // In older Visual Studios, the VC directory is the toolchain. |
| 96 | Path = std::move(*VCInstallDir); |
| 97 | IsVS2017OrNewer = false; |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // We couldn't find any VC environment variables. Let's walk through PATH and |
| 102 | // see if it leads us to a VC toolchain bin directory. If it does, pick the |
| 103 | // first one that we find. |
| 104 | if (llvm::Optional<std::string> PathEnv = |
| 105 | llvm::sys::Process::GetEnv("PATH")) { |
| 106 | llvm::SmallVector<llvm::StringRef, 8> PathEntries; |
| 107 | llvm::StringRef(*PathEnv).split(PathEntries, llvm::sys::EnvPathSeparator); |
| 108 | for (llvm::StringRef PathEntry : PathEntries) { |
| 109 | if (PathEntry.empty()) |
| 110 | continue; |
| 111 | |
| 112 | llvm::SmallString<256> ExeTestPath; |
| 113 | |
| 114 | // If cl.exe doesn't exist, then this definitely isn't a VC toolchain. |
| 115 | ExeTestPath = PathEntry; |
| 116 | llvm::sys::path::append(ExeTestPath, "cl.exe"); |
| 117 | if (!llvm::sys::fs::exists(ExeTestPath)) |
| 118 | continue; |
| 119 | |
| 120 | // cl.exe existing isn't a conclusive test for a VC toolchain; clang also |
| 121 | // has a cl.exe. So let's check for link.exe too. |
| 122 | ExeTestPath = PathEntry; |
| 123 | llvm::sys::path::append(ExeTestPath, "link.exe"); |
| 124 | if (!llvm::sys::fs::exists(ExeTestPath)) |
| 125 | continue; |
| 126 | |
| 127 | // whatever/VC/bin --> old toolchain, VC dir is toolchain dir. |
| 128 | if (llvm::sys::path::filename(PathEntry) == "bin") { |
| 129 | llvm::StringRef ParentPath = llvm::sys::path::parent_path(PathEntry); |
| 130 | if (llvm::sys::path::filename(ParentPath) == "VC") { |
| 131 | Path = ParentPath; |
| 132 | IsVS2017OrNewer = false; |
| 133 | return true; |
| 134 | } |
| 135 | } else { |
| 136 | // This could be a new (>=VS2017) toolchain. If it is, we should find |
| 137 | // path components with these prefixes when walking backwards through |
| 138 | // the path. |
| 139 | // Note: empty strings match anything. |
| 140 | llvm::StringRef ExpectedPrefixes[] = |
| 141 | { "", "Host", "bin", "", "MSVC", "Tools", "VC" }; |
| 142 | |
| 143 | llvm::sys::path::reverse_iterator |
| 144 | It = llvm::sys::path::rbegin(PathEntry), |
| 145 | End = llvm::sys::path::rend(PathEntry); |
| 146 | for (llvm::StringRef Prefix : ExpectedPrefixes) { |
| 147 | if (It == End) goto NotAToolChain; |
| 148 | if (!It->startswith(Prefix)) goto NotAToolChain; |
| 149 | ++It; |
| 150 | } |
| 151 | |
| 152 | // We've found a new toolchain! |
| 153 | // Back up 3 times (/bin/Host/arch) to get the root path. |
| 154 | llvm::StringRef ToolChainPath(PathEntry); |
| 155 | for (int i = 0; i < 3; ++i) |
| 156 | ToolChainPath = llvm::sys::path::parent_path(ToolChainPath); |
| 157 | |
| 158 | Path = ToolChainPath; |
| 159 | IsVS2017OrNewer = true; |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | NotAToolChain: |
| 164 | continue; |
| 165 | } |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | // Query the Setup Config server for installs, then pick the newest version |
| 171 | // and find its default VC toolchain. |
| 172 | // This is the preferred way to discover new Visual Studios, as they're no |
| 173 | // longer listed in the registry. |
| 174 | static bool findVCToolChainViaSetupConfig(std::string &Path, |
| 175 | bool &IsVS2017OrNewer) { |
| 176 | #ifndef USE_VS_SETUP_CONFIG |
| 177 | return false; |
| 178 | #else |
| 179 | llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::SingleThreaded); |
| 180 | HRESULT HR; |
| 181 | |
| 182 | // _com_ptr_t will throw a _com_error if a COM calls fail. |
| 183 | // The LLVM coding standards forbid exception handling, so we'll have to |
| 184 | // stop them from being thrown in the first place. |
| 185 | // The destructor will put the regular error handler back when we leave |
| 186 | // this scope. |
| 187 | struct SuppressCOMErrorsRAII { |
| 188 | SuppressCOMErrorsRAII() { |
| 189 | _set_com_error_handler([](HRESULT, IErrorInfo *) { }); |
| 190 | } |
| 191 | ~SuppressCOMErrorsRAII() { |
| 192 | _set_com_error_handler(_com_raise_error); |
| 193 | } |
| 194 | } COMErrorSuppressor; |
| 195 | |
| 196 | ISetupConfigurationPtr Query; |
| 197 | HR = Query.CreateInstance(__uuidof(SetupConfiguration)); |
| 198 | if (FAILED(HR)) return false; |
| 199 | |
| 200 | IEnumSetupInstancesPtr EnumInstances; |
| 201 | HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances); |
| 202 | if (FAILED(HR)) return false; |
| 203 | |
| 204 | ISetupInstancePtr Instance; |
| 205 | HR = EnumInstances->Next(1, &Instance, nullptr); |
| 206 | if (HR != S_OK) return false; |
| 207 | |
| 208 | ISetupInstancePtr NewestInstance(Instance); |
| 209 | uint64_t NewestVersionNum; |
| 210 | { |
| 211 | bstr_t VersionString; |
| 212 | HR = NewestInstance->GetInstallationVersion(VersionString.GetAddress()); |
| 213 | if (FAILED(HR)) return false; |
| 214 | HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, |
| 215 | &NewestVersionNum); |
| 216 | if (FAILED(HR)) return false; |
| 217 | } |
| 218 | |
| 219 | while ((HR = EnumInstances->Next(1, &Instance, nullptr)) == S_OK) { |
| 220 | bstr_t VersionString; |
| 221 | uint64_t VersionNum; |
| 222 | HR = Instance->GetInstallationVersion(VersionString.GetAddress()); |
| 223 | if (FAILED(HR)) continue; |
| 224 | HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, |
| 225 | &VersionNum); |
| 226 | if (FAILED(HR)) continue; |
| 227 | if (VersionNum > NewestVersionNum) { |
| 228 | NewestInstance = Instance; |
| 229 | NewestVersionNum = VersionNum; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | bstr_t VCPathWide; |
| 234 | HR = NewestInstance->ResolvePath(L"VC", |
| 235 | VCPathWide.GetAddress()); |
| 236 | if (FAILED(HR)) return false; |
| 237 | |
| 238 | std::string VCRootPath; |
| 239 | llvm::convertWideToUTF8(std::wstring(VCPathWide), VCRootPath); |
| 240 | |
| 241 | llvm::SmallString<256> ToolsVersionFilePath(VCRootPath); |
| 242 | llvm::sys::path::append(ToolsVersionFilePath, |
| 243 | "Auxiliary", |
| 244 | "Build", |
| 245 | "Microsoft.VCToolsVersion.default.txt"); |
| 246 | |
| 247 | auto ToolsVersionFile = |
| 248 | clang::vfs::getRealFileSystem()->getBufferForFile(ToolsVersionFilePath); |
| 249 | if (!ToolsVersionFile) |
| 250 | return false; |
| 251 | |
| 252 | llvm::SmallString<256> ToolchainPath(VCRootPath); |
| 253 | llvm::sys::path::append(ToolchainPath, |
| 254 | "Tools", |
| 255 | "MSVC", |
| 256 | ToolsVersionFile->get()->getBuffer().rtrim()); |
| 257 | if (!llvm::sys::fs::is_directory(ToolchainPath)) |
| 258 | return false; |
| 259 | |
| 260 | Path = ToolchainPath.str(); |
| 261 | IsVS2017OrNewer = true; |
| 262 | return true; |
| 263 | #endif /*USE_VS_SETUP_CONFIG*/ |
| 264 | } |
| 265 | |
| 266 | // Look in the registry for Visual Studio installs, and use that to get |
| 267 | // a toolchain path. VS2017 and newer don't get added to the registry. |
| 268 | // So if we find something here, we know that it's an older version. |
| 269 | static bool findVCToolChainViaRegistry(std::string &Path, |
| 270 | bool &IsVS2017OrNewer) { |
| 271 | std::string VSInstallPath; |
| 272 | if (getSystemRegistryString(R"(SOFTWARE\Microsoft\VisualStudio\$VERSION)", |
| 273 | "InstallDir", VSInstallPath, nullptr) || |
| 274 | getSystemRegistryString(R"(SOFTWARE\Microsoft\VCExpress\$VERSION)", |
| 275 | "InstallDir", VSInstallPath, nullptr)) { |
| 276 | if (!VSInstallPath.empty()) { |
| 277 | llvm::SmallString<256> |
| 278 | VCPath(llvm::StringRef(VSInstallPath.c_str(), |
| 279 | VSInstallPath.find(R"(\Common7\IDE)"))); |
| 280 | llvm::sys::path::append(VCPath, "VC"); |
| 281 | |
| 282 | Path = VCPath.str(); |
| 283 | IsVS2017OrNewer = false; |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | MSVCToolChain::MSVCToolChain(const Driver &D, const llvm::Triple& Triple, |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 291 | const ArgList &Args) |
Justin Lebar | 5889190 | 2017-01-05 16:52:29 +0000 | [diff] [blame] | 292 | : ToolChain(D, Triple, Args), CudaInstallation(D, Triple, Args) { |
Zachary Turner | 719f58c | 2014-12-01 23:06:47 +0000 | [diff] [blame] | 293 | getProgramPaths().push_back(getDriver().getInstalledDir()); |
| 294 | if (getDriver().getInstalledDir() != getDriver().Dir) |
| 295 | getProgramPaths().push_back(getDriver().Dir); |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 296 | |
| 297 | // Check the environment first, since that's probably the user telling us |
| 298 | // what they want to use. |
| 299 | // Failing that, just try to find the newest Visual Studio version we can |
| 300 | // and use its default VC toolchain. |
| 301 | findVCToolChainViaEnvironment(VCToolChainPath, IsVS2017OrNewer) |
| 302 | || findVCToolChainViaSetupConfig(VCToolChainPath, IsVS2017OrNewer) |
| 303 | || findVCToolChainViaRegistry(VCToolChainPath, IsVS2017OrNewer); |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 306 | Tool *MSVCToolChain::buildLinker() const { |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 307 | if (VCToolChainPath.empty()) { |
| 308 | getDriver().Diag(clang::diag::err_drv_msvc_not_found); |
| 309 | return nullptr; |
| 310 | } |
Douglas Katzman | 9535429 | 2015-06-23 20:42:09 +0000 | [diff] [blame] | 311 | return new tools::visualstudio::Linker(*this); |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 314 | Tool *MSVCToolChain::buildAssembler() const { |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 315 | if (getTriple().isOSBinFormatMachO()) |
Douglas Katzman | 9535429 | 2015-06-23 20:42:09 +0000 | [diff] [blame] | 316 | return new tools::darwin::Assembler(*this); |
Alp Toker | c8d4f0f | 2013-11-22 08:27:46 +0000 | [diff] [blame] | 317 | getDriver().Diag(clang::diag::err_no_external_assembler); |
Craig Topper | 92fc2df | 2014-05-17 16:56:41 +0000 | [diff] [blame] | 318 | return nullptr; |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 321 | bool MSVCToolChain::IsIntegratedAssemblerDefault() const { |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 325 | bool MSVCToolChain::IsUnwindTablesDefault() const { |
Reid Kleckner | 6b3a940 | 2014-09-04 18:13:12 +0000 | [diff] [blame] | 326 | // Emit unwind tables by default on Win64. All non-x86_32 Windows platforms |
| 327 | // such as ARM and PPC actually require unwind tables, but LLVM doesn't know |
| 328 | // how to generate them yet. |
Akira Hatanaka | e421813 | 2016-05-05 01:41:07 +0000 | [diff] [blame] | 329 | |
| 330 | // Don't emit unwind tables by default for MachO targets. |
| 331 | if (getTriple().isOSBinFormatMachO()) |
| 332 | return false; |
| 333 | |
Reid Kleckner | 6b3a940 | 2014-09-04 18:13:12 +0000 | [diff] [blame] | 334 | return getArch() == llvm::Triple::x86_64; |
Hans Wennborg | 1cc6cce | 2013-08-30 09:42:06 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Reid Kleckner | 8c19083 | 2016-12-28 17:41:36 +0000 | [diff] [blame] | 337 | bool MSVCToolChain::isPICDefault() const { |
| 338 | return getArch() == llvm::Triple::x86_64; |
| 339 | } |
| 340 | |
| 341 | bool MSVCToolChain::isPIEDefault() const { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | bool MSVCToolChain::isPICDefaultForced() const { |
| 346 | return getArch() == llvm::Triple::x86_64; |
| 347 | } |
| 348 | |
Justin Lebar | 5889190 | 2017-01-05 16:52:29 +0000 | [diff] [blame] | 349 | void MSVCToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs, |
| 350 | ArgStringList &CC1Args) const { |
| 351 | CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); |
| 352 | } |
| 353 | |
| 354 | void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const { |
| 355 | CudaInstallation.print(OS); |
| 356 | } |
| 357 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 358 | // Windows SDKs and VC Toolchains group their contents into subdirectories based |
| 359 | // on the target architecture. This function converts an llvm::Triple::ArchType |
| 360 | // to the corresponding subdirectory name. |
| 361 | static const char *llvmArchToWindowsSDKArch(llvm::Triple::ArchType Arch) { |
| 362 | using ArchType = llvm::Triple::ArchType; |
| 363 | switch (Arch) { |
| 364 | case ArchType::x86: |
| 365 | return "x86"; |
| 366 | case ArchType::x86_64: |
| 367 | return "x64"; |
| 368 | case ArchType::arm: |
| 369 | return "arm"; |
| 370 | default: |
| 371 | return ""; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // Similar to the above function, but for Visual Studios before VS2017. |
| 376 | static const char *llvmArchToLegacyVCArch(llvm::Triple::ArchType Arch) { |
| 377 | using ArchType = llvm::Triple::ArchType; |
| 378 | switch (Arch) { |
| 379 | case ArchType::x86: |
| 380 | // x86 is default in legacy VC toolchains. |
| 381 | // e.g. x86 libs are directly in /lib as opposed to /lib/x86. |
| 382 | return ""; |
| 383 | case ArchType::x86_64: |
| 384 | return "amd64"; |
| 385 | case ArchType::arm: |
| 386 | return "arm"; |
| 387 | default: |
| 388 | return ""; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Get the path to a specific subdirectory in the current toolchain for |
| 393 | // a given target architecture. |
| 394 | // VS2017 changed the VC toolchain layout, so this should be used instead |
| 395 | // of hardcoding paths. |
| 396 | std::string |
| 397 | MSVCToolChain::getSubDirectoryPath(SubDirectoryType Type, |
| 398 | llvm::Triple::ArchType TargetArch) const { |
| 399 | llvm::SmallString<256> Path(VCToolChainPath); |
| 400 | switch (Type) { |
| 401 | case SubDirectoryType::Bin: |
| 402 | if (IsVS2017OrNewer) { |
| 403 | bool HostIsX64 = llvm::Triple(llvm::sys::getProcessTriple()).isArch64Bit(); |
| 404 | llvm::sys::path::append(Path, |
| 405 | "bin", |
| 406 | (HostIsX64 ? "HostX64" : "HostX86"), |
| 407 | llvmArchToWindowsSDKArch(TargetArch)); |
| 408 | } |
| 409 | else { |
| 410 | llvm::sys::path::append(Path, |
| 411 | "bin", |
| 412 | llvmArchToLegacyVCArch(TargetArch)); |
| 413 | } |
| 414 | break; |
| 415 | case SubDirectoryType::Include: |
| 416 | llvm::sys::path::append(Path, "include"); |
| 417 | break; |
| 418 | case SubDirectoryType::Lib: |
| 419 | llvm::sys::path::append(Path, |
| 420 | "lib", |
| 421 | IsVS2017OrNewer |
| 422 | ? llvmArchToWindowsSDKArch(TargetArch) |
| 423 | : llvmArchToLegacyVCArch(TargetArch)); |
| 424 | break; |
| 425 | } |
| 426 | return Path.str(); |
| 427 | } |
| 428 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 429 | #ifdef USE_WIN32 |
| 430 | static bool readFullStringValue(HKEY hkey, const char *valueName, |
| 431 | std::string &value) { |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 432 | std::wstring WideValueName; |
| 433 | if (!llvm::ConvertUTF8toWide(valueName, WideValueName)) |
| 434 | return false; |
| 435 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 436 | DWORD result = 0; |
| 437 | DWORD valueSize = 0; |
| 438 | DWORD type = 0; |
| 439 | // First just query for the required size. |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 440 | result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL, |
| 441 | &valueSize); |
| 442 | if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize) |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 443 | return false; |
| 444 | std::vector<BYTE> buffer(valueSize); |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 445 | result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0], |
| 446 | &valueSize); |
| 447 | if (result == ERROR_SUCCESS) { |
| 448 | std::wstring WideValue(reinterpret_cast<const wchar_t *>(buffer.data()), |
| 449 | valueSize / sizeof(wchar_t)); |
Zachary Turner | e78a347 | 2016-07-28 17:13:32 +0000 | [diff] [blame] | 450 | if (valueSize && WideValue.back() == L'\0') { |
Etienne Bergeron | 982a3bc | 2016-08-30 18:38:25 +0000 | [diff] [blame] | 451 | WideValue.pop_back(); |
Zachary Turner | e78a347 | 2016-07-28 17:13:32 +0000 | [diff] [blame] | 452 | } |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 453 | // The destination buffer must be empty as an invariant of the conversion |
| 454 | // function; but this function is sometimes called in a loop that passes in |
| 455 | // the same buffer, however. Simply clear it out so we can overwrite it. |
| 456 | value.clear(); |
| 457 | return llvm::convertWideToUTF8(WideValue, value); |
| 458 | } |
| 459 | return false; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 460 | } |
| 461 | #endif |
| 462 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 463 | /// \brief Read registry string. |
| 464 | /// This also supports a means to look for high-versioned keys by use |
| 465 | /// of a $VERSION placeholder in the key path. |
| 466 | /// $VERSION in the key path is a placeholder for the version number, |
| 467 | /// causing the highest value path to be searched for and used. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 468 | /// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION". |
| 469 | /// There can be additional characters in the component. Only the numeric |
| 470 | /// characters are compared. This function only searches HKLM. |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 471 | static bool getSystemRegistryString(const char *keyPath, const char *valueName, |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 472 | std::string &value, std::string *phValue) { |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 473 | #ifndef USE_WIN32 |
| 474 | return false; |
| 475 | #else |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 476 | HKEY hRootKey = HKEY_LOCAL_MACHINE; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 477 | HKEY hKey = NULL; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 478 | long lResult; |
| 479 | bool returnValue = false; |
| 480 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 481 | const char *placeHolder = strstr(keyPath, "$VERSION"); |
| 482 | std::string bestName; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 483 | // If we have a $VERSION placeholder, do the highest-version search. |
| 484 | if (placeHolder) { |
| 485 | const char *keyEnd = placeHolder - 1; |
| 486 | const char *nextKey = placeHolder; |
| 487 | // Find end of previous key. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 488 | while ((keyEnd > keyPath) && (*keyEnd != '\\')) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 489 | keyEnd--; |
| 490 | // Find end of key containing $VERSION. |
| 491 | while (*nextKey && (*nextKey != '\\')) |
| 492 | nextKey++; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 493 | size_t partialKeyLength = keyEnd - keyPath; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 494 | char partialKey[256]; |
Daniel Marjamaki | e114669 | 2016-01-27 07:33:50 +0000 | [diff] [blame] | 495 | if (partialKeyLength >= sizeof(partialKey)) |
| 496 | partialKeyLength = sizeof(partialKey) - 1; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 497 | strncpy(partialKey, keyPath, partialKeyLength); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 498 | partialKey[partialKeyLength] = '\0'; |
| 499 | HKEY hTopKey = NULL; |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 500 | lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY, |
| 501 | &hTopKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 502 | if (lResult == ERROR_SUCCESS) { |
| 503 | char keyName[256]; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 504 | double bestValue = 0.0; |
| 505 | DWORD index, size = sizeof(keyName) - 1; |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 506 | for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL, |
| 507 | NULL, NULL) == ERROR_SUCCESS; |
| 508 | index++) { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 509 | const char *sp = keyName; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 510 | while (*sp && !isDigit(*sp)) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 511 | sp++; |
| 512 | if (!*sp) |
| 513 | continue; |
| 514 | const char *ep = sp + 1; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 515 | while (*ep && (isDigit(*ep) || (*ep == '.'))) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 516 | ep++; |
| 517 | char numBuf[32]; |
| 518 | strncpy(numBuf, sp, sizeof(numBuf) - 1); |
| 519 | numBuf[sizeof(numBuf) - 1] = '\0'; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 520 | double dvalue = strtod(numBuf, NULL); |
| 521 | if (dvalue > bestValue) { |
| 522 | // Test that InstallDir is indeed there before keeping this index. |
| 523 | // Open the chosen key path remainder. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 524 | bestName = keyName; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 525 | // Append rest of key. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 526 | bestName.append(nextKey); |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 527 | lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0, |
| 528 | KEY_READ | KEY_WOW64_32KEY, &hKey); |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 529 | if (lResult == ERROR_SUCCESS) { |
Zachary Turner | e78a347 | 2016-07-28 17:13:32 +0000 | [diff] [blame] | 530 | if (readFullStringValue(hKey, valueName, value)) { |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 531 | bestValue = dvalue; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 532 | if (phValue) |
| 533 | *phValue = bestName; |
Hans Wennborg | d219231 | 2013-10-10 18:03:08 +0000 | [diff] [blame] | 534 | returnValue = true; |
| 535 | } |
| 536 | RegCloseKey(hKey); |
| 537 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 538 | } |
| 539 | size = sizeof(keyName) - 1; |
| 540 | } |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 541 | RegCloseKey(hTopKey); |
| 542 | } |
| 543 | } else { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 544 | lResult = |
Aaron Ballman | b06a359 | 2016-06-23 14:33:53 +0000 | [diff] [blame] | 545 | RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 546 | if (lResult == ERROR_SUCCESS) { |
Zachary Turner | e78a347 | 2016-07-28 17:13:32 +0000 | [diff] [blame] | 547 | if (readFullStringValue(hKey, valueName, value)) |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 548 | returnValue = true; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 549 | if (phValue) |
| 550 | phValue->clear(); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 551 | RegCloseKey(hKey); |
| 552 | } |
| 553 | } |
| 554 | return returnValue; |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 555 | #endif // USE_WIN32 |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 558 | // Find the most recent version of Universal CRT or Windows 10 SDK. |
| 559 | // vcvarsqueryregistry.bat from Visual Studio 2015 sorts entries in the include |
| 560 | // directory by name and uses the last one of the list. |
| 561 | // So we compare entry names lexicographically to find the greatest one. |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 562 | static bool getWindows10SDKVersionFromPath(const std::string &SDKPath, |
| 563 | std::string &SDKVersion) { |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 564 | SDKVersion.clear(); |
| 565 | |
| 566 | std::error_code EC; |
| 567 | llvm::SmallString<128> IncludePath(SDKPath); |
| 568 | llvm::sys::path::append(IncludePath, "Include"); |
| 569 | for (llvm::sys::fs::directory_iterator DirIt(IncludePath, EC), DirEnd; |
| 570 | DirIt != DirEnd && !EC; DirIt.increment(EC)) { |
| 571 | if (!llvm::sys::fs::is_directory(DirIt->path())) |
| 572 | continue; |
| 573 | StringRef CandidateName = llvm::sys::path::filename(DirIt->path()); |
| 574 | // If WDK is installed, there could be subfolders like "wdf" in the |
| 575 | // "Include" directory. |
| 576 | // Allow only directories which names start with "10.". |
| 577 | if (!CandidateName.startswith("10.")) |
| 578 | continue; |
| 579 | if (CandidateName > SDKVersion) |
| 580 | SDKVersion = CandidateName; |
| 581 | } |
| 582 | |
| 583 | return !SDKVersion.empty(); |
| 584 | } |
| 585 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 586 | /// \brief Get Windows SDK installation directory. |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 587 | static bool getWindowsSDKDir(std::string &Path, int &Major, |
| 588 | std::string &WindowsSDKIncludeVersion, |
| 589 | std::string &WindowsSDKLibVersion) { |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 590 | std::string RegistrySDKVersion; |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 591 | // Try the Windows registry. |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 592 | if (!getSystemRegistryString( |
| 593 | "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION", |
| 594 | "InstallationFolder", Path, &RegistrySDKVersion)) |
| 595 | return false; |
| 596 | if (Path.empty() || RegistrySDKVersion.empty()) |
| 597 | return false; |
| 598 | |
| 599 | WindowsSDKIncludeVersion.clear(); |
| 600 | WindowsSDKLibVersion.clear(); |
| 601 | Major = 0; |
| 602 | std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major); |
| 603 | if (Major <= 7) |
| 604 | return true; |
| 605 | if (Major == 8) { |
| 606 | // Windows SDK 8.x installs libraries in a folder whose names depend on the |
| 607 | // version of the OS you're targeting. By default choose the newest, which |
| 608 | // usually corresponds to the version of the OS you've installed the SDK on. |
| 609 | const char *Tests[] = {"winv6.3", "win8", "win7"}; |
| 610 | for (const char *Test : Tests) { |
| 611 | llvm::SmallString<128> TestPath(Path); |
| 612 | llvm::sys::path::append(TestPath, "Lib", Test); |
| 613 | if (llvm::sys::fs::exists(TestPath.c_str())) { |
| 614 | WindowsSDKLibVersion = Test; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | return !WindowsSDKLibVersion.empty(); |
| 619 | } |
| 620 | if (Major == 10) { |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 621 | if (!getWindows10SDKVersionFromPath(Path, WindowsSDKIncludeVersion)) |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 622 | return false; |
| 623 | WindowsSDKLibVersion = WindowsSDKIncludeVersion; |
| 624 | return true; |
| 625 | } |
| 626 | // Unsupported SDK version |
| 627 | return false; |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 630 | // Gets the library path required to link against the Windows SDK. |
| 631 | bool MSVCToolChain::getWindowsSDKLibraryPath(std::string &path) const { |
| 632 | std::string sdkPath; |
| 633 | int sdkMajor = 0; |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 634 | std::string windowsSDKIncludeVersion; |
| 635 | std::string windowsSDKLibVersion; |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 636 | |
| 637 | path.clear(); |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 638 | if (!getWindowsSDKDir(sdkPath, sdkMajor, windowsSDKIncludeVersion, |
| 639 | windowsSDKLibVersion)) |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 640 | return false; |
| 641 | |
| 642 | llvm::SmallString<128> libPath(sdkPath); |
| 643 | llvm::sys::path::append(libPath, "Lib"); |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 644 | if (sdkMajor >= 8) { |
| 645 | llvm::sys::path::append(libPath, |
| 646 | windowsSDKLibVersion, |
| 647 | "um", |
| 648 | llvmArchToWindowsSDKArch(getArch())); |
| 649 | } else { |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 650 | switch (getArch()) { |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 651 | // In Windows SDK 7.x, x86 libraries are directly in the Lib folder. |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 652 | case llvm::Triple::x86: |
| 653 | break; |
| 654 | case llvm::Triple::x86_64: |
| 655 | llvm::sys::path::append(libPath, "x64"); |
| 656 | break; |
| 657 | case llvm::Triple::arm: |
| 658 | // It is not necessary to link against Windows SDK 7.x when targeting ARM. |
| 659 | return false; |
| 660 | default: |
| 661 | return false; |
| 662 | } |
Zachary Turner | 10d75b2 | 2014-10-22 20:40:43 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | path = libPath.str(); |
| 666 | return true; |
| 667 | } |
| 668 | |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 669 | // Check if the Include path of a specified version of Visual Studio contains |
| 670 | // specific header files. If not, they are probably shipped with Universal CRT. |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 671 | bool MSVCToolChain::useUniversalCRT() const { |
| 672 | llvm::SmallString<128> TestPath(getSubDirectoryPath(SubDirectoryType::Include)); |
| 673 | llvm::sys::path::append(TestPath, "stdlib.h"); |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 674 | return !llvm::sys::fs::exists(TestPath); |
| 675 | } |
| 676 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 677 | static bool getUniversalCRTSdkDir(std::string &Path, |
| 678 | std::string &UCRTVersion) { |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 679 | // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry |
| 680 | // for the specific key "KitsRoot10". So do we. |
| 681 | if (!getSystemRegistryString( |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 682 | "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", |
| 683 | "KitsRoot10", Path, nullptr)) |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 684 | return false; |
| 685 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 686 | return getWindows10SDKVersionFromPath(Path, UCRTVersion); |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | bool MSVCToolChain::getUniversalCRTLibraryPath(std::string &Path) const { |
| 690 | std::string UniversalCRTSdkPath; |
| 691 | std::string UCRTVersion; |
| 692 | |
| 693 | Path.clear(); |
| 694 | if (!getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) |
| 695 | return false; |
| 696 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 697 | StringRef ArchName = llvmArchToWindowsSDKArch(getArch()); |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 698 | if (ArchName.empty()) |
| 699 | return false; |
| 700 | |
| 701 | llvm::SmallString<128> LibPath(UniversalCRTSdkPath); |
| 702 | llvm::sys::path::append(LibPath, "Lib", UCRTVersion, "ucrt", ArchName); |
| 703 | |
| 704 | Path = LibPath.str(); |
| 705 | return true; |
| 706 | } |
| 707 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 708 | static VersionTuple getMSVCVersionFromTriple(const llvm::Triple &Triple) { |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 709 | unsigned Major, Minor, Micro; |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 710 | Triple.getEnvironmentVersion(Major, Minor, Micro); |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 711 | if (Major || Minor || Micro) |
| 712 | return VersionTuple(Major, Minor, Micro); |
| 713 | return VersionTuple(); |
| 714 | } |
| 715 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 716 | static VersionTuple getMSVCVersionFromExe(const std::string &BinDir) { |
Adrian McCarthy | e4b26fc | 2016-05-13 23:20:11 +0000 | [diff] [blame] | 717 | VersionTuple Version; |
| 718 | #ifdef USE_WIN32 |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 719 | SmallString<128> ClExe(BinDir); |
Adrian McCarthy | e4b26fc | 2016-05-13 23:20:11 +0000 | [diff] [blame] | 720 | llvm::sys::path::append(ClExe, "cl.exe"); |
| 721 | |
| 722 | std::wstring ClExeWide; |
| 723 | if (!llvm::ConvertUTF8toWide(ClExe.c_str(), ClExeWide)) |
| 724 | return Version; |
| 725 | |
| 726 | const DWORD VersionSize = ::GetFileVersionInfoSizeW(ClExeWide.c_str(), |
| 727 | nullptr); |
| 728 | if (VersionSize == 0) |
| 729 | return Version; |
| 730 | |
| 731 | SmallVector<uint8_t, 4 * 1024> VersionBlock(VersionSize); |
| 732 | if (!::GetFileVersionInfoW(ClExeWide.c_str(), 0, VersionSize, |
| 733 | VersionBlock.data())) |
| 734 | return Version; |
| 735 | |
| 736 | VS_FIXEDFILEINFO *FileInfo = nullptr; |
| 737 | UINT FileInfoSize = 0; |
| 738 | if (!::VerQueryValueW(VersionBlock.data(), L"\\", |
| 739 | reinterpret_cast<LPVOID *>(&FileInfo), &FileInfoSize) || |
| 740 | FileInfoSize < sizeof(*FileInfo)) |
| 741 | return Version; |
| 742 | |
| 743 | const unsigned Major = (FileInfo->dwFileVersionMS >> 16) & 0xFFFF; |
| 744 | const unsigned Minor = (FileInfo->dwFileVersionMS ) & 0xFFFF; |
| 745 | const unsigned Micro = (FileInfo->dwFileVersionLS >> 16) & 0xFFFF; |
| 746 | |
| 747 | Version = VersionTuple(Major, Minor, Micro); |
| 748 | #endif |
| 749 | return Version; |
| 750 | } |
| 751 | |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 752 | void MSVCToolChain::AddSystemIncludeWithSubfolder( |
| 753 | const ArgList &DriverArgs, ArgStringList &CC1Args, |
| 754 | const std::string &folder, const Twine &subfolder1, const Twine &subfolder2, |
| 755 | const Twine &subfolder3) const { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 756 | llvm::SmallString<128> path(folder); |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 757 | llvm::sys::path::append(path, subfolder1, subfolder2, subfolder3); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 758 | addSystemInclude(DriverArgs, CC1Args, path); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 761 | void MSVCToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
| 762 | ArgStringList &CC1Args) const { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 763 | if (DriverArgs.hasArg(options::OPT_nostdinc)) |
| 764 | return; |
| 765 | |
| 766 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 767 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, getDriver().ResourceDir, |
| 768 | "include"); |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Nico Weber | fd3e1ad | 2016-04-12 19:04:37 +0000 | [diff] [blame] | 771 | // Add %INCLUDE%-like directories from the -imsvc flag. |
| 772 | for (const auto &Path : DriverArgs.getAllArgValues(options::OPT__SLASH_imsvc)) |
| 773 | addSystemInclude(DriverArgs, CC1Args, Path); |
| 774 | |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 775 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
| 776 | return; |
| 777 | |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 778 | // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat. |
David Majnemer | 85c25b4 | 2016-07-24 17:44:03 +0000 | [diff] [blame] | 779 | if (llvm::Optional<std::string> cl_include_dir = |
| 780 | llvm::sys::Process::GetEnv("INCLUDE")) { |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 781 | SmallVector<StringRef, 8> Dirs; |
David Majnemer | 85c25b4 | 2016-07-24 17:44:03 +0000 | [diff] [blame] | 782 | StringRef(*cl_include_dir) |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame] | 783 | .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false); |
| 784 | for (StringRef Dir : Dirs) |
| 785 | addSystemInclude(DriverArgs, CC1Args, Dir); |
| 786 | if (!Dirs.empty()) |
| 787 | return; |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 790 | // When built with access to the proper Windows APIs, try to actually find |
| 791 | // the correct include paths first. |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 792 | if (!VCToolChainPath.empty()) { |
| 793 | addSystemInclude(DriverArgs, |
| 794 | CC1Args, |
| 795 | getSubDirectoryPath(SubDirectoryType::Include)); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 796 | |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 797 | if (useUniversalCRT()) { |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 798 | std::string UniversalCRTSdkPath; |
| 799 | std::string UCRTVersion; |
| 800 | if (getUniversalCRTSdkDir(UniversalCRTSdkPath, UCRTVersion)) { |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 801 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, UniversalCRTSdkPath, |
| 802 | "Include", UCRTVersion, "ucrt"); |
Reid Kleckner | 7531f7d | 2015-09-11 00:09:39 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 806 | std::string WindowsSDKDir; |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 807 | int major; |
| 808 | std::string windowsSDKIncludeVersion; |
| 809 | std::string windowsSDKLibVersion; |
| 810 | if (getWindowsSDKDir(WindowsSDKDir, major, windowsSDKIncludeVersion, |
| 811 | windowsSDKLibVersion)) { |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 812 | if (major >= 8) { |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 813 | // Note: windowsSDKIncludeVersion is empty for SDKs prior to v10. |
| 814 | // Anyway, llvm::sys::path::append is able to manage it. |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 815 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 816 | "include", windowsSDKIncludeVersion, |
| 817 | "shared"); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 818 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 819 | "include", windowsSDKIncludeVersion, |
| 820 | "um"); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 821 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
Igor Kudrin | f2e7524 | 2015-09-24 05:16:36 +0000 | [diff] [blame] | 822 | "include", windowsSDKIncludeVersion, |
| 823 | "winrt"); |
Zachary Turner | 0eaf8fc | 2014-10-22 20:40:28 +0000 | [diff] [blame] | 824 | } else { |
| 825 | AddSystemIncludeWithSubfolder(DriverArgs, CC1Args, WindowsSDKDir, |
| 826 | "include"); |
| 827 | } |
Reid Kleckner | 77b45ba | 2014-04-23 00:15:01 +0000 | [diff] [blame] | 828 | } |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 829 | |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 832 | |
David Majnemer | d5f7d19 | 2016-07-25 04:47:44 +0000 | [diff] [blame] | 833 | #if defined(LLVM_ON_WIN32) |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 834 | // As a fallback, select default install paths. |
Alp Toker | fcce183 | 2014-06-22 03:27:45 +0000 | [diff] [blame] | 835 | // FIXME: Don't guess drives and paths like this on Windows. |
Joao Matos | 792d7af | 2012-09-04 17:29:52 +0000 | [diff] [blame] | 836 | const StringRef Paths[] = { |
| 837 | "C:/Program Files/Microsoft Visual Studio 10.0/VC/include", |
| 838 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/include", |
| 839 | "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include", |
| 840 | "C:/Program Files/Microsoft Visual Studio 8/VC/include", |
| 841 | "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include" |
| 842 | }; |
| 843 | addSystemIncludes(DriverArgs, CC1Args, Paths); |
David Majnemer | d5f7d19 | 2016-07-25 04:47:44 +0000 | [diff] [blame] | 844 | #endif |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Saleem Abdulrasool | 819f391 | 2014-10-22 02:37:29 +0000 | [diff] [blame] | 847 | void MSVCToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
| 848 | ArgStringList &CC1Args) const { |
Chandler Carruth | 1fc603e | 2011-12-17 23:10:01 +0000 | [diff] [blame] | 849 | // FIXME: There should probably be logic here to find libc++ on Windows. |
| 850 | } |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 851 | |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 852 | VersionTuple MSVCToolChain::computeMSVCVersion(const Driver *D, |
| 853 | const ArgList &Args) const { |
| 854 | bool IsWindowsMSVC = getTriple().isWindowsMSVCEnvironment(); |
| 855 | VersionTuple MSVT = ToolChain::computeMSVCVersion(D, Args); |
Reid Kleckner | 723fabf | 2017-02-02 19:29:46 +0000 | [diff] [blame^] | 856 | if (MSVT.empty()) |
| 857 | MSVT = getMSVCVersionFromTriple(getTriple()); |
| 858 | if (MSVT.empty() && IsWindowsMSVC) |
| 859 | MSVT = getMSVCVersionFromExe(getSubDirectoryPath(SubDirectoryType::Bin)); |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 860 | if (MSVT.empty() && |
| 861 | Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions, |
| 862 | IsWindowsMSVC)) { |
| 863 | // -fms-compatibility-version=18.00 is default. |
| 864 | // FIXME: Consider bumping this to 19 (MSVC2015) soon. |
| 865 | MSVT = VersionTuple(18); |
| 866 | } |
| 867 | return MSVT; |
| 868 | } |
| 869 | |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 870 | std::string |
| 871 | MSVCToolChain::ComputeEffectiveClangTriple(const ArgList &Args, |
| 872 | types::ID InputType) const { |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 873 | // The MSVC version doesn't care about the architecture, even though it |
| 874 | // may look at the triple internally. |
| 875 | VersionTuple MSVT = computeMSVCVersion(/*D=*/nullptr, Args); |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 876 | MSVT = VersionTuple(MSVT.getMajor(), MSVT.getMinor().getValueOr(0), |
| 877 | MSVT.getSubminor().getValueOr(0)); |
| 878 | |
David L. Jones | 24fb20c | 2016-12-07 23:41:58 +0000 | [diff] [blame] | 879 | // For the rest of the triple, however, a computed architecture name may |
| 880 | // be needed. |
| 881 | llvm::Triple Triple(ToolChain::ComputeEffectiveClangTriple(Args, InputType)); |
David Majnemer | 75fdd6b | 2015-06-09 06:30:01 +0000 | [diff] [blame] | 882 | if (Triple.getEnvironment() == llvm::Triple::MSVC) { |
| 883 | StringRef ObjFmt = Triple.getEnvironmentName().split('-').second; |
| 884 | if (ObjFmt.empty()) |
| 885 | Triple.setEnvironmentName((Twine("msvc") + MSVT.getAsString()).str()); |
| 886 | else |
| 887 | Triple.setEnvironmentName( |
| 888 | (Twine("msvc") + MSVT.getAsString() + Twine('-') + ObjFmt).str()); |
| 889 | } |
David Majnemer | e11d373 | 2015-06-08 00:22:46 +0000 | [diff] [blame] | 890 | return Triple.getTriple(); |
| 891 | } |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 892 | |
| 893 | SanitizerMask MSVCToolChain::getSupportedSanitizers() const { |
| 894 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
| 895 | Res |= SanitizerKind::Address; |
Alexey Samsonov | 7f2a0d2 | 2015-06-19 21:36:47 +0000 | [diff] [blame] | 896 | return Res; |
| 897 | } |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 898 | |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 899 | static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL, |
| 900 | bool SupportsForcingFramePointer, |
| 901 | const char *ExpandChar, const OptTable &Opts) { |
| 902 | assert(A->getOption().matches(options::OPT__SLASH_O)); |
| 903 | |
| 904 | StringRef OptStr = A->getValue(); |
| 905 | for (size_t I = 0, E = OptStr.size(); I != E; ++I) { |
| 906 | const char &OptChar = *(OptStr.data() + I); |
| 907 | switch (OptChar) { |
| 908 | default: |
| 909 | break; |
| 910 | case '1': |
| 911 | case '2': |
| 912 | case 'x': |
| 913 | case 'd': |
| 914 | if (&OptChar == ExpandChar) { |
| 915 | if (OptChar == 'd') { |
| 916 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_O0)); |
| 917 | } else { |
| 918 | if (OptChar == '1') { |
| 919 | DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); |
| 920 | } else if (OptChar == '2' || OptChar == 'x') { |
| 921 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); |
| 922 | DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); |
| 923 | } |
David Majnemer | 259d71a | 2016-01-21 23:01:11 +0000 | [diff] [blame] | 924 | if (SupportsForcingFramePointer && |
| 925 | !DAL.hasArgNoClaim(options::OPT_fno_omit_frame_pointer)) |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 926 | DAL.AddFlagArg(A, |
| 927 | Opts.getOption(options::OPT_fomit_frame_pointer)); |
| 928 | if (OptChar == '1' || OptChar == '2') |
| 929 | DAL.AddFlagArg(A, |
| 930 | Opts.getOption(options::OPT_ffunction_sections)); |
| 931 | } |
| 932 | } |
| 933 | break; |
| 934 | case 'b': |
Hans Wennborg | 3270bdb | 2016-05-24 21:23:29 +0000 | [diff] [blame] | 935 | if (I + 1 != E && isdigit(OptStr[I + 1])) { |
| 936 | switch (OptStr[I + 1]) { |
| 937 | case '0': |
| 938 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_inline)); |
| 939 | break; |
| 940 | case '1': |
Hans Wennborg | 44d061a | 2016-06-22 16:56:16 +0000 | [diff] [blame] | 941 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_hint_functions)); |
Hans Wennborg | 3270bdb | 2016-05-24 21:23:29 +0000 | [diff] [blame] | 942 | break; |
| 943 | case '2': |
| 944 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_finline_functions)); |
| 945 | break; |
| 946 | } |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 947 | ++I; |
Hans Wennborg | 3270bdb | 2016-05-24 21:23:29 +0000 | [diff] [blame] | 948 | } |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 949 | break; |
| 950 | case 'g': |
| 951 | break; |
| 952 | case 'i': |
| 953 | if (I + 1 != E && OptStr[I + 1] == '-') { |
| 954 | ++I; |
| 955 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_fno_builtin)); |
| 956 | } else { |
| 957 | DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin)); |
| 958 | } |
| 959 | break; |
| 960 | case 's': |
| 961 | DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "s"); |
| 962 | break; |
| 963 | case 't': |
| 964 | DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2"); |
| 965 | break; |
| 966 | case 'y': { |
| 967 | bool OmitFramePointer = true; |
| 968 | if (I + 1 != E && OptStr[I + 1] == '-') { |
| 969 | OmitFramePointer = false; |
| 970 | ++I; |
| 971 | } |
| 972 | if (SupportsForcingFramePointer) { |
| 973 | if (OmitFramePointer) |
| 974 | DAL.AddFlagArg(A, |
| 975 | Opts.getOption(options::OPT_fomit_frame_pointer)); |
| 976 | else |
| 977 | DAL.AddFlagArg( |
| 978 | A, Opts.getOption(options::OPT_fno_omit_frame_pointer)); |
Nico Weber | 9c3fca3 | 2016-03-23 15:37:41 +0000 | [diff] [blame] | 979 | } else { |
| 980 | // Don't warn about /Oy- in 64-bit builds (where |
| 981 | // SupportsForcingFramePointer is false). The flag having no effect |
| 982 | // there is a compiler-internal optimization, and people shouldn't have |
| 983 | // to special-case their build files for 64-bit clang-cl. |
| 984 | A->claim(); |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 985 | } |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | static void TranslateDArg(Arg *A, llvm::opt::DerivedArgList &DAL, |
| 993 | const OptTable &Opts) { |
| 994 | assert(A->getOption().matches(options::OPT_D)); |
| 995 | |
| 996 | StringRef Val = A->getValue(); |
| 997 | size_t Hash = Val.find('#'); |
| 998 | if (Hash == StringRef::npos || Hash > Val.find('=')) { |
| 999 | DAL.append(A); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | std::string NewVal = Val; |
| 1004 | NewVal[Hash] = '='; |
| 1005 | DAL.AddJoinedArg(A, Opts.getOption(options::OPT_D), NewVal); |
| 1006 | } |
| 1007 | |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1008 | llvm::opt::DerivedArgList * |
| 1009 | MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args, |
Samuel Antao | 31fef98 | 2016-10-27 17:39:44 +0000 | [diff] [blame] | 1010 | StringRef BoundArch, Action::OffloadKind) const { |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1011 | DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); |
| 1012 | const OptTable &Opts = getDriver().getOpts(); |
| 1013 | |
David Majnemer | 7ab76f2 | 2015-08-25 00:46:45 +0000 | [diff] [blame] | 1014 | // /Oy and /Oy- only has an effect under X86-32. |
| 1015 | bool SupportsForcingFramePointer = getArch() == llvm::Triple::x86; |
| 1016 | |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1017 | // The -O[12xd] flag actually expands to several flags. We must desugar the |
| 1018 | // flags so that options embedded can be negated. For example, the '-O2' flag |
| 1019 | // enables '-Oy'. Expanding '-O2' into its constituent flags allows us to |
| 1020 | // correctly handle '-O2 -Oy-' where the trailing '-Oy-' disables a single |
| 1021 | // aspect of '-O2'. |
| 1022 | // |
| 1023 | // Note that this expansion logic only applies to the *last* of '[12xd]'. |
| 1024 | |
| 1025 | // First step is to search for the character we'd like to expand. |
| 1026 | const char *ExpandChar = nullptr; |
| 1027 | for (Arg *A : Args) { |
| 1028 | if (!A->getOption().matches(options::OPT__SLASH_O)) |
| 1029 | continue; |
| 1030 | StringRef OptStr = A->getValue(); |
| 1031 | for (size_t I = 0, E = OptStr.size(); I != E; ++I) { |
Hans Wennborg | debfed9 | 2016-05-25 00:43:45 +0000 | [diff] [blame] | 1032 | char OptChar = OptStr[I]; |
| 1033 | char PrevChar = I > 0 ? OptStr[I - 1] : '0'; |
| 1034 | if (PrevChar == 'b') { |
| 1035 | // OptChar does not expand; it's an argument to the previous char. |
| 1036 | continue; |
| 1037 | } |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1038 | if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd') |
| 1039 | ExpandChar = OptStr.data() + I; |
| 1040 | } |
| 1041 | } |
| 1042 | |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1043 | for (Arg *A : Args) { |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 1044 | if (A->getOption().matches(options::OPT__SLASH_O)) { |
| 1045 | // The -O flag actually takes an amalgam of other options. For example, |
| 1046 | // '/Ogyb2' is equivalent to '/Og' '/Oy' '/Ob2'. |
| 1047 | TranslateOptArg(A, *DAL, SupportsForcingFramePointer, ExpandChar, Opts); |
| 1048 | } else if (A->getOption().matches(options::OPT_D)) { |
| 1049 | // Translate -Dfoo#bar into -Dfoo=bar. |
| 1050 | TranslateDArg(A, *DAL, Opts); |
| 1051 | } else { |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1052 | DAL->append(A); |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
Hans Wennborg | 21d73d2 | 2016-01-12 23:17:03 +0000 | [diff] [blame] | 1055 | |
David Majnemer | 015ce0f | 2015-07-27 07:32:11 +0000 | [diff] [blame] | 1056 | return DAL; |
| 1057 | } |