From ce698f98583112384a0e548523327da2ab5a8818 Mon Sep 17 00:00:00 2001 From: Chao-Ju Chen Date: Sat, 5 Sep 2026 13:13:39 +0800 Subject: [PATCH] build: keep the NVML -L out of LDFLAGS LDFLAGS is a user variable. A value passed on the command line, or exported by a packaging system such as dpkg-buildflags or rpm's build flags, replaces the makefile's `LDFLAGS +=` and takes the NVML -L with it, so the link fails on exactly the layouts the lookup was added for (Debian keeps libnvidia-ml.so.1 in /usr/lib//nvidia/current, which only ld.so searches). Hold the flag in NVML_LDFLAGS and put it in LIBS, which is not a user variable, so a caller-supplied LDFLAGS composes with it instead of replacing it. test_makefile_nvml_detection.sh now asserts on NVML_LDFLAGS, which also stops an exported LDFLAGS from failing `make check`. --- Makefile | 6 ++++-- tests/test_makefile_nvml_detection.sh | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2bdaa50..48d952b 100644 --- a/Makefile +++ b/Makefile @@ -16,11 +16,13 @@ CFLAGS += -Iinclude -Werror=implicit-function-declaration # driver, whereas the unversioned libnvidia-ml.so symlink only comes with # -dev packages or the CUDA toolkit. Some distributions keep it outside # ld's default search path, so use the directory registered with ldconfig. +# Keep it out of LDFLAGS: that is a user variable, and a value passed on the +# command line or exported by a packaging system would replace it and drop -L. LDCONFIG ?= /sbin/ldconfig NVML_CACHE_PATTERN = libnvidia-ml\.so\.1 \(libc6, NVML_LIBDIR ?= $(shell $(LDCONFIG) -p 2>/dev/null | awk '/$(NVML_CACHE_PATTERN)/{print $$NF; exit}' | xargs -r dirname) -LDFLAGS += $(if $(NVML_LIBDIR),-L$(NVML_LIBDIR)) -LIBS = -l:libnvidia-ml.so.1 -ljansson -lncursesw +NVML_LDFLAGS = $(if $(NVML_LIBDIR),-L$(NVML_LIBDIR)) +LIBS = $(NVML_LDFLAGS) -l:libnvidia-ml.so.1 -ljansson -lncursesw SRCDIR = src BUILDDIR = build diff --git a/tests/test_makefile_nvml_detection.sh b/tests/test_makefile_nvml_detection.sh index 7f39154..4d27ba3 100644 --- a/tests/test_makefile_nvml_detection.sh +++ b/tests/test_makefile_nvml_detection.sh @@ -17,11 +17,11 @@ chmod +x "$tmp_dir/ldconfig" # shellcheck disable=SC2016 ldflags=$(make -C "$repo_dir" --no-print-directory -s \ LDCONFIG="$tmp_dir/ldconfig" \ - --eval='print-nvml-ldflags:;@echo $(LDFLAGS)' print-nvml-ldflags) + --eval='print-nvml-ldflags:;@echo $(NVML_LDFLAGS)' print-nvml-ldflags) [ "$ldflags" = "-L/test/nvidia" ] # shellcheck disable=SC2016 override_flags=$(make -C "$repo_dir" --no-print-directory -s \ LDCONFIG=/does/not/exist NVML_LIBDIR=/vendor/lib \ - --eval='print-nvml-override:;@echo $(LDFLAGS)' print-nvml-override) + --eval='print-nvml-override:;@echo $(NVML_LDFLAGS)' print-nvml-override) [ "$override_flags" = "-L/vendor/lib" ]