18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00

build(linux): load the shared libraries from the game folder and ship them in the build (#320)

This commit is contained in:
2026-11-11 03:29:36 +00:00
committed by GitHub
parent 226155d430
commit 5764e942a7
4 changed files with 136 additions and 1 deletions

View File

@@ -103,6 +103,7 @@ jobs:
--exclude='*.o' \ --exclude='*.o' \
--exclude='*.a' \ --exclude='*.a' \
--exclude='*.so' \ --exclude='*.so' \
--exclude='*.so.*' \
-C "$GITHUB_WORKSPACE" . -C "$GITHUB_WORKSPACE" .
- name: Upload analysis input - name: Upload analysis input

View File

@@ -269,7 +269,31 @@ jobs:
cmake \ cmake \
--build build \ --build build \
--parallel "$(nproc)" --parallel "$(nproc)"
# the build copies the shared libraries next to the game (CMake_modules/bundle_runtime_libs.cmake) and the
# game looks for them there first ($ORIGIN): each one it loads from outside build/bin has to be one of the system
- name: Check the bundled libraries
run: |
GAME="$(find build/bin -maxdepth 1 -type f -name 'eu07*' -perm -u+x | head -n 1)"
test -n "$GAME"
echo "Game: $GAME"
readelf -d "$GAME" | grep -E 'R(UN)?PATH'
readelf -d "$GAME" | grep -q 'Library rpath: \[\$ORIGIN\]'
cat build/bin/bundled_libs.txt
ldd "$GAME"
if ldd "$GAME" | grep -q 'not found'; then
echo "::error::the game needs a library that is neither bundled nor in the system"
exit 1
fi
while read -r name; do
[ -n "$name" ] || continue
loaded="$(ldd "$GAME" | awk -v n="$name" '$1 == n { print $3 }')"
if [ -z "$loaded" ] || [ "$(realpath "$loaded")" != "$(realpath "build/bin/$name")" ]; then
echo "::error::$name is bundled, but the game loads it from '$loaded'"
exit 1
fi
done < build/bin/bundled_libs.txt
- name: Prepare Linux Python environment - name: Prepare Linux Python environment
run: | run: |
PYTHON_ROOT="$(python -c 'import sys; print(sys.base_prefix)')" PYTHON_ROOT="$(python -c 'import sys; print(sys.base_prefix)')"

View File

@@ -683,6 +683,41 @@ if (WIN32)
) )
endif () endif ()
# Linux: the game loads its shared libraries from its own folder first ($ORIGIN), and the build copies them there,
# so the folder runs on a system that doesn't have them, or has other versions. the RPATH is a DT_RPATH rather
# than a DT_RUNPATH, because only that one is also used for the libraries the copied ones load (libsndfile's codecs)
if (UNIX AND NOT APPLE)
option(BUNDLE_RUNTIME_LIBS "Linux: copy the shared libraries the game needs next to it and load them from there" ON)
endif()
if (UNIX AND NOT APPLE AND BUNDLE_RUNTIME_LIBS)
set_target_properties(${PROJECT_NAME} PROPERTIES
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH "$ORIGIN")
target_link_options(${PROJECT_NAME} PRIVATE "LINKER:--disable-new-dtags")
# the RPATH no longer leads to libraries outside the system folders (Python from setup-python, an OpenAL Soft
# in /usr/local), so their folders are given to the script as well
set(EU07_BUNDLE_SEARCH_DIRS "")
foreach(library IN LISTS Python3_LIBRARIES OPENAL_LIBRARY LUAJIT_LIBRARIES libserialport_LIBRARY)
if (IS_ABSOLUTE "${library}")
get_filename_component(library_dir "${library}" DIRECTORY)
list(APPEND EU07_BUNDLE_SEARCH_DIRS "${library_dir}")
endif()
endforeach()
list(APPEND EU07_BUNDLE_SEARCH_DIRS ${Python3_LIBRARY_DIRS})
list(REMOVE_DUPLICATES EU07_BUNDLE_SEARCH_DIRS)
string(REPLACE ";" "|" EU07_BUNDLE_SEARCH_DIRS "${EU07_BUNDLE_SEARCH_DIRS}")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DGAME=$<TARGET_FILE:${PROJECT_NAME}>
-DSEARCH_DIRS=${EU07_BUNDLE_SEARCH_DIRS}
-DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
-P "${CMAKE_CURRENT_SOURCE_DIR}/CMake_modules/bundle_runtime_libs.cmake"
COMMENT "Copying the shared libraries next to the game"
VERBATIM)
endif()
if(WIN32 AND WITH_PYTHON) if(WIN32 AND WITH_PYTHON)
# the Python runtime next to the game, copied when it isn't there yet or the Python install changed, rather # the Python runtime next to the game, copied when it isn't there yet or the Python install changed, rather
# than after every link of the game: the library alone is thousands of files. the folder is the game's # than after every link of the game: the library alone is thousands of files. the folder is the game's

