Skip to content

Commit e9c2819

Browse files
nathanchancemasahir0y
authored andcommitted
kbuild: Make $(LLVM) more flexible
The LLVM make variable allows a developer to quickly switch between the GNU and LLVM tools. However, it does not handle versioned binaries, such as the ones shipped by Debian, as LLVM=1 just defines the tool variables with the unversioned binaries. There was some discussion during the review of the patch that introduces LLVM=1 around versioned binaries, ultimately coming to the conclusion that developers can just add the folder that contains the unversioned binaries to their PATH, as Debian's versioned suffixed binaries are really just symlinks to the unversioned binaries in /usr/lib/llvm-#/bin: $ realpath /usr/bin/clang-14 /usr/lib/llvm-14/bin/clang $ PATH=/usr/lib/llvm-14/bin:$PATH make ... LLVM=1 However, that can be cumbersome to developers who are constantly testing series with different toolchains and versions. It is simple enough to support these versioned binaries directly in the Kbuild system by allowing the developer to specify the version suffix with LLVM=, which is shorter than the above suggestion: $ make ... LLVM=-14 It does not change the meaning of LLVM=1 (which will continue to use unversioned binaries) and it does not add too much additional complexity to the existing $(LLVM) code, while allowing developers to quickly test their series with different versions of the whole LLVM suite of tools. Some developers may build LLVM from source but not add the binaries to their PATH, as they may not want to use that toolchain systemwide. Support those developers by allowing them to supply the directory that the LLVM tools are available in, as it is no more complex to support than the version suffix change above. $ make ... LLVM=/path/to/llvm/ Update and reorder the documentation to reflect these new additions. At the same time, notate that LLVM=0 is not the same as just omitting it altogether, which has confused people in the past. Link: https://lore.kernel.org/r/[email protected]/ Link: https://lore.kernel.org/r/[email protected]/ Suggested-by: Masahiro Yamada <[email protected]> Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Nathan Chancellor <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 9fbed27 commit e9c2819

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

Documentation/kbuild/llvm.rst

+25-6
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,36 @@ example: ::
4949
LLVM Utilities
5050
--------------
5151

52-
LLVM has substitutes for GNU binutils utilities. Kbuild supports ``LLVM=1``
53-
to enable them. ::
54-
55-
make LLVM=1
56-
57-
They can be enabled individually. The full list of the parameters: ::
52+
LLVM has substitutes for GNU binutils utilities. They can be enabled individually.
53+
The full list of supported make variables::
5854

5955
make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
6056
OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
6157
HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld
6258

59+
To simplify the above command, Kbuild supports the ``LLVM`` variable::
60+
61+
make LLVM=1
62+
63+
If your LLVM tools are not available in your PATH, you can supply their
64+
location using the LLVM variable with a trailing slash::
65+
66+
make LLVM=/path/to/llvm/
67+
68+
which will use ``/path/to/llvm/clang``, ``/path/to/llvm/ld.lld``, etc.
69+
70+
If your LLVM tools have a version suffix and you want to test with that
71+
explicit version rather than the unsuffixed executables like ``LLVM=1``, you
72+
can pass the suffix using the ``LLVM`` variable::
73+
74+
make LLVM=-14
75+
76+
which will use ``clang-14``, ``ld.lld-14``, etc.
77+
78+
``LLVM=0`` is not the same as omitting ``LLVM`` altogether, it will behave like
79+
``LLVM=1``. If you only wish to use certain LLVM utilities, use their respective
80+
make variables.
81+
6382
The integrated assembler is enabled by default. You can pass ``LLVM_IAS=0`` to
6483
disable it.
6584

Makefile

+16-10
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,14 @@ HOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
424424
HOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
425425

426426
ifneq ($(LLVM),)
427-
HOSTCC = clang
428-
HOSTCXX = clang++
427+
ifneq ($(filter %/,$(LLVM)),)
428+
LLVM_PREFIX := $(LLVM)
429+
else ifneq ($(filter -%,$(LLVM)),)
430+
LLVM_SUFFIX := $(LLVM)
431+
endif
432+
433+
HOSTCC = $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
434+
HOSTCXX = $(LLVM_PREFIX)clang++$(LLVM_SUFFIX)
429435
else
430436
HOSTCC = gcc
431437
HOSTCXX = g++
@@ -444,14 +450,14 @@ KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
444450
# Make variables (CC, etc...)
445451
CPP = $(CC) -E
446452
ifneq ($(LLVM),)
447-
CC = clang
448-
LD = ld.lld
449-
AR = llvm-ar
450-
NM = llvm-nm
451-
OBJCOPY = llvm-objcopy
452-
OBJDUMP = llvm-objdump
453-
READELF = llvm-readelf
454-
STRIP = llvm-strip
453+
CC = $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
454+
LD = $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
455+
AR = $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
456+
NM = $(LLVM_PREFIX)llvm-nm$(LLVM_SUFFIX)
457+
OBJCOPY = $(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)
458+
OBJDUMP = $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX)
459+
READELF = $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
460+
STRIP = $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
455461
else
456462
CC = $(CROSS_COMPILE)gcc
457463
LD = $(CROSS_COMPILE)ld

tools/scripts/Makefile.include

+14-8
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ define allow-override
5252
endef
5353

5454
ifneq ($(LLVM),)
55-
$(call allow-override,CC,clang)
56-
$(call allow-override,AR,llvm-ar)
57-
$(call allow-override,LD,ld.lld)
58-
$(call allow-override,CXX,clang++)
59-
$(call allow-override,STRIP,llvm-strip)
55+
ifneq ($(filter %/,$(LLVM)),)
56+
LLVM_PREFIX := $(LLVM)
57+
else ifneq ($(filter -%,$(LLVM)),)
58+
LLVM_SUFFIX := $(LLVM)
59+
endif
60+
61+
$(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX))
62+
$(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX))
63+
$(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
64+
$(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX))
65+
$(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX))
6066
else
6167
# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
6268
$(call allow-override,CC,$(CROSS_COMPILE)gcc)
@@ -69,9 +75,9 @@ endif
6975
CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
7076

7177
ifneq ($(LLVM),)
72-
HOSTAR ?= llvm-ar
73-
HOSTCC ?= clang
74-
HOSTLD ?= ld.lld
78+
HOSTAR ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
79+
HOSTCC ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
80+
HOSTLD ?= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
7581
else
7682
HOSTAR ?= ar
7783
HOSTCC ?= gcc

tools/testing/selftests/lib.mk

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# This mimics the top-level Makefile. We do it explicitly here so that this
22
# Makefile can operate with or without the kbuild infrastructure.
33
ifneq ($(LLVM),)
4-
CC := clang
4+
ifneq ($(filter %/,$(LLVM)),)
5+
LLVM_PREFIX := $(LLVM)
6+
else ifneq ($(filter -%,$(LLVM)),)
7+
LLVM_SUFFIX := $(LLVM)
8+
endif
9+
10+
CC := $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
511
else
612
CC := $(CROSS_COMPILE)gcc
713
endif

0 commit comments

Comments
 (0)