Building with LLVM=... could result in a different version of lld being
used than the main toolchain for liburandom_read.so because it's
hardcoded to "lld" in this makefile.

Make it consistent with the rest of the LLVM toolchain by adding an LLD
variable to Makefile.include. Keep the fallback for other architectures
in tools/testing/selftests/bpf/Makefile as it seems like it's something
specific to this make rule and shouldn't be global.

Clang only accepts either a full path to "/x/x/ld.lld" or "lld-15" style
inputs to "-fuse-ld=", so the only way to make it work with both
prefixed and postfixed paths is to always take the full path. Also I
don't think the original use of "lld" over "ld.lld" was significant as
this is always a linux build, so that can be changed to make it work in
both cases.

Add a warning for an empty USE_LD as "shell command -v" can return empty
and GCC won't complain.

Signed-off-by: James Clark <[email protected]>
---
 tools/scripts/Makefile.include       |  2 ++
 tools/testing/selftests/bpf/Makefile | 12 ++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
index 
46a3872b87624a7bf475d9b42a3e471f36789dfd..92056d3e330eb6596250635b6d932b3b73e7f45f
 100644
--- a/tools/scripts/Makefile.include
+++ b/tools/scripts/Makefile.include
@@ -67,6 +67,7 @@ ifneq ($(LLVM),)
   $(call allow-override,HOSTAR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX))
   $(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
   $(call allow-override,HOSTLD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
+  $(call allow-override,LLD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
   $(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX))
   $(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX))
   $(call allow-override,LLVM_STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX))
@@ -93,6 +94,7 @@ else
   # Some tools still require Clang, LLC and/or LLVM utils
   $(call allow-override,CLANG,clang)
   $(call allow-override,LLC,llc)
+  $(call allow-override,LLD,ld.lld)
   $(call allow-override,LLVM_CONFIG,llvm-config)
   $(call allow-override,LLVM_OBJCOPY,llvm-objcopy)
   $(call allow-override,LLVM_STRIP,llvm-strip)
diff --git a/tools/testing/selftests/bpf/Makefile 
b/tools/testing/selftests/bpf/Makefile
index 
b642ee489ea64046a9b19852780c3a6f009cbd82..d829979861eeabd33272a0d6eca82e7066eabfd7
 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -264,30 +264,34 @@ $(OUTPUT)/%:%.c
 
 # LLVM's ld.lld doesn't support all the architectures, so use it only on x86
 ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 riscv))
-LLD := lld
+USE_LD := $(shell command -v $(LLD))
+USE_LD_ERR = linker '$(LLD)' not found - add it to PATH, or pass LLVM=/path/ 
or LLD=/abs/ld.lld)
 else
-LLD := $(shell command -v $(LD))
+USE_LD := $(shell command -v $(LD))
+USE_LD_ERR = linker '$(LD)' not found - add it to PATH, or pass LLVM=/path/ or 
LD=/abs/ld)
 endif
 
 # Filter out -static for liburandom_read.so and its dependent targets so that 
static builds
 # do not fail. Static builds leave urandom_read relying on system-wide shared 
libraries.
 $(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c 
liburandom_read.map
        $(call msg,LIB,,$@)
+       $(Q)test -n "$(USE_LD)" || { echo "$@: $(USE_LD_ERR)" >&2; exit 1; }
        $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
                     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) \
                     $(filter %.c,$^) $(filter-out -static,$(LDLIBS)) \
                     -Wno-unused-command-line-argument \
-                    -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
+                    -fuse-ld=$(USE_LD) -Wl,-znoseparate-code 
-Wl,--build-id=sha1 \
                     -Wl,--version-script=liburandom_read.map \
                     -fPIC -shared -o $@
 
 $(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c 
$(OUTPUT)/liburandom_read.so
        $(call msg,BINARY,,$@)
+       $(Q)test -n "$(USE_LD)" || { echo "$@: $(USE_LD_ERR)" >&2; exit 1; }
        $(Q)$(CLANG) $(CLANG_TARGET_ARCH) \
                     $(filter-out -static,$(COMMON_CFLAGS) $(LDFLAGS)) $(filter 
%.c,$^) \
                     -Wno-unused-command-line-argument \
                     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT) 
\
-                    -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
+                    -fuse-ld=$(USE_LD) -Wl,-znoseparate-code 
-Wl,--build-id=sha1 \
                     -Wl,-rpath=. -o $@
 
 $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c

-- 
2.34.1


Reply via email to