Browse Source

Merge topic 'swift-linker-selection'

3fb4092826 Swift: Add CMAKE_LINKER_TYPE support

Acked-by: Kitware Robot <[email protected]>
Merge-request: !9380
Brad King 1 năm trước cách đây
mục cha
commit
3c8d4ed51a

+ 6 - 0
Help/release/3.29.rst

@@ -199,3 +199,9 @@ Other Changes
   ``perMachine`` value.  Previously, without a custom WiX template,
   it produced installers that would only create start menu and uninstall
   entries for the current user, even though they install for all users.
+
+.. 3.29.1 (unreleased)
+
+  * The :variable:`CMAKE_LINKER_TYPE` variable and corresponding
+    :prop_tgt:`LINKER_TYPE` target property now work with compilers
+    for the ``Swift`` language.

+ 22 - 17
Help/variable/LINKER_PREDEFINED_TYPES.txt

@@ -13,39 +13,43 @@ built-in types. The pre-defined linker types are:
 
 ``SYSTEM``
   Use the standard linker provided by the platform or toolchain. For example,
-  this implies the Microsoft linker for all ``MSVC``-compatible compilers.
+  this implies the Microsoft linker for all MSVC-compatible compilers.
   This type is supported for the following platform-compiler combinations:
 
-  * Linux: ``GNU``, ``Clang``, ``LLVMFlang`` and ``NVIDIA`` compilers.
-  * All Apple variants: ``AppleClang``, ``Clang`` and ``GNU`` compilers.
-  * Windows: ``MSVC``, ``GNU``, ``Clang`` and ``NVIDIA`` compilers.
+  * Linux: ``GNU``, ``Clang``, ``LLVMFlang``, ``NVIDIA``, and ``Swift``
+    compilers.
+  * Apple platforms: ``AppleClang``, ``Clang``, ``GNU``, and ``Swift``
+    compilers.
+  * Windows: ``MSVC``, ``GNU``, ``Clang``, ``NVIDIA``, and ``Swift`` compilers.
 
 ``LLD``
   Use the ``LLVM`` linker. This type is supported for the following
   platform-compiler combinations:
 
-  * Linux: ``GNU``, ``Clang``, ``LLVMFlang`` and ``NVIDIA`` compilers.
-  * All Apple variants: ``Clang`` and ``AppleClang`` compilers.
-  * Windows: ``GNU``, ``Clang``, ``Clang`` compilers with ``GNU`` front-end,
-    ``MSVC`` and ``NVIDIA`` compilers with ``MSVC`` front-end.
+  * Linux: ``GNU``, ``Clang``, ``LLVMFlang``, ``NVIDIA``, and ``Swift``
+    compilers.
+  * Apple platforms: ``Clang``, ``AppleClang``, and ``Swift`` compilers.
+  * Windows: ``GNU``, ``Clang`` with MSVC-like front-end, ``Clang`` with
+    GNU-like front-end, ``MSVC``, ``NVIDIA`` with MSVC-like front-end,
+    and ``Swift``.
 
 ``BFD``
   Use the ``GNU`` linker.  This type is supported for the following
   platform-compiler combinations:
 
-  * Linux: ``GNU``, ``Clang``, ``LLVMFlang`` and ``NVIDIA`` compilers.
-  * Windows: ``GNU``, ``Clang`` compilers with ``GNU`` front-end.
+  * Linux: ``GNU``, ``Clang``, ``LLVMFlang``, and ``NVIDIA`` compilers.
+  * Windows: ``GNU``, ``Clang`` with GNU-like front-end.
 
 ``GOLD``
-  Supported on Linux platform with ``GNU``, ``Clang``, ``LLVMFlang`` and
-  ``NVIDIA`` compilers.
+  Supported on Linux platform with ``GNU``, ``Clang``, ``LLVMFlang``,
+  ``NVIDIA``, and ``Swift`` compilers.
 
 ``MOLD``
   Use the `mold linker <https://github.com/rui314/mold>`_. This type is
   supported on the following platform-compiler combinations:
 
