Skip to content

Commit e45580d

Browse files
committed
[cmake] Add runpath for libBlocksRuntime.so
Dispatch build leaves artifacts in both the root directory and the src directory, but only the src directory was being added to the runpath, so the libBlocksRuntime.so library might not have been found. Additionally, remove the rpath flags from libdispatch_ldflags, which are used to compile Foundation, but will point to the build directory results. Add those flags instead to a list used for the test binaries. In the end Foundation ends up with a path of `$ORIGIN`; TestFoundation has paths to Foundation, to Dispatch and BlocksRuntime, and to XCTest; while xdgTestHelper points to Foundation, and Dispatch and BlocksRuntime. Extract a small function for adding rpaths to a list, to make the rpath easier to read. Also, remove Android from the rpath flags, since Android will not work with the build tree rpaths, and will need its own solution. For the time being, do not set any rpath.
1 parent 75fa8e0 commit e45580d

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

CMakeLists.txt

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,39 @@ set(libdispatch_ldflags)
9595
if(FOUNDATION_ENABLE_LIBDISPATCH)
9696
set(libdispatch_cflags -I;${FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE};-I;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src/swift;-Xcc;-fblocks)
9797
set(libdispatch_ldflags -L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD};-L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src;-ldispatch;-lswiftDispatch)
98-
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
99-
list(APPEND libdispatch_ldflags -Xlinker;-rpath;-Xlinker;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
100-
endif()
10198
endif()
10299

103-
if(CMAKE_SYSTEM_NAME STREQUAL Android OR CMAKE_SYSTEM_NAME STREQUAL Linux)
104-
set(Foundation_RPATH -Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN")
105-
set(XDG_TEST_HELPER_RPATH -Xlinker;-rpath;-Xlinker;${CMAKE_CURRENT_BINARY_DIR})
100+
101+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
102+
append_linker_rpath(Foundation_RPATH "\\\$\$ORIGIN")
103+
append_linker_rpath(TestFoundation_RPATH ${CMAKE_CURRENT_BINARY_DIR})
104+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_XCTEST_BUILD})
105+
append_linker_rpath(xdgTestHelper_RPATH ${CMAKE_CURRENT_BINARY_DIR})
106+
if (FOUNDATION_ENABLE_LIBDISPATCH)
107+
# NOTE: the following two are for testing LLDB and others, but should be removed
108+
append_linker_rpath(Foundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
109+
append_linker_rpath(Foundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
110+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
111+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
112+
append_linker_rpath(xdgTestHelper_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
113+
append_linker_rpath(xdgTestHelper_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
114+
endif()
115+
elseif(CMAKE_SYSTEM_NAME STREQUAL Android)
116+
append_linker_rpath(Foundation_RPATH "\\\$\$ORIGIN")
106117
elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
107-
set(Foundation_RPATH -Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN")
108-
set(XDG_TEST_HELPER_RPATH -Xlinker;-rpath;-Xlinker;${CMAKE_CURRENT_BINARY_DIR})
118+
append_linker_rpath(Foundation_RPATH "\\\$\$ORIGIN")
119+
append_linker_rpath(TestFoundation_RPATH ${CMAKE_CURRENT_BINARY_DIR})
120+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_XCTEST_BUILD})
121+
append_linker_rpath(xdgTestHelper_RPATH ${CMAKE_CURRENT_BINARY_DIR})
122+
if (FOUNDATION_ENABLE_LIBDISPATCH)
123+
# NOTE: the following two are for testing LLDB and others, but should be removed
124+
append_linker_rpath(Foundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
125+
append_linker_rpath(Foundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
126+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
127+
append_linker_rpath(TestFoundation_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
128+
append_linker_rpath(xdgTestHelper_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
129+
append_linker_rpath(xdgTestHelper_RPATH ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD})
130+
endif ()
109131
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
110132
# FIXME(SR9138) Silence "locally defined symbol '…' imported in function '…'
111133
set(WORKAROUND_SR9138 -Xlinker;-ignore:4049;-Xlinker;-ignore:4217)
@@ -419,7 +441,7 @@ if(ENABLE_TESTING)
419441
-L${CMAKE_CURRENT_BINARY_DIR}
420442
-lFoundation
421443
${Foundation_INTERFACE_LIBRARIES}
422-
${XDG_TEST_HELPER_RPATH}
444+
${xdgTestHelper_RPATH}
423445
SOURCES
424446
TestFoundation/xdgTestHelper/main.swift
425447
SWIFT_FLAGS
@@ -539,6 +561,7 @@ if(ENABLE_TESTING)
539561
-L${FOUNDATION_PATH_TO_XCTEST_BUILD}
540562
-lXCTest
541563
${WORKAROUND_SR9138}
564+
${TestFoundation_RPATH}
542565
$<$<PLATFORM_ID:Windows>:-lWS2_32>
543566
RESOURCES
544567
${CMAKE_SOURCE_DIR}/TestFoundation/Resources/Info.plist
@@ -594,8 +617,6 @@ if(ENABLE_TESTING)
594617
${CMAKE_CURRENT_BINARY_DIR}/TestFoundation)
595618
set_tests_properties(TestFoundation
596619
PROPERTIES
597-
ENVIRONMENT
598-
LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}:${FOUNDATION_PATH_TO_XCTEST_BUILD}:${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}:${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src
599620
DEPENDS
600621
${CMAKE_CURRENT_BINARY_DIR}/TestFoundation/xdgTestHelper${CMAKE_EXECUTABLE_SUFFIX})
601622
endif()

cmake/modules/SwiftSupport.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,20 @@ function(get_swift_host_arch result_var_name)
272272
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
273273
endif()
274274
endfunction()
275+
276+
277+
# Appends an the given path as an rpath prefixed by the -Xlinker arguments for
278+
# the Swift compiler to pass to the linker.
279+
#
280+
# Usage:
281+
# append_linker_rpath(MyTarget_RPATH one)
282+
# append_linker_rpath(MyTarget_RPATH two)
283+
# # MyTarget_RPATH=-Xlinker;-rpath;-Xlinker;one;-Xlinker;-rpath;-Xlinker;two
284+
#
285+
function(append_linker_rpath result_var_name rpath)
286+
set(tmp ${${result_var_name}})
287+
288+
list(APPEND tmp -Xlinker -rpath -Xlinker "${rpath}")
289+
290+
set(${result_var_name} ${tmp} PARENT_SCOPE)
291+
endfunction()

0 commit comments

Comments
 (0)