# ----------------------------------------------------------------------------------
# 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/ <path_to_source>
#
# 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()
message(STATUS "Using TARGET_SYSROOT: ${TARGET_SYSROOT}")

set(TARGET_TRIPLE x86_64-linux-gnu) # Standard Debian/Ubuntu triple

# ----------------------------------------------------------------------------------
# SYSROOT and COMPILER CONFIGURATION
# ----------------------------------------------------------------------------------

set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSROOT ${TARGET_SYSROOT})
message(STATUS "Using CMAKE_SYSROOT: ${CMAKE_SYSROOT}")

# The CMAKE_FIND_ROOT_PATH tells CMake where to look for programs, libraries, etc.
set(CMAKE_FIND_ROOT_PATH ${CMAKE_SYSROOT})
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)

# 1. Architecture and Platform Identification
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86_64)

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
# ----------------------------------------------------------------------------------

