Skip to content

Commit

Permalink
Allow uname calls in download mode + Fix handling of files without RP…
Browse files Browse the repository at this point in the history
…ATH (microsoft#37129)

... and don't fetch patchelf unless we actually need it. For example
cmake helper ports have no need to fetch patchelf.

In microsoft#36056 , things were changed
to unconditionally patchelf when targeting Linux. Unfortunately that
broke --only-downloads a lot of the time. This fixes that.

Also contains @Osyotr 's fixes from
microsoft#37190 to coalesce world rebuilds
a bit, so repeating comments from there:

Fixes microsoft#37183
Towards microsoft#25668
  • Loading branch information
BillyONeal authored Mar 7, 2024
1 parent 8f542fd commit e38b936
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion scripts/cmake/vcpkg_find_acquire_program(NINJA).cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
set(paths_to_search "${DOWNLOADS}/tools/${tool_subdirectory}-freebsd")
else()
execute_process(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
vcpkg_execute_in_download_mode(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
if(HOST_ARCH MATCHES "x86_64|amd64|AMD64")
set(download_filename "ninja-linux-${program_version}.zip")
set(download_urls "https://github.com/ninja-build/ninja/releases/download/v${program_version}/ninja-linux.zip")
Expand Down
2 changes: 1 addition & 1 deletion scripts/cmake/vcpkg_find_acquire_program(PATCHELF).cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(program_name patchelf)
set(program_version 0.14.5)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
execute_process(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
vcpkg_execute_in_download_mode(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
if(HOST_ARCH STREQUAL "aarch64")
set(patchelf_platform "aarch64")
set(download_sha512 "3B5EB4405FAB1D5202728AA390DD9F059CD7AFD582BAD9C50383CAD605127BC77DFCE3F2F26E9714F6BD5CCFFD49D3973BA2F061D2E2931B6E1BD0C263B99E75")
Expand Down
20 changes: 15 additions & 5 deletions scripts/cmake/z_vcpkg_fixup_rpath.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function(z_vcpkg_calculate_corrected_rpath)
# duplication removal
list(REMOVE_ITEM rpath_norm "\$ORIGIN")
list(REMOVE_ITEM rpath_norm "\$ORIGIN/${relative_to_lib}")
list(REMOVE_DUPLICATES rpath_norm)

if(NOT X_VCPKG_RPATH_KEEP_SYSTEM_PATHS)
list(FILTER rpath_norm INCLUDE REGEX "\\\$ORIGIN.+") # Only keep paths relativ to ORIGIN
Expand All @@ -55,14 +54,13 @@ function(z_vcpkg_calculate_corrected_rpath)
endif()
list(PREPEND rpath_norm "\$ORIGIN") # Make ORIGIN the first entry
list(TRANSFORM rpath_norm REPLACE "/$" "")
list(REMOVE_DUPLICATES rpath_norm)
cmake_path(CONVERT "${rpath_norm}" TO_NATIVE_PATH_LIST new_rpath)

set("${arg_OUT_NEW_RPATH_VAR}" "${new_rpath}" PARENT_SCOPE)
endfunction()

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}/*")
Expand All @@ -72,6 +70,12 @@ function(z_vcpkg_fixup_rpath_in_dir)
list(JOIN folders_to_skip "|" folders_to_skip_regex)
set(folders_to_skip_regex "^(${folders_to_skip_regex})$")

# 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()

foreach(folder IN LISTS root_entries)
if(NOT IS_DIRECTORY "${folder}")
continue()
Expand All @@ -91,14 +95,16 @@ function(z_vcpkg_fixup_rpath_in_dir)
continue()
endif()

vcpkg_find_acquire_program(PATCHELF) # Note that this relies on vcpkg_find_acquire_program short
# circuiting after the first run
# 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}")
if(NOT "${read_rpath_error}" STREQUAL "" OR "${readelf_output}" STREQUAL "")
if(NOT "${read_rpath_error}" STREQUAL "")
continue()
endif()

Expand All @@ -116,8 +122,12 @@ function(z_vcpkg_fixup_rpath_in_dir)
ERROR_VARIABLE set_rpath_error
)

message(STATUS "Adjusted RPATH of '${elf_file}' (From '${org_rpath}' -> To '${new_rpath}')")
if(NOT "${set_rpath_error}" STREQUAL "")
message(WARNING "Couldn't adjust RPATH of '${elf_file}': ${set_rpath_error}")
continue()
endif()

message(STATUS "Adjusted RPATH of '${elf_file}' (From '${readelf_output}' -> To '${new_rpath}')")
endforeach()
endforeach()
endfunction()
6 changes: 5 additions & 1 deletion scripts/ports.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ unset(CMAKE_TWEAK_VERSION)

set(SCRIPTS "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "Location to stored scripts")
list(APPEND CMAKE_MODULE_PATH "${SCRIPTS}/cmake")

# Increment this number if we intentionally need to invalidate all binary caches due a change in
# the following scripts: 1
include("${SCRIPTS}/cmake/execute_process.cmake")
include("${SCRIPTS}/cmake/vcpkg_acquire_msys.cmake")
include("${SCRIPTS}/cmake/vcpkg_add_to_path.cmake")
Expand Down Expand Up @@ -171,7 +174,8 @@ if(CMD STREQUAL "BUILD")

include("${CURRENT_PORT_DIR}/portfile.cmake")
if(DEFINED PORT)
if(VCPKG_FIXUP_ELF_RPATH OR VCPKG_TARGET_IS_LINUX)
# Always fixup RPATH on linux unless explicitly disabled.
if(VCPKG_FIXUP_ELF_RPATH OR (VCPKG_TARGET_IS_LINUX AND NOT DEFINED VCPKG_FIXUP_ELF_RPATH))
z_vcpkg_fixup_rpath_in_dir()
endif()
include("${SCRIPTS}/build_info.cmake")
Expand Down
35 changes: 34 additions & 1 deletion scripts/test_ports/vcpkg-fix-rpath/portfile.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
set(VCPKG_POLICY_EMPTY_PACKAGE enabled)

# Test for empty string
set(elf_dir "${CURRENT_PACKAGES_DIR}/lib")
set(test_rpath "")
set(expected "$ORIGIN")

z_vcpkg_calculate_corrected_rpath(
ELF_FILE_DIR "${elf_dir}"
ORG_RPATH "${test_rpath}"
OUT_NEW_RPATH_VAR new_rpath
)

if(NOT "x${new_rpath}x" STREQUAL "x${expected}x")
message(FATAL_ERROR "--- Calculated rpath does not agree with expected rpath: '${new_rpath}' != '${expected}' ")
else()
message(STATUS "--- Calculated rpath agrees with expected rpath: '${new_rpath}' ")
endif()

# Test for empty string in the tools directory
set(elf_dir "${CURRENT_PACKAGES_DIR}/tools/hdf5")
set(test_rpath "")
set(expected "$ORIGIN:$ORIGIN/../../lib")

z_vcpkg_calculate_corrected_rpath(
ELF_FILE_DIR "${elf_dir}"
ORG_RPATH "${test_rpath}"
OUT_NEW_RPATH_VAR new_rpath
)

if(NOT "x${new_rpath}x" STREQUAL "x${expected}x")
message(FATAL_ERROR "--- Calculated rpath does not agree with expected rpath: '${new_rpath}' != '${expected}' ")
else()
message(STATUS "--- Calculated rpath agrees with expected rpath: '${new_rpath}' ")
endif()

# Simple replacement and outside path test
set(elf_dir "${CURRENT_PACKAGES_DIR}/lib")
Expand Down Expand Up @@ -86,4 +119,4 @@ if(NOT "x${new_rpath}x" STREQUAL "x${expected}x")
message(FATAL_ERROR "--- Calculated rpath does not agree with expected rpath: '${new_rpath}' != '${expected}' ")
else()
message(STATUS "--- Calculated rpath agrees with expected rpath: '${new_rpath}' ")
endif()
endif()

0 comments on commit e38b936

Please sign in to comment.