diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c9ce73bdc..8a1349535 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -103,6 +103,7 @@ jobs: --exclude='*.o' \ --exclude='*.a' \ --exclude='*.so' \ + --exclude='*.so.*' \ -C "$GITHUB_WORKSPACE" . - name: Upload analysis input diff --git a/.github/workflows/build_bin.yml b/.github/workflows/build_bin.yml index 7542ff705..6eef3c7ed 100644 --- a/.github/workflows/build_bin.yml +++ b/.github/workflows/build_bin.yml @@ -269,7 +269,31 @@ jobs: cmake \ --build build \ --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 run: | PYTHON_ROOT="$(python -c 'import sys; print(sys.base_prefix)')" diff --git a/CMakeLists.txt b/CMakeLists.txt index 243ac8b79..0f2184311 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -683,6 +683,41 @@ if (WIN32) ) 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=$ + -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) # 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 diff --git a/CMake_modules/bundle_runtime_libs.cmake b/CMake_modules/bundle_runtime_libs.cmake new file mode 100644 index 000000000..a0f71543e --- /dev/null +++ b/CMake_modules/bundle_runtime_libs.cmake @@ -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= -DSEARCH_DIRS= [-DCMAKE_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}")