mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Merge remote-tracking branch 'origin/main' into rebranch
This commit is contained in:
@@ -2135,12 +2135,23 @@ bool ModuleDecl::registerEntryPointFile(
|
||||
}
|
||||
|
||||
void ModuleDecl::collectLinkLibraries(LinkLibraryCallback callback) const {
|
||||
// FIXME: The proper way to do this depends on the decls used.
|
||||
FORWARD(collectLinkLibraries, (callback));
|
||||
}
|
||||
bool hasSourceFile = false;
|
||||
|
||||
for (auto *file : getFiles()) {
|
||||
if (isa<SourceFile>(file)) {
|
||||
hasSourceFile = true;
|
||||
} else {
|
||||
file->collectLinkLibraries(callback);
|
||||
}
|
||||
|
||||
if (auto *synth = file->getSynthesizedFile()) {
|
||||
synth->collectLinkLibraries(callback);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasSourceFile)
|
||||
return;
|
||||
|
||||
void
|
||||
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {
|
||||
llvm::SmallDenseSet<ModuleDecl *, 32> visited;
|
||||
SmallVector<ImportedModule, 32> stack;
|
||||
|
||||
@@ -2148,17 +2159,15 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
|
||||
ModuleDecl::ImportFilterKind::Exported,
|
||||
ModuleDecl::ImportFilterKind::Default};
|
||||
|
||||
auto *topLevel = getParentModule();
|
||||
|
||||
ModuleDecl::ImportFilter topLevelFilter = filter;
|
||||
topLevelFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
|
||||
topLevelFilter |= ModuleDecl::ImportFilterKind::InternalOrBelow;
|
||||
topLevelFilter |= ModuleDecl::ImportFilterKind::PackageOnly,
|
||||
topLevelFilter |= ModuleDecl::ImportFilterKind::SPIOnly;
|
||||
topLevel->getImportedModules(stack, topLevelFilter);
|
||||
getImportedModules(stack, topLevelFilter);
|
||||
|
||||
// Make sure the top-level module is first; we want pre-order-ish traversal.
|
||||
stack.emplace_back(ImportPath::Access(), topLevel);
|
||||
stack.emplace_back(ImportPath::Access(), const_cast<ModuleDecl *>(this));
|
||||
|
||||
while (!stack.empty()) {
|
||||
auto next = stack.pop_back_val().importedModule;
|
||||
@@ -2166,7 +2175,7 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
|
||||
if (!visited.insert(next).second)
|
||||
continue;
|
||||
|
||||
if (next->getName() != getParentModule()->getName()) {
|
||||
if (next->getName() != getName()) {
|
||||
next->collectLinkLibraries(callback);
|
||||
}
|
||||
|
||||
@@ -2174,6 +2183,9 @@ SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SourceFile::collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {}
|
||||
|
||||
bool ModuleDecl::walk(ASTWalker &Walker) {
|
||||
llvm::SaveAndRestore<ASTWalker::ParentTy> SAR(Walker.Parent, this);
|
||||
for (auto SF : getFiles())
|
||||
|
||||
@@ -476,47 +476,6 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
|
||||
emitGlobalDecl(localDecl);
|
||||
for (auto *opaqueDecl : SF.getOpaqueReturnTypeDecls())
|
||||
maybeEmitOpaqueTypeDecl(opaqueDecl);
|
||||
|
||||
auto registerLinkLibrary = [this](const LinkLibrary &ll) {
|
||||
this->addLinkLibrary(ll);
|
||||
};
|
||||
|
||||
SF.collectLinkLibraries([registerLinkLibrary](LinkLibrary linkLib) {
|
||||
registerLinkLibrary(linkLib);
|
||||
});
|
||||
|
||||
if (ObjCInterop)
|
||||
registerLinkLibrary(LinkLibrary("objc", LibraryKind::Library));
|
||||
|
||||
// If C++ interop is enabled, add -lc++ on Darwin and -lstdc++ on linux.
|
||||
// Also link with C++ bridging utility module (Cxx) and C++ stdlib overlay
|
||||
// (std) if available.
|
||||
if (Context.LangOpts.EnableCXXInterop) {
|
||||
bool hasStaticCxx = false;
|
||||
bool hasStaticCxxStdlib = false;
|
||||
if (const auto *M = Context.getModuleByName("Cxx"))
|
||||
hasStaticCxx = M->isStaticLibrary();
|
||||
if (Context.LangOpts.Target.getOS() == llvm::Triple::Win32)
|
||||
if (const auto *M = Context.getModuleByName("CxxStdlib"))
|
||||
hasStaticCxxStdlib = M->isStaticLibrary();
|
||||
dependencies::registerCxxInteropLibraries(Context.LangOpts.Target,
|
||||
getSwiftModule()->getName().str(),
|
||||
hasStaticCxx,
|
||||
hasStaticCxxStdlib,
|
||||
registerLinkLibrary);
|
||||
}
|
||||
|
||||
// FIXME: It'd be better to have the driver invocation or build system that
|
||||
// executes the linker introduce these compatibility libraries, since at
|
||||
// that point we know whether we're building an executable, which is the only
|
||||
// place where the compatibility libraries take effect. For the benefit of
|
||||
// build systems that build Swift code, but don't use Swift to drive
|
||||
// the linker, we can also use autolinking to pull in the compatibility
|
||||
// libraries. This may however cause the library to get pulled in in
|
||||
// situations where it isn't useful, such as for dylibs, though this is
|
||||
// harmless aside from code size.
|
||||
if (!IRGen.Opts.UseJIT && !Context.LangOpts.hasFeature(Feature::Embedded))
|
||||
dependencies::registerBackDeployLibraries(IRGen.Opts, registerLinkLibrary);
|
||||
}
|
||||
|
||||
/// Emit all the top-level code in the synthesized file unit.
|
||||
|
||||
@@ -1192,13 +1192,11 @@ GeneratedModule IRGenRequest::evaluate(Evaluator &evaluator,
|
||||
if (auto *synthSFU = file->getSynthesizedFile()) {
|
||||
IGM.emitSynthesizedFileUnit(*synthSFU);
|
||||
}
|
||||
} else {
|
||||
file->collectLinkLibraries([&IGM](LinkLibrary LinkLib) {
|
||||
IGM.addLinkLibrary(LinkLib);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
IGM.addLinkLibraries();
|
||||
|
||||
// Okay, emit any definitions that we suddenly need.
|
||||
irgen.emitLazyDefinitions();
|
||||
|
||||
@@ -1413,7 +1411,6 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
|
||||
IRGenModule *IGM = new IRGenModule(
|
||||
irgen, std::move(targetMachine), nextSF, desc.ModuleName, *OutputIter++,
|
||||
nextSF->getFilename(), nextSF->getPrivateDiscriminator().str());
|
||||
IGMcreated = true;
|
||||
|
||||
initLLVMModule(*IGM, *SILMod);
|
||||
if (!DidRunSILCodeGenPreparePasses) {
|
||||
@@ -1424,6 +1421,11 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
|
||||
}
|
||||
|
||||
(void)layoutStringsEnabled(*IGM, /*diagnose*/ true);
|
||||
|
||||
// Only need to do this once.
|
||||
if (!IGMcreated)
|
||||
IGM->addLinkLibraries();
|
||||
IGMcreated = true;
|
||||
}
|
||||
|
||||
if (!IGMcreated) {
|
||||
@@ -1446,10 +1448,6 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
|
||||
CurrentIGMPtr IGM = irgen.getGenModule(&synthSFU->getFileUnit());
|
||||
IGM->emitSynthesizedFileUnit(*synthSFU);
|
||||
}
|
||||
} else {
|
||||
File->collectLinkLibraries([&](LinkLibrary LinkLib) {
|
||||
irgen.getPrimaryIGM()->addLinkLibrary(LinkLib);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "swift/AST/IRGenOptions.h"
|
||||
#include "swift/AST/IRGenRequests.h"
|
||||
#include "swift/AST/Module.h"
|
||||
#include "swift/AST/ModuleDependencies.h"
|
||||
#include "swift/Basic/Assertions.h"
|
||||
#include "swift/Basic/LLVMExtras.h"
|
||||
#include "swift/ClangImporter/ClangImporter.h"
|
||||
@@ -1617,6 +1618,50 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
|
||||
}
|
||||
}
|
||||
|
||||
void IRGenModule::addLinkLibraries() {
|
||||
auto registerLinkLibrary = [this](const LinkLibrary &ll) {
|
||||
this->addLinkLibrary(ll);
|
||||
};
|
||||
|
||||
getSwiftModule()->collectLinkLibraries(
|
||||
[registerLinkLibrary](LinkLibrary linkLib) {
|
||||
registerLinkLibrary(linkLib);
|
||||
});
|
||||
|
||||
if (ObjCInterop)
|
||||
registerLinkLibrary(LinkLibrary("objc", LibraryKind::Library));
|
||||
|
||||
// If C++ interop is enabled, add -lc++ on Darwin and -lstdc++ on linux.
|
||||
// Also link with C++ bridging utility module (Cxx) and C++ stdlib overlay
|
||||
// (std) if available.
|
||||
if (Context.LangOpts.EnableCXXInterop) {
|
||||
bool hasStaticCxx = false;
|
||||
bool hasStaticCxxStdlib = false;
|
||||
if (const auto *M = Context.getModuleByName("Cxx"))
|
||||
hasStaticCxx = M->isStaticLibrary();
|
||||
if (Context.LangOpts.Target.getOS() == llvm::Triple::Win32)
|
||||
if (const auto *M = Context.getModuleByName("CxxStdlib"))
|
||||
hasStaticCxxStdlib = M->isStaticLibrary();
|
||||
dependencies::registerCxxInteropLibraries(Context.LangOpts.Target,
|
||||
getSwiftModule()->getName().str(),
|
||||
hasStaticCxx,
|
||||
hasStaticCxxStdlib,
|
||||
registerLinkLibrary);
|
||||
}
|
||||
|
||||
// FIXME: It'd be better to have the driver invocation or build system that
|
||||
// executes the linker introduce these compatibility libraries, since at
|
||||
// that point we know whether we're building an executable, which is the only
|
||||
// place where the compatibility libraries take effect. For the benefit of
|
||||
// build systems that build Swift code, but don't use Swift to drive
|
||||
// the linker, we can also use autolinking to pull in the compatibility
|
||||
// libraries. This may however cause the library to get pulled in in
|
||||
// situations where it isn't useful, such as for dylibs, though this is
|
||||
// harmless aside from code size.
|
||||
if (!IRGen.Opts.UseJIT && !Context.LangOpts.hasFeature(Feature::Embedded))
|
||||
dependencies::registerBackDeployLibraries(IRGen.Opts, registerLinkLibrary);
|
||||
}
|
||||
|
||||
static bool replaceModuleFlagsEntry(llvm::LLVMContext &Ctx,
|
||||
llvm::Module &Module, StringRef EntryName,
|
||||
llvm::Module::ModFlagBehavior Behavior,
|
||||
|
||||
@@ -1564,6 +1564,8 @@ public:
|
||||
|
||||
void emitSourceFile(SourceFile &SF);
|
||||
void emitSynthesizedFileUnit(SynthesizedFileUnit &SFU);
|
||||
|
||||
void addLinkLibraries();
|
||||
void addLinkLibrary(const LinkLibrary &linkLib);
|
||||
|
||||
/// Attempt to finalize the module.
|
||||
|
||||
Reference in New Issue
Block a user