Skip to content

Commit 1fda6f6

Browse files
committed
[llvm-driver] Add lld
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
1 parent 4e6b071 commit 1fda6f6

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

lld/tools/lld/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ add_lld_tool(lld
66
lld.cpp
77

88
SUPPORT_PLUGINS
9+
GENERATE_DRIVER
910
)
1011
export_executable_symbols_for_plugins(lld)
1112

12-
target_link_libraries(lld
13+
function(lld_target_link_libraries target type)
14+
target_link_libraries(${target} ${type} ${ARGN})
15+
if (TARGET obj.${target})
16+
target_link_libraries(obj.${target} ${ARGN})
17+
endif()
18+
endfunction()
19+
20+
lld_target_link_libraries(lld
1321
PRIVATE
1422
lldCommon
1523
lldCOFF

lld/tools/lld/lld.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static unsigned inTestVerbosity() {
210210
return v;
211211
}
212212

213-
int main(int argc, const char **argv) {
213+
int lld_main(int argc, char **argv) {
214214
InitLLVM x(argc, argv);
215215
sys::Process::UseANSIEscapeCodes(true);
216216

@@ -223,7 +223,8 @@ int main(int argc, const char **argv) {
223223
// Not running in lit tests, just take the shortest codepath with global
224224
// exception handling and no memory cleanup on exit.
225225
if (!inTestVerbosity())
226-
return lldMain(argc, argv, llvm::outs(), llvm::errs());
226+
return lldMain(argc, const_cast<const char **>(argv), llvm::outs(),
227+
llvm::errs());
227228

228229
Optional<int> mainRet;
229230
CrashRecoveryContext::Enable();
@@ -233,7 +234,8 @@ int main(int argc, const char **argv) {
233234
inTestOutputDisabled = (i != 1);
234235

235236
// Execute one iteration.
236-
auto r = safeLldMain(argc, argv, llvm::outs(), llvm::errs());
237+
auto r = safeLldMain(argc, const_cast<const char **>(argv), llvm::outs(),
238+
llvm::errs());
237239
if (!r.canRunAgain)
238240
exitLld(r.ret); // Exit now, can't re-execute again.
239241

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lit.llvm import llvm_config
2+
3+
if llvm_config.use_lld(required=False):
4+
config.available_features.add('lld')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# REQUIRES: lld
2+
3+
# RUN: %llvm ld.lld --help | FileCheck %s
4+
# RUN: %llvm lld -flavor ld.lld --help | FileCheck %s
5+
6+
# CHECK: supported targets: elf

llvm/tools/llvm-driver/llvm-driver.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ static int findTool(int Argc, char **Argv) {
4949

5050
StringRef Stem = sys::path::stem(ToolName);
5151
auto Is = [=](StringRef Tool) {
52-
auto I = Stem.rfind_insensitive(Tool);
53-
return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
54-
!llvm::isAlnum(Stem[I + Tool.size()]));
52+
auto IsImpl = [=](StringRef Stem) {
53+
auto I = Stem.rfind_insensitive(Tool);
54+
return I != StringRef::npos && (I + Tool.size() == Stem.size() ||
55+
!llvm::isAlnum(Stem[I + Tool.size()]));
56+
};
57+
for (StringRef S : {Stem, ToolName})
58+
if (IsImpl(S))
59+
return true;
60+
return false;
5561
};
5662

5763
#define LLVM_DRIVER_TOOL(tool, entry) \

utils/bazel/llvm-project-overlay/lld/BUILD.bazel

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5+
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
56
load("//llvm:tblgen.bzl", "gentbl")
67

78
package(
@@ -240,12 +241,21 @@ cc_library(
240241
],
241242
)
242243

244+
expand_template(
245+
name = "lld_main",
246+
out = "lld-driver.cpp",
247+
substitutions = {
248+
"@TOOL_NAME@": "lld",
249+
},
250+
template = "//llvm:cmake/modules/llvm-driver-template.cpp.in",
251+
)
252+
243253
cc_binary(
244254
name = "lld",
245255
srcs = glob([
246256
"tools/lld/*.cpp",
247257
"tools/lld/*.h",
248-
]),
258+
]) + ["lld-driver.cpp"],
249259
deps = [
250260
":COFF",
251261
":Common",

0 commit comments

Comments
 (0)