Skip to content

Commit 53ebbf0

Browse files
authored
Merge pull request #11305 from huonw/tbd-install-name
Add -tbd-install_name
2 parents f32884a + a6e4fdb commit 53ebbf0

File tree

9 files changed

+246
-119
lines changed

9 files changed

+246
-119
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ class FrontendOptions {
297297
/// Compare the symbols in the IR against the TBD file we would generate.
298298
TBDValidationMode ValidateTBDAgainstIR = TBDValidationMode::None;
299299

300+
/// The install_name to use in the TBD file.
301+
std::string TBDInstallName;
302+
300303
/// An enum with different modes for automatically crashing at defined times.
301304
enum class DebugCrashMode {
302305
None, ///< Don't automatically crash.

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def emit_fixits_path
6262
: Separate<["-"], "emit-fixits-path">, MetaVarName<"<path>">,
6363
HelpText<"Output compiler fixits as source edits to <path>">;
6464

65+
def tbd_install_name
66+
: Separate<["-"], "tbd-install_name">, MetaVarName<"<path>">,
67+
HelpText<"The install_name to use in an emitted TBD file">;
68+
69+
def tbd_install_name_EQ : Joined<["-"], "tbd-install_name=">,
70+
Alias<tbd_install_name>;
71+
6572
def verify : Flag<["-"], "verify">,
6673
HelpText<"Verify diagnostics against expected-{error|warning|note} "
6774
"annotations">;

include/swift/TBDGen/TBDGen.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,28 @@
1212
#ifndef SWIFT_IRGEN_TBDGEN_H
1313
#define SWIFT_IRGEN_TBDGEN_H
1414

15+
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/ADT/StringSet.h"
1617

18+
namespace llvm {
19+
class raw_ostream;
20+
}
21+
1722
namespace swift {
1823
class FileUnit;
24+
class ModuleDecl;
1925

2026
void enumeratePublicSymbols(FileUnit *module, llvm::StringSet<> &symbols,
21-
bool hasMultipleIRGenThreads, bool isWholeModule,
27+
bool hasMultipleIRGenThreads,
28+
bool silSerializeWitnessTables);
29+
void enumeratePublicSymbols(ModuleDecl *module, llvm::StringSet<> &symbols,
30+
bool hasMultipleIRGenThreads,
2231
bool silSerializeWitnessTables);
32+
33+
void writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
34+
bool hasMultipleIRGenThreads, bool silSerializeWitnessTables,
35+
llvm::StringRef installName);
36+
2337
} // end namespace swift
2438

2539
#endif

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
211211
}
212212
}
213213

214+
if (const Arg *A = Args.getLastArg(OPT_tbd_install_name)) {
215+
Opts.TBDInstallName = A->getValue();
216+
}
217+
214218
if (const Arg *A = Args.getLastArg(OPT_warn_long_function_bodies)) {
215219
unsigned attempt;
216220
if (StringRef(A->getValue()).getAsInteger(10, attempt)) {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,12 @@ static bool performCompile(CompilerInstance &Instance,
745745
if (!opts.TBDPath.empty()) {
746746
const auto &silOpts = Invocation.getSILOptions();
747747
auto hasMultipleIRGenThreads = silOpts.NumThreads > 1;
748+
auto installName = opts.TBDInstallName.empty()
749+
? "lib" + Invocation.getModuleName().str() + ".dylib"
750+
: opts.TBDInstallName;
751+
748752
if (writeTBD(Instance.getMainModule(), hasMultipleIRGenThreads,
749-
silOpts.SILSerializeWitnessTables, opts.TBDPath))
753+
silOpts.SILSerializeWitnessTables, opts.TBDPath, installName))
750754
return true;
751755
}
752756

lib/FrontendTool/TBD.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,18 @@ static std::vector<StringRef> sortSymbols(llvm::StringSet<> &symbols) {
3939
}
4040

4141
bool swift::writeTBD(ModuleDecl *M, bool hasMultipleIRGenThreads,
42-
bool silSerializeWitnessTables, StringRef OutputFilename) {
42+
bool silSerializeWitnessTables, StringRef OutputFilename,
43+
StringRef installName) {
4344
std::error_code EC;
4445
llvm::raw_fd_ostream OS(OutputFilename, EC, llvm::sys::fs::F_None);
4546
if (EC) {
4647
M->getASTContext().Diags.diagnose(SourceLoc(), diag::error_opening_output,
4748
OutputFilename, EC.message());
4849
return true;
4950
}
50-
llvm::StringSet<> symbols;
51-
for (auto file : M->getFiles())
52-
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
53-
/*isWholeModule=*/true, silSerializeWitnessTables);
5451

55-
// Ensure the order is stable.
56-
for (auto &symbol : sortSymbols(symbols)) {
57-
OS << symbol << "\n";
58-
}
52+
writeTBDFile(M, OS, hasMultipleIRGenThreads, silSerializeWitnessTables,
53+
installName);
5954

6055
return false;
6156
}
@@ -128,9 +123,8 @@ bool swift::validateTBD(ModuleDecl *M, llvm::Module &IRModule,
128123
bool silSerializeWitnessTables,
129124
bool diagnoseExtraSymbolsInTBD) {
130125
llvm::StringSet<> symbols;
131-
for (auto file : M->getFiles())
132-
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
133-
/*isWholeModule=*/true, silSerializeWitnessTables);
126+
enumeratePublicSymbols(M, symbols, hasMultipleIRGenThreads,
127+
silSerializeWitnessTables);
134128

