Skip to content

Commit fef225c

Browse files
authored
Merge pull request #39387 from nkcsgexi/emit-module-emit-abi
Frontend: teach -emit-module and -merge-modules to emit ABI descriptor files
2 parents 500c643 + f97653e commit fef225c

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
lines changed

include/swift/Serialization/SerializationOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace swift {
3232
const char *DocOutputPath = nullptr;
3333
const char *SourceInfoOutputPath = nullptr;
3434
std::string SymbolGraphOutputDir;
35+
std::string ABIDescriptorPath;
3536
bool SkipSymbolGraphInheritedDocs = true;
3637
bool IncludeSPISymbolsInSymbolGraph = false;
3738
llvm::VersionTuple UserModuleVersion;

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
151151
serializationOpts.PublicDependentLibraries =
152152
getIRGenOptions().PublicLinkLibraries;
153153
serializationOpts.SDKName = getLangOptions().SDKName;
154+
serializationOpts.ABIDescriptorPath = outs.ABIDescriptorOutputPath.c_str();
154155

155156
if (opts.EmitSymbolGraph) {
156157
if (!opts.SymbolGraphOutputDir.empty()) {

lib/Frontend/FrontendOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) {
526526
}
527527
bool FrontendOptions::canActionEmitABIDescriptor(ActionType action) {
528528
switch (action) {
529+
case ActionType::MergeModules:
530+
case ActionType::EmitModuleOnly:
529531
case ActionType::CompileModuleFromInterface:
530532
return true;
531533
case ActionType::NoneAction:
@@ -550,8 +552,6 @@ bool FrontendOptions::canActionEmitABIDescriptor(ActionType action) {
550552
case ActionType::ScanDependencies:
551553
case ActionType::PrintVersion:
552554
case ActionType::PrintFeature:
553-
case ActionType::MergeModules:
554-
case ActionType::EmitModuleOnly:
555555
case ActionType::EmitSIL:
556556
case ActionType::EmitSIBGen:
557557
case ActionType::EmitSIB:

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "swift/Frontend/ModuleInterfaceSupport.h"
2525
#include "swift/SILOptimizer/PassManager/Passes.h"
2626
#include "swift/Serialization/SerializationOptions.h"
27-
#include "swift/APIDigester/ModuleAnalyzerNodes.h"
2827
#include "clang/Frontend/CompilerInstance.h"
2928
#include "clang/Lex/PreprocessorOptions.h"
3029
#include "llvm/ADT/Hashing.h"
@@ -258,7 +257,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
258257
SerializationOpts.ModuleInterface = InPath;
259258

260259
SerializationOpts.SDKName = SubInstance.getASTContext().LangOpts.SDKName;
261-
260+
SerializationOpts.ABIDescriptorPath = ABIDescriptorPath.str();
262261
SmallVector<FileDependency, 16> Deps;
263262
bool serializeHashes = FEOpts.SerializeModuleInterfaceDependencyHashes;
264263
if (collectDepsForSerialization(SubInstance, Deps, serializeHashes)) {
@@ -286,9 +285,6 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
286285
if (SubInstance.getDiags().hadAnyError()) {
287286
return std::make_error_code(std::errc::not_supported);
288287
}
289-
if (!ABIDescriptorPath.empty()) {
290-
swift::ide::api::dumpModuleContent(Mod, ABIDescriptorPath, true);
291-
}
292288
return std::error_code();
293289
});
294290
}, ThreadStackSize);

lib/Option/features.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
},
1212
{
1313
"name": "library-level"
14+
},
15+
{
16+
"name": "emit-abi-descriptor"
1417
}
1518
]
1619
}

lib/Serialization/Serialization.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/AST/SynthesizedFileUnit.h"
3737
#include "swift/AST/TypeCheckRequests.h"
3838
#include "swift/AST/TypeVisitor.h"
39+
#include "swift/APIDigester/ModuleAnalyzerNodes.h"
3940
#include "swift/Basic/Defer.h"
4041
#include "swift/Basic/Dwarf.h"
4142
#include "swift/Basic/FileSystem.h"
@@ -5624,6 +5625,17 @@ bool Serializer::allowCompilerErrors() const {
56245625
return getASTContext().LangOpts.AllowModuleWithCompilerErrors;
56255626
}
56265627

