From be87af07b35841a5e3868d7ff759f30220caac9a Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Fri, 2 May 2025 02:31:00 +0200 Subject: [PATCH] cmake: populate CMAKE_FIND_ROOT_PATH when cross-compiling for a different architecture on Linux --- CMakeLists.txt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9281226135..df202dd45d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -416,6 +416,30 @@ set(COMMONLIST ${COMMONLIST} ${TINYFORMATLIST}) # Libraries ################################################################################ +# Find libraries when cross-compiling for a different architecture on Linux systems. +# We purposedly do not use the `LINUX` variable for two reasons: +# - We're not testing for the target but for the system running the compiler, +# - It should exclude Android. +# Conveniently it also finds the MinGW root path when cross-compiling for Windows on Linux. +if (CMAKE_SYSTEM_NAME MATCHES "Linux") + # This code setting MACHINE_TRIPLE may be moved to DaemonCompiler. + execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -dumpmachine + OUTPUT_VARIABLE MACHINE_TRIPLE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + # If the target architecture has a cross-compiler subfolder, use it. + if (MACHINE_TRIPLE) + set(MACHINE_ROOT_PATH "/usr/${MACHINE_TRIPLE}") + + if (NOT IS_DIRECTORY "${MACHINE_ROOT_PATH}") + unset(MACHINE_ROOT_PATH) + endif() + endif() +endif() + +list(APPEND CMAKE_FIND_ROOT_PATH ${MACHINE_ROOT_PATH}) + # Import external dependencies if (DEPS_DIR) # Warn on old version of deps @@ -484,6 +508,7 @@ if (DEPS_DIR) set(CMAKE_INCLUDE_PATH ${DEPS_DIR} ${DEPS_DIR}/include ${CMAKE_INCLUDE_PATH}) set(CMAKE_FRAMEWORK_PATH ${DEPS_DIR} ${CMAKE_FRAMEWORK_PATH}) set(CMAKE_PREFIX_PATH ${DEPS_DIR} ${CMAKE_PREFIX_PATH}) + if (DAEMON_PARENT_SCOPE_DIR) # Also set parent scope so the top level CMakeLists can find precompiled deps set(CMAKE_FIND_ROOT_PATH ${DEPS_DIR} ${CMAKE_FIND_ROOT_PATH} PARENT_SCOPE)