135129
return validateSymbolSet(M->getASTContext().Diags, symbols, IRModule,
136130
diagnoseExtraSymbolsInTBD);
@@ -142,7 +136,7 @@ bool swift::validateTBD(FileUnit *file, llvm::Module &IRModule,
142136
bool diagnoseExtraSymbolsInTBD) {
143137
llvm::StringSet<> symbols;
144138
enumeratePublicSymbols(file, symbols, hasMultipleIRGenThreads,
145-
/*isWholeModule=*/false, silSerializeWitnessTables);
139+
silSerializeWitnessTables);
146140

147141
return validateSymbolSet(file->getParentModule()->getASTContext().Diags,
148142
symbols, IRModule, diagnoseExtraSymbolsInTBD);

lib/FrontendTool/TBD.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class FileUnit;
2525
class FrontendOptions;
2626

2727
bool writeTBD(ModuleDecl *M, bool hasMultipleIRGenThreads,
28-
bool silSerializeWitnessTables, llvm::StringRef OutputFilename);
28+
bool silSerializeWitnessTables, llvm::StringRef OutputFilename,
29+
llvm::StringRef installName);
2930
bool inputFileKindCanHaveTBDValidated(InputFileKind kind);
3031
bool validateTBD(ModuleDecl *M, llvm::Module &IRModule,
3132
bool hasMultipleIRGenThreads, bool silSerializeWitnessTables,

lib/TBDGen/TBDGen.cpp

Lines changed: 66 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -28,101 +28,17 @@
2828
#include "swift/SIL/TypeLowering.h"
2929
#include "llvm/ADT/StringSet.h"
3030

31+
#include "TBDGenVisitor.h"
32+
3133
using namespace swift;
3234
using namespace swift::irgen;
35+
using namespace swift::tbdgen;
3336
using StringSet = llvm::StringSet<>;
3437

3538
static bool isPrivateDecl(ValueDecl *VD) {
3639
return getDeclLinkage(VD) != FormalLinkage::PublicUnique;
3740
}
3841

39-
namespace {
40-
class TBDGenVisitor : public ASTVisitor<TBDGenVisitor> {
41-
StringSet &Symbols;
42-
const UniversalLinkageInfo &UniversalLinkInfo;
43-
ModuleDecl *SwiftModule;
44-
bool FileHasEntryPoint;
45-
bool SILSerializeWitnessTables;
46-
47-
bool InsideAbstractStorageDecl = false;
48-
49-
void addSymbol(StringRef name) {
50-
auto isNewValue = Symbols.insert(name).second;
51-
(void)isNewValue;
52-
assert(isNewValue && "already inserted");
53-
}
54-
55-
void addSymbol(SILDeclRef declRef);
56-
57-
void addSymbol(LinkEntity entity) {
58-
auto linkage =
59-
LinkInfo::get(UniversalLinkInfo, SwiftModule, entity, ForDefinition);
60-
61-
auto externallyVisible =
62-
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
63-
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
64-
65-
if (externallyVisible)
66-
addSymbol(linkage.getName());
67-
}
68-
69-
void addConformances(DeclContext *DC);
70-
71-
public:
72-
TBDGenVisitor(StringSet &symbols,
73-
const UniversalLinkageInfo &universalLinkInfo,
74-
ModuleDecl *swiftModule, bool fileHasEntryPoint,
75-
bool silSerializeWitnessTables)
76-
: Symbols(symbols), UniversalLinkInfo(universalLinkInfo),
77-
SwiftModule(swiftModule), FileHasEntryPoint(fileHasEntryPoint),
78-
SILSerializeWitnessTables(silSerializeWitnessTables) {}
79-
80-
void visitMembers(Decl *D) {
81-
SmallVector<Decl *, 4> members;
82-
auto addMembers = [&](DeclRange range) {
83-
for (auto member : range)
84-
members.push_back(member);
85-
};
86-
if (auto ED = dyn_cast<ExtensionDecl>(D))
87-
addMembers(ED->getMembers());
88-
else if (auto NTD = dyn_cast<NominalTypeDecl>(D))
89-
addMembers(NTD->getMembers());
90-
else if (auto ASD = dyn_cast<AbstractStorageDecl>(D))
91-
ASD->getAllAccessorFunctions(members);
92-
93-
for (auto member : members) {
94-
ASTVisitor::visit(member);
95-
}
96-
}
97-
98-
void visitPatternBindingDecl(PatternBindingDecl *PBD);
99-
100-
void visitValueDecl(ValueDecl *VD);
101-
102-
void visitAbstractFunctionDecl(AbstractFunctionDecl *AFD);
103-
104-
void visitTypeAliasDecl(TypeAliasDecl *TAD) {
105-
// any information here is encoded elsewhere
106-
}
107-
108-
void visitNominalTypeDecl(NominalTypeDecl *NTD);
109-
110-
void visitClassDecl(ClassDecl *CD);
111-
112-
void visitConstructorDecl(ConstructorDecl *CD);
113-
114-
void visitExtensionDecl(ExtensionDecl *ED);
115-
116-
void visitProtocolDecl(ProtocolDecl *PD);
117-
118-
void visitAbstractStorageDecl(AbstractStorageDecl *ASD);
119-
120-
void visitVarDecl(VarDecl *VD);
121-
122-
void visitDecl(Decl *D) { visitMembers(D); }
123-
};
124-
} // end anonymous namespace
125-
12642
static bool isGlobalOrStaticVar(VarDecl *VD) {
12743
return VD->isStatic() || VD->getDeclContext()->isModuleScopeContext();
12844
}
@@ -410,23 +326,70 @@ void TBDGenVisitor::visitProtocolDecl(ProtocolDecl *PD) {
410326
#endif
411327
}
412328

329+
static void enumeratePublicSymbolsAndWrite(ModuleDecl *M, FileUnit *singleFile,
330+
StringSet &symbols,
331+
bool hasMultipleIRGenThreads,
332+
bool silSerializeWitnessTables,
333+
llvm::raw_ostream *os,
334+
StringRef installName) {
335+
auto isWholeModule = singleFile == nullptr;
336+
const auto &target = M->getASTContext().LangOpts.Target;
337+
UniversalLinkageInfo linkInfo(target, hasMultipleIRGenThreads, isWholeModule);
338+
339+
TBDGenVisitor visitor(symbols, target, linkInfo, M, silSerializeWitnessTables,
340+
installName);
341+
342+
auto visitFile = [&](FileUnit *file) {
343+
SmallVector<Decl *, 16> decls;
344+
file->getTopLevelDecls(decls);
345+
346+
visitor.setFileHasEntryPoint(file->hasEntryPoint());
347+
348+
for (auto d : decls)
349+
visitor.visit(d);
350+
};
351+
352+
if (singleFile) {
353+
assert(M == singleFile->getParentModule() && "mismatched file and module");
354+
visitFile(singleFile);
355+
} else {
356+
for (auto *file : M->getFiles()) {
357+
visitFile(file);
358+
}
359+
}
360+
361+
if (os) {
362+
// The correct TBD formatting code is temporarily non-open source, so this
363+
// is just a list of the symbols.
364+
std::vector<StringRef> sorted;
365+
for (auto &symbol : symbols)
366+
sorted.push_back(symbol.getKey());
367+
std::sort(sorted.begin(), sorted.end());
368+
for (const auto &symbol : sorted) {
369+
*os << symbol << "\n";
370+
}
371+
}
372+
}
373+
413374
void swift::enumeratePublicSymbols(FileUnit *file, StringSet &symbols,
414375
bool hasMultipleIRGenThreads,
415-
bool isWholeModule,
416376
bool silSerializeWitnessTables) {
417-
UniversalLinkageInfo linkInfo(file->getASTContext().LangOpts.Target,
418-
hasMultipleIRGenThreads, isWholeModule);
419-
420-
SmallVector<Decl *, 16> decls;
421-
file->getTopLevelDecls(decls);
422-
423-
auto hasEntryPoint = file->hasEntryPoint();
424-
425-
TBDGenVisitor visitor(symbols, linkInfo, file->getParentModule(),
426-
hasEntryPoint, silSerializeWitnessTables);
427-
for (auto d : decls)
428-
visitor.visit(d);
429-
430-
if (hasEntryPoint)
431-
symbols.insert("main");
377+
enumeratePublicSymbolsAndWrite(
378+
file->getParentModule(), file, symbols, hasMultipleIRGenThreads,
379+
silSerializeWitnessTables, nullptr, StringRef());
380+
}
381+
void swift::enumeratePublicSymbols(ModuleDecl *M, StringSet &symbols,
382+
bool hasMultipleIRGenThreads,
383+
bool silSerializeWitnessTables) {
384+
enumeratePublicSymbolsAndWrite(M, nullptr, symbols, hasMultipleIRGenThreads,
385+
silSerializeWitnessTables, nullptr,
386+
StringRef());
387+
}
388+
void swift::writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
389+
bool hasMultipleIRGenThreads,
390+
bool silSerializeWitnessTables,
391+
StringRef installName) {
392+
StringSet symbols;
393+
enumeratePublicSymbolsAndWrite(M, nullptr, symbols, hasMultipleIRGenThreads,
394+
silSerializeWitnessTables, &os, installName);
432395
}

0 commit comments

Comments
 (0)