Index: include/clang/Driver/Job.h =================================================================== --- include/clang/Driver/Job.h +++ include/clang/Driver/Job.h @@ -69,6 +69,9 @@ /// file std::string ResponseFileFlag; + /// See Command::setEnvironment + std::vector Environment; + /// When a response file is needed, we try to put most arguments in an /// exclusive file, while others remains as regular command line arguments. /// This functions fills a vector with the regular command line arguments, @@ -111,6 +114,14 @@ InputFileList = std::move(List); } + /// \brief Set the environment to be used by the new process. + /// \param NewEnvironment A vector of environment variables. + /// \remark If NewEnvironment is empty, or infact if the environment remains + /// unset, then the environment from this process will be used. + void setEnvironment(std::vector NewEnvironment) { + Environment = std::move(NewEnvironment); + } + const char *getExecutable() const { return Executable; } const llvm::opt::ArgStringList &getArguments() const { return Arguments; } Index: lib/Driver/Job.cpp =================================================================== --- lib/Driver/Job.cpp +++ lib/Driver/Job.cpp @@ -304,6 +304,9 @@ int Command::Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) const { SmallVector Argv; + auto Envp = Environment.empty() + ? nullptr + : const_cast(Environment.data()); if (ResponseFile == nullptr) { Argv.push_back(Executable); @@ -310,7 +313,7 @@ Argv.append(Arguments.begin(), Arguments.end()); Argv.push_back(nullptr); - return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr, + return llvm::sys::ExecuteAndWait(Executable, Argv.data(), Envp, Redirects, /*secondsToWait*/ 0, /*memoryLimit*/ 0, ErrMsg, ExecutionFailed); @@ -337,7 +340,7 @@ return -1; } - return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr, + return llvm::sys::ExecuteAndWait(Executable, Argv.data(), Envp, Redirects, /*secondsToWait*/ 0, /*memoryLimit*/ 0, ErrMsg, ExecutionFailed); } Index: lib/Driver/ToolChains/MSVC.cpp =================================================================== --- lib/Driver/ToolChains/MSVC.cpp +++ lib/Driver/ToolChains/MSVC.cpp @@ -45,17 +45,21 @@ #endif #include -// Make sure this comes before MSVCSetupApi.h -#include + // Undefine this macro so we can call the ANSI version of the function. + #undef GetEnvironmentStrings + #define GetEnvironmentStringsA GetEnvironmentStrings -#include "MSVCSetupApi.h" -#include "llvm/Support/COM.h" -_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration)); -_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2)); -_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper)); -_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances)); -_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance)); -_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2)); + // Make sure this comes before MSVCSetupApi.h + #include + + #include "MSVCSetupApi.h" + #include "llvm/Support/COM.h" + _COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration)); + _COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2)); + _COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper)); + _COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances)); + _COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance)); + _COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2)); #endif using namespace clang::driver; @@ -441,6 +445,8 @@ TC.addProfileRTLibs(Args, CmdArgs); + std::vector Environment; + // We need to special case some linker paths. In the case of lld, we need to // translate 'lld' into 'lld-link', and in the case of the regular msvc // linker, we need to use a special search algorithm. @@ -454,6 +460,59 @@ // from the program PATH, because other environments like GnuWin32 install // their own link.exe which may come first. linkPath = FindVisualStudioExecutable(TC, "link.exe"); + +#ifdef USE_WIN32 + if (TC.getIsVS2017OrNewer()) { + // When cross-compiling with VS2017 or newer, link.exe expects to have + // its containing bin directory at the top of PATH, followed by the + // native target bin directory. + // e.g. when compiling for x86 on an x64 host, PATH should start with: + // /bin/HostX64/x86;/bin/HostX64/x64 + llvm::Triple Host(llvm::sys::getProcessTriple()); + if (Host.getArch() == TC.getArch()) goto SkipSettingEnvironment; + + char *EnvBlock = GetEnvironmentStringsA(); + if (EnvBlock == nullptr) goto SkipSettingEnvironment; + + size_t EnvCount = 0; + for (const char *Cursor = EnvBlock; + *Cursor != '\0'; + Cursor += strlen(Cursor) + 1/*null-terminator*/) + ++EnvCount; + + Environment.reserve(EnvCount); + + // Now loop over each string in the block and copy them into the + // environment vector, adjusting the PATH variable as needed when we + // find it. + for (const char *Cursor = EnvBlock; *Cursor != '\0';) { + llvm::StringRef EnvVar(Cursor); + if (EnvVar.startswith_lower("path=")) { + using SubDirectoryType = toolchains::MSVCToolChain::SubDirectoryType; + constexpr size_t PrefixLen = 5; // strlen("path=") + Environment.push_back(Args.MakeArgString( + EnvVar.substr(0, PrefixLen) + + TC.getSubDirectoryPath(SubDirectoryType::Bin) + + llvm::Twine(llvm::sys::EnvPathSeparator) + + TC.getSubDirectoryPath(SubDirectoryType::Bin, Host.getArch()) + + (EnvVar.size() > PrefixLen + ? llvm::Twine(llvm::sys::EnvPathSeparator) + + EnvVar.substr(PrefixLen) + : "") + )); + } + else { + Environment.push_back(Args.MakeArgString(EnvVar)); + } + Cursor += EnvVar.size() + 1/*null-terminator*/; + } + + FreeEnvironmentStringsA(EnvBlock); + + SkipSettingEnvironment: + ; + } +#endif } else { linkPath = Linker; llvm::sys::path::replace_extension(linkPath, "exe"); @@ -460,8 +519,11 @@ linkPath = TC.GetProgramPath(linkPath.c_str()); } - const char *Exec = Args.MakeArgString(linkPath); - C.addCommand(llvm::make_unique(JA, *this, Exec, CmdArgs, Inputs)); + auto LinkCmd = llvm::make_unique( + JA, *this, Args.MakeArgString(linkPath), CmdArgs, Inputs); + if (!Environment.empty()) + LinkCmd->setEnvironment(std::move(Environment)); + C.addCommand(std::move(LinkCmd)); } void visualstudio::Compiler::ConstructJob(Compilation &C, const JobAction &JA,