Skip to content

Commit 6ab6d3f

Browse files
committed
build: introduce and use swift_target_link_search_directories
Introduce a swift_target_link_search_directories which allows you to append a search path to the linker. This interface also allows a tweak to make the library search directory addition more portable across compilers (e.g. cl vs clang).
1 parent cd59d96 commit 6ab6d3f

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ endfunction()
294294
function(_add_variant_link_flags)
295295
set(oneValueArgs SDK ARCH BUILD_TYPE ENABLE_ASSERTIONS ANALYZE_CODE_COVERAGE
296296
DEPLOYMENT_VERSION_OSX DEPLOYMENT_VERSION_IOS DEPLOYMENT_VERSION_TVOS DEPLOYMENT_VERSION_WATCHOS
297-
RESULT_VAR_NAME ENABLE_LTO LTO_OBJECT_NAME)
297+
RESULT_VAR_NAME ENABLE_LTO LTO_OBJECT_NAME LIBRARY_SEARCH_DIRECTORIES)
298298
cmake_parse_arguments(LFLAGS
299299
""
300300
"${oneValueArgs}"
@@ -310,6 +310,7 @@ function(_add_variant_link_flags)
310310
endif()
311311

312312
set(result ${${LFLAGS_RESULT_VAR_NAME}})
313+
set(library_search_directories ${${LFLAGS_LIBRARY_SEARCH_DIRECTORIES}})
313314

314315
_add_variant_c_compile_link_flags(
315316
SDK "${LFLAGS_SDK}"
@@ -337,8 +338,9 @@ function(_add_variant_link_flags)
337338
elseif("${LFLAGS_SDK}" STREQUAL "ANDROID")
338339
list(APPEND result
339340
"-ldl"
340-
"-L${SWIFT_ANDROID_PREBUILT_PATH}/lib/gcc/arm-linux-androideabi/${SWIFT_ANDROID_NDK_GCC_VERSION}.x"
341341
"${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so")
342+
list(APPEND library_search_directories
343+
"${SWIFT_ANDROID_PREBUILT_PATH}/lib/gcc/arm-linux-androideabi/${SWIFT_ANDROID_NDK_GCC_VERSION}.x")
342344
else()
343345
list(APPEND result "-lobjc")
344346

@@ -357,13 +359,14 @@ function(_add_variant_link_flags)
357359
endif()
358360

359361
if(NOT "${SWIFT_${LFLAGS_SDK}_ICU_UC}" STREQUAL "")
360-
list(APPEND result "-L${SWIFT_${sdk}_ICU_UC}")
362+
list(APPEND library_search_directories "${SWIFT_${sdk}_ICU_UC}")
361363
endif()
362364
if(NOT "${SWIFT_${LFLAGS_SDK}_ICU_I18N}" STREQUAL "")
363-
list(APPEND result "-L${SWIFT_${sdk}_ICU_I18N}")
365+
list(APPEND library_search_directories "${SWIFT_${sdk}_ICU_I18N}")
364366
endif()
365367

366368
set("${LFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
369+
set("${LFLAGS_LIBRARY_SEARCH_DIRECTORIES}" "${library_search_directories}" PARENT_SCOPE)
367370
endfunction()
368371

369372
# Look up extra flags for a module that matches a regexp.
@@ -460,6 +463,14 @@ function(_add_swift_lipo_target)
460463
endif()
461464
endfunction()
462465

466+
function(swift_target_link_search_directories target directories)
467+
set(STLD_FLAGS "")
468+
foreach(directory ${directories})
469+
set(STLD_FLAGS " ${CMAKE_LIBRARY_PATH_FLAG}${directory}")
470+
endforeach()
471+
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS ${STLD_FLAGS})
472+
endfunction()
473+
463474
# Add a single variant of a new Swift library.
464475
#
465476
# Usage:
@@ -988,6 +999,10 @@ function(_add_swift_library_single target name)
988999
# Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly.
9891000
set(c_compile_flags ${SWIFTLIB_SINGLE_C_COMPILE_FLAGS})
9901001
set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS})
1002+
set(library_search_directories
1003+
"${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
1004+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
1005+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
9911006

9921007
# Add variant-specific flags.
9931008
if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
@@ -1029,6 +1044,7 @@ function(_add_swift_library_single target name)
10291044
DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}"
10301045
DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}"
10311046
RESULT_VAR_NAME link_flags
1047+
LIBRARY_SEARCH_DIRECTORIES library_search_directories
10321048
)
10331049

10341050
if(SWIFT_ENABLE_GOLD_LINKER AND
@@ -1081,7 +1097,8 @@ function(_add_swift_library_single target name)
10811097
set_property(TARGET "${target}" APPEND_STRING PROPERTY
10821098
COMPILE_FLAGS " ${c_compile_flags}")
10831099
set_property(TARGET "${target}" APPEND_STRING PROPERTY
1084-
LINK_FLAGS " ${link_flags} -L${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1100+
LINK_FLAGS " ${link_flags}")
1101+
swift_target_link_search_directories(${target} ${library_search_directories})
10851102

10861103
# Adjust the linked libraries for windows targets. On Windows, the link is
10871104
# performed against the import library, and the runtime uses the dll. Not
@@ -1133,8 +1150,12 @@ function(_add_swift_library_single target name)
11331150
if(target_static)
11341151
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
11351152
COMPILE_FLAGS " ${c_compile_flags}")
1136-
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
1137-
LINK_FLAGS " ${link_flags} -L${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1153+
set(library_search_directories
1154+
"${SWIFTSTATICLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}"
1155+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
1156+
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
1157+
swift_target_link_search_directories(${target_static}
1158+
${library_search_directories})
11381159
target_link_libraries("${target_static}" PRIVATE
11391160
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
11401161
endif()
@@ -1773,9 +1794,6 @@ function(_add_swift_executable_single name)
17731794
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
17741795
RESULT_VAR_NAME link_flags)
17751796

1776-
list(APPEND link_flags
1777-
"-L${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
1778-
17791797
if(SWIFTEXE_SINGLE_DISABLE_ASLR)
17801798
list(APPEND link_flags "-Wl,-no_pie")
17811799
endif()
@@ -1839,6 +1857,8 @@ function(_add_swift_executable_single name)
18391857

18401858
set_property(TARGET ${name} APPEND_STRING PROPERTY
18411859
COMPILE_FLAGS " ${c_compile_flags}")
1860+
swift_target_link_search_directories(${name}
1861+
"${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
18421862
set_property(TARGET ${name} APPEND_STRING PROPERTY
18431863
LINK_FLAGS " ${link_flags}")
18441864
if (SWIFT_PARALLEL_LINK_JOBS)

0 commit comments

Comments
 (0)