Index: lib/Driver/ToolChains.h =================================================================== --- lib/Driver/ToolChains.h +++ lib/Driver/ToolChains.h @@ -746,6 +746,7 @@ llvm::opt::ArgStringList &CC1Args) const override; bool getWindowsSDKDir(std::string &path, int &major, int &minor) const; + bool getWindowsSDKLibraryPath(std::string &path, bool x64) const; bool getVisualStudioDir(std::string &path) const; protected: Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -7870,6 +7870,32 @@ CmdArgs.push_back("-defaultlib:libcmt"); } + if (!llvm::sys::Process::GetEnv("LIB")) { + // If the VC environment hasn't been configured (perhaps because the user + // did not run vcvarsall, try to build a consistent link environment. If + // the environment was set by the user, we trust the user to know what he's + // doing. + std::string VisualStudioDir; + const toolchains::Windows &WTC = + static_cast(getToolChain()); + if (WTC.getVisualStudioDir(VisualStudioDir)) { + SmallString<128> VSDir(VisualStudioDir); + llvm::sys::path::append(VSDir, "VC\\lib"); + if (Args.hasArg(options::OPT_m64)) + llvm::sys::path::append(VSDir, "amd64"); + + CmdArgs.push_back( + Args.MakeArgString(std::string("-libpath:") + VSDir.c_str())); + } + + std::string WindowsSdkLibPath; + if (WTC.getWindowsSDKLibraryPath(WindowsSdkLibPath, + Args.hasArg(options::OPT_m64))) { + CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") + + WindowsSdkLibPath.c_str())); + } + } + CmdArgs.push_back("-nologo"); if (Args.hasArg(options::OPT_g_Group)) { Index: lib/Driver/WindowsToolChain.cpp =================================================================== --- lib/Driver/WindowsToolChain.cpp +++ lib/Driver/WindowsToolChain.cpp @@ -30,6 +30,7 @@ #define NOGDI #define NOMINMAX #include +#include #endif using namespace clang::driver; @@ -211,6 +212,40 @@ return hasSDKDir && !path.empty(); } +// \brief Gets the library path required to link against the Windows SDK. +bool Windows::getWindowsSDKLibraryPath(std::string &path, bool x64) const { + std::string sdkPath; + int sdkMajor = 0; + int sdkMinor = 0; + + if (!getWindowsSDKDir(sdkPath, sdkMajor, sdkMinor)) + return false; + llvm::SmallString<128> tempPath(sdkPath); + llvm::sys::path::append(tempPath, "Lib"); + bool unknownSdkLayout = false; + if (sdkMajor <= 7) { + if (x64) + llvm::sys::path::append(tempPath, "amd64"); + } else { + if (IsWindows8Point1OrGreater()) + llvm::sys::path::append(tempPath, "winv6.3\\um"); + else if (IsWindows8OrGreater()) + llvm::sys::path::append(tempPath, "win8\\um"); + else if (IsWindows7OrGreater()) + llvm::sys::path::append(tempPath, "win7\\um"); + else + unknownSdkLayout = true; + + if (x64) + llvm::sys::path::append(tempPath, "x64"); + else + llvm::sys::path::append(tempPath, "x86"); + } + if (!unknownSdkLayout) + path = tempPath.c_str(); + return !unknownSdkLayout; +} + // Get Visual Studio installation directory. bool Windows::getVisualStudioDir(std::string &path) const { // First check the environment variables that vsvars32.bat sets.