Rework vcpkg's triplet environment reading
The triplet is "run" and vcpkg observes the environment.
Previously, the environment was deduced by the triplet's name
MODIFY
include/PostBuildLint.h
include/PostBuildLint.h
namespace vcpkg::PostBuildLint
{
- size_t perform_all_checks(const PackageSpec& spec, const VcpkgPaths& paths, const Build::BuildInfo& build_info);
+ size_t perform_all_checks(const PackageSpec& spec,
+ const VcpkgPaths& paths,
+ const Build::PreBuildInfo& pre_build_info,
+ const Build::BuildInfo& build_info);
}

MODIFY
include/triplet.h
include/triplet.h
@@ -15,8 +15,6 @@ namespace vcpkg
static const Triplet ARM_UWP;
const std::string& canonical_name() const;
- std::string architecture() const;
- std::string system() const;
const std::string& to_string() const;
private:

MODIFY
include/vcpkg_Build.h
include/vcpkg_Build.h
#include "vcpkg_Files.h"
#include "vcpkg_optional.h"
#include <map>
-#include <string>
#include <unordered_map>
#include <vector>
@@ -33,7 +32,17 @@ namespace vcpkg::Build
std::string create_error_message(const BuildResult build_result, const PackageSpec& spec);
std::string create_user_troubleshooting_message(const PackageSpec& spec);
- std::wstring make_build_env_cmd(const Triplet& triplet, const Toolset& toolset);
+ struct PreBuildInfo
+ {
+ static PreBuildInfo from_triplet_file(const VcpkgPaths& paths, const Triplet& triplet);
+
+ std::string target_architecture;
+ std::string cmake_system_name;
+ std::string cmake_system_version;
+ std::string platform_toolset;
+ };
+
+ std::wstring make_build_env_cmd(const PreBuildInfo& pre_build_info, const Toolset& toolset);
struct ExtendedBuildResult
{

MODIFY
include/vcpkg_System.h
include/vcpkg_System.h
@@ -65,6 +65,18 @@ namespace vcpkg::System
Optional<std::wstring> get_registry_string(HKEY base, const CWStringView subkey, const CWStringView valuename);
+ enum class CPUArchitecture
+ {
+ X86,
+ X64,
+ ARM,
+ ARM64,
+ };
+
+ Optional<CPUArchitecture> to_cpu_architecture(CStringView arch);
+
+ CPUArchitecture get_host_processor();
+
const fs::path& get_ProgramFiles_32_bit();
const fs::path& get_ProgramFiles_platform_bitness();

MODIFY
src/PostBuildLint.cpp
src/PostBuildLint.cpp
#include "vcpkg_System.h"
#include "vcpkg_Util.h"
+using vcpkg::Build::PreBuildInfo;
using vcpkg::Build::BuildInfo;
namespace vcpkg::PostBuildLint
@@ -320,7 +321,7 @@ namespace vcpkg::PostBuildLint
const std::vector<fs::path>& dlls,
const fs::path dumpbin_exe)
{
- if (expected_system_name != "uwp")
+ if (expected_system_name != "WindowsStore")
{
return LintStatus::SUCCESS;
}
@@ -719,6 +720,7 @@ namespace vcpkg::PostBuildLint
static size_t perform_all_checks_and_return_error_count(const PackageSpec& spec,
const VcpkgPaths& paths,
+ const PreBuildInfo& pre_build_info,
const BuildInfo& build_info)
{
const auto& fs = paths.get_filesystem();
@@ -763,7 +765,7 @@ namespace vcpkg::PostBuildLint
libs.insert(libs.cend(), debug_libs.cbegin(), debug_libs.cend());
libs.insert(libs.cend(), release_libs.cbegin(), release_libs.cend());
- error_count += check_lib_architecture(spec.triplet().architecture(), libs);
+ error_count += check_lib_architecture(pre_build_info.target_architecture, libs);
}
switch (build_info.library_linkage)
@@ -787,8 +789,8 @@ namespace vcpkg::PostBuildLint
dlls.insert(dlls.cend(), release_dlls.cbegin(), release_dlls.cend());
error_count += check_exports_of_dlls(dlls, toolset.dumpbin);
- error_count += check_uwp_bit_of_dlls(spec.triplet().system(), dlls, toolset.dumpbin);
- error_count += check_dll_architecture(spec.triplet().architecture(), dlls);
+ error_count += check_uwp_bit_of_dlls(pre_build_info.cmake_system_name, dlls, toolset.dumpbin);
+ error_count += check_dll_architecture(pre_build_info.target_architecture, dlls);
error_count += check_outdated_crt_linkage_of_dlls(dlls, toolset.dumpbin);
break;
@@ -825,10 +827,13 @@ namespace vcpkg::PostBuildLint
return error_count;
}
- size_t perform_all_checks(const PackageSpec& spec, const VcpkgPaths& paths, const BuildInfo& build_info)
+ size_t perform_all_checks(const PackageSpec& spec,
+ const VcpkgPaths& paths,
+ const PreBuildInfo& pre_build_info,
+ const BuildInfo& build_info)
{
System::println("-- Performing post-build validation");
- const size_t error_count = perform_all_checks_and_return_error_count(spec, paths, build_info);
+ const size_t error_count = perform_all_checks_and_return_error_count(spec, paths, pre_build_info, build_info);
if (error_count != 0)
{

MODIFY
src/commands_build.cpp
src/commands_build.cpp
#include "Paragraphs.h"
#include "PostBuildLint.h"
#include "StatusParagraphs.h"
-#include "metrics.h"
-#include "vcpkg_Chrono.h"
#include "vcpkg_Commands.h"
#include "vcpkg_Dependencies.h"
#include "vcpkg_Enums.h"
#include "vcpkg_Input.h"
#include "vcpkg_System.h"
-#include "vcpkg_Util.h"
#include "vcpkglib.h"
using vcpkg::Build::BuildResult;
@@ -29,8 +26,9 @@ namespace vcpkg::Commands::BuildCommand
{
if (options.find(OPTION_CHECKS_ONLY) != options.end())
{
+ auto pre_build_info = Build::PreBuildInfo::from_triplet_file(paths, spec.triplet());
auto build_info = Build::read_build_info(paths.get_filesystem(), paths.build_info_file_path(spec));
- const size_t error_count = PostBuildLint::perform_all_checks(spec, paths, build_info);
+ const size_t error_count = PostBuildLint::perform_all_checks(spec, paths, pre_build_info, build_info);
Checks::check_exit(VCPKG_LINE_INFO, error_count == 0);
Checks::exit_success(VCPKG_LINE_INFO);
}

MODIFY
src/commands_env.cpp
src/commands_env.cpp
@@ -12,7 +12,8 @@ namespace vcpkg::Commands::Env
args.check_exact_arg_count(0, example);
args.check_and_get_optional_command_arguments({});
- System::cmd_execute_clean(Build::make_build_env_cmd(default_triplet, paths.get_toolset()) + L" && cmd");
+ auto pre_build_info = Build::PreBuildInfo::from_triplet_file(paths, default_triplet);
+ System::cmd_execute_clean(Build::make_build_env_cmd(pre_build_info, paths.get_toolset()) + L" && cmd");
Checks::exit_success(VCPKG_LINE_INFO);
}

MODIFY
src/triplet.cpp
src/triplet.cpp
@@ -32,17 +32,5 @@ namespace vcpkg
const std::string& Triplet::canonical_name() const { return this->m_canonical_name; }
- std::string Triplet::architecture() const
- {
- auto it = std::find(this->m_canonical_name.cbegin(), this->m_canonical_name.cend(), '-');
- return std::string(this->m_canonical_name.cbegin(), it);
- }
-
- std::string Triplet::system() const
- {
- auto it = std::find(this->m_canonical_name.cbegin(), this->m_canonical_name.cend(), '-');
- return std::string(it + 1, this->m_canonical_name.cend());
- }
-
const std::string& Triplet::to_string() const { return this->m_canonical_name; }
}

MODIFY
src/vcpkg_Build.cpp
src/vcpkg_Build.cpp
@@ -26,7 +26,54 @@ namespace vcpkg::Build
static const std::string LIBRARY_LINKAGE = "LibraryLinkage";
}
- std::wstring make_build_env_cmd(const Triplet& triplet, const Toolset& toolset)
+ CWStringView to_vcvarsall_target(const std::string& cmake_system_name)
+ {
+ if (cmake_system_name == "") return L"";
+ if (cmake_system_name == "Windows") return L"";
+ if (cmake_system_name == "WindowsStore") return L"store";
+
+ Checks::exit_with_message(VCPKG_LINE_INFO, "Unsupported vcvarsall target %s", cmake_system_name);
+ }
+
+ CWStringView to_vcvarsall_toolchain(const std::string& target_architecture)
+ {
+ using CPU = System::CPUArchitecture;
+
+ struct ArchOption
+ {
+ CWStringView name;
+ CPU host_arch;
+ CPU target_arch;
+ };
+
+ static constexpr ArchOption X86 = {L"x86", CPU::X86, CPU::X86};
+ static constexpr ArchOption X86_X64 = {L"x86_x64", CPU::X86, CPU::X64};
+ static constexpr ArchOption X86_ARM = {L"x86_arm", CPU::X86, CPU::ARM};
+ static constexpr ArchOption X86_ARM64 = {L"x86_arm64", CPU::X86, CPU::ARM64};
+
+ static constexpr ArchOption X64 = {L"x64", CPU::X64, CPU::X64};
+ static constexpr ArchOption X64_X86 = {L"x64_x86", CPU::X64, CPU::X86};
+ static constexpr ArchOption X64_ARM = {L"x64_arm", CPU::X64, CPU::ARM};
+ static constexpr ArchOption X64_ARM64 = {L"x64_arm64", CPU::X64, CPU::ARM64};
+
+ static constexpr std::array<ArchOption, 8> VALUES = {
+ X86, X86_X64, X86_ARM, X86_ARM64, X64, X64_X86, X64_ARM, X64_ARM64};
+
+ auto target_arch = System::to_cpu_architecture(target_architecture);
+ auto host_arch = System::get_host_processor();
+
+ for (auto&& value : VALUES)
+ {
+ if (target_arch == value.target_arch && host_arch == value.host_arch)
+ {
+ return value.name;
+ }
+ }
+
+ Checks::exit_with_message(VCPKG_LINE_INFO, "Unsupported toolchain combination %s", target_architecture);
+ }
+
+ std::wstring make_build_env_cmd(const PreBuildInfo& pre_build_info, const Toolset& toolset)
{
const wchar_t* tonull = L" >nul";
if (g_debugging)
@@ -34,8 +81,10 @@ namespace vcpkg::Build
tonull = L"";
}
- return Strings::wformat(
- LR"("%s" %s %s 2>&1)", toolset.vcvarsall.native(), Strings::to_utf16(triplet.architecture()), tonull);
+ auto arch = to_vcvarsall_toolchain(pre_build_info.target_architecture);
+ auto target = to_vcvarsall_target(pre_build_info.cmake_system_name);
+
+ return Strings::wformat(LR"("%s" %s %s %s 2>&1)", toolset.vcvarsall.native(), arch, target, tonull);
}
static void create_binary_control_file(const VcpkgPaths& paths,
@@ -82,7 +131,8 @@ namespace vcpkg::Build
const fs::path ports_cmake_script_path = paths.ports_cmake;
const Toolset& toolset = paths.get_toolset();
- const auto cmd_set_environment = make_build_env_cmd(triplet, toolset);
+ auto pre_build_info = PreBuildInfo::from_triplet_file(paths, triplet);
+ const auto cmd_set_environment = make_build_env_cmd(pre_build_info, toolset);
const std::wstring cmd_launch_cmake =
make_cmake_cmd(cmake_exe_path,
@@ -113,7 +163,7 @@ namespace vcpkg::Build
}
auto build_info = read_build_info(paths.get_filesystem(), paths.build_info_file_path(spec));
- const size_t error_count = PostBuildLint::perform_all_checks(spec, paths, build_info);
+ const size_t error_count = PostBuildLint::perform_all_checks(spec, paths, pre_build_info, build_info);
if (error_count != 0)
{
@@ -214,4 +264,74 @@ namespace vcpkg::Build
Checks::check_exit(VCPKG_LINE_INFO, pghs.get() != nullptr, "Invalid BUILD_INFO file for package");
return BuildInfo::create(*pghs.get());
}
+
+ PreBuildInfo PreBuildInfo::from_triplet_file(const VcpkgPaths& paths, const Triplet& triplet)
+ {
+ static constexpr CStringView FLAG_GUID = "c35112b6-d1ba-415b-aa5d-81de856ef8eb";
+
+ const fs::path& cmake_exe_path = paths.get_cmake_exe();
+ const fs::path ports_cmake_script_path = paths.scripts / "get_triplet_environment.cmake";
+ const fs::path triplet_file_path = paths.triplets / (triplet.canonical_name() + ".cmake");
+
+ const std::wstring cmd_launch_cmake = make_cmake_cmd(cmake_exe_path,
+ ports_cmake_script_path,
+ {
+ {L"CMAKE_TRIPLET_FILE", triplet_file_path},
+ });
+
+ const std::wstring command = Strings::wformat(LR"(%s)", cmd_launch_cmake);
+ auto ec_data = System::cmd_execute_and_capture_output(command);
+ Checks::check_exit(VCPKG_LINE_INFO, ec_data.exit_code == 0);
+
+ const std::vector<std::string> lines = Strings::split(ec_data.output, "\n");
+
+ PreBuildInfo pre_build_info;
+
+ auto e = lines.cend();
+ auto cur = std::find(lines.cbegin(), e, FLAG_GUID);
+ if (cur != e) ++cur;
+
+ for (; cur != e; ++cur)
+ {
+ auto&& line = *cur;
+
+ const std::vector<std::string> s = Strings::split(line, "=");
+ Checks::check_exit(VCPKG_LINE_INFO,
+ s.size() == 1 || s.size() == 2,
+ "Expected format is [VARIABLE_NAME=VARIABLE_VALUE], but was [%s]",
+ line);
+
+ const bool variable_with_no_value = s.size() == 1;
+ const std::string variable_name = s.at(0);
+ const std::string variable_value = variable_with_no_value ? "" : s.at(1);
+
+ if (variable_name == "VCPKG_TARGET_ARCHITECTURE")
+ {
+ pre_build_info.target_architecture = variable_value;
+ continue;
+ }
+
+ if (variable_name == "VCPKG_CMAKE_SYSTEM_NAME")
+ {
+ pre_build_info.cmake_system_name = variable_value;
+ continue;
+ }
+
+ if (variable_name == "VCPKG_CMAKE_SYSTEM_VERSION")
+ {
+ pre_build_info.cmake_system_version = variable_value;
+ continue;
+ }
+
+ if (variable_name == "VCPKG_PLATFORM_TOOLSET")
+ {
+ pre_build_info.platform_toolset = variable_value;
+ continue;
+ }
+
+ Checks::exit_with_message(VCPKG_LINE_INFO, "Unknown variable name %s", line);
+ }
+
+ return pre_build_info;
+ }
}

