Skip to content

[CMake] Simplify two binary variables into one tri-state variable #32042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,20 @@ set(SWIFT_COMPILER_VERSION "" CACHE STRING
set(CLANG_COMPILER_VERSION "" CACHE STRING
"The internal version of the Clang compiler")

# Indicate whether Swift should attempt to use the lld linker.
if(CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
set(SWIFT_ENABLE_LLD_LINKER_default TRUE)
# Which default linker to use. Prefer LLVM_USE_LINKER if it set, otherwise use
# our own defaults. This should only be possible in a unified (not stand alone)
# build environment.
if(LLVM_USE_LINKER)
set(SWIFT_USE_LINKER_default "${LLVM_USE_LINKER}")
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
set(SWIFT_USE_LINKER_default "lld")
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(SWIFT_USE_LINKER_default "")
else()
set(SWIFT_ENABLE_LLD_LINKER_default FALSE)
set(SWIFT_USE_LINKER_default "gold")
endif()
set(SWIFT_ENABLE_LLD_LINKER ${SWIFT_ENABLE_LLD_LINKER_default} CACHE BOOL
"Enable using the lld linker when available")

# Indicate whether Swift should attempt to use the gold linker.
# This is not used on Darwin.
if(CMAKE_SYSTEM_NAME STREQUAL Darwin OR CMAKE_SYSTEM_NAME STREQUAL Windows)
set(SWIFT_ENABLE_GOLD_LINKER_default FALSE)
else()
set(SWIFT_ENABLE_GOLD_LINKER_default TRUE)
endif()
set(SWIFT_ENABLE_GOLD_LINKER ${SWIFT_ENABLE_GOLD_LINKER_default} CACHE BOOL
"Enable using the gold linker when available")
set(SWIFT_USE_LINKER ${SWIFT_USE_LINKER_default} CACHE STRING
"Build Swift with a non-default linker")

set(SWIFT_TOOLS_ENABLE_LTO OFF CACHE STRING "Build Swift tools with LTO. One
must specify the form of LTO by setting this to one of: 'full', 'thin'. This
Expand Down
9 changes: 3 additions & 6 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function(_add_host_variant_c_compile_link_flags name)
if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID)
# lld can handle targeting the android build. However, if lld is not
# enabled, then fallback to the linker included in the android NDK.
if(NOT SWIFT_ENABLE_LLD_LINKER)
if(NOT SWIFT_USE_LINKER STREQUAL "lld")
swift_android_tools_path(${SWIFT_HOST_VARIANT_ARCH} tools_path)
target_compile_options(${name} PRIVATE -B${tools_path})
endif()
Expand Down Expand Up @@ -368,12 +368,9 @@ function(_add_host_variant_link_flags target)
endif()

if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
if(SWIFT_ENABLE_LLD_LINKER)
if(SWIFT_USE_LINKER)
target_link_options(${target} PRIVATE
-fuse-ld=lld$<$<STREQUAL:${CMAKE_HOST_SYSTEM_NAME},Windows>:.exe>)
elseif(SWIFT_ENABLE_GOLD_LINKER)
target_link_options(${target} PRIVATE
-fuse-ld=gold$<$<STREQUAL:${CMAKE_HOST_SYSTEM_NAME},Windows>:.exe>)
-fuse-ld=${SWIFT_USE_LINKER}$<$<STREQUAL:${CMAKE_HOST_SYSTEM_NAME},Windows>:.exe>)
endif()
endif()

Expand Down
10 changes: 2 additions & 8 deletions cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,9 @@ function(add_swift_unittest test_dirname)
_ENABLE_EXTENDED_ALIGNED_STORAGE)
endif()

find_program(LDLLD_PATH "ld.lld")
# Strangely, macOS finds lld and then can't find it when using -fuse-ld=
if(SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE)
if(SWIFT_USE_LINKER)
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
LINK_FLAGS " -fuse-ld=lld")
elseif(SWIFT_ENABLE_GOLD_LINKER AND
"${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
set_property(TARGET "${test_dirname}" APPEND_STRING PROPERTY
LINK_FLAGS " -fuse-ld=gold")
LINK_FLAGS " -fuse-ld=${SWIFT_USE_LINKER}")
endif()

if(SWIFT_ANALYZE_CODE_COVERAGE)
Expand Down
21 changes: 6 additions & 15 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function(_add_target_variant_c_compile_link_flags)
if("${CFLAGS_SDK}" STREQUAL "ANDROID")
# lld can handle targeting the android build. However, if lld is not
# enabled, then fallback to the linker included in the android NDK.
if(NOT SWIFT_ENABLE_LLD_LINKER)
if(NOT SWIFT_USE_LINKER STREQUAL "lld")
swift_android_tools_path(${CFLAGS_ARCH} tools_path)
list(APPEND result "-B" "${tools_path}")
endif()
Expand Down Expand Up @@ -414,20 +414,11 @@ function(_add_target_variant_link_flags)
list(APPEND library_search_directories "${SWIFT_${LFLAGS_SDK}_${LFLAGS_ARCH}_ICU_I18N_LIBDIR}")
endif()

if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
# FIXME: On Apple platforms, find_program needs to look for "ld64.lld"
find_program(LDLLD_PATH "ld.lld")
if((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR
("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND
NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WINDOWS"))
list(APPEND result "-fuse-ld=lld")
elseif(SWIFT_ENABLE_GOLD_LINKER AND
"${SWIFT_SDK_${LFLAGS_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
list(APPEND result "-fuse-ld=gold.exe")
else()
list(APPEND result "-fuse-ld=gold")
endif()
if(SWIFT_USE_LINKER AND NOT SWIFT_COMPILER_IS_MSVC_LIKE)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
list(APPEND result "-fuse-ld=${SWIFT_USE_LINKER}.exe")
else()
list(APPEND result "-fuse-ld=${SWIFT_USE_LINKER}")
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ if "@SWIFT_ENABLE_SOURCEKIT_TESTS@" == "TRUE":
if "@SWIFT_HAVE_LIBXML2@" == "TRUE":
config.available_features.add('libxml2')

if "@SWIFT_ENABLE_LLD_LINKER@" == "TRUE":
if "@SWIFT_USE_LINKER@" == "lld":
config.android_linker_name = "lld"
else:
# even if SWIFT_ENABLE_GOLD_LINKER isn't set, we cannot use BFD for Android
# even if SWIFT_USE_LINKER isn't set, we cannot use BFD for Android
config.android_linker_name = "gold"

if '@SWIFT_INCLUDE_TOOLS@' == 'TRUE':
Expand Down
4 changes: 2 additions & 2 deletions validation-test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ if "@CMAKE_GENERATOR@" == "Xcode":

config.available_features.add("CMAKE_GENERATOR=@CMAKE_GENERATOR@")

if "@SWIFT_ENABLE_LLD_LINKER@" == "TRUE":
if "@SWIFT_USE_LINKER@" == "lld":
config.android_linker_name = "lld"
else:
# even if SWIFT_ENABLE_GOLD_LINKER isn't set, we cannot use BFD for Android
# even if SWIFT_USE_LINKER isn't set, we cannot use BFD for Android
config.android_linker_name = "gold"

# Let the main config do the real work.
Expand Down