Skip to content

Commit e38b936

Browse files
authored
Allow uname calls in download mode + Fix handling of files without RPATH (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
1 parent 8f542fd commit e38b936

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

scripts/cmake/vcpkg_find_acquire_program(NINJA).cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
1515
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "FreeBSD")
1616
set(paths_to_search "${DOWNLOADS}/tools/${tool_subdirectory}-freebsd")
1717
else()
18-
execute_process(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
18+
vcpkg_execute_in_download_mode(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
1919
if(HOST_ARCH MATCHES "x86_64|amd64|AMD64")
2020
set(download_filename "ninja-linux-${program_version}.zip")
2121
set(download_urls "https://github.com/ninja-build/ninja/releases/download/v${program_version}/ninja-linux.zip")

scripts/cmake/vcpkg_find_acquire_program(PATCHELF).cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(program_name patchelf)
22
set(program_version 0.14.5)
33
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
4-
execute_process(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
4+
vcpkg_execute_in_download_mode(COMMAND "uname" "-m" OUTPUT_VARIABLE HOST_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
55
if(HOST_ARCH STREQUAL "aarch64")
66
set(patchelf_platform "aarch64")
77
set(download_sha512 "3B5EB4405FAB1D5202728AA390DD9F059CD7AFD582BAD9C50383CAD605127BC77DFCE3F2F26E9714F6BD5CCFFD49D3973BA2F061D2E2931B6E1BD0C263B99E75")

scripts/cmake/z_vcpkg_fixup_rpath.cmake

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function(z_vcpkg_calculate_corrected_rpath)
4343
# duplication removal
4444
list(REMOVE_ITEM rpath_norm "\$ORIGIN")
4545
list(REMOVE_ITEM rpath_norm "\$ORIGIN/${relative_to_lib}")
46-
list(REMOVE_DUPLICATES rpath_norm)
4746

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

6060
set("${arg_OUT_NEW_RPATH_VAR}" "${new_rpath}" PARENT_SCOPE)
6161
endfunction()
6262

6363
function(z_vcpkg_fixup_rpath_in_dir)
64-
vcpkg_find_acquire_program(PATCHELF)
65-
6664
# We need to iterate trough everything because we
6765
# can't predict where an elf file will be located
6866
file(GLOB root_entries LIST_DIRECTORIES TRUE "${CURRENT_PACKAGES_DIR}/*")
@@ -72,6 +70,12 @@ function(z_vcpkg_fixup_rpath_in_dir)
7270
list(JOIN folders_to_skip "|" folders_to_skip_regex)
7371
set(folders_to_skip_regex "^(${folders_to_skip_regex})$")
7472

73+
# In download mode, we don't know if we're going to need PATCHELF, so be pessimistic and fetch
74+
# it so it ends up in the downloads directory.
75+
if(VCPKG_DOWNLOAD_MODE)
76+
vcpkg_find_acquire_program(PATCHELF)
77+
endif()
78+
7579
foreach(folder IN LISTS root_entries)
7680
if(NOT IS_DIRECTORY "${folder}")
7781
continue()
@@ -91,14 +95,16 @@ function(z_vcpkg_fixup_rpath_in_dir)
9195
continue()
9296
endif()
9397

98+
vcpkg_find_acquire_program(PATCHELF) # Note that this relies on vcpkg_find_acquire_program short
99+
# circuiting after the first run
94100
# If this fails, the file is not an elf
95101
execute_process(
96102
COMMAND "${PATCHELF}" --print-rpath "${elf_file}"
97103
OUTPUT_VARIABLE readelf_output
98104
ERROR_VARIABLE read_rpath_error
99105
)
100106
string(REPLACE "\n" "" readelf_output "${readelf_output}")
101-
if(NOT "${read_rpath_error}" STREQUAL "" OR "${readelf_output}" STREQUAL "")
107+
if(NOT "${read_rpath_error}" STREQUAL "")
102108
continue()
103109
endif()
104110

@@ -116,8 +122,12 @@ function(z_vcpkg_fixup_rpath_in_dir)
116122
ERROR_VARIABLE set_rpath_error
117123
)
118124

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

130+
message(STATUS "Adjusted RPATH of '${elf_file}' (From '${readelf_output}' -> To '${new_rpath}')")
121131
endforeach()
122132
endforeach()
123133
endfunction()

scripts/ports.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ unset(CMAKE_TWEAK_VERSION)
2323

2424
set(SCRIPTS "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "Location to stored scripts")
2525
list(APPEND CMAKE_MODULE_PATH "${SCRIPTS}/cmake")
26+
27+
# Increment this number if we intentionally need to invalidate all binary caches due a change in
28+
# the following scripts: 1
2629
include("${SCRIPTS}/cmake/execute_process.cmake")
2730
include("${SCRIPTS}/cmake/vcpkg_acquire_msys.cmake")
2831
include("${SCRIPTS}/cmake/vcpkg_add_to_path.cmake")
@@ -171,7 +174,8 @@ if(CMD STREQUAL "BUILD")
171174

172175
include("${CURRENT_PORT_DIR}/portfile.cmake")
173176
if(DEFINED PORT)
174-
if(VCPKG_FIXUP_ELF_RPATH OR VCPKG_TARGET_IS_LINUX)
177+
# Always fixup RPATH on linux unless explicitly disabled.
178+
if(VCPKG_FIXUP_ELF_RPATH OR (VCPKG_TARGET_IS_LINUX AND NOT DEFINED VCPKG_FIXUP_ELF_RPATH))
175179
z_vcpkg_fixup_rpath_in_dir()
176180
endif()
177181
include("${SCRIPTS}/build_info.cmake")

scripts/test_ports/vcpkg-fix-rpath/portfile.cmake

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
set(VCPKG_POLICY_EMPTY_PACKAGE enabled)
22

3+
# Test for empty string
4+
set(elf_dir "${CURRENT_PACKAGES_DIR}/lib")
5+
set(test_rpath "")
6+
set(expected "$ORIGIN")
7+
8+
z_vcpkg_calculate_corrected_rpath(
9+
ELF_FILE_DIR "${elf_dir}"
10+
ORG_RPATH "${test_rpath}"
11+
OUT_NEW_RPATH_VAR new_rpath
12+
)
13+
14+
if(NOT "x${new_rpath}x" STREQUAL "x${expected}x")
15+
message(FATAL_ERROR "--- Calculated rpath does not agree with expected rpath: '${new_rpath}' != '${expected}' ")
16+
else()
17+
message(STATUS "--- Calculated rpath agrees with expected rpath: '${new_rpath}' ")
18+
endif()
19+
20+
# Test for empty string in the tools directory
21+
set(elf_dir "${CURRENT_PACKAGES_DIR}/tools/hdf5")
22+
set(test_rpath "")
23+
set(expected "$ORIGIN:$ORIGIN/../../lib")
24+
25+
z_vcpkg_calculate_corrected_rpath(
26+
ELF_FILE_DIR "${elf_dir}"
27+
ORG_RPATH "${test_rpath}"
28+
OUT_NEW_RPATH_VAR new_rpath
29+
)
30+
31+
if(NOT "x${new_rpath}x" STREQUAL "x${expected}x")
32+
message(FATAL_ERROR "--- Calculated rpath does not agree with expected rpath: '${new_rpath}' != '${expected}' ")
33+
else()
34+
message(STATUS "--- Calculated rpath agrees with expected rpath: '${new_rpath}' ")
35+
endif()
336

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

0 commit comments

Comments
 (0)