MODIFY
src/vcpkg_System.cpp
src/vcpkg_System.cpp
@@ -23,6 +23,25 @@ namespace vcpkg::System
return fs::path(buf, buf + bytes);
}
+ Optional<CPUArchitecture> to_cpu_architecture(CStringView arch)
+ {
+ if (_stricmp(arch, "x86") == 0) return CPUArchitecture::X86;
+ if (_stricmp(arch, "x64") == 0) return CPUArchitecture::X64;
+ if (_stricmp(arch, "amd64") == 0) return CPUArchitecture::X64;
+ if (_stricmp(arch, "arm") == 0) return CPUArchitecture::ARM;
+ if (_stricmp(arch, "arm64") == 0) return CPUArchitecture::ARM64;
+ return nullopt;
+ }
+
+ CPUArchitecture get_host_processor()
+ {
+ auto w6432 = get_environment_variable(L"PROCESSOR_ARCHITEW6432");
+ if (auto p = w6432.get()) return to_cpu_architecture(Strings::to_utf8(*p)).value_or_exit(VCPKG_LINE_INFO);
+
+ auto procarch = get_environment_variable(L"PROCESSOR_ARCHITECTURE").value_or_exit(VCPKG_LINE_INFO);
+ return to_cpu_architecture(Strings::to_utf8(procarch)).value_or_exit(VCPKG_LINE_INFO);
+ }
+
int cmd_execute_clean(const CWStringView cmd_line)
{
static const std::wstring system_root = get_environment_variable(L"SystemRoot").value_or_exit(VCPKG_LINE_INFO);

Rework vcpkg's triplet environment reading

The triplet is "run" and vcpkg observes the environment.
Previously, the environment was deduced by the triplet's name