-  * Linux: ``GNU``, ``Clang``, ``LLVMFlang`` and ``NVIDIA`` compilers.
-  * All Apple variants: ``Clang`` and ``AppleClang`` compilers (acts as an
+  * Linux: ``GNU``, ``Clang``, ``LLVMFlang``, and ``NVIDIA`` compilers.
+  * Apple platforms: ``Clang`` and ``AppleClang`` compilers (acts as an
     alias to the `sold linker`_).
 
 ``SOLD``
@@ -54,11 +58,12 @@ built-in types. The pre-defined linker types are:
 
 ``APPLE_CLASSIC``
   Use the Apple linker in the classic behavior (i.e. before ``Xcode 15.0``).
-  This type is only supported on Apple platforms with ``GNU``, ``Clang`` and
-  ``AppleClang`` compilers.
+  This type is only supported on Apple platforms with ``GNU``, ``Clang``,
+  ``AppleClang``, and ``Swift`` compilers.
 
 ``MSVC``
   Use the Microsoft linker. This type is only supported on the Windows
-  platform with ``MSVC`` and ``Clang`` compiler with ``MSVC`` front-end.
+  platform with ``MSVC``, ``Clang`` with MSVC-like front-end, and ``Swift``
+  compilers.
 
 .. _sold linker: https://github.com/bluewhalesystems/sold

+ 13 - 0
Modules/Platform/Apple-Apple-Swift.cmake

@@ -1 +1,14 @@
 set(CMAKE_Swift_SYSROOT_FLAG "-sdk")
+
+# Linker Selections
+if("${CMAKE_GENERATOR}" STREQUAL Xcode)
+  # Xcode always uses clang to link, regardless of what the cmake link language
+  # is. Pass the clang flags when linking with Xcode.
+  set(CMAKE_Swift_USING_LINKER_APPLE_CLASSIC "-fuse-ld=ld" "LINKER:-ld_classic")
+  set(CMAKE_Swift_USING_LINKER_LLD "-fuse-ld=lld")
+  set(CMAKE_Swift_USING_LINKER_SYSTEM "-fuse-ld=ld")
+else()
+  set(CMAKE_Swift_USING_LINKER_APPLE_CLASSIC "-use-ld=ld" "LINKER:-ld_classic")
+  set(CMAKE_Swift_USING_LINKER_LLD "-use-ld=lld")
+  set(CMAKE_Swift_USING_LINKER_SYSTEM "-use-ld=ld")
+endif()

+ 5 - 0
Modules/Platform/Linux-Apple-Swift.cmake

@@ -0,0 +1,5 @@
+# Linker Selection
+# BFD is known to mislink Swift objects resulting in missing type info
+set(CMAKE_Swift_USING_LINKER_SYSTEM "")
+set(CMAKE_Swift_USING_LINKER_GOLD "-use-ld=gold")
+set(CMAKE_Swift_USING_LINKER_LLD "-use-ld=lld")

+ 5 - 0
Modules/Platform/Windows-Apple-Swift.cmake

@@ -1,3 +1,8 @@
 set(CMAKE_Swift_IMPLIB_LINKER_FLAGS "-Xlinker -implib:<TARGET_IMPLIB>")
 set(CMAKE_Swift_FLAGS_DEBUG_LINKER_FLAGS "-Xlinker -debug")
 set(CMAKE_Swift_FLAGS_RELWITHDEBINFO_LINKER_FLAGS "-Xlinker -debug")
+
+# Linker Selection
+set(CMAKE_Swift_USING_LINKER_SYSTEM "-use-ld=link")
+set(CMAKE_Swift_USING_LINKER_LLD "-use-ld=lld")
+set(CMAKE_Swift_USING_LINKER_MSVC "-use-ld=link")

+ 1 - 0
Tests/RunCMake/CMakeLists.txt

@@ -789,6 +789,7 @@ if (CMAKE_SYSTEM_NAME MATCHES "(Linux|Darwin|Windows)"
     AND CMAKE_C_COMPILER_ID MATCHES "^(AppleClang|Clang|GNU|MSVC|NVIDIA)$"
     AND NOT CMAKE_GENERATOR STREQUAL "Green Hills MULTI")
   add_RunCMake_test(LinkerSelection -DCMake_TEST_CUDA=${CMake_TEST_CUDA}
+                                    -DCMake_TEST_Swift=${CMake_TEST_Swift}
                                     -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
                                     -DCMAKE_C_COMPILER_VERSION=${CMAKE_C_COMPILER_VERSION})
 endif()

+ 6 - 1
Tests/RunCMake/LinkerSelection/AppleClassic.cmake

@@ -1,7 +1,12 @@
-
 enable_language(C)
 
 set(CMAKE_LINKER_TYPE APPLE_CLASSIC)
 
 add_executable(main main.c)
 target_link_libraries(main PRIVATE m m)
+
+if(CMake_TEST_Swift)
+  enable_language(Swift)
+  add_executable(main_swift main.swift)
+  target_link_libraries(main_swift PRIVATE m m)
+endif()

+ 17 - 0
Tests/RunCMake/LinkerSelection/CustomLinkerType.cmake

@@ -15,6 +15,14 @@ if(CMake_TEST_CUDA)
   set_property(TARGET mainCU PROPERTY LINKER_TYPE "$<$<LINK_LANGUAGE:C>:FOO_C>$<$<LINK_LANGUAGE:CUDA>:FOO_CUDA>")
 endif()
 
+if(CMake_TEST_Swift)
+  enable_language(Swift)
+
+  set(CMAKE_Swift_USING_LINKER_FOO_Swift "${CMAKE_Swift_USING_LINKER_LLD}")
+  add_executable(mainSwift main.swift)
+  set_property(TARGET mainSwift PROPERTY LINKER_TYPE FOO_Swift)
+endif()
+
 #
 # Generate file for validation
 #
@@ -32,5 +40,14 @@ if(CMake_TEST_CUDA)
   string(APPEND LINKER_TYPE_OPTION "|${CUDA_LINKER}")
 endif()
 
+if(CMake_TEST_Swift)
+  if(CMAKE_Swift_USING_LINKER_MODE STREQUAL "TOOL")
+    cmake_path(GET CMAKE_Swift_USING_LINKER_FOO_Swift FILENAME Swift_LINKER)
+  else()
+    set(Swift_LINKER "${CMAKE_Swift_USING_LINKER_FOO_Swift}")
+  endif()
+  string(APPEND LINKER_TYPE_OPTION "|${Swift_LINKER}")
+endif()
+
 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/LINKER_TYPE_OPTION.cmake"
   "set(LINKER_TYPE_OPTION \"${LINKER_TYPE_OPTION}\")\n")

