|
21 | 21 | #include "swift/AST/IRGenOptions.h"
|
22 | 22 | #include "swift/AST/IRGenRequests.h"
|
23 | 23 | #include "swift/AST/Module.h"
|
| 24 | +#include "swift/AST/ModuleDependencies.h" |
24 | 25 | #include "swift/Basic/Assertions.h"
|
25 | 26 | #include "swift/Basic/LLVMExtras.h"
|
26 | 27 | #include "swift/ClangImporter/ClangImporter.h"
|
@@ -1617,6 +1618,50 @@ void IRGenModule::addLinkLibrary(const LinkLibrary &linkLib) {
|
1617 | 1618 | }
|
1618 | 1619 | }
|
1619 | 1620 |
|
| 1621 | +void IRGenModule::addLinkLibraries() { |
| 1622 | + auto registerLinkLibrary = [this](const LinkLibrary &ll) { |
| 1623 | + this->addLinkLibrary(ll); |
| 1624 | + }; |
| 1625 | + |
| 1626 | + getSwiftModule()->collectLinkLibraries( |
| 1627 | + [registerLinkLibrary](LinkLibrary linkLib) { |
| 1628 | + registerLinkLibrary(linkLib); |
| 1629 | + }); |
| 1630 | + |
| 1631 | + if (ObjCInterop) |
| 1632 | + registerLinkLibrary(LinkLibrary("objc", LibraryKind::Library)); |
| 1633 | + |
| 1634 | + // If C++ interop is enabled, add -lc++ on Darwin and -lstdc++ on linux. |
| 1635 | + // Also link with C++ bridging utility module (Cxx) and C++ stdlib overlay |
| 1636 | + // (std) if available. |
| 1637 | + if (Context.LangOpts.EnableCXXInterop) { |
| 1638 | + bool hasStaticCxx = false; |
| 1639 | + bool hasStaticCxxStdlib = false; |
| 1640 | + if (const auto *M = Context.getModuleByName("Cxx")) |
| 1641 | + hasStaticCxx = M->isStaticLibrary(); |
| 1642 | + if (Context.LangOpts.Target.getOS() == llvm::Triple::Win32) |
| 1643 | + if (const auto *M = Context.getModuleByName("CxxStdlib")) |
| 1644 | + hasStaticCxxStdlib = M->isStaticLibrary(); |
| 1645 | + dependencies::registerCxxInteropLibraries(Context.LangOpts.Target, |
| 1646 | + getSwiftModule()->getName().str(), |
| 1647 | + hasStaticCxx, |
| 1648 | + hasStaticCxxStdlib, |
| 1649 | + registerLinkLibrary); |
| 1650 | + } |
| 1651 | + |
| 1652 | + // FIXME: It'd be better to have the driver invocation or build system that |
| 1653 | + // executes the linker introduce these compatibility libraries, since at |
| 1654 | + // that point we know whether we're building an executable, which is the only |
| 1655 | + // place where the compatibility libraries take effect. For the benefit of |
| 1656 | + // build systems that build Swift code, but don't use Swift to drive |
| 1657 | + // the linker, we can also use autolinking to pull in the compatibility |
| 1658 | + // libraries. This may however cause the library to get pulled in in |
| 1659 | + // situations where it isn't useful, such as for dylibs, though this is |
| 1660 | + // harmless aside from code size. |
| 1661 | + if (!IRGen.Opts.UseJIT && !Context.LangOpts.hasFeature(Feature::Embedded)) |
| 1662 | + dependencies::registerBackDeployLibraries(IRGen.Opts, registerLinkLibrary); |
| 1663 | +} |
| 1664 | + |
1620 | 1665 | static bool replaceModuleFlagsEntry(llvm::LLVMContext &Ctx,
|
1621 | 1666 | llvm::Module &Module, StringRef EntryName,
|
1622 | 1667 | llvm::Module::ModFlagBehavior Behavior,
|
|
0 commit comments