[llvm-driver] Add lld
authorAlex Brachet <abrachet@google.com>
Thu, 13 Oct 2022 19:23:25 +0000 (19:23 +0000)
committerAlex Brachet <abrachet@google.com>
Thu, 13 Oct 2022 19:23:25 +0000 (19:23 +0000)
The llvm-driver, enabled with LLVM_TOOL_LLVM_DRIVER_BUILD combines many llvm executables
into one to save overall toolchain size. This patch adds the capability for lld to be part of the
llvm-driver.

Differential Revision: https://reviews.llvm.org/D127472

lld/tools/lld/CMakeLists.txt
lld/tools/lld/lld.cpp
llvm/test/tools/llvm-driver/lit.local.cfg [new file with mode: 0644]
llvm/test/tools/llvm-driver/passthrough-lld.test [new file with mode: 0644]
llvm/tools/llvm-driver/llvm-driver.cpp
utils/bazel/llvm-project-overlay/lld/BUILD.bazel

index df48cc0..e4ae94f 100644 (file)
@@ -6,10 +6,18 @@ add_lld_tool(lld
   lld.cpp
 
   SUPPORT_PLUGINS
+  GENERATE_DRIVER
   )
 export_executable_symbols_for_plugins(lld)
 
-target_link_libraries(lld
+function(lld_target_link_libraries target type)
+  target_link_libraries(${target} ${type} ${ARGN})
+  if (TARGET obj.${target})
+    target_link_libraries(obj.${target} ${ARGN})
+  endif()
+endfunction()
+
+lld_target_link_libraries(lld
   PRIVATE
   lldCommon
   lldCOFF
index 3ac5987..b0e28d1 100644 (file)
@@ -210,7 +210,7 @@ static unsigned inTestVerbosity() {
   return v;
 }
 
-int main(int argc, const char **argv) {
+int lld_main(int argc, char **argv) {
   InitLLVM x(argc, argv);
   sys::Process::UseANSIEscapeCodes(true);
 
@@ -223,7 +223,8 @@ int main(int argc, const char **argv) {
   // Not running in lit tests, just take the shortest codepath with global
   // exception handling and no memory cleanup on exit.
   if (!inTestVerbosity())
-    return lldMain(argc, argv, llvm::outs(), llvm::errs());
+    return lldMain(argc, const_cast<const char **>(argv), llvm::outs(),
+                   llvm::errs());
 
   Optional<int> mainRet;
   CrashRecoveryContext::Enable();
@@ -233,7 +234,8 @@ int main(int argc, const char **argv) {
     inTestOutputDisabled = (i != 1);
 
     // Execute one iteration.
-    auto r = safeLldMain(argc, argv, llvm::outs(), llvm::errs());
+    auto r = safeLldMain(argc, const_cast<const char **>(argv), llvm::outs(),
+                         llvm::errs());
     if (!r.canRunAgain)
       exitLld(r.ret); // Exit now, can't re-execute again.
 
diff --git a/llvm/test/tools/llvm-driver/lit.local.cfg b/llvm/test/tools/llvm-driver/lit.local.cfg
new file mode 100644 (file)
index 0000000..940e462
--- /dev/null
@@ -0,0 +1,4 @@
+from lit.llvm import llvm_config
+
+if llvm_config.use_lld(required=False):
+    config.available_features.add('lld')
diff --git a/llvm/test/tools/llvm-driver/passthrough-lld.test b/llvm/test/tools/llvm-driver/passthrough-lld.test
new file mode 100644 (file)
index 0000000..ed54a38
--- /dev/null
@@ -0,0 +1,6 @@
+# REQUIRES: lld
+
+# RUN: %llvm ld.lld --help | FileCheck %s
+# RUN: %llvm lld -flavor ld.lld --help | FileCheck %s
+
+# CHECK: supported targets: elf
index 3f54336..33b5e32 100644 (file)
@@ -49,9 +49,15 @@ static int findTool(int Argc, char **Argv) {
 
   StringRef Stem = sys::path::stem(ToolName);
   auto Is = [=](StringRef Tool) {
-    auto I = Stem.rfind_insensitive(Tool);
-    return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
-                                    !llvm::isAlnum(Stem[I + Tool.size()]));
+    auto IsImpl = [=](StringRef Stem) {
+      auto I = Stem.rfind_insensitive(Tool);
+      return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
+                                      !llvm::isAlnum(Stem[I + Tool.size()]));
+    };
+    for (StringRef S : {Stem, ToolName})
+      if (IsImpl(S))
+        return true;
+    return false;
   };
 
 #define LLVM_DRIVER_TOOL(tool, entry)                                          \
index 098a681..a307a06 100644 (file)
@@ -2,6 +2,7 @@
 # See https://llvm.org/LICENSE.txt for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
 load("//llvm:tblgen.bzl", "gentbl")
 
 package(
@@ -240,12 +241,21 @@ cc_library(
     ],
 )
 
+expand_template(
+    name = "lld_main",
+    out = "lld-driver.cpp",
+    substitutions = {
+        "@TOOL_NAME@": "lld",
+    },
+    template = "//llvm:cmake/modules/llvm-driver-template.cpp.in",
+)
+
 cc_binary(
     name = "lld",
     srcs = glob([
         "tools/lld/*.cpp",
         "tools/lld/*.h",
-    ]),
+    ]) + ["lld-driver.cpp"],
     deps = [
         ":COFF",
         ":Common",