Skip to content

Commit 3fa5011

Browse files
authored
Merge pull request #28912 from nkcsgexi/auto-link-force-load
ModuleInterface: preserve AutolinkForceLoad option when generating .swiftmodule from .swiftinterface
2 parents 14defc5 + 7a1ac6b commit 3fa5011

File tree

10 files changed

+98
-6
lines changed

10 files changed

+98
-6
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,7 @@ function(add_swift_target_library name)
17211721
SWIFT_MODULE_DEPENDS_TVOS
17221722
SWIFT_MODULE_DEPENDS_WATCHOS
17231723
SWIFT_MODULE_DEPENDS_WINDOWS
1724+
SWIFT_MODULE_DEPENDS_FROM_SDK
17241725
TARGET_SDKS)
17251726

17261727
cmake_parse_arguments(SWIFTLIB
@@ -1960,6 +1961,9 @@ function(add_swift_target_library name)
19601961
set(swiftlib_c_compile_flags_all ${SWIFTLIB_C_COMPILE_FLAGS})
19611962
if(sdk IN_LIST SWIFT_APPLE_PLATFORMS AND SWIFTLIB_IS_SDK_OVERLAY)
19621963
set(swiftlib_swift_compile_private_frameworks_flag "-Fsystem" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/System/Library/PrivateFrameworks/")
1964+
foreach(tbd_lib ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FROM_SDK})
1965+
list(APPEND swiftlib_link_flags_all "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/usr/lib/swift/libswift${tbd_lib}.tbd")
1966+
endforeach()
19631967
endif()
19641968

19651969
list(APPEND swiftlib_c_compile_flags_all "-DSWIFT_TARGET_LIBRARY_NAME=${name}")

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ bool ModuleInterfaceBuilder::buildSwiftModule(
348348
std::string OutPathStr = OutPath;
349349
SerializationOpts.OutputPath = OutPathStr.c_str();
350350
SerializationOpts.ModuleLinkName = FEOpts.ModuleLinkName;
351+
SerializationOpts.AutolinkForceLoad =
352+
!subInvocation.getIRGenOptions().ForceLoadSymbolName.empty();
351353

352354
// Record any non-SDK module interface files for the debug info.
353355
StringRef SDKPath = SubInstance.getASTContext().SearchPathOpts.SDKPath;

stdlib/public/Darwin/ARKit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_swift_target_library(swiftARKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS_
1111
TARGET_SDKS IOS IOS_SIMULATOR
1212
SWIFT_MODULE_DEPENDS_IOS Darwin CoreImage CoreGraphics Metal UIKit Dispatch GLKit SceneKit simd Foundation AVFoundation SpriteKit CoreMedia QuartzCore ModelIO CoreFoundation CoreAudio ObjectiveC # auto-updated
1313
FRAMEWORK_DEPENDS_WEAK ARKit
14+
SWIFT_MODULE_DEPENDS_FROM_SDK CoreMIDI
1415

1516
DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_ARKIT_IOS}
1617
INSTALL_IN_COMPONENT sdk-overlay

stdlib/public/Darwin/AVFoundation/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_swift_target_library(swiftAVFoundation ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYP
1616

1717
TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
1818
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
19-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
19+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" "-L${sdk}/usr/lib/swift/"
2020
SWIFT_MODULE_DEPENDS_OSX Darwin CoreImage CoreGraphics Metal Dispatch IOKit simd Foundation CoreMedia QuartzCore XPC CoreFoundation CoreAudio ObjectiveC # auto-updated
2121
SWIFT_MODULE_DEPENDS_IOS Darwin CoreGraphics Metal Dispatch simd Foundation CoreMedia QuartzCore CoreFoundation CoreAudio ObjectiveC # auto-updated
2222
SWIFT_MODULE_DEPENDS_TVOS Darwin CoreGraphics Metal Dispatch simd Foundation CoreMedia QuartzCore CoreFoundation CoreAudio ObjectiveC # auto-updated

stdlib/public/Darwin/MediaPlayer/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ add_swift_target_library(swiftMediaPlayer ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPE
77
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
88

99
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
10-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
10+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" "-L${sdk}/usr/lib/swift/"
1111
TARGET_SDKS IOS IOS_SIMULATOR
1212

1313
SWIFT_MODULE_DEPENDS_IOS Darwin CoreImage CoreGraphics Metal UIKit Dispatch simd Foundation AVFoundation CoreMedia QuartzCore CoreFoundation CoreAudio ObjectiveC # auto-updated
14+
SWIFT_MODULE_DEPENDS_FROM_SDK CoreMIDI
1415
FRAMEWORK_DEPENDS_WEAK MediaPlayer
1516

1617
DEPLOYMENT_VERSION_IOS ${SWIFTLIB_DEPLOYMENT_VERSION_MEDIAPLAYER_IOS}

stdlib/public/Darwin/Photos/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ add_swift_target_library(swiftPhotos ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS
88
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
99

1010
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
11-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
11+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" "-L${sdk}/usr/lib/swift/"
1212
TARGET_SDKS IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR OSX
1313
SWIFT_MODULE_DEPENDS_IOS Darwin CoreImage CoreGraphics Metal Dispatch simd Foundation AVFoundation CoreMedia CoreLocation QuartzCore CoreFoundation CoreAudio ObjectiveC # auto-updated
1414
SWIFT_MODULE_DEPENDS_TVOS Darwin CoreImage CoreGraphics Metal Dispatch simd Foundation AVFoundation CoreMedia CoreLocation QuartzCore CoreFoundation CoreAudio ObjectiveC # auto-updated

stdlib/public/Darwin/Vision/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_swift_target_library(swiftVision ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES} IS
77
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
88

99
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
10-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
10+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" "-L${sdk}/usr/lib/swift/"
1111
TARGET_SDKS OSX IOS IOS_SIMULATOR TVOS TVOS_SIMULATOR
1212
SWIFT_MODULE_DEPENDS_OSX Darwin CoreImage CoreGraphics Metal Dispatch IOKit simd Foundation XPC CoreFoundation ObjectiveC # auto-updated
1313
SWIFT_MODULE_DEPENDS_IOS Darwin CoreImage CoreGraphics Metal Dispatch simd Foundation CoreFoundation ObjectiveC # auto-updated

stdlib/public/Darwin/WatchKit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_swift_target_library(swiftWatchKit ${SWIFT_SDK_OVERLAY_LIBRARY_BUILD_TYPES}
77
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
88

99
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
10-
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
10+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" "-L${sdk}/usr/lib/swift/"
1111
TARGET_SDKS WATCHOS WATCHOS_SIMULATOR
1212
SWIFT_MODULE_DEPENDS_WATCHOS Darwin HomeKit CoreGraphics UIKit Dispatch SceneKit simd Foundation MapKit CoreLocation CoreFoundation ObjectiveC # auto-updated
1313
FRAMEWORK_DEPENDS_WEAK WatchKit
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -typecheck -module-name Foo -emit-module-interface-path %t/Foo.swiftinterface %s -module-link-name Foo -enable-library-evolution -autolink-force-load
3+
// RUN: %target-swift-frontend -compile-module-from-interface -o %t/Foo.swiftmodule %t/Foo.swiftinterface
4+
// RUN: %target-swift-ide-test -print-module-metadata -module-to-print Foo -I %t -source-filename %s | %FileCheck %s
5+
6+
public func foo() {}
7+
8+
// CHECK: link library: Foo, force load: true

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ enum class ActionType {
8686
PrintASTNotTypeChecked,
8787
PrintASTTypeChecked,
8888
PrintModule,
89+
PrintModuleMetadata,
8990
PrintHeader,
9091
PrintSwiftFileInterface,
9192
PrintDecl,
@@ -181,6 +182,8 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
181182
"print-ast-typechecked", "Print the typechecked AST"),
182183
clEnumValN(ActionType::PrintModule,
183184
"print-module", "Print visible declarations in a module"),
185+
clEnumValN(ActionType::PrintModuleMetadata,
186+
"print-module-metadata", "Print meta-data in a module"),
184187
clEnumValN(ActionType::PrintHeader,
185188
"print-header", "Print visible declarations in a header file"),
186189
clEnumValN(ActionType::PrintSwiftFileInterface,
@@ -2037,6 +2040,76 @@ static int doPrintModuleGroups(const CompilerInvocation &InitInvok,
20372040
return ExitCode;
20382041
}
20392042

2043+
static void printModuleMetadata(ModuleDecl *MD) {
2044+
auto &OS = llvm::outs();
2045+
MD->collectLinkLibraries([&](LinkLibrary lib) {
2046+
OS << "link library: " << lib.getName()
2047+
<< ", force load: " << (lib.shouldForceLoad() ? "true" : "false") << "\n";
2048+
});
2049+
}
2050+
2051+
static int doPrintModuleMetaData(const CompilerInvocation &InitInvok,
2052+
const std::vector<std::string> ModulesToPrint) {
2053+
CompilerInvocation Invocation(InitInvok);
2054+
2055+
CompilerInstance CI;
2056+
// Display diagnostics to stderr.
2057+
PrintingDiagnosticConsumer PrintDiags;
2058+
CI.addDiagnosticConsumer(&PrintDiags);
2059+
if (CI.setup(Invocation))
2060+
return 1;
2061+
registerIDERequestFunctions(CI.getASTContext().evaluator);
2062+
auto &Context = CI.getASTContext();
2063+
2064+
// Load standard library so that Clang importer can use it.
2065+
auto *Stdlib = getModuleByFullName(Context, Context.StdlibModuleName);
2066+
if (!Stdlib) {
2067+
llvm::errs() << "Failed loading stdlib\n";
2068+
return 1;
2069+
}
2070+
int ExitCode = 0;
2071+
for (StringRef ModuleToPrint : ModulesToPrint) {
2072+
if (ModuleToPrint.empty()) {
2073+
ExitCode = 1;
2074+
continue;
2075+
}
2076+
2077+
// Get the (sub)module to print.
2078+
auto *M = getModuleByFullName(Context, ModuleToPrint);
2079+
if (!M) {
2080+
llvm::errs() << "error: could not find module '" << ModuleToPrint
2081+
<< "'\n";
2082+
ExitCode = 1;
2083+
continue;
2084+
}
2085+
2086+
// Split the module path.
2087+
std::vector<StringRef> ModuleName;
2088+
while (!ModuleToPrint.empty()) {
2089+
StringRef SubModuleName;
2090+
std::tie(SubModuleName, ModuleToPrint) = ModuleToPrint.split('.');
2091+
ModuleName.push_back(SubModuleName);
2092+
}
2093+
assert(!ModuleName.empty());
2094+
2095+
// FIXME: If ModuleToPrint is a submodule, get its top-level module, which
2096+
// will be the DeclContext for all of its Decls since we don't have first-
2097+
// class submodules.
2098+
if (ModuleName.size() > 1) {
2099+
M = getModuleByFullName(Context, ModuleName[0]);
2100+
if (!M) {
2101+
llvm::errs() << "error: could not find module '" << ModuleName[0]
2102+
<< "'\n";
2103+
ExitCode = 1;
2104+
continue;
2105+
}
2106+
}
2107+
printModuleMetadata(M);
2108+
}
2109+
2110+
return ExitCode;
2111+
}
2112+
20402113
static int doPrintModules(const CompilerInvocation &InitInvok,
20412114
const std::vector<std::string> ModulesToPrint,
20422115
const std::vector<std::string> GroupsToPrint,
@@ -3543,7 +3616,10 @@ int main(int argc, char *argv[]) {
35433616
}
35443617
break;
35453618
}
3546-
3619+
case ActionType::PrintModuleMetadata: {
3620+
ExitCode = doPrintModuleMetaData(InitInvok, options::ModuleToPrint);
3621+
break;
3622+
}
35473623
case ActionType::PrintHeader: {
35483624
ExitCode = doPrintHeaders(
35493625
InitInvok, options::HeaderToPrint, PrintOpts,

0 commit comments

Comments
 (0)