139 lines
4.8 KiB
CMake
139 lines
4.8 KiB
CMake
cmake_minimum_required(VERSION 4.0)
|
|
|
|
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
|
|
message(FATAL_ERROR "Component dependency bundling currently supports Linux only")
|
|
endif()
|
|
foreach(_required IN ITEMS BUNDLE_ROOT PATCHELF_EXECUTABLE)
|
|
if(NOT DEFINED ${_required} OR "${${_required}}" STREQUAL "")
|
|
message(FATAL_ERROR "${_required} is required")
|
|
endif()
|
|
endforeach()
|
|
if((NOT DEFINED BUNDLE_EXECUTABLES OR BUNDLE_EXECUTABLES STREQUAL "") AND
|
|
(NOT DEFINED BUNDLE_LIBRARIES OR BUNDLE_LIBRARIES STREQUAL ""))
|
|
message(STATUS "Component has no dynamic runtime payload")
|
|
return()
|
|
endif()
|
|
|
|
cmake_path(ABSOLUTE_PATH BUNDLE_ROOT NORMALIZE OUTPUT_VARIABLE _bundle_root)
|
|
set(_library_dir "${_bundle_root}/lib")
|
|
file(MAKE_DIRECTORY "${_library_dir}")
|
|
|
|
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 compiler runtime seed: ${_seed}")
|
|
endif()
|
|
file(COPY "${_seed}" DESTINATION "${_library_dir}" FOLLOW_SYMLINK_CHAIN)
|
|
endforeach()
|
|
|
|
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$")
|
|
set(_seed_dependency_regexes
|
|
"^libstdc\\+\\+\\.so.*$"
|
|
"^libgcc_s\\.so.*$")
|
|
|
|
file(GET_RUNTIME_DEPENDENCIES
|
|
RESOLVED_DEPENDENCIES_VAR _resolved
|
|
UNRESOLVED_DEPENDENCIES_VAR _unresolved
|
|
CONFLICTING_DEPENDENCIES_PREFIX _conflicts
|
|
EXECUTABLES ${BUNDLE_EXECUTABLES}
|
|
LIBRARIES ${BUNDLE_LIBRARIES}
|
|
DIRECTORIES ${BUNDLE_SEARCH_DIRECTORIES}
|
|
PRE_EXCLUDE_REGEXES
|
|
${_system_dependency_regexes}
|
|
${_seed_dependency_regexes})
|
|
if(_unresolved)
|
|
list(SORT _unresolved)
|
|
list(JOIN _unresolved ", " _unresolved_text)
|
|
message(FATAL_ERROR "Unresolved component dependencies: ${_unresolved_text}")
|
|
endif()
|
|
if(_conflicts_FILENAMES)
|
|
list(SORT _conflicts_FILENAMES)
|
|
list(JOIN _conflicts_FILENAMES ", " _conflict_text)
|
|
message(FATAL_ERROR "Conflicting component dependencies: ${_conflict_text}")
|
|
endif()
|
|
|
|
list(REMOVE_DUPLICATES _resolved)
|
|
list(SORT _resolved)
|
|
foreach(_dependency IN LISTS _resolved)
|
|
cmake_path(GET _dependency FILENAME _name)
|
|
if(NOT EXISTS "${_library_dir}/${_name}" AND
|
|
NOT IS_SYMLINK "${_library_dir}/${_name}")
|
|
file(COPY "${_dependency}"
|
|
DESTINATION "${_library_dir}"
|
|
FOLLOW_SYMLINK_CHAIN)
|
|
endif()
|
|
endforeach()
|
|
|
|
function(_echoion_component_is_elf path result)
|
|
if(IS_SYMLINK "${path}" OR NOT EXISTS "${path}")
|
|
set(${result} FALSE PARENT_SCOPE)
|
|
return()
|
|
endif()
|
|
file(READ "${path}" _magic OFFSET 0 LIMIT 4 HEX)
|
|
string(TOLOWER "${_magic}" _magic)
|
|
if(_magic STREQUAL "7f454c46")
|
|
set(${result} TRUE PARENT_SCOPE)
|
|
else()
|
|
set(${result} FALSE PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
file(GLOB_RECURSE _entries LIST_DIRECTORIES FALSE "${_bundle_root}/*")
|
|
list(SORT _entries)
|
|
foreach(_entry IN LISTS _entries)
|
|
_echoion_component_is_elf("${_entry}" _is_elf)
|
|
if(NOT _is_elf)
|
|
continue()
|
|
endif()
|
|
cmake_path(RELATIVE_PATH _entry BASE_DIRECTORY "${_bundle_root}"
|
|
OUTPUT_VARIABLE _relative)
|
|
if(_relative MATCHES "^bin/")
|
|
set(_runpath "$ORIGIN/../lib")
|
|
elseif(_relative MATCHES "^plugins/[^/]+/")
|
|
set(_runpath "$ORIGIN/../../lib")
|
|
else()
|
|
set(_runpath "$ORIGIN")
|
|
endif()
|
|
execute_process(
|
|
COMMAND "${PATCHELF_EXECUTABLE}" --set-rpath "${_runpath}" "${_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()
|
|
|
|
execute_process(
|
|
COMMAND "${PATCHELF_EXECUTABLE}" --print-soname "${_entry}"
|
|
RESULT_VARIABLE _soname_result
|
|
OUTPUT_VARIABLE _soname
|
|
ERROR_VARIABLE _soname_error
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
|
if(_soname_result EQUAL 0 AND NOT _soname STREQUAL "" AND
|
|
_relative MATCHES "^lib/")
|
|
if(NOT _soname MATCHES "^[A-Za-z0-9][A-Za-z0-9._+~-]*$")
|
|
message(FATAL_ERROR "Unsafe SONAME '${_soname}' in ${_entry}")
|
|
endif()
|
|
cmake_path(GET _entry FILENAME _entry_name)
|
|
if(NOT _entry_name STREQUAL _soname AND
|
|
NOT EXISTS "${_library_dir}/${_soname}" AND
|
|
NOT IS_SYMLINK "${_library_dir}/${_soname}")
|
|
file(CREATE_LINK "${_entry_name}" "${_library_dir}/${_soname}" SYMBOLIC)
|
|
endif()
|
|
endif()
|
|
endforeach()
|
|
|
|
list(LENGTH _resolved _dependency_count)
|
|
message(STATUS "Bundled ${_dependency_count} private component dependencies")
|