singe/cmake/zig/aarch64-linux-gnu.cmake
2026-09-04 22:36:58 -05:00

93 lines
4.2 KiB
CMake

#
# Singe 3
# Copyright (C) 2006-2026 Scott Duensing <scott@kangaroopunch.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public Licens
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# Raspberry Pi (64-bit Raspberry Pi OS) with zig as the compiler. zig supplies glibc pinned to the
# version below plus the kernel headers; the platform libraries Singe, SDL3 and FFmpeg build against
# (libdrm, ALSA, X11, GBM/EGL/GLES, udev, ...) come from the Debian packages listed in
# piPackages.cmake, downloaded and unpacked into a sysroot under .builddir/toolchains on first use.
# Nothing outside the source tree is touched; dpkg-deb from the host does the unpacking.
include(${CMAKE_CURRENT_LIST_DIR}/zig.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/piPackages.cmake)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
# Raspberry Pi OS bullseye shipped glibc 2.31; a binary built against it runs there and on everything since.
set(SINGE_GLIBC_VERSION 2.31)
get_filename_component(piToolchains ${CMAKE_CURRENT_LIST_DIR}/../../.builddir/toolchains ABSOLUTE)
set(SINGE_SYSROOT ${piToolchains}/sysroot-aarch64-linux-gnu CACHE PATH "Pi platform headers and libraries" FORCE)
set(piDebs ${piToolchains}/debs-aarch64-linux-gnu)
set(piStamp ${SINGE_SYSROOT}/.complete)
# The stamp holds the manifest that built the sysroot; a changed manifest rebuilds it.
string(SHA256 piManifest "${piPackages}")
set(piHave "")
if(EXISTS ${piStamp})
file(READ ${piStamp} piHave)
string(STRIP "${piHave}" piHave)
endif()
if(NOT piHave STREQUAL piManifest)
find_program(SINGE_DPKG_DEB dpkg-deb)
if(NOT SINGE_DPKG_DEB)
message(FATAL_ERROR "dpkg-deb is needed to unpack the Pi sysroot packages (apt-get install dpkg).")
endif()
message(STATUS "Building the Pi sysroot in ${SINGE_SYSROOT}")
file(REMOVE_RECURSE ${SINGE_SYSROOT})
file(MAKE_DIRECTORY ${SINGE_SYSROOT} ${piDebs})
foreach(entry IN LISTS piPackages)
string(REGEX MATCH "^([^|]*)\\|(.*)$" unused "${entry}")
set(url ${CMAKE_MATCH_1})
set(sha ${CMAKE_MATCH_2})
get_filename_component(deb ${url} NAME)
set(debPath ${piDebs}/${deb})
if(NOT EXISTS ${debPath})
message(STATUS "Downloading ${deb}")
file(DOWNLOAD ${url} ${debPath} EXPECTED_HASH SHA256=${sha} STATUS status)
list(GET status 0 code)
if(NOT code EQUAL 0)
file(REMOVE ${debPath})
message(FATAL_ERROR "Download of ${url} failed: ${status}")
endif()
endif()
execute_process(COMMAND ${SINGE_DPKG_DEB} -x ${debPath} ${SINGE_SYSROOT} RESULT_VARIABLE code)
if(NOT code EQUAL 0)
message(FATAL_ERROR "Unpacking ${deb} failed.")
endif()
endforeach()
file(WRITE ${piStamp} "${piManifest}\n")
endif()
# The sysroot's headers come after zig's own; its libraries are linked by name and left unresolved
# at link time (they are only stubs for what the Pi provides at run time).
set(piLibs ${SINGE_SYSROOT}/usr/lib/aarch64-linux-gnu)
singeZigToolchain(aarch64-linux-gnu.${SINGE_GLIBC_VERSION} "-idirafter ${SINGE_SYSROOT}/usr/include/aarch64-linux-gnu -idirafter ${SINGE_SYSROOT}/usr/include -L${piLibs} -L${SINGE_SYSROOT}/lib/aarch64-linux-gnu -Wl,--allow-shlib-undefined")
# find_package and pkg-config look only in the sysroot; programs come from the host.
set(CMAKE_FIND_ROOT_PATH ${SINGE_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)
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu CACHE STRING "" FORCE)
set(CMAKE_C_LIBRARY_ARCHITECTURE aarch64-linux-gnu CACHE STRING "" FORCE)
set(CMAKE_LIBRARY_PATH ${piLibs} CACHE PATH "" FORCE)
set(KANGAROO_OS pi)
set(KANGAROO_ARCH aarch64)