From d5a1d1270c898057afc5b51fb6d0f2defa89d56d Mon Sep 17 00:00:00 2001 From: James Clark Date: Wed, 15 Jul 2026 15:53:48 +0100 Subject: tools/build: Allow versioning of all LLVM tools defined in Makefile.include The version of LLVM tools can be given on the build command with LLVM=-15, but this isn't applied to all tools. For example $(CC) gets versioned, but $(CLANG) doesn't. This causes a Perf build with LTO=1 to fail with an error about mixed clang versions: ld.lld: error: libperf/core.o: Unknown attribute kind (86) (Producer: 'LLVM18.1.8' Reader: 'LLVM 15.0.7') This file has two "ifneq ($(LLVM),)" blocks adjacent to each other, so merge these blocks making it obvious that all tools should be versioned consistently and there is nothing special about each block. This also reveals that ?= and "allow-override" are used inconsistently between the blocks. "allow-override" is technically only required for builtin variables, but isn't only used on them, and doesn't do any harm if used on a non-builtin. Make them all "allow-override" for consistency. The only functional difference this will cause is if there is a file level definition of one of the variables followed by an "#include of Makefile.include" which will now overwrite. But this isn't done and in a later commit some of the duplicate definitions will be removed for good measure. There are also some other LLVM tools that are not defined here and will be moved in a later commit. Signed-off-by: James Clark Reviewed-by: Ian Rogers Acked-by: Kumar Kartikeya Dwivedi Fixes: e9c281928c24 ("kbuild: Make $(LLVM) more flexible") Signed-off-by: Namhyung Kim --- tools/scripts/Makefile.include | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'tools/scripts') diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include index 41971a68972d..7022e78208a2 100644 --- a/tools/scripts/Makefile.include +++ b/tools/scripts/Makefile.include @@ -61,10 +61,18 @@ $(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst) endif $(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) +$(call allow-override,CLANG,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) +$(call allow-override,HOSTCC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) $(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) +$(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,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)) +$(call allow-override,LLC,$(LLVM_PREFIX)llc$(LLVM_SUFFIX)) +$(call allow-override,LLVM_CONFIG,$(LLVM_PREFIX)llvm-config$(LLVM_SUFFIX)) +$(call allow-override,LLVM_OBJCOPY,$(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)) else # Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. $(call allow-override,CC,$(CROSS_COMPILE)gcc) @@ -72,26 +80,21 @@ $(call allow-override,AR,$(CROSS_COMPILE)ar) $(call allow-override,LD,$(CROSS_COMPILE)ld) $(call allow-override,CXX,$(CROSS_COMPILE)g++) $(call allow-override,STRIP,$(CROSS_COMPILE)strip) -endif - -CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) -ifneq ($(LLVM),) -HOSTAR ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX) -HOSTCC ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX) -HOSTLD ?= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX) -else -HOSTAR ?= ar -HOSTCC ?= gcc -HOSTLD ?= ld +# Host versions aren't prefixed +$(call allow-override,HOSTAR,ar) +$(call allow-override,HOSTCC,gcc) +$(call allow-override,HOSTLD,ld) + +# Some tools still require Clang, LLC and/or LLVM utils +$(call allow-override,CLANG,clang) +$(call allow-override,LLC,llc) +$(call allow-override,LLVM_CONFIG,llvm-config) +$(call allow-override,LLVM_OBJCOPY,llvm-objcopy) +$(call allow-override,LLVM_STRIP,llvm-strip) endif -# Some tools require Clang, LLC and/or LLVM utils -CLANG ?= clang -LLC ?= llc -LLVM_CONFIG ?= llvm-config -LLVM_OBJCOPY ?= llvm-objcopy -LLVM_STRIP ?= llvm-strip +CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) # Some tools require bpftool SYSTEM_BPFTOOL ?= bpftool -- cgit From 1d2af6c43bc22825a2e7ac2ea9cff32b0e2b1f22 Mon Sep 17 00:00:00 2001 From: James Clark Date: Wed, 15 Jul 2026 15:53:49 +0100 Subject: tools/build: Indent if else blocks These blocks are quite big and unreadable without indentation. Indent them. No functional changes intended. Signed-off-by: James Clark Reviewed-by: Ian Rogers Acked-by: Kumar Kartikeya Dwivedi Signed-off-by: Namhyung Kim --- tools/scripts/Makefile.include | 76 +++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'tools/scripts') diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include index 7022e78208a2..e81e5b479c56 100644 --- a/tools/scripts/Makefile.include +++ b/tools/scripts/Makefile.include @@ -52,46 +52,46 @@ define allow-override endef ifneq ($(LLVM),) -ifneq ($(filter %/,$(LLVM)),) -LLVM_PREFIX := $(LLVM) -else ifneq ($(filter -%,$(LLVM)),) -LLVM_SUFFIX := $(LLVM) -else ifneq ($(LLVM),1) -$(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst) -endif + ifneq ($(filter %/,$(LLVM)),) + LLVM_PREFIX := $(LLVM) + else ifneq ($(filter -%,$(LLVM)),) + LLVM_SUFFIX := $(LLVM) + else ifneq ($(LLVM),1) + $(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst) + endif -$(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) -$(call allow-override,CLANG,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) -$(call allow-override,HOSTCC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) -$(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) -$(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,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)) -$(call allow-override,LLC,$(LLVM_PREFIX)llc$(LLVM_SUFFIX)) -$(call allow-override,LLVM_CONFIG,$(LLVM_PREFIX)llvm-config$(LLVM_SUFFIX)) -$(call allow-override,LLVM_OBJCOPY,$(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)) + $(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) + $(call allow-override,CLANG,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) + $(call allow-override,HOSTCC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) + $(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) + $(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,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)) + $(call allow-override,LLC,$(LLVM_PREFIX)llc$(LLVM_SUFFIX)) + $(call allow-override,LLVM_CONFIG,$(LLVM_PREFIX)llvm-config$(LLVM_SUFFIX)) + $(call allow-override,LLVM_OBJCOPY,$(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)) else -# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. -$(call allow-override,CC,$(CROSS_COMPILE)gcc) -$(call allow-override,AR,$(CROSS_COMPILE)ar) -$(call allow-override,LD,$(CROSS_COMPILE)ld) -$(call allow-override,CXX,$(CROSS_COMPILE)g++) -$(call allow-override,STRIP,$(CROSS_COMPILE)strip) - -# Host versions aren't prefixed -$(call allow-override,HOSTAR,ar) -$(call allow-override,HOSTCC,gcc) -$(call allow-override,HOSTLD,ld) - -# Some tools still require Clang, LLC and/or LLVM utils -$(call allow-override,CLANG,clang) -$(call allow-override,LLC,llc) -$(call allow-override,LLVM_CONFIG,llvm-config) -$(call allow-override,LLVM_OBJCOPY,llvm-objcopy) -$(call allow-override,LLVM_STRIP,llvm-strip) + # Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. + $(call allow-override,CC,$(CROSS_COMPILE)gcc) + $(call allow-override,AR,$(CROSS_COMPILE)ar) + $(call allow-override,LD,$(CROSS_COMPILE)ld) + $(call allow-override,CXX,$(CROSS_COMPILE)g++) + $(call allow-override,STRIP,$(CROSS_COMPILE)strip) + + # Host versions aren't prefixed + $(call allow-override,HOSTAR,ar) + $(call allow-override,HOSTCC,gcc) + $(call allow-override,HOSTLD,ld) + + # Some tools still require Clang, LLC and/or LLVM utils + $(call allow-override,CLANG,clang) + $(call allow-override,LLC,llc) + $(call allow-override,LLVM_CONFIG,llvm-config) + $(call allow-override,LLVM_OBJCOPY,llvm-objcopy) + $(call allow-override,LLVM_STRIP,llvm-strip) endif CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) -- cgit From 738d0cc77e4e7f7fdc91d9f48c3b15d3cbb046ed Mon Sep 17 00:00:00 2001 From: James Clark Date: Wed, 15 Jul 2026 15:53:50 +0100 Subject: tools/build: Allow versioning LLVM readelf Documentation/kbuild/llvm.rst mentions that readelf is included in the LLVM toolchain, but it's not currently included in this block. Add it so that LLVM=... options also apply to readelf. Users in tools/ were Perf which was hardcoding it, and another was the BPF makefile. Both already include Makefile.include so convert them to use the new variables. Where readelf wasn't doing anything arch specific, use HOSTREADELF because it's more likely to be installed. Reviewed-by: Ian Rogers Signed-off-by: James Clark Acked-by: Ihor Solodrai Acked-by: Kumar Kartikeya Dwivedi Signed-off-by: Namhyung Kim --- tools/lib/bpf/Makefile | 8 ++++---- tools/perf/Makefile.perf | 1 - tools/scripts/Makefile.include | 4 ++++ 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'tools/scripts') diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index eca584fb061e..269fe21fc8da 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile @@ -115,12 +115,12 @@ PC_FILE := $(addprefix $(OUTPUT),$(PC_FILE)) TAGS_PROG := $(if $(shell which etags 2>/dev/null),etags,ctags) -GLOBAL_SYM_COUNT = $(shell readelf -s --wide $(BPF_IN_SHARED) | \ +GLOBAL_SYM_COUNT = $(shell $(HOSTREADELF) -s --wide $(BPF_IN_SHARED) | \ cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \ sed 's/\[.*\]//' | \ awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \ sort -u | wc -l) -VERSIONED_SYM_COUNT = $(shell readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \ +VERSIONED_SYM_COUNT = $(shell $(HOSTREADELF) --dyn-syms --wide $(OUTPUT)libbpf.so | \ sed 's/\[.*\]//' | \ awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}' | \ grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | sort -u | wc -l) @@ -183,12 +183,12 @@ check_abi: $(OUTPUT)libbpf.so $(VERSION_SCRIPT) "versioned symbols in $^ ($(VERSIONED_SYM_COUNT))." \ "Please make sure all LIBBPF_API symbols are" \ "versioned in $(VERSION_SCRIPT)." >&2; \ - readelf -s --wide $(BPF_IN_SHARED) | \ + $(HOSTREADELF) -s --wide $(BPF_IN_SHARED) | \ cut -d "@" -f1 | sed 's/_v[0-9]_[0-9]_[0-9].*//' | \ sed 's/\[.*\]//' | \ awk '/GLOBAL/ && /DEFAULT/ && !/UND/ {print $$NF}'| \ sort -u > $(OUTPUT)libbpf_global_syms.tmp; \ - readelf --dyn-syms --wide $(OUTPUT)libbpf.so | \ + $(HOSTREADELF) --dyn-syms --wide $(OUTPUT)libbpf.so | \ sed 's/\[.*\]//' | \ awk '/GLOBAL/ && /DEFAULT/ && !/UND|ABS/ {print $$NF}'| \ grep -Eo '[^ ]+@LIBBPF_' | cut -d@ -f1 | \ diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index 0031112c036e..d5ffc69d7374 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -215,7 +215,6 @@ FLEX ?= flex BISON ?= bison STRIP = strip AWK = awk -READELF ?= readelf # include Makefile.config by default and rule out # non-config cases diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include index e81e5b479c56..46a3872b8762 100644 --- a/tools/scripts/Makefile.include +++ b/tools/scripts/Makefile.include @@ -73,6 +73,8 @@ ifneq ($(LLVM),) $(call allow-override,LLC,$(LLVM_PREFIX)llc$(LLVM_SUFFIX)) $(call allow-override,LLVM_CONFIG,$(LLVM_PREFIX)llvm-config$(LLVM_SUFFIX)) $(call allow-override,LLVM_OBJCOPY,$(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)) + $(call allow-override,READELF,$(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)) + $(call allow-override,HOSTREADELF,$(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)) else # Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. $(call allow-override,CC,$(CROSS_COMPILE)gcc) @@ -80,11 +82,13 @@ else $(call allow-override,LD,$(CROSS_COMPILE)ld) $(call allow-override,CXX,$(CROSS_COMPILE)g++) $(call allow-override,STRIP,$(CROSS_COMPILE)strip) + $(call allow-override,READELF,$(CROSS_COMPILE)readelf) # Host versions aren't prefixed $(call allow-override,HOSTAR,ar) $(call allow-override,HOSTCC,gcc) $(call allow-override,HOSTLD,ld) + $(call allow-override,HOSTREADELF,readelf) # Some tools still require Clang, LLC and/or LLVM utils $(call allow-override,CLANG,clang) -- cgit From 380e3f23bb9b394dba6fc8d6b5ef70cbd67f0081 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Tue, 11 Aug 2026 15:28:54 -0300 Subject: tools build: Only probe the compiler at parse time when it is installed Two parse-time probes still invoke $(CC) unconditionally: - LP64 in tools/scripts/Makefile.arch, probing with $(CC) -E -x c, pulled in twice by tools/perf/Makefile.perf; - CC_NO_CLANG in tools/scripts/Makefile.include, probing with $(CC) -dM -E -x c /dev/null. In the corner case where gcc is not yet installed, the very setup the install-build-deps target, added in the next patch of this series, is meant for, these probes make even targets that never compile parse-time spew errors like: /bin/sh: 1: gcc: not found /bin/sh: 1: gcc: not found /bin/sh: 1: gcc: not found Guard both probes with 'command -v' using the first word of CC so a missing compiler is handled silently with the same result as a failing probe (CC_NO_CLANG and LP64 unset/0), and with no behavior change when the compiler is installed. Only the first word is consulted because CC may carry arguments such as 'ccache gcc', and shell implementations differ in how 'command -v' handles multiple words (dash only checks the first, bash any of them), so validating the whole CC value would silently disable both probes on some make SHELLs. Assisted-by: opencode:deepseek-v4-flash-free Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Namhyung Kim --- tools/scripts/Makefile.arch | 8 +++++++- tools/scripts/Makefile.include | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'tools/scripts') diff --git a/tools/scripts/Makefile.arch b/tools/scripts/Makefile.arch index eabfe9f411d9..ed5f008d400e 100644 --- a/tools/scripts/Makefile.arch +++ b/tools/scripts/Makefile.arch @@ -38,7 +38,13 @@ ifeq ($(ARCH),loongarch64) SRCARCH := loongarch endif -LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) +# Probe for __LP64__ only when the compiler is installed: this runs at +# parse time for every target, including ones that never compile, e.g. +# install-build-deps, and would otherwise spew "gcc: not found" when the +# compiler is not yet installed. Only the first word of CC is consulted, +# as CC may carry arguments, e.g. 'ccache gcc'. When the guard fails it +# prints nothing, leaving LP64 unset, as if the probe had failed. +LP64 := $(shell if command -v $(firstword ${CC}) >/dev/null 2>&1; then echo __LP64__ | ${CC} ${CFLAGS} -E -x c -; fi | tail -n 1) ifeq ($(LP64), 1) IS_64_BIT := 1 else diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include index 46a3872b8762..378387f62960 100644 --- a/tools/scripts/Makefile.include +++ b/tools/scripts/Makefile.include @@ -98,7 +98,13 @@ else $(call allow-override,LLVM_STRIP,llvm-strip) endif -CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) +# The CC_NO_CLANG probe also runs at parse time for targets that never +# compile, so guard it with 'command -v': when the compiler is not yet +# installed, e.g. the first install-build-deps run, it would otherwise +# spew "gcc: not found". Only the first word of CC is consulted, as CC +# may carry arguments, e.g. 'ccache gcc'. A missing compiler leaves +# CC_NO_CLANG at 1, as if the probe had failed. +CC_NO_CLANG := $(shell if command -v $(firstword $(CC)) >/dev/null 2>&1; then $(CC) -dM -E -x c /dev/null; fi | grep -Fq "__clang__"; echo $$?) # Some tools require bpftool SYSTEM_BPFTOOL ?= bpftool -- cgit