[PATCH] Make a good guess about where MSVC and Windows SDK libraries are for linking.

Aaron Ballman aaron.ballman at gmail.com
Tue Oct 21 06:21:58 PDT 2014


On Mon, Oct 20, 2014 at 5:24 PM, Zachary Turner <zturner at google.com> wrote:
> Hi aaron.ballman, hans, majnemer, rnk,
>
>     When a user doesn't run vcvarsall, clang doesn't know where system
>     libraries are located.  This patch causes clang to still use the
>     configured environment variables when present, but when they are
>     not present, to make a (very good) guess.
>
> http://reviews.llvm.org/D5873
>
> Files:
>   lib/Driver/ToolChains.h
>   lib/Driver/Tools.cpp
>   lib/Driver/WindowsToolChain.cpp

> 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<const toolchains::Windows &>(getToolChain());

Use auto instead?

> +    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");

Don't we currently support WoA, so we might need the arm directory as well?

> +
> +      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 <windows.h>
> +#include <VersionHelpers.h>
>  #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");

For my Windows SDK path, I have:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64

instead of 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.
>

~Aaron



More information about the cfe-commits mailing list