CMake vs vcpkg 三元组选择

CMake vs vcpkg triplet selection

我想从 cmake 脚本中 select 三元组 vcpgk。但是,我遇到了两个问题。

  1. 如果我在project()
  2. 之后设置VCPKG_TARGET_TRIPLET
cmake_minimum_required(VERSION 3.5)

project(xxx CXX)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(STATUS "----------------- x64")
    set(__ARCH "x64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    message(STATUS "----------------- x86")
    set(__ARCH "x86")
else()
    set(__ARCH "xUNKNOWN")
    message(STATUS "----------------- xUNKNOWN")
endif()

if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)

if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()

find_package(Protobuf REQUIRED)

我遇到错误

CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
  1. 如果我在 project() 之前设置 VCPKG_TARGET_TRIPLET,它会按预期工作,但我无法确定它是 x64 还是 x86
cmake_minimum_required(VERSION 3.5)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(STATUS "----------------- x64")
    set(__ARCH "x64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    message(STATUS "----------------- x86")
    set(__ARCH "x86")
else()
    set(__ARCH "xUNKNOWN")
    message(STATUS "----------------- xUNKNOWN")
endif()

if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)

if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()

project(xxx CXX)
find_package(Protobuf REQUIRED)

并获取此打印件

----------------- xUNKNOWN

PS:由于环境问题,我无法使用 -DVCPKG_TARGET_TRIPLET=<>。限制。这必须在配置期间检测到

也许不是最好的解决方案,但最后我通过编译器检查了 arch

if(CMAKE_CXX_COMPILER MATCHES "x64/cl.exe")
    set(__ARCH "x64")
elseif(CMAKE_CXX_COMPILER MATCHES "x86/cl.exe")
    set(__ARCH "x86")
else()
    set(__ARCH "xUNKNOWN")
    message(ERRRO "Can't determine target architecture")
endif()

#now setting triplet
if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
    set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
    message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)

if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()

#and only now setup the project with configured triplet
project(xxx CXX)

对于 Windows MSVC,这对我来说很好用。