diff --git a/include/vcpkg/base/system.h b/include/vcpkg/base/system.h index 6531546af1..c5115e35d2 100644 --- a/include/vcpkg/base/system.h +++ b/include/vcpkg/base/system.h @@ -14,7 +14,7 @@ namespace vcpkg Optional get_environment_variable(ZStringView varname) noexcept; void set_environment_variable(ZStringView varname, Optional value) noexcept; - std::string get_environment_variables(); + std::vector get_environment_variables(); const ExpectedL& get_home_dir() noexcept; diff --git a/src/vcpkg.cpp b/src/vcpkg.cpp index 2b62cb5506..8b71440aa9 100644 --- a/src/vcpkg.cpp +++ b/src/vcpkg.cpp @@ -289,7 +289,7 @@ int main(const int argc, const char* const* const argv) { msg::write_unlocalized_text(Color::none, "[DEBUG] The following environment variables are currently set:\n" + - get_environment_variables() + '\n'); + Strings::join("\n", get_environment_variables()) + '\n'); } else if (Debug::g_debugging) { diff --git a/src/vcpkg/base/system.cpp b/src/vcpkg/base/system.cpp index ac4d4b8d83..adea4b510c 100644 --- a/src/vcpkg/base/system.cpp +++ b/src/vcpkg/base/system.cpp @@ -367,9 +367,9 @@ namespace vcpkg #endif } - std::string get_environment_variables() + std::vector get_environment_variables() { - std::string result; + std::vector result; #if defined(_WIN32) const struct EnvironmentStringsW { @@ -382,12 +382,12 @@ namespace vcpkg for (LPWCH i = env_block.strings; *i; i += len + 1) { len = wcslen(i); - result.append(Strings::to_utf8(i, len)).push_back('\n'); + result.emplace_back(Strings::to_utf8(i, len)); } #else for (char** s = environ; *s; s++) { - result.append(*s).push_back('\n'); + result.emplace_back(*s); } #endif return result; diff --git a/src/vcpkg/base/system.process.cpp b/src/vcpkg/base/system.process.cpp index ac4215365c..3bfd5275d9 100644 --- a/src/vcpkg/base/system.process.cpp +++ b/src/vcpkg/base/system.process.cpp @@ -561,7 +561,7 @@ namespace vcpkg system32_env, "\\WindowsPowerShell\\v1.0\\"); - std::vector env_strings = { + std::unordered_set env_strings = { "ALLUSERSPROFILE", "APPDATA", "CommonProgramFiles", @@ -610,15 +610,6 @@ namespace vcpkg "SSH_AUTH_SOCK", "SSH_AGENT_PID", // Enables find_package(CUDA) and enable_language(CUDA) in CMake - "CUDA_PATH", - "CUDA_PATH_V9_0", - "CUDA_PATH_V9_1", - "CUDA_PATH_V10_0", - "CUDA_PATH_V10_1", - "CUDA_PATH_V10_2", - "CUDA_PATH_V11_0", - "CUDA_PATH_V11_1", - "CUDA_PATH_V11_2", "CUDA_TOOLKIT_ROOT_DIR", // Environment variable generated automatically by CUDA after installation "NVCUDASAMPLES_ROOT", @@ -643,6 +634,11 @@ namespace vcpkg "GXDKLatest", }; + std::vector env_prefix_string = { + // Enables find_package(CUDA) and enable_language(CUDA) in CMake + "CUDA_PATH", + }; + const Optional keep_vars = get_environment_variable(EnvironmentVariableVcpkgKeepEnvVars); const auto k = keep_vars.get(); @@ -660,20 +656,23 @@ namespace vcpkg } else { - env_strings.push_back(std::move(var)); + env_strings.emplace(std::move(var)); } } } Environment env; - for (auto&& env_string : env_strings) + for (auto&& env_var : get_environment_variables()) { - const Optional value = get_environment_variable(env_string); - const auto v = value.get(); - if (!v || v->empty()) continue; - - env.add_entry(env_string, *v); + auto pos = env_var.find('='); + auto key = env_var.substr(0, pos); + if (Util::Sets::contains(env_strings, key) || + Util::any_of(env_prefix_string, [&](auto&& group) { return Strings::starts_with(key, group); })) + { + auto value = pos == std::string::npos ? "" : env_var.substr(pos + 1); + env.add_entry(key, value); + } } const auto path_iter = extra_env.find(EnvironmentVariablePath.to_string());