Skip to content

Commit ade9ef4

Browse files
committed
[CMake] fix runpath for ELF platforms
Remove the absolute path to the host toolchain's stdlib from the three Foundation shared libraries. Also, add it as a CMake BUILD_PATH to plutil, so that it's removed upon installation.
1 parent 14f0c8a commit ade9ef4

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Sources/Foundation/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
157157
add_dependencies(Foundation CoreFoundationResources)
158158
target_link_options(Foundation PRIVATE
159159
$<TARGET_OBJECTS:CoreFoundationResources>)
160+
elseif(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
161+
target_link_options(Foundation PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
160162
endif()
161163

162164

Sources/FoundationNetworking/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ set_target_properties(FoundationNetworking PROPERTIES
6666
Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift
6767
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/swift)
6868

69+
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
70+
target_link_options(FoundationNetworking PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
71+
endif()
72+
6973

7074
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS FoundationNetworking)
7175
install(TARGETS FoundationNetworking

Sources/FoundationXML/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ set_target_properties(FoundationXML PROPERTIES
2020
Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift
2121
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/swift)
2222

23+
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
24+
target_link_options(FoundationXML PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
25+
endif()
26+
2327

2428
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS FoundationXML)
2529
install(TARGETS FoundationXML

Sources/Tools/plutil/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ add_executable(plutil
22
main.swift)
33
target_link_libraries(plutil PRIVATE
44
Foundation)
5+
6+
# On ELF platforms, remove the absolute rpath to the host toolchain's stdlib, then add it
7+
# back temporarily as a BUILD_RPATH just for the tests.
8+
if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
9+
target_link_options(plutil PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
10+
11+
# This assumes the host is the target, pass a target flag when cross-compiling.
12+
execute_process(
13+
COMMAND ${CMAKE_Swift_COMPILER} -print-target-info
14+
OUTPUT_VARIABLE output
15+
ERROR_VARIABLE error_output
16+
RESULT_VARIABLE result
17+
)
18+
if(NOT ${result} EQUAL 0)
19+
message(FATAL_ERROR "Error getting target info with\n"
20+
" `${CMAKE_Swift_COMPILER} -print-target-info`\n"
21+
"Error:\n"
22+
" ${error_output}")
23+
endif()
24+
25+
string(REGEX MATCH "\"runtimeLibraryPaths\": \\[\n\ +\"([^\"]+)\"" path ${output})
26+
set_target_properties(plutil PROPERTIES BUILD_RPATH ${CMAKE_MATCH_1})
27+
endif()
28+
529
set_target_properties(plutil PROPERTIES
630
INSTALL_RPATH "$ORIGIN/../lib/swift/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
731

0 commit comments

Comments
 (0)