From 09d86747416f69f0a25ce586d3df77c4b5454d63 Mon Sep 17 00:00:00 2001 From: Osyotr Date: Thu, 10 Feb 2022 18:50:55 +0300 Subject: [PATCH 1/3] Fixup rpath after building dynamic libraries on linux --- .../cmake/vcpkg_find_acquire_program.cmake | 18 ++++++ scripts/cmake/z_vcpkg_fixup_rpath.cmake | 57 +++++++++++++++++++ scripts/ports.cmake | 3 + triplets/community/x64-linux-dynamic.cmake | 7 +++ triplets/x64-linux.cmake | 3 +- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 scripts/cmake/z_vcpkg_fixup_rpath.cmake create mode 100644 triplets/community/x64-linux-dynamic.cmake diff --git a/scripts/cmake/vcpkg_find_acquire_program.cmake b/scripts/cmake/vcpkg_find_acquire_program.cmake index fd96bd70108c6e..8fa5c184887fa0 100644 --- a/scripts/cmake/vcpkg_find_acquire_program.cmake +++ b/scripts/cmake/vcpkg_find_acquire_program.cmake @@ -545,6 +545,24 @@ function(vcpkg_find_acquire_program program) set(apt_package_name pkg-config) set(paths_to_search "/bin" "/usr/bin" "/usr/local/bin") endif() + elseif(program STREQUAL "PATCHELF") + set(program_name patchelf) + set(program_version 0.14.5) + set(supported_on_unix ON) + if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") + if(HOST_ARCH STREQUAL "aarch64") + set(patchelf_platform "aarch64") + set(download_sha512 "3B5EB4405FAB1D5202728AA390DD9F059CD7AFD582BAD9C50383CAD605127BC77DFCE3F2F26E9714F6BD5CCFFD49D3973BA2F061D2E2931B6E1BD0C263B99E75") + else() + set(patchelf_platform "x86_64") + set(download_sha512 "5E983A25B3F3F3B8582D1DE6C7DE30812E8D6E58E96F711F33A2634D3FB1F2370531DA179927AA401328319F92465E6F76274A6F994D1DC54C74B98E704D0D29") + endif() + set(download_filename "${program_name}-${program_version}-${patchelf_platform}.tar.gz") + set(download_urls "https://github.com/NixOS/patchelf/releases/download/${program_version}/${download_filename}") + set(tool_subdirectory "${program_version}-${patchelf_platform}-linux") + set(paths_to_search "${DOWNLOADS}/tools/patchelf/${program_version}-${patchelf_platform}-linux/bin") + endif() + set(version_command --version) else() message(FATAL "unknown tool ${program} -- unable to acquire.") endif() diff --git a/scripts/cmake/z_vcpkg_fixup_rpath.cmake b/scripts/cmake/z_vcpkg_fixup_rpath.cmake new file mode 100644 index 00000000000000..c61bda5a5dedbb --- /dev/null +++ b/scripts/cmake/z_vcpkg_fixup_rpath.cmake @@ -0,0 +1,57 @@ +function(z_vcpkg_fixup_rpath_in_dir) + vcpkg_find_acquire_program(PATCHELF) + + # 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})$") + + 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}/*") + foreach(elf_file IN LISTS elf_files) + if(IS_SYMLINK "${elf_file}") + continue() + endif() + + get_filename_component(elf_file_dir "${elf_file}" DIRECTORY) + + set(current_prefix "${CURRENT_PACKAGES_DIR}") + if(elf_file_dir MATCHES "debug/") + set(current_prefix "${CURRENT_PACKAGES_DIR}/debug") + endif() + + # compute path relative to lib + file(RELATIVE_PATH relative_to_lib "${elf_file_dir}" "${current_prefix}/lib") + if(relative_to_lib STREQUAL "") + set(rpath "\$ORIGIN") + else() + set(rpath "\$ORIGIN:\$ORIGIN/${relative_to_lib}") + endif() + + # If this fails, the file is not an elf + execute_process( + COMMAND "${PATCHELF}" --set-rpath "${rpath}" "${elf_file}" + OUTPUT_QUIET + ERROR_VARIABLE set_rpath_error + ) + if("${set_rpath_error}" STREQUAL "") + message(STATUS "Fixed rpath: ${elf_file} (${rpath})") + endif() + endforeach() + endforeach() +endfunction() + +z_vcpkg_fixup_rpath_in_dir() diff --git a/scripts/ports.cmake b/scripts/ports.cmake index e8592824fbf8fe..100dc18ce69688 100644 --- a/scripts/ports.cmake +++ b/scripts/ports.cmake @@ -145,6 +145,9 @@ if(CMD STREQUAL "BUILD") include("${CURRENT_PORT_DIR}/portfile.cmake") if(DEFINED PORT) + foreach(VCPKG_POST_BUILD_SCRIPT IN LISTS VCPKG_POST_BUILD_SCRIPTS) + include("${VCPKG_POST_BUILD_SCRIPT}") + endforeach() include("${SCRIPTS}/build_info.cmake") endif() elseif(CMD STREQUAL "CREATE") diff --git a/triplets/community/x64-linux-dynamic.cmake b/triplets/community/x64-linux-dynamic.cmake new file mode 100644 index 00000000000000..b029bccf53cb90 --- /dev/null +++ b/triplets/community/x64-linux-dynamic.cmake @@ -0,0 +1,7 @@ +set(VCPKG_TARGET_ARCHITECTURE x64) +set(VCPKG_CRT_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE dynamic) + +set(VCPKG_CMAKE_SYSTEM_NAME Linux) + +set(VCPKG_POST_BUILD_SCRIPTS "${SCRIPTS}/cmake/z_vcpkg_fixup_rpath.cmake") diff --git a/triplets/x64-linux.cmake b/triplets/x64-linux.cmake index 88221345605fd9..b029bccf53cb90 100644 --- a/triplets/x64-linux.cmake +++ b/triplets/x64-linux.cmake @@ -1,6 +1,7 @@ set(VCPKG_TARGET_ARCHITECTURE x64) set(VCPKG_CRT_LINKAGE dynamic) -set(VCPKG_LIBRARY_LINKAGE static) +set(VCPKG_LIBRARY_LINKAGE dynamic) set(VCPKG_CMAKE_SYSTEM_NAME Linux) +set(VCPKG_POST_BUILD_SCRIPTS "${SCRIPTS}/cmake/z_vcpkg_fixup_rpath.cmake") From 25b5a4dbb3848e993ab3df40421b60ead774f9f2 Mon Sep 17 00:00:00 2001 From: Osyotr Date: Thu, 7 Jul 2022 23:05:54 +0300 Subject: [PATCH 2/3] Switch back to a single variable VCPKG_FIXUP_ELF_RPATH Co-authored-by: Victor Romero --- scripts/ports.cmake | 6 +++--- triplets/community/x64-linux-dynamic.cmake | 2 +- triplets/x64-linux.cmake | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/ports.cmake b/scripts/ports.cmake index 100dc18ce69688..491cbf93c91d38 100644 --- a/scripts/ports.cmake +++ b/scripts/ports.cmake @@ -145,9 +145,9 @@ if(CMD STREQUAL "BUILD") include("${CURRENT_PORT_DIR}/portfile.cmake") if(DEFINED PORT) - foreach(VCPKG_POST_BUILD_SCRIPT IN LISTS VCPKG_POST_BUILD_SCRIPTS) - include("${VCPKG_POST_BUILD_SCRIPT}") - endforeach() + if(VCPKG_FIXUP_ELF_RPATH) + include("${SCRIPTS}/cmake/z_vcpkg_fixup_rpath.cmake") + endif() include("${SCRIPTS}/build_info.cmake") endif() elseif(CMD STREQUAL "CREATE") diff --git a/triplets/community/x64-linux-dynamic.cmake b/triplets/community/x64-linux-dynamic.cmake index b029bccf53cb90..abccb40b1faf35 100644 --- a/triplets/community/x64-linux-dynamic.cmake +++ b/triplets/community/x64-linux-dynamic.cmake @@ -4,4 +4,4 @@ set(VCPKG_LIBRARY_LINKAGE dynamic) set(VCPKG_CMAKE_SYSTEM_NAME Linux) -set(VCPKG_POST_BUILD_SCRIPTS "${SCRIPTS}/cmake/z_vcpkg_fixup_rpath.cmake") +set(VCPKG_FIXUP_ELF_RPATH ON) diff --git a/triplets/x64-linux.cmake b/triplets/x64-linux.cmake index b029bccf53cb90..35ab2eec376c4d 100644 --- a/triplets/x64-linux.cmake +++ b/triplets/x64-linux.cmake @@ -1,7 +1,7 @@ set(VCPKG_TARGET_ARCHITECTURE x64) set(VCPKG_CRT_LINKAGE dynamic) -set(VCPKG_LIBRARY_LINKAGE dynamic) +set(VCPKG_LIBRARY_LINKAGE static) set(VCPKG_CMAKE_SYSTEM_NAME Linux) -set(VCPKG_POST_BUILD_SCRIPTS "${SCRIPTS}/cmake/z_vcpkg_fixup_rpath.cmake") +set(VCPKG_FIXUP_ELF_RPATH ON) From f07e8b10522bcec49e1fb97c30f53abd7063920f Mon Sep 17 00:00:00 2001 From: Osyotr <8740768+Osyotr@users.noreply.github.com> Date: Fri, 15 Jul 2022 00:30:03 +0300 Subject: [PATCH 3/3] Don't force fixup in x64-linux triplet yet --- triplets/x64-linux.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/triplets/x64-linux.cmake b/triplets/x64-linux.cmake index 35ab2eec376c4d..88221345605fd9 100644 --- a/triplets/x64-linux.cmake +++ b/triplets/x64-linux.cmake @@ -4,4 +4,3 @@ set(VCPKG_LIBRARY_LINKAGE static) set(VCPKG_CMAKE_SYSTEM_NAME Linux) -set(VCPKG_FIXUP_ELF_RPATH ON)