From 3679436eafd541e6273441813ba456e7df9258e8 Mon Sep 17 00:00:00 2001 From: LunaTheFoxgirl Date: Wed, 20 Nov 2024 03:50:41 +0100 Subject: [PATCH] Add objc linker option --- driver/linker-gcc.cpp | 13 +++++++++++++ driver/linker.cpp | 8 ++++++++ driver/linker.h | 5 +++++ 3 files changed, 26 insertions(+) diff --git a/driver/linker-gcc.cpp b/driver/linker-gcc.cpp index a0aad16a972..9ac3691c4e7 100644 --- a/driver/linker-gcc.cpp +++ b/driver/linker-gcc.cpp @@ -48,6 +48,10 @@ static llvm::cl::opt linkNoCpp( "link-no-cpp", llvm::cl::ZeroOrMore, llvm::cl::Hidden, llvm::cl::desc("Disable automatic linking with the C++ standard library.")); +static llvm::cl::opt linkNoObjc( + "link-no-objc", llvm::cl::ZeroOrMore, llvm::cl::Hidden, + llvm::cl::desc("Disable automatic linking with the Objective-C runtime library.")); + ////////////////////////////////////////////////////////////////////////////// namespace { @@ -72,6 +76,7 @@ class ArgsBuilder { virtual void addXRayLinkFlags(const llvm::Triple &triple); virtual bool addCompilerRTArchiveLinkFlags(llvm::StringRef baseName, const llvm::Triple &triple); + virtual void addObjcStdlibLinkFlags(const llvm::Triple &triple); virtual void addLinker(); virtual void addUserSwitches(); @@ -467,6 +472,13 @@ void ArgsBuilder::addCppStdlibLinkFlags(const llvm::Triple &triple) { } } +void ArgsBuilder::addObjcStdlibLinkFlags(const llvm::Triple &triple) { + if (linkNoObjc) + return; + + args.push_back(("-l"+getObjcLibName()).str()); +} + // Adds all required link flags for PGO. void ArgsBuilder::addProfileRuntimeLinkFlags(const llvm::Triple &triple) { const auto searchPaths = @@ -710,6 +722,7 @@ void ArgsBuilder::addDefaultPlatformLibs() { addSoname = true; args.push_back("-lpthread"); args.push_back("-lm"); + this->addObjcStdlibLinkFlags(triple); break; case llvm::Triple::Solaris: diff --git a/driver/linker.cpp b/driver/linker.cpp index bd309e83b71..b593994b063 100644 --- a/driver/linker.cpp +++ b/driver/linker.cpp @@ -257,6 +257,14 @@ llvm::StringRef getMscrtLibName(const bool *useInternalToolchain) { ////////////////////////////////////////////////////////////////////////////// +llvm::StringRef getObjcLibName() { + // TODO: Support scanning for and loading alternate + // objective-c runtimes like the GNUStep runtime? + return "objc"; +} + +////////////////////////////////////////////////////////////////////////////// + /// Insert an LLVM bitcode file into the module static void insertBitcodeIntoModule(const char *bcFile, llvm::Module &M, llvm::LLVMContext &Context) { diff --git a/driver/linker.h b/driver/linker.h index 28362d86526..78478af0f2e 100644 --- a/driver/linker.h +++ b/driver/linker.h @@ -58,6 +58,11 @@ llvm::StringRef getExplicitMscrtLibName(); */ llvm::StringRef getMscrtLibName(const bool *useInternalToolchain = nullptr); +/** + * Returns the name of the Objective-C runtime library to link with. + */ +llvm::StringRef getObjcLibName(); + /** * Inserts bitcode files passed on the commandline into a module. */