From 8e5d28de3cbbe0cf70de202241d8ff0a12e22912 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 17 Jul 2024 11:17:48 -0600 Subject: [PATCH] CMake: Force vcpkg to use CMAKE_CXX_COMPILER as specified to CMake Override the vcpkg/scripts/detect_compiler behavior of always pulling $CC and $CXX at the time that vcpkg install is determined to need called by forcing $ENV{CXX} and $ENV{CC} to our CMake-determined compiler. This prevents strange behavior such as running the following: ./Meta/ladybird.sh run make changes... ninja -C Build/ladybird Where the second build step would be run without CC or CXX set in the environment, causing a total cache miss from vcpkg and a full rebuild. It also helps prevent full rebuilds when an IDE passes a slightly different compiler to the build step than ladybird.sh. --- CMakeLists.txt | 5 +++++ Meta/CMake/vcpkg/base-triplets/arm64-linux.cmake | 2 +- Meta/CMake/vcpkg/base-triplets/arm64-osx.cmake | 2 +- Meta/CMake/vcpkg/base-triplets/base.cmake | 2 ++ Meta/CMake/vcpkg/base-triplets/x64-linux.cmake | 2 +- Meta/CMake/vcpkg/base-triplets/x64-osx.cmake | 2 +- .../vcpkg/generate_vcpkg_toolchain_variables.cmake | 11 +++++++++++ 7 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 Meta/CMake/vcpkg/base-triplets/base.cmake create mode 100644 Meta/CMake/vcpkg/generate_vcpkg_toolchain_variables.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 60f01ea7f48..a9dbc967ef3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,11 @@ if (VCPKG_TARGET_ANDROID) include("Ladybird/Android/vcpkg_android.cmake") endif() +# Pass additional information to vcpkg toolchain files if we are using vcpkg. +if (CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg.cmake$") + set(CMAKE_PROJECT_ladybird_INCLUDE_BEFORE "Meta/CMake/vcpkg/generate_vcpkg_toolchain_variables.cmake") +endif() + project(ladybird VERSION 0.1.0 LANGUAGES C CXX diff --git a/Meta/CMake/vcpkg/base-triplets/arm64-linux.cmake b/Meta/CMake/vcpkg/base-triplets/arm64-linux.cmake index 8c993d71e58..a5e57a1ec7d 100644 --- a/Meta/CMake/vcpkg/base-triplets/arm64-linux.cmake +++ b/Meta/CMake/vcpkg/base-triplets/arm64-linux.cmake @@ -2,4 +2,4 @@ set(VCPKG_CMAKE_SYSTEM_NAME Linux) set(VCPKG_TARGET_ARCHITECTURE arm64) set(VCPKG_CRT_LINKAGE dynamic) -include(${CMAKE_CURRENT_LIST_DIR}/../user-variables.cmake OPTIONAL) +include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) diff --git a/Meta/CMake/vcpkg/base-triplets/arm64-osx.cmake b/Meta/CMake/vcpkg/base-triplets/arm64-osx.cmake index 9d0bb3e55c6..312c49acc42 100644 --- a/Meta/CMake/vcpkg/base-triplets/arm64-osx.cmake +++ b/Meta/CMake/vcpkg/base-triplets/arm64-osx.cmake @@ -3,4 +3,4 @@ set(VCPKG_TARGET_ARCHITECTURE arm64) set(VCPKG_OSX_ARCHITECTURES arm64) set(VCPKG_CRT_LINKAGE dynamic) -include(${CMAKE_CURRENT_LIST_DIR}/../user-variables.cmake OPTIONAL) +include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) diff --git a/Meta/CMake/vcpkg/base-triplets/base.cmake b/Meta/CMake/vcpkg/base-triplets/base.cmake new file mode 100644 index 00000000000..6e00403cfc9 --- /dev/null +++ b/Meta/CMake/vcpkg/base-triplets/base.cmake @@ -0,0 +1,2 @@ +include(${CMAKE_CURRENT_LIST_DIR}/../user-variables.cmake OPTIONAL) +include(${_VCPKG_INSTALLED_DIR}/../build-vcpkg-variables.cmake OPTIONAL) diff --git a/Meta/CMake/vcpkg/base-triplets/x64-linux.cmake b/Meta/CMake/vcpkg/base-triplets/x64-linux.cmake index 6e161607a2a..2365fba7d98 100644 --- a/Meta/CMake/vcpkg/base-triplets/x64-linux.cmake +++ b/Meta/CMake/vcpkg/base-triplets/x64-linux.cmake @@ -2,4 +2,4 @@ set(VCPKG_CMAKE_SYSTEM_NAME Linux) set(VCPKG_TARGET_ARCHITECTURE x64) set(VCPKG_CRT_LINKAGE dynamic) -include(${CMAKE_CURRENT_LIST_DIR}/../user-variables.cmake OPTIONAL) +include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) diff --git a/Meta/CMake/vcpkg/base-triplets/x64-osx.cmake b/Meta/CMake/vcpkg/base-triplets/x64-osx.cmake index 5e3bd5316d1..ce125bc4ede 100644 --- a/Meta/CMake/vcpkg/base-triplets/x64-osx.cmake +++ b/Meta/CMake/vcpkg/base-triplets/x64-osx.cmake @@ -3,4 +3,4 @@ set(VCPKG_TARGET_ARCHITECTURE x64) set(VCPKG_OSX_ARCHITECTURES x86_64) set(VCPKG_CRT_LINKAGE dynamic) -include(${CMAKE_CURRENT_LIST_DIR}/../user-variables.cmake OPTIONAL) +include(${CMAKE_CURRENT_LIST_DIR}/base.cmake) diff --git a/Meta/CMake/vcpkg/generate_vcpkg_toolchain_variables.cmake b/Meta/CMake/vcpkg/generate_vcpkg_toolchain_variables.cmake new file mode 100644 index 00000000000..a43a4493d69 --- /dev/null +++ b/Meta/CMake/vcpkg/generate_vcpkg_toolchain_variables.cmake @@ -0,0 +1,11 @@ +# The generated file here is read by vcpkg/base-triplets/base.cmake to ensure consistency between the project +# build and the vcpkg build. +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}")