From e4e700c362dec9ae30ff144b8eff733476108d33 Mon Sep 17 00:00:00 2001 From: Latent Prion Date: Fri, 17 Oct 2025 12:43:06 -0400 Subject: [PATCH] Changes to CMake toolchain files We still haven't successfully xcompiled, but we're working toward it. --- cmake/aarch64-linux-gnu.cmake | 2 +- cmake/x86_64-linux-gnu.cmake | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cmake/x86_64-linux-gnu.cmake diff --git a/cmake/aarch64-linux-gnu.cmake b/cmake/aarch64-linux-gnu.cmake index 6767693..5a3ab3d 100644 --- a/cmake/aarch64-linux-gnu.cmake +++ b/cmake/aarch64-linux-gnu.cmake @@ -31,7 +31,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # Search for libraries and headers in the target directories set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) # Set pkg-config to use the cross-compiled libraries set(ENV{PKG_CONFIG_PATH} "/usr/aarch64-linux-gnu/lib/pkgconfig:/usr/lib/aarch64-linux-gnu/pkgconfig") diff --git a/cmake/x86_64-linux-gnu.cmake b/cmake/x86_64-linux-gnu.cmake new file mode 100644 index 0000000..ddf2354 --- /dev/null +++ b/cmake/x86_64-linux-gnu.cmake @@ -0,0 +1,74 @@ +# ---------------------------------------------------------------------------------- +# MANDATORY USER VARIABLE +# ---------------------------------------------------------------------------------- +# IMPORTANT: This variable MUST be set when running CMake to specify where the +# laptop's sysroot (the root directory of the mounted laptop filesystem) is located. +# +# Usage example: cmake -DCMAKE_TOOLCHAIN_FILE=laptop_x86_sysroot.cmake +# -DTARGET_SYSROOT=/mnt/laptop_sysroot/ +# +# If the variable is not defined, we fall back to a common system root path for safety. +if(NOT DEFINED TARGET_SYSROOT) + set(TARGET_SYSROOT "/usr/lib/x86_64-linux-gnu") + message(STATUS "TARGET_SYSROOT not explicitly defined. Defaulting to ${TARGET_SYSROOT}") +endif() + +# ---------------------------------------------------------------------------------- +# SYSROOT and COMPILER CONFIGURATION +# ---------------------------------------------------------------------------------- + +set(CMAKE_CROSSCOMPILING TRUE) +# 1. Architecture and Platform Identification +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_PROCESSOR x86_64) +set(TARGET_TRIPLE x86_64-linux-gnu) # Standard Debian/Ubuntu triple + +# 2. Set the CMAKE_SYSROOT using the user-defined variable +set(CMAKE_SYSROOT ${TARGET_SYSROOT}) +message(STATUS "Using CMAKE_SYSROOT: ${CMAKE_SYSROOT}") + +# 3. Specify the Cross Compilers +# These binaries must be installed on your RPi: +# sudo apt install g++-x86-64-linux-gnu gcc-x86-64-linux-gnu +set(CMAKE_C_COMPILER ${TARGET_TRIPLE}-gcc) +set(CMAKE_CXX_COMPILER ${TARGET_TRIPLE}-g++) + +# ---------------------------------------------------------------------------------- +# PKG-CONFIG CONFIGURATION (CRUCIAL FOR CROSS-COMPILING) +# ---------------------------------------------------------------------------------- + +# 1. Define the search path for .pc files, relative to the sysroot. +# This ensures we look in the target's standard pkgconfig locations. +set(PKG_CONFIG_SEARCH_PATHS + "${CMAKE_SYSROOT}/usr/lib/${TARGET_TRIPLE}/pkgconfig" # Primary location on Debian/Ubuntu + "${CMAKE_SYSROOT}/usr/share/pkgconfig" # Secondary shared location + "${CMAKE_SYSROOT}/usr/lib/pkgconfig" # Another common location +) + +# Join the paths using the system's path separator (colon on Linux) +string(REPLACE ";" ":" PKG_CONFIG_LIBDIR_STRING "${PKG_CONFIG_SEARCH_PATHS}") + +# Set the environment variable PKG_CONFIG_LIBDIR +# This tells pkg-config exactly where to find the x86_64 .pc files. +# 2. Set the sysroot directory for pkg-config +# This tells pkg-config to prepend CMAKE_SYSROOT to any paths it finds in the .pc files. +set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT}) +set(ENV{PKG_CONFIG_LIBDIR} ${PKG_CONFIG_LIBDIR_STRING}) +set(ENV{PKG_CONFIG_PATH} "") + +message(STATUS "PKG_CONFIG_SYSROOT_DIR set to: ${CMAKE_SYSROOT}") +message(STATUS "PKG_CONFIG_LIBDIR set to: ${PKG_CONFIG_LIBDIR_STRING}") + +# ---------------------------------------------------------------------------------- +# CMAkE FIND BEHAVIOR +# ---------------------------------------------------------------------------------- + +# The CMAKE_FIND_ROOT_PATH tells CMake where to look for programs, libraries, etc. +set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT}) + +# Restrict search to the sysroot, not in the RPi's native locations, +# but allow programs (like pkg-config itself) to be found on the RPi. +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)