+ 4 - 4
Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake

@@ -17,12 +17,11 @@ endif()
 find_program(LLD_LINKER NAMES ${LINKER_NAMES})
 
 macro(run_cmake_and_build test)
-  run_cmake_with_options(${test} -DCMake_TEST_CUDA=${CMake_TEST_CUDA})
+  run_cmake_with_options(${test}
+    -DCMake_TEST_CUDA=${CMake_TEST_CUDA}
+    -DCMake_TEST_Swift=${CMake_TEST_Swift})
   set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/${test}-build)
   set(RunCMake_TEST_NO_CLEAN 1)
-  if(CMake_TEST_CUDA)
-    string(APPEND "|${CMAKE_CUDA_USING_LINKER_LLD}")
-  endif()
   run_cmake_command(${test}-build ${CMAKE_COMMAND} --build . --config Release --verbose ${ARGN})
 
   unset(RunCMake_TEST_BINARY_DIR)
@@ -34,6 +33,7 @@ if(LLD_LINKER)
     set(CMAKE_VERBOSE_MAKEFILE TRUE)
     set(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES FALSE)
     set(CMAKE_CUDA_USE_RESPONSE_FILE_FOR_LIBRARIES FALSE)
+    set(CMAKE_Swift_USE_RESPONSE_FILE_FOR_LIBRARIES FALSE)
 
     run_cmake_and_build(ValidLinkerType)
     run_cmake_and_build(CustomLinkerType)

+ 13 - 0
Tests/RunCMake/LinkerSelection/ValidLinkerType.cmake

@@ -11,6 +11,11 @@ if(CMake_TEST_CUDA)
   add_executable(mainCU main.cu)
 endif()
 
+if(CMake_TEST_Swift)
+  enable_language(Swift)
+  add_executable(mainSwift main.swift)
+endif()
+
 #
 # Generate file for validation
 #
@@ -27,6 +32,14 @@ if(CMake_TEST_CUDA)
   endif()
   string(APPEND LINKER_TYPE_OPTION "|${CUDA_LINKER}")
 endif()
+if(CMake_TEST_Swift)
+  if(CMAKE_Swift_USING_LINKER_MODE STREQUAL "TOOL")
+    cmake_path(GET CMAKE_Swift_USING_LINKER_LLD FILENAME LINKER_TYPE_OPTION)
+  else()
+    set(Swift_LINKER "${CMAKE_Swift_USING_LINKER_LLD}")
+  endif()
+  string(APPEND LINKER_TYPE_OPTION "|${Swift_LINKER}")
+endif()
 
 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/LINKER_TYPE_OPTION.cmake"
   "set(LINKER_TYPE_OPTION \"${LINKER_TYPE_OPTION}\")\n")

+ 1 - 0
Tests/RunCMake/LinkerSelection/main.swift

@@ -0,0 +1 @@
+print("hi")