Skip to content

Commit 90008d7

Browse files
committed
Split link flags and link libraries lists to make gold happy.
gold is more strict than lld about the order of the arguments, that's why CMake offers two different properties for the linker: LINK_FLAGS and LINK_LIBRARIES. The former _add_variant_link_flags was adding the libraries to LINK_FLAGS, when the correct thing is to add them to LINK_LIBRARIES. The change adds a new output variable for _add_variant_link_flags which will containt the linked libraries, and CMake will be able to generate the correct command line invocation for when gold is used. This should fix the Android CI build.
1 parent 82e19dd commit 90008d7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ endfunction()
376376
function(_add_variant_link_flags)
377377
set(oneValueArgs SDK ARCH BUILD_TYPE ENABLE_ASSERTIONS ANALYZE_CODE_COVERAGE
378378
DEPLOYMENT_VERSION_OSX DEPLOYMENT_VERSION_IOS DEPLOYMENT_VERSION_TVOS DEPLOYMENT_VERSION_WATCHOS
379-
RESULT_VAR_NAME ENABLE_LTO LTO_OBJECT_NAME LIBRARY_SEARCH_DIRECTORIES_VAR_NAME)
379+
RESULT_VAR_NAME ENABLE_LTO LTO_OBJECT_NAME LINK_LIBRARIES_VAR_NAME LIBRARY_SEARCH_DIRECTORIES_VAR_NAME)
380380
cmake_parse_arguments(LFLAGS
381381
""
382382
"${oneValueArgs}"
@@ -387,6 +387,7 @@ function(_add_variant_link_flags)
387387
precondition(LFLAGS_ARCH MESSAGE "Should specify an architecture")
388388

389389
set(result ${${LFLAGS_RESULT_VAR_NAME}})
390+
set(link_libraries ${${LFLAGS_LINK_LIBRARIES_VAR_NAME}})
390391
set(library_search_directories ${${LFLAGS_LIBRARY_SEARCH_DIRECTORIES_VAR_NAME}})
391392

392393
_add_variant_c_compile_link_flags(
@@ -403,9 +404,9 @@ function(_add_variant_link_flags)
403404
RESULT_VAR_NAME result)
404405

405406
if("${LFLAGS_SDK}" STREQUAL "LINUX")
406-
list(APPEND result "-lpthread" "-latomic" "-ldl")
407+
list(APPEND link_libraries "pthread" "atomic" "dl")
407408
elseif("${LFLAGS_SDK}" STREQUAL "FREEBSD")
408-
list(APPEND result "-lpthread")
409+
list(APPEND link_libraries "pthread")
409410
elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN")
410411
# No extra libraries required.
411412
elseif("${LFLAGS_SDK}" STREQUAL "WINDOWS")
@@ -423,9 +424,10 @@ function(_add_variant_link_flags)
423424
list(APPEND library_search_directories
424425
${CMAKE_BINARY_DIR}/winsdk_lib_${LFLAGS_ARCH}_symlinks)
425426
elseif("${LFLAGS_SDK}" STREQUAL "HAIKU")
426-
list(APPEND result "-lbsd" "-latomic" "-Wl,-Bsymbolic")
427+
list(APPEND link_libraries "bsd" "atomic")
428+
list(APPEND result "-Wl,-Bsymbolic")
427429
elseif("${LFLAGS_SDK}" STREQUAL "ANDROID")
428-
list(APPEND result "-ldl" "-llog" "-latomic" "-licudataswift" "-licui18nswift" "-licuucswift")
430+
list(APPEND link_libraries "dl" "log" "atomic" "icudataswift" "icui18nswift" "icuucswift")
429431
if("${LFLAGS_ARCH}" MATCHES armv7)
430432
list(APPEND result "${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so")
431433
elseif("${LFLAGS_ARCH}" MATCHES aarch64)
@@ -435,7 +437,7 @@ function(_add_variant_link_flags)
435437
endif()
436438
swift_android_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB)
437439
foreach(path IN LISTS ${LFLAGS_ARCH}_LIB)
438-
list(APPEND library_search_directories ${path})
440+
list(APPEND library_search_directories ${path})
439441
endforeach()
440442
else()
441443
# If lto is enabled, we need to add the object path flag so that the LTO code
@@ -475,6 +477,7 @@ function(_add_variant_link_flags)
475477
endif()
476478

477479
set("${LFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
480+
set("${LFLAGS_LINK_LIBRARIES_VAR_NAME}" "${link_libraries}" PARENT_SCOPE)
478481
set("${LFLAGS_LIBRARY_SEARCH_DIRECTORIES_VAR_NAME}" "${library_search_directories}" PARENT_SCOPE)
479482
endfunction()
480483

@@ -1207,6 +1210,7 @@ function(_add_swift_library_single target name)
12071210
DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}"
12081211
DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}"
12091212
RESULT_VAR_NAME link_flags
1213+
LINK_LIBRARIES_VAR_NAME link_libraries
12101214
LIBRARY_SEARCH_DIRECTORIES_VAR_NAME library_search_directories
12111215
)
12121216

@@ -1251,6 +1255,7 @@ function(_add_swift_library_single target name)
12511255
COMPILE_FLAGS " ${c_compile_flags}")
12521256
set_property(TARGET "${target}" APPEND_STRING PROPERTY
12531257
LINK_FLAGS " ${link_flags}")
1258+
set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES ${link_libraries})
12541259
swift_target_link_search_directories("${target}" "${library_search_directories}")
12551260

12561261
# Adjust the linked libraries for windows targets. On Windows, the link is
@@ -2119,6 +2124,7 @@ function(_add_swift_executable_single name)
21192124
LTO_OBJECT_NAME "${name}-${SWIFTEXE_SINGLE_SDK}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
21202125
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
21212126
RESULT_VAR_NAME link_flags
2127+
LINK_LIBRARIES_VAR_NAME link_libraries
21222128
LIBRARY_SEARCH_DIRECTORIES_VAR_NAME library_search_directories)
21232129

21242130
if(${SWIFTEXE_SINGLE_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
@@ -2176,6 +2182,7 @@ function(_add_swift_executable_single name)
21762182
swift_target_link_search_directories("${name}" "${library_search_directories}")
21772183
set_property(TARGET ${name} APPEND_STRING PROPERTY
21782184
LINK_FLAGS " ${link_flags}")
2185+
set_property(TARGET ${name} APPEND PROPERTY LINK_LIBRARIES ${link_libraries})
21792186
if (SWIFT_PARALLEL_LINK_JOBS)
21802187
set_property(TARGET ${name} PROPERTY JOB_POOL_LINK swift_link_job_pool)
21812188
endif()

0 commit comments

Comments
 (0)