5628+
5629+
static void emitABIDescriptor(ModuleOrSourceFile DC,
5630+
const SerializationOptions &options) {
5631+
if (!options.ABIDescriptorPath.empty()) {
5632+
if (DC.is<ModuleDecl*>()) {
5633+
swift::ide::api::dumpModuleContent(DC.get<ModuleDecl*>(),
5634+
options.ABIDescriptorPath, true);
5635+
}
5636+
}
5637+
}
5638+
56275639
void swift::serializeToBuffers(
56285640
ModuleOrSourceFile DC, const SerializationOptions &options,
56295641
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
@@ -5647,6 +5659,7 @@ void swift::serializeToBuffers(
56475659
});
56485660
if (hadError)
56495661
return;
5662+
emitABIDescriptor(DC, options);
56505663
if (moduleBuffer)
56515664
*moduleBuffer = std::make_unique<llvm::SmallVectorMemoryBuffer>(
56525665
std::move(buf), options.OutputPath);
@@ -5751,4 +5764,5 @@ void swift::serialize(ModuleOrSourceFile DC,
57515764
symbolgraphgen::emitSymbolGraphForModule(M, SGOpts);
57525765
}
57535766
}
5767+
emitABIDescriptor(DC, options);
57545768
}

test/ModuleInterface/emit-abi-descriptor.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
// RUN: %empty-directory(%t/Foo.swiftmodule)
33
// RUN: %empty-directory(%t/ResourceDir/%target-sdk-name/prebuilt-modules/Foo.swiftmodule)
44
// RUN: echo "public func foo() {}" > %t/Foo.swift
5-
// RUN: echo "public enum DisplayStyle { case tuple, optional, collection }" > %t/Foo.swift
5+
// RUN: echo "public enum DisplayStyle { case tuple, optional, collection }" >> %t/Foo.swift
6+
7+
// RUN: echo "public func bar() {}" > %t/Bar.swift
68

79
// RUN: %target-swift-frontend -emit-module %t/Foo.swift -module-name Foo -emit-module-interface-path %t/Foo.swiftinterface
810
// RUN: %target-swift-frontend -compile-module-from-interface %t/Foo.swiftinterface -o %t/Foo.swiftmodule -module-name Foo -emit-abi-descriptor-path %t/Foo.json
911

1012
// RUN: %FileCheck %s < %t/Foo.json
1113

14+
// RUN: %target-swift-frontend -emit-module -module-name Bar -emit-module-path %t/Bar.swiftmodule -emit-abi-descriptor-path %t/Bar.json %t/Bar.swift
15+
16+
// RUN: %FileCheck %s < %t/Bar.json
17+
18+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/Foo~partial1.swiftmodule %t/Foo.swift -module-name FooBar
19+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/Foo~partial2.swiftmodule %t/Bar.swift -module-name FooBar
20+
// RUN: %target-swift-frontend -merge-modules -emit-module -emit-module-path %t/FooBar.swiftmodule %t/Foo~partial1.swiftmodule %t/Foo~partial2.swiftmodule -module-name FooBar -emit-abi-descriptor-path %t/FooBar.json
21+
22+
// RUN: %FileCheck %s < %t/FooBar.json
23+
1224
// CHECK: "kind": "Root"
1325
// CHECK-NEXT: "name": "TopLevel"
1426
// CHECK-NEXT: "printedName": "TopLevel"

tools/libSwiftScan/libSwiftScan.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,10 @@ swiftscan_compiler_supported_arguments_query() {
509509

510510
swiftscan_string_set_t *
511511
swiftscan_compiler_supported_features_query() {
512-
// TODO: We are yet to figure out how "Features" will be organized.
513-
return nullptr;
512+
std::vector<std::string> allFeatures;
513+
allFeatures.emplace_back("library-level");
514+
allFeatures.emplace_back("emit-abi-descriptor");
515+
return create_set(allFeatures);
514516
}
515517

516518
int invoke_swift_compiler(int argc, const char **argv) {

0 commit comments

Comments
 (0)