Files
Echoion/cmake/BundleRuntimeDependencies.cmake
faumaray bdd69f0aad update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

218 lines
8.1 KiB
CMake

cmake_minimum_required(VERSION 4.0)
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
message(FATAL_ERROR "Echoion runtime dependency bundling currently supports Linux only")
endif()
if(NOT DEFINED BUNDLE_ROOT OR BUNDLE_ROOT STREQUAL "")
message(FATAL_ERROR "BUNDLE_ROOT must name the staged runtime tree")
endif()
if(NOT DEFINED PATCHELF_EXECUTABLE OR PATCHELF_EXECUTABLE STREQUAL "")
find_program(PATCHELF_EXECUTABLE NAMES patchelf REQUIRED)
endif()
if(NOT DEFINED BUNDLE_SEED_FILES OR BUNDLE_SEED_FILES STREQUAL "")
message(FATAL_ERROR
"BUNDLE_SEED_FILES must contain the exact compiler runtime libraries")
endif()
cmake_path(ABSOLUTE_PATH BUNDLE_ROOT NORMALIZE OUTPUT_VARIABLE _bundle_root)
set(_executable "${_bundle_root}/bin/echoiond")
set(_updater "${_bundle_root}/bin/echoion-updater")
set(_core "${_bundle_root}/lib/Echoion.ts")
set(_library_dir "${_bundle_root}/lib")
if(NOT DEFINED BUNDLE_RESOLVER_EXECUTABLES OR
BUNDLE_RESOLVER_EXECUTABLES STREQUAL "")
if(DEFINED BUNDLE_RESOLVER_EXECUTABLE AND
NOT BUNDLE_RESOLVER_EXECUTABLE STREQUAL "")
set(BUNDLE_RESOLVER_EXECUTABLES "${BUNDLE_RESOLVER_EXECUTABLE}")
else()
set(BUNDLE_RESOLVER_EXECUTABLES "${_executable}")
if(EXISTS "${_updater}")
list(APPEND BUNDLE_RESOLVER_EXECUTABLES "${_updater}")
endif()
endif()
endif()
if(NOT DEFINED BUNDLE_RESOLVER_LIBRARIES OR
BUNDLE_RESOLVER_LIBRARIES STREQUAL "")
set(BUNDLE_RESOLVER_LIBRARIES "${_core}")
endif()
if(NOT EXISTS "${_executable}")
message(FATAL_ERROR "Missing staged daemon: ${_executable}")
endif()
if(NOT EXISTS "${_core}")
message(FATAL_ERROR "Missing staged Core library: ${_core}")
endif()
foreach(_resolver_executable IN LISTS BUNDLE_RESOLVER_EXECUTABLES)
if(NOT IS_ABSOLUTE "${_resolver_executable}" OR
NOT EXISTS "${_resolver_executable}" OR
IS_DIRECTORY "${_resolver_executable}")
message(FATAL_ERROR
"Missing absolute dependency resolver executable: ${_resolver_executable}")
endif()
endforeach()
foreach(_resolver_library IN LISTS BUNDLE_RESOLVER_LIBRARIES)
if(NOT IS_ABSOLUTE "${_resolver_library}" OR
NOT EXISTS "${_resolver_library}" OR
IS_DIRECTORY "${_resolver_library}")
message(FATAL_ERROR
"Missing absolute dependency resolver library: ${_resolver_library}")
endif()
endforeach()
file(MAKE_DIRECTORY "${_library_dir}")
# Linux dependency resolution checks RUNPATH and then ldconfig before consulting
# DIRECTORIES. Seed the exact compiler runtimes into $ORIGIN first so an older
# host libstdc++/libgcc with the same SONAME cannot be selected accidentally.
foreach(_seed IN LISTS BUNDLE_SEED_FILES)
if(NOT IS_ABSOLUTE "${_seed}" OR NOT EXISTS "${_seed}" OR IS_DIRECTORY "${_seed}")
message(FATAL_ERROR "Missing absolute runtime seed: ${_seed}")
endif()
file(COPY "${_seed}"
DESTINATION "${_library_dir}"
FOLLOW_SYMLINK_CHAIN)
endforeach()
# glibc and the ELF loader define the portable ABI floor and must be supplied
# by the target host. Everything else, including libgcc_s and libstdc++, is
# resolved recursively and copied into lib/.
set(_system_dependency_regexes
"^linux-vdso\\.so\\.1$"
"^ld-linux-x86-64\\.so\\.2$"
"^libc\\.so\\.6$"
"^libm\\.so\\.6$"
"^libmvec\\.so\\.1$"
"^libdl\\.so\\.2$"
"^libpthread\\.so\\.0$"
"^librt\\.so\\.1$"
"^libresolv\\.so\\.2$"
"^libutil\\.so\\.1$"
"^libanl\\.so\\.1$"
"^libBrokenLocale\\.so\\.1$"
"^libthread_db\\.so\\.1$")
# These runtimes are explicit, compiler-derived seed files. Never let the
# resolver select a different host copy with the same SONAME through ldconfig.
set(_seed_dependency_regexes
"^libstdc\\+\\+\\.so.*$"
"^libgcc_s\\.so.*$")
file(GET_RUNTIME_DEPENDENCIES
RESOLVED_DEPENDENCIES_VAR _resolved_dependencies
UNRESOLVED_DEPENDENCIES_VAR _unresolved_dependencies
CONFLICTING_DEPENDENCIES_PREFIX _conflicts
EXECUTABLES ${BUNDLE_RESOLVER_EXECUTABLES}
LIBRARIES ${BUNDLE_RESOLVER_LIBRARIES}
DIRECTORIES ${BUNDLE_SEARCH_DIRECTORIES}
PRE_EXCLUDE_REGEXES
${_system_dependency_regexes}
${_seed_dependency_regexes})
if(_unresolved_dependencies)
list(SORT _unresolved_dependencies)
list(JOIN _unresolved_dependencies ", " _unresolved_text)
message(FATAL_ERROR "Unresolved runtime dependencies: ${_unresolved_text}")
endif()
if(_conflicts_FILENAMES)
list(SORT _conflicts_FILENAMES)
list(JOIN _conflicts_FILENAMES ", " _conflict_text)
message(FATAL_ERROR "Conflicting runtime dependency names: ${_conflict_text}")
endif()
list(REMOVE_DUPLICATES _resolved_dependencies)
list(SORT _resolved_dependencies)
foreach(_dependency IN LISTS _resolved_dependencies)
if(NOT EXISTS "${_dependency}")
message(FATAL_ERROR "Resolved dependency disappeared: ${_dependency}")
endif()
cmake_path(GET _dependency PARENT_PATH _dependency_directory)
cmake_path(NORMAL_PATH _dependency_directory)
if(_dependency_directory STREQUAL _library_dir)
continue()
endif()
cmake_path(GET _dependency FILENAME _dependency_name)
if(EXISTS "${_library_dir}/${_dependency_name}" OR
IS_SYMLINK "${_library_dir}/${_dependency_name}")
continue()
endif()
file(COPY "${_dependency}"
DESTINATION "${_library_dir}"
FOLLOW_SYMLINK_CHAIN)
endforeach()
function(_echoion_is_elf path result_var)
if(IS_SYMLINK "${path}" OR NOT EXISTS "${path}")
set(${result_var} FALSE PARENT_SCOPE)
return()
endif()
file(READ "${path}" _magic OFFSET 0 LIMIT 4 HEX)
string(TOLOWER "${_magic}" _magic)
if(_magic STREQUAL "7f454c46")
set(${result_var} TRUE PARENT_SCOPE)
else()
set(${result_var} FALSE PARENT_SCOPE)
endif()
endfunction()
# Ensure a SONAME link exists even when CMake resolved a dependency directly to
# its real versioned file instead of the symlink used by DT_NEEDED.
file(GLOB _library_entries LIST_DIRECTORIES FALSE "${_library_dir}/*")
list(SORT _library_entries)
foreach(_entry IN LISTS _library_entries)
_echoion_is_elf("${_entry}" _is_elf)
if(NOT _is_elf)
continue()
endif()
execute_process(
COMMAND "${PATCHELF_EXECUTABLE}" --print-soname "${_entry}"
RESULT_VARIABLE _soname_result
OUTPUT_VARIABLE _soname
ERROR_VARIABLE _soname_error
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT _soname_result EQUAL 0)
message(FATAL_ERROR
"Cannot read SONAME from ${_entry}: ${_soname_error}")
endif()
if(NOT _soname STREQUAL "")
if(NOT _soname MATCHES "^[A-Za-z0-9][A-Za-z0-9._+~-]*$")
message(FATAL_ERROR "Unsafe SONAME '${_soname}' in ${_entry}")
endif()
get_filename_component(_entry_name "${_entry}" NAME)
set(_soname_path "${_library_dir}/${_soname}")
if(NOT _entry_name STREQUAL _soname
AND NOT EXISTS "${_soname_path}"
AND NOT IS_SYMLINK "${_soname_path}")
file(CREATE_LINK "${_entry_name}" "${_soname_path}" SYMBOLIC)
endif()
endif()
execute_process(
COMMAND "${PATCHELF_EXECUTABLE}" --set-rpath "$ORIGIN" "${_entry}"
RESULT_VARIABLE _patch_result
ERROR_VARIABLE _patch_error)
if(NOT _patch_result EQUAL 0)
message(FATAL_ERROR "Cannot set RUNPATH on ${_entry}: ${_patch_error}")
endif()
endforeach()
file(GLOB _bundle_executable_entries LIST_DIRECTORIES FALSE
"${_bundle_root}/bin/*")
list(SORT _bundle_executable_entries)
foreach(_entry IN LISTS _bundle_executable_entries)
_echoion_is_elf("${_entry}" _is_elf)
if(NOT _is_elf)
continue()
endif()
execute_process(
COMMAND "${PATCHELF_EXECUTABLE}" --set-rpath "$ORIGIN/../lib" "${_entry}"
RESULT_VARIABLE _patch_executable_result
ERROR_VARIABLE _patch_executable_error)
if(NOT _patch_executable_result EQUAL 0)
message(FATAL_ERROR
"Cannot set RUNPATH on ${_entry}: ${_patch_executable_error}")
endif()
endforeach()
list(LENGTH _resolved_dependencies _dependency_count)
message(STATUS "Bundled ${_dependency_count} resolved runtime dependencies")