2024-02-29 03:24:48 +01:00
|
|
|
function(z_vcpkg_calculate_corrected_rpath)
|
|
|
|
cmake_parse_arguments(PARSE_ARGV 0 "arg"
|
|
|
|
""
|
|
|
|
"ELF_FILE_DIR;ORG_RPATH;OUT_NEW_RPATH_VAR"
|
|
|
|
"")
|
|
|
|
|
|
|
|
set(current_prefix "${CURRENT_PACKAGES_DIR}")
|
|
|
|
set(current_installed_prefix "${CURRENT_INSTALLED_DIR}")
|
2024-04-23 21:09:16 +02:00
|
|
|
file(RELATIVE_PATH relative_from_packages "${CURRENT_PACKAGES_DIR}" "${arg_ELF_FILE_DIR}")
|
|
|
|
if("${relative_from_packages}/" MATCHES "^debug/|^(manual-tools|tools)/[^/]*/debug/")
|
2024-02-29 03:24:48 +01:00
|
|
|
set(current_prefix "${CURRENT_PACKAGES_DIR}/debug")
|
|
|
|
set(current_installed_prefix "${CURRENT_INSTALLED_DIR}/debug")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# compute path relative to lib
|
2024-04-23 21:09:16 +02:00
|
|
|
file(RELATIVE_PATH relative_to_lib "${arg_ELF_FILE_DIR}" "${current_prefix}/lib")
|
2024-02-29 03:24:48 +01:00
|
|
|
# compute path relative to prefix
|
2024-04-23 21:09:16 +02:00
|
|
|
file(RELATIVE_PATH relative_to_prefix "${arg_ELF_FILE_DIR}" "${current_prefix}")
|
2024-02-29 03:24:48 +01:00
|
|
|
|
|
|
|
set(rpath_norm "")
|
2024-04-23 21:09:16 +02:00
|
|
|
if(NOT "${arg_ORG_RPATH}" STREQUAL "")
|
|
|
|
cmake_path(CONVERT "${arg_ORG_RPATH}" TO_CMAKE_PATH_LIST rpath_norm)
|
|
|
|
|
|
|
|
# pattern matching helpers
|
|
|
|
list(TRANSFORM rpath_norm PREPEND "::")
|
|
|
|
list(TRANSFORM rpath_norm APPEND "/")
|
|
|
|
|
|
|
|
string(REPLACE "::${arg_ELF_FILE_DIR}/" "::\$ORIGIN/" rpath_norm "${rpath_norm}")
|
2024-02-29 03:24:48 +01:00
|
|
|
# Remove unnecessary up/down ; don't use normalize $ORIGIN/../ will be removed otherwise
|
2024-04-23 21:09:16 +02:00
|
|
|
string(REPLACE "/lib/pkgconfig/../../" "/" rpath_norm "${rpath_norm}")
|
2024-02-29 03:24:48 +01:00
|
|
|
# lib relative corrections
|
2024-04-23 21:09:16 +02:00
|
|
|
string(REPLACE "::${current_prefix}/lib/" "::\$ORIGIN/${relative_to_lib}/" rpath_norm "${rpath_norm}")
|
|
|
|
string(REPLACE "::${current_installed_prefix}/lib/" "::\$ORIGIN/${relative_to_lib}/" rpath_norm "${rpath_norm}")
|
2024-02-29 03:24:48 +01:00
|
|
|
# prefix relativ
|
2024-04-23 21:09:16 +02:00
|
|
|
string(REPLACE "::${current_prefix}/" "::\$ORIGIN/${relative_to_prefix}/" rpath_norm "${rpath_norm}")
|
|
|
|
string(REPLACE "::${current_installed_prefix}/" "::\$ORIGIN/${relative_to_prefix}/" rpath_norm "${rpath_norm}")
|
|
|
|
|
|
|
|
if(NOT X_VCPKG_RPATH_KEEP_SYSTEM_PATHS)
|
|
|
|
list(FILTER rpath_norm INCLUDE REGEX "::\\\$ORIGIN.+") # Only keep paths relativ to ORIGIN
|
|
|
|
endif()
|
2024-02-29 03:24:48 +01:00
|
|
|
|
|
|
|
# Path normalization
|
|
|
|
list(TRANSFORM rpath_norm REPLACE "/+" "/")
|
|
|
|
|
2024-04-23 21:09:16 +02:00
|
|
|
# remove pattern matching helpers
|
|
|
|
list(TRANSFORM rpath_norm REPLACE "^::" "")
|
|
|
|
list(TRANSFORM rpath_norm REPLACE "/\$" "")
|
2024-02-29 03:24:48 +01:00
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT relative_to_lib STREQUAL "")
|
|
|
|
list(PREPEND rpath_norm "\$ORIGIN/${relative_to_lib}")
|
|
|
|
endif()
|
|
|
|
list(PREPEND rpath_norm "\$ORIGIN") # Make ORIGIN the first entry
|
|
|
|
list(TRANSFORM rpath_norm REPLACE "/$" "")
|
2024-03-07 15:19:17 -08:00
|
|
|
list(REMOVE_DUPLICATES rpath_norm)
|
2024-02-29 03:24:48 +01:00
|
|
|
cmake_path(CONVERT "${rpath_norm}" TO_NATIVE_PATH_LIST new_rpath)
|
|
|
|
|
|
|
|
set("${arg_OUT_NEW_RPATH_VAR}" "${new_rpath}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
|
|
|
|
2022-07-18 22:44:34 +03:00
|
|
|
function(z_vcpkg_fixup_rpath_in_dir)
|
|
|
|
# We need to iterate trough everything because we
|
|
|
|
# can't predict where an elf file will be located
|
|
|
|
file(GLOB root_entries LIST_DIRECTORIES TRUE "${CURRENT_PACKAGES_DIR}/*")
|
|
|
|
|
|
|
|
# Skip some folders for better throughput
|
|
|
|
list(APPEND folders_to_skip "include")
|
|
|
|
list(JOIN folders_to_skip "|" folders_to_skip_regex)
|
|
|
|
set(folders_to_skip_regex "^(${folders_to_skip_regex})$")
|
|
|
|
|
2024-03-07 15:19:17 -08:00
|
|
|
# In download mode, we don't know if we're going to need PATCHELF, so be pessimistic and fetch
|
|
|
|
# it so it ends up in the downloads directory.
|
|
|
|
if(VCPKG_DOWNLOAD_MODE)
|
|
|
|
vcpkg_find_acquire_program(PATCHELF)
|
|
|
|
endif()
|
|
|
|
|
2022-07-18 22:44:34 +03:00
|
|
|
foreach(folder IN LISTS root_entries)
|
|
|
|
if(NOT IS_DIRECTORY "${folder}")
|
|
|
|
continue()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
get_filename_component(folder_name "${folder}" NAME)
|
|
|
|
if(folder_name MATCHES "${folders_to_skip_regex}")
|
|
|
|
continue()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
file(GLOB_RECURSE elf_files LIST_DIRECTORIES FALSE "${folder}/*")
|
2024-02-29 03:24:48 +01:00
|
|
|
list(FILTER elf_files EXCLUDE REGEX "\\\.(cpp|cc|cxx|c|hpp|h|hh|hxx|inc|json|toml|yaml|man|m4|ac|am|in|log|txt|pyi?|pyc|pyx|pxd|pc|cmake|f77|f90|f03|fi|f|cu|mod|ini|whl|cat|csv|rst|md|npy|npz|template|build)$")
|
|
|
|
list(FILTER elf_files EXCLUDE REGEX "/(copyright|LICENSE|METADATA)$")
|
|
|
|
|
2022-07-18 22:44:34 +03:00
|
|
|
foreach(elf_file IN LISTS elf_files)
|
|
|
|
if(IS_SYMLINK "${elf_file}")
|
|
|
|
continue()
|
|
|
|
endif()
|
|
|
|
|
2024-03-07 15:19:17 -08:00
|
|
|
vcpkg_find_acquire_program(PATCHELF) # Note that this relies on vcpkg_find_acquire_program short
|
|
|
|
# circuiting after the first run
|
2024-02-29 03:24:48 +01:00
|
|
|
# If this fails, the file is not an elf
|
|
|
|
execute_process(
|
|
|
|
COMMAND "${PATCHELF}" --print-rpath "${elf_file}"
|
|
|
|
OUTPUT_VARIABLE readelf_output
|
|
|
|
ERROR_VARIABLE read_rpath_error
|
|
|
|
)
|
|
|
|
string(REPLACE "\n" "" readelf_output "${readelf_output}")
|
2024-03-07 15:19:17 -08:00
|
|
|
if(NOT "${read_rpath_error}" STREQUAL "")
|
2024-02-29 03:24:48 +01:00
|
|
|
continue()
|
2022-07-18 22:44:34 +03:00
|
|
|
endif()
|
|
|
|
|
2024-02-29 03:24:48 +01:00
|
|
|
get_filename_component(elf_file_dir "${elf_file}" DIRECTORY)
|
|
|
|
|
2024-04-23 21:09:16 +02:00
|
|
|
z_vcpkg_calculate_corrected_rpath(
|
2024-02-29 03:24:48 +01:00
|
|
|
ELF_FILE_DIR "${elf_file_dir}"
|
|
|
|
ORG_RPATH "${readelf_output}"
|
|
|
|
OUT_NEW_RPATH_VAR new_rpath
|
|
|
|
)
|
2022-07-18 22:44:34 +03:00
|
|
|
|
|
|
|
execute_process(
|
2024-02-29 03:24:48 +01:00
|
|
|
COMMAND "${PATCHELF}" --set-rpath "${new_rpath}" "${elf_file}"
|
2022-07-18 22:44:34 +03:00
|
|
|
OUTPUT_QUIET
|
|
|
|
ERROR_VARIABLE set_rpath_error
|
|
|
|
)
|
2024-02-29 03:24:48 +01:00
|
|
|
|
2024-03-07 15:19:17 -08:00
|
|
|
if(NOT "${set_rpath_error}" STREQUAL "")
|
|
|
|
message(WARNING "Couldn't adjust RPATH of '${elf_file}': ${set_rpath_error}")
|
|
|
|
continue()
|
|
|
|
endif()
|
2024-02-29 03:24:48 +01:00
|
|
|
|
2024-03-07 15:19:17 -08:00
|
|
|
message(STATUS "Adjusted RPATH of '${elf_file}' (From '${readelf_output}' -> To '${new_rpath}')")
|
2022-07-18 22:44:34 +03:00
|
|
|
endforeach()
|
|
|
|
endforeach()
|
|
|
|
endfunction()
|