Skip to content

Commit 444d48f

Browse files
committed
Add workaround for building executables with older CMake versions.
Some time between CMake 3.16 and 3.17 the default behaviour seems to have changed as follows: For a project which will build an executable, e.g.: ``` add_executable(exe_target main.swift) target_link_libraries(exe_target PUBLIC libA libB) ``` In 3.17, the invocation command to create the `exe_target` executable includes `-Xlinker -rpath -Xlinkier <libA_path/lib>` and so forth for the linked libraries and their dependencies. In 3.16, the linker is not passed the rpath flags, so the executable ends up without the `LC_RPATH` required for it to run. This affects executable targets only, libraries do still have rpaths propagated correctly.
1 parent 391204e commit 444d48f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/swift-driver/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ target_link_libraries(swift-driver PUBLIC
1212
SwiftDriver
1313
SwiftDriverExecution)
1414

15+
# This is a fairly egregious workaround for the fact that in versions < 3.17,
16+
# executables do not get `-rpath` linker arguments for their linked library
17+
# dependencies (direct and transitive)
18+
if(CMAKE_VERSION VERSION_LESS 3.17)
19+
get_target_property(TSC_UTIL_LIB TSCUtility LOCATION)
20+
get_filename_component(TSC_LIB_DIR ${TSC_UTIL_LIB} DIRECTORY)
1521

22+
get_target_property(LLBUILD_LIB llbuildSwift LOCATION)
23+
get_filename_component(LLBUILD_LIB_DIR ${LLBUILD_LIB} DIRECTORY)
24+
25+
get_target_property(ARGPARSE_LIB ArgumentParser LOCATION)
26+
get_filename_component(ARGPARSE_LIB_DIR ${ARGPARSE_LIB} DIRECTORY)
27+
28+
target_link_options(swift-driver PUBLIC "LINKER:-rpath,${CMAKE_LIBRARY_OUTPUT_DIRECTORY},-rpath,${TSC_LIB_DIR},-rpath,${LLBUILD_LIB_DIR},-rpath,${ARGPARSE_LIB_DIR}")
29+
endif()

Sources/swift-help/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ target_link_libraries(swift-help PUBLIC
1313
ArgumentParser
1414
TSCBasic)
1515

16+
# This is a fairly egregious workaround for the fact that in versions < 3.17,
17+
# executables do not get `-rpath` linker arguments for their linked library
18+
# dependencies (direct and transitive)
19+
if(CMAKE_VERSION VERSION_LESS 3.17)
20+
get_target_property(TSC_UTIL_LIB TSCUtility LOCATION)
21+
get_filename_component(TSC_LIB_DIR ${TSC_UTIL_LIB} DIRECTORY)
22+
23+
get_target_property(ARGPARSE_LIB ArgumentParser LOCATION)
24+
get_filename_component(ARGPARSE_LIB_DIR ${ARGPARSE_LIB} DIRECTORY)
25+
26+
target_link_options(swift-help PUBLIC "LINKER:-rpath,${CMAKE_LIBRARY_OUTPUT_DIRECTORY},-rpath,${TSC_LIB_DIR},-rpath,${ARGPARSE_LIB_DIR}")
27+
endif()

0 commit comments

Comments
 (0)