From c96e8cb378a67ad5389a6cbe46cddc1df62366bf Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Fri, 30 Oct 2026 19:27:53 +0000 Subject: [PATCH] [vcpkg.cmake] Add VCPKG_PREFER_HOST_TOOLS, on by default VCPKG_USE_HOST_TOOLS puts only the host triplet's tools in CMAKE_PROGRAM_PATH. That breaks build tools that come with a target port and are not available as host tools, such as wxrc from wxwidgets. Add VCPKG_PREFER_HOST_TOOLS, which searches the host triplet's tools first and then the target triplet's. It is on by default when VCPKG_HOST_TRIPLET is set, in place of VCPKG_USE_HOST_TOOLS, which is now off by default and still takes precedence when set. Unless it is already set, CMAKE_FIND_ROOT_PATH_MODE_PROGRAM becomes NEVER for cross builds, so find_program() does not find a target tool first because it is under the find root. Tools found for the target triplet, such as wxrc, may not run on the build machine when cross compiling. Assisted-By: Claude Opus 5.5 Signed-off-by: Rafael Kitover --- scripts/buildsystems/vcpkg.cmake | 48 ++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/scripts/buildsystems/vcpkg.cmake b/scripts/buildsystems/vcpkg.cmake index 65ec8f13bfdfcd..dc750326b5c59d 100644 --- a/scripts/buildsystems/vcpkg.cmake +++ b/scripts/buildsystems/vcpkg.cmake @@ -578,27 +578,45 @@ set(VCPKG_CAN_USE_HOST_TOOLS OFF) if(DEFINED VCPKG_HOST_TRIPLET AND NOT VCPKG_HOST_TRIPLET STREQUAL "") set(VCPKG_CAN_USE_HOST_TOOLS ON) endif() -cmake_dependent_option(VCPKG_USE_HOST_TOOLS "Setup CMAKE_PROGRAM_PATH to use host tools" ON "VCPKG_CAN_USE_HOST_TOOLS" OFF) +cmake_dependent_option(VCPKG_USE_HOST_TOOLS "Setup CMAKE_PROGRAM_PATH to use only host tools" OFF "VCPKG_CAN_USE_HOST_TOOLS" OFF) +# Tools such as wxrc from wxwidgets are only installed for the target triplet, +# so fall back to the target tools after the host tools. +cmake_dependent_option(VCPKG_PREFER_HOST_TOOLS "Setup CMAKE_PROGRAM_PATH to use host tools, then target tools" ON "VCPKG_CAN_USE_HOST_TOOLS" OFF) unset(VCPKG_CAN_USE_HOST_TOOLS) if(VCPKG_SETUP_CMAKE_PROGRAM_PATH) - set(tools_base_path "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/tools") if(VCPKG_USE_HOST_TOOLS) - set(tools_base_path "${VCPKG_INSTALLED_DIR}/${VCPKG_HOST_TRIPLET}/tools") + set(Z_VCPKG_TOOLS_TRIPLETS "${VCPKG_HOST_TRIPLET}") + elseif(VCPKG_PREFER_HOST_TOOLS) + set(Z_VCPKG_TOOLS_TRIPLETS "${VCPKG_HOST_TRIPLET}" "${VCPKG_TARGET_TRIPLET}") + list(REMOVE_DUPLICATES Z_VCPKG_TOOLS_TRIPLETS) + # The target triplet's tree is in CMAKE_FIND_ROOT_PATH, and in the default BOTH + # mode find_program() searches inside it first, which would find the target + # copy of a tool installed for both triplets. + if(NOT VCPKG_HOST_TRIPLET STREQUAL VCPKG_TARGET_TRIPLET AND NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM) + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + endif() + else() + set(Z_VCPKG_TOOLS_TRIPLETS "${VCPKG_TARGET_TRIPLET}") endif() - list(APPEND CMAKE_PROGRAM_PATH "${tools_base_path}") - file(GLOB Z_VCPKG_TOOLS_DIRS LIST_DIRECTORIES true "${tools_base_path}/*") - file(GLOB Z_VCPKG_TOOLS_FILES LIST_DIRECTORIES false "${tools_base_path}/*") - file(GLOB Z_VCPKG_TOOLS_DIRS_BIN LIST_DIRECTORIES true "${tools_base_path}/*/bin") - file(GLOB Z_VCPKG_TOOLS_FILES_BIN LIST_DIRECTORIES false "${tools_base_path}/*/bin") - list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_FILES} "") # need at least one item for REMOVE_ITEM if CMake <= 3.19 - list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS_BIN ${Z_VCPKG_TOOLS_FILES_BIN} "") - string(REPLACE "/bin" "" Z_VCPKG_TOOLS_DIRS_TO_REMOVE "${Z_VCPKG_TOOLS_DIRS_BIN}") - list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_DIRS_TO_REMOVE} "") - list(APPEND Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_DIRS_BIN}) - foreach(Z_VCPKG_TOOLS_DIR IN LISTS Z_VCPKG_TOOLS_DIRS) - list(APPEND CMAKE_PROGRAM_PATH "${Z_VCPKG_TOOLS_DIR}") + foreach(Z_VCPKG_TOOLS_TRIPLET IN LISTS Z_VCPKG_TOOLS_TRIPLETS) + set(tools_base_path "${VCPKG_INSTALLED_DIR}/${Z_VCPKG_TOOLS_TRIPLET}/tools") + list(APPEND CMAKE_PROGRAM_PATH "${tools_base_path}") + file(GLOB Z_VCPKG_TOOLS_DIRS LIST_DIRECTORIES true "${tools_base_path}/*") + file(GLOB Z_VCPKG_TOOLS_FILES LIST_DIRECTORIES false "${tools_base_path}/*") + file(GLOB Z_VCPKG_TOOLS_DIRS_BIN LIST_DIRECTORIES true "${tools_base_path}/*/bin") + file(GLOB Z_VCPKG_TOOLS_FILES_BIN LIST_DIRECTORIES false "${tools_base_path}/*/bin") + list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_FILES} "") # need at least one item for REMOVE_ITEM if CMake <= 3.19 + list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS_BIN ${Z_VCPKG_TOOLS_FILES_BIN} "") + string(REPLACE "/bin" "" Z_VCPKG_TOOLS_DIRS_TO_REMOVE "${Z_VCPKG_TOOLS_DIRS_BIN}") + list(REMOVE_ITEM Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_DIRS_TO_REMOVE} "") + list(APPEND Z_VCPKG_TOOLS_DIRS ${Z_VCPKG_TOOLS_DIRS_BIN}) + foreach(Z_VCPKG_TOOLS_DIR IN LISTS Z_VCPKG_TOOLS_DIRS) + list(APPEND CMAKE_PROGRAM_PATH "${Z_VCPKG_TOOLS_DIR}") + endforeach() endforeach() + unset(Z_VCPKG_TOOLS_TRIPLET) + unset(Z_VCPKG_TOOLS_TRIPLETS) unset(Z_VCPKG_TOOLS_DIR) unset(Z_VCPKG_TOOLS_DIRS) unset(Z_VCPKG_TOOLS_FILES)