View File

@@ -0,0 +1,75 @@
# copies the shared libraries the game needs next to it, where its RPATH ($ORIGIN) finds them first. Linux only:
# cmake -DGAME=<game binary> -DSEARCH_DIRS=<dir|dir|...> [-DCMAKE_OBJDUMP=<objdump>] -P bundle_runtime_libs.cmake
#
# the libraries of the system itself stay out: the C library and the loader, the GPU driver stack (libGL and its
# kin come with the driver), X11 / Wayland, the sound servers, libstdc++ (the GPU drivers bring their own need of a
# newer one) and a few more every distribution has. what a library left out depends on isn't bundled either
get_filename_component(game_folder "${GAME}" DIRECTORY)
set(manifest "${game_folder}/bundled_libs.txt")
# the libraries copied by the previous build go first: the game's RPATH would find them in its folder, and then
# a library updated in the system would never be copied again
if(EXISTS "${manifest}")
file(STRINGS "${manifest}" previous)
foreach(name IN LISTS previous)
if(NOT name STREQUAL "")
file(REMOVE "${game_folder}/${name}")
endif()
endforeach()
file(REMOVE "${manifest}")
endif()
string(REPLACE "|" ";" search_dirs "${SEARCH_DIRS}")
if(DEFINED ENV{LD_LIBRARY_PATH})
string(REPLACE ":" ";" ld_library_path "$ENV{LD_LIBRARY_PATH}")
list(APPEND search_dirs ${ld_library_path})
endif()
list(REMOVE_ITEM search_dirs "")
list(REMOVE_DUPLICATES search_dirs)
set(system_libraries
"^ld-linux"
"^linux-vdso"
"^lib(c|m|dl|rt|pthread|resolv|util|mvec|anl|nsl|crypt|BrokenLocale|thread_db)\\.so"
"^libstdc\\+\\+\\.so"
"^libgcc_s\\.so"
"^lib(GL|GLX|EGL|OpenGL|GLdispatch|GLESv1_CM|GLESv2|GLU|gbm|drm|vulkan)\\.so"
"^libX[A-Za-z0-9_-]*\\.so"
"^libxcb"
"^libxkbcommon"
"^libwayland-"
"^libdecor"
"^lib(asound|pulse|pulse-simple|jack|pipewire-[0-9.]+)\\.so"
"^lib(udev|systemd|dbus-1)\\.so"
"^libz\\.so"
"^lib(fontconfig|freetype|harfbuzz|expat|uuid)\\.so")
file(GET_RUNTIME_DEPENDENCIES
EXECUTABLES "${GAME}"
RESOLVED_DEPENDENCIES_VAR resolved
UNRESOLVED_DEPENDENCIES_VAR unresolved
DIRECTORIES ${search_dirs}
PRE_EXCLUDE_REGEXES ${system_libraries})
if(unresolved)
list(JOIN unresolved ", " unresolved_text)
message(WARNING "Bundled libraries: not found, so not copied next to the game: ${unresolved_text}")
endif()
# each library is copied under the name the game asks for (libpng16.so.16), as a file rather than the chain of
# symlinks it is in the system: the build artifacts don't keep symlinks
set(names "")
foreach(library IN LISTS resolved)
get_filename_component(name "${library}" NAME)
file(REAL_PATH "${library}" source)
file(COPY_FILE "${source}" "${game_folder}/${name}")
list(APPEND names "${name}")
endforeach()
list(SORT names)
list(JOIN names "\n" manifest_text)
file(WRITE "${manifest}" "${manifest_text}\n")
list(LENGTH names count)
list(JOIN names " " names_text)
message("Bundled libraries (${count}): ${names_text}")