Merge topic 'swift-module-libraries'

56e5cea600 Swift: Support module libraries with command-line build systems

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !9379
stage/master/nightly/2024/04/17
Brad King 2024-04-16 12:46:34 +00:00 committed by Kitware Robot
commit 1c9e514d28
8 changed files with 33 additions and 37 deletions

View File

@ -138,7 +138,7 @@ if(CMAKE_Swift_COMPILATION_MODE_DEFAULT)
endif()
if(NOT CMAKE_Swift_CREATE_SHARED_MODULE)
set(CMAKE_Swift_CREATE_SHARED_MODULE ${CMAKE_Swift_CREATE_SHARED_LIBRARY})
set(CMAKE_Swift_CREATE_SHARED_MODULE "${CMAKE_Swift_CREATE_SHARED_LIBRARY} <CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS>")
endif()
if(NOT CMAKE_Swift_LINK_EXECUTABLE)
@ -162,7 +162,7 @@ else()
endif()
if(NOT CMAKE_Swift_CREATE_SHARED_MODULE)
set(CMAKE_Swift_CREATE_SHARED_MODULE ${CMAKE_Swift_CREATE_SHARED_LIBRARY})
set(CMAKE_Swift_CREATE_SHARED_MODULE "${CMAKE_Swift_CREATE_SHARED_LIBRARY} <CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS>")
endif()
if(NOT CMAKE_Swift_LINK_EXECUTABLE)

View File

@ -7,8 +7,11 @@ if("${CMAKE_GENERATOR}" STREQUAL 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")
set(CMAKE_SHARED_MODULE_LOADER_Swift_FLAG "-Wl,-bundle_loader,")
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")
set(CMAKE_SHARED_MODULE_LOADER_Swift_FLAG "-Xclang-linker -Wl,-bundle_loader,")
set(CMAKE_SHARED_MODULE_CREATE_Swift_FLAGS "-Xlinker -bundle")
endif()

View File

@ -1,3 +1,5 @@
set(CMAKE_EXE_EXPORTS_Swift_FLAG "-Xclang-linker -Wl,--export-dynamic")
# Linker Selection
# BFD is known to mislink Swift objects resulting in missing type info
set(CMAKE_Swift_USING_LINKER_SYSTEM "")

View File

@ -1478,7 +1478,7 @@ void cmLocalGenerator::GetTargetFlags(
CM_FALLTHROUGH;
case cmStateEnums::SHARED_LIBRARY: {
std::string sharedLibFlags;
if (linkLanguage != "Swift") {
if (this->IsSplitSwiftBuild() || linkLanguage != "Swift") {
sharedLibFlags = cmStrCat(
this->Makefile->GetSafeDefinition(libraryLinkVariable), ' ');
if (!configUpper.empty()) {
@ -1521,6 +1521,13 @@ void cmLocalGenerator::GetTargetFlags(
} break;
case cmStateEnums::EXECUTABLE: {
std::string exeFlags;
if (linkLanguage.empty()) {
cmSystemTools::Error(
"CMake can not determine linker language for target: " +
target->GetName());
return;
}
if (linkLanguage != "Swift") {
exeFlags = this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
exeFlags += " ";
@ -1529,28 +1536,22 @@ void cmLocalGenerator::GetTargetFlags(
cmStrCat("CMAKE_EXE_LINKER_FLAGS_", configUpper));
exeFlags += " ";
}
if (linkLanguage.empty()) {
cmSystemTools::Error(
"CMake can not determine linker language for target: " +
target->GetName());
return;
}
}
if (target->IsWin32Executable(config)) {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", linkLanguage, "_CREATE_WIN32_EXE"));
exeFlags += " ";
} else {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", linkLanguage, "_CREATE_CONSOLE_EXE"));
exeFlags += " ";
}
if (target->IsWin32Executable(config)) {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", linkLanguage, "_CREATE_WIN32_EXE"));
exeFlags += " ";
} else {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", linkLanguage, "_CREATE_CONSOLE_EXE"));
exeFlags += " ";
}
if (target->IsExecutableWithExports()) {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG"));
exeFlags += " ";
}
if (target->IsExecutableWithExports()) {
exeFlags += this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG"));
exeFlags += " ";
}
this->AddLanguageFlagsForLinking(flags, target, linkLanguage, config);

View File

@ -57,16 +57,7 @@ add_dependencies(P SwiftOnly)
add_library(SwiftIface INTERFACE)
target_link_libraries(SwiftOnly PRIVATE SwiftIface)
# @_alwaysEmitIntoClient ensures that the function body is inserted into the
# swiftmodule instead of as a symbol in the binary itself. I'm doing this to
# avoid having to link the executable. There are some flags required in order to
# link an executable into a library that I didn't see CMake emitting for Swift
# on macOS. AEIC is the easiest workaround that still tests this functionality.
# Unfortunately, AEIC was only added recently (~Swift 5.2), so we need to check
# that it is available before using it.
if(CMAKE_Swift_COMPILER_VERSION VERSION_GREATER_EQUAL 5.2)
add_subdirectory("SwiftPlugin")
endif()
add_subdirectory("SwiftPlugin")
function(test_cmp0157_default mode)
if(POLICY CMP0157)

View File

@ -1,5 +1,5 @@
add_executable(main main.swift)
set_target_properties(main PROPERTIES ENABLE_EXPORTS TRUE)
add_library(plugin plugin.swift)
add_library(plugin MODULE plugin.swift)
target_link_libraries(plugin PRIVATE main)

View File

@ -1,4 +1,3 @@
@_alwaysEmitIntoClient
public func exported() -> Int { 32 }
public func exported() -> Int { return 32 }
print(exported())

View File

@ -1,3 +1,3 @@
import main
public func importing() -> Int { main.exported() + 1 }
public func importing() -> Int { return main.exported() + 1 }