Open ADKaster opened 6 days ago
Note that I can work around this by doing something like this:
/path/to/my/local/vcpkg-triplets/release-triplets/x64-linux.cmake
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_BUILD_TYPE release)
set(VCPKG_LIBRARY_LINKAGE static)
# Hackery using a private vcpkg variable to get to the root of the invoking project's CMake binary dir when using manifest mode
include(${_VCPKG_INSTALLED_DIR}/../build-vcpkg-variables.cmake OPTIONAL)
And then generating build-vcpkg-variables.cmake
like so:
CMakeLists.txt
cmake_minimum_required(VERSION 3.25)
# Pass additional information to vcpkg toolchain files if we are using vcpkg.
if (CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg.cmake$")
set(CMAKE_PROJECT_MyProject_INCLUDE_BEFORE "vcpkg/generate_vcpkg_toolchain_variables.cmake")
endif()
project(MyProject LANGUAGES C CXX)
vcpkg/generate_vcpkg_toolchain_variables.cmake
set(EXTRA_VCPKG_VARIABLES "")
if (NOT "${CMAKE_C_COMPILER}" STREQUAL "")
string(APPEND EXTRA_VCPKG_VARIABLES "set(ENV{CC} ${CMAKE_C_COMPILER})\n")
endif()
if (NOT "${CMAKE_CXX_COMPILER}" STREQUAL "")
string(APPEND EXTRA_VCPKG_VARIABLES "set(ENV{CXX} ${CMAKE_CXX_COMPILER})\n")
endif()
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/build-vcpkg-variables.cmake" "${EXTRA_VCPKG_VARIABLES}")
But this approach feels verbose and possibly fragile? For something that I would expect to "just work" (and does seem to just work for MSVC toolsets).
For additional context, this seems to be a thing limited to Unix Makefiles and Ninja Generators (and other bespoke generators, I suppose).
To quote Brad King from a recent CMake Issue https://gitlab.kitware.com/cmake/cmake/-/issues/26316#note_1571027
The VS and Xcode generators do not support specifying the path to the compiler directly for any language. The situation is the same for CMAKE_C_COMPILER and CMAKE_CXX_COMPILER. For those generators one is supposed to set CMAKE_GENERATOR_TOOLSET to control compiler selection.
So it seems like for VS and Xcode, the toolset should specify all the information needed to select a specific compiler. But for other generators, users expect that CMAKE_C_COMPILER and CMAKE_CXX_COMPILER will set the name/path of the desired compiler.
Describe the bug vcpkg-tool only considers the target triplet when detecting what compiler to use to build vcpkg dependencies for CMake projects.
Environment
To Reproduce Steps to reproduce the behavior, on Ubuntu 22.04:
The compiler name/path is not used when detecting what compiler to use when building vcpkg ports. Only the default search mechanisms for CMake are involved. That means, /usr/bin/cc and $CC environment variable
Expected behavior
vcpkg will build ports with the compiler specified to be used via -DCMAKE_C_COMPILER, -DCMAKE_CXX_COMPILER.
This behavior should be consistent with how chain-loading another toolchain file works.
Failure logs
Terminal output shown inside details
``` andrew@Andrew-Workstation:~/dev/vcpkg-cmake-cxx-compiler$ cat vcpkg.json { "dependencies": [ "fmt" ] } andrew@Andrew-Workstation:~/dev/vcpkg-cmake-cxx-compiler$ cat CMakeLists.txt cmake_minimum_required(VERSION 3.22) project(Example LANGUAGES CXX) find_package(fmt CONFIG REQUIRED) add_executable(example main.cpp) target_link_libraries(example PRIVATE fmt::fmt) target_compile_features(example PUBLIC cxx_std_17) andrew@Andrew-Workstation:~/dev/vcpkg-cmake-cxx-compiler$ cat main.cpp #includeAs seen in the terminal output above, vcpkg finds /usr/bin/c++, which is g++-11 in my system. However, I explicitly asked for clang++-18, which is properly detected by the CMake output for configuring my project proper.
vcpkg:
my project:
Additional context
The code involved is here: https://github.com/microsoft/vcpkg-tool/blob/2af426b284d7a321628471c6baef46452385534d/src/vcpkg/commands.build.cpp#L683-L699
The code seems to use the
toolset
argument, which I would expect corresponds to the-T
flag used for MSVC compiles. However, on Linux and macOS, the toolset flag is not valid and not used for Ninja, Unix Makefile and Xcode generators, and most users will configure with -DCMAKE_C_COMPILER and -DCMAKE_CXX_COMPILER.