Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: Add environment variables for userprogs flags

Allow additional arguments be passed to userprogs compilation.
Reproducible clang builds need to provide a sysroot and gcc path to
ensure the same toolchain is used across hosts. KCFLAGS is not currently
used for any user programs compilation, so add new USERCFLAGS and
USERLDFLAGS which serves similar purpose as HOSTCFLAGS/HOSTLDFLAGS.

Clang might detect GCC installation on hosts which have it installed
to a default location in /. With addition of these environment
variables, you can specify flags such as:

$ make USERCFLAGS=--sysroot=/path/to/sysroot

This can also be used to specify different sysroots such as musl or
bionic which may be installed on the host in paths that the compiler
may not search by default.

Signed-off-by: Elliot Berman <quic_eberman@quicinc.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Fangrui Song <maskray@google.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

authored by

Elliot Berman and committed by
Masahiro Yamada
f67695c9 a5575df5

+26 -8
+11
Documentation/kbuild/kbuild.rst
··· 77 77 ---------- 78 78 Additional libraries to link against when building host programs. 79 79 80 + .. _userkbuildflags: 81 + 82 + USERCFLAGS 83 + ---------- 84 + Additional options used for $(CC) when compiling userprogs. 85 + 86 + USERLDFLAGS 87 + ----------- 88 + Additional options used for $(LD) when linking userprogs. userprogs are linked 89 + with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable. 90 + 80 91 KBUILD_KCONFIG 81 92 -------------- 82 93 Set the top-level Kconfig file to the value of this environment
+2
Documentation/kbuild/makefiles.rst
··· 982 982 983 983 When linking bpfilter_umh, it will be passed the extra option -static. 984 984 985 + From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used. 986 + 985 987 5.4 When userspace programs are actually built 986 988 ---------------------------------------------- 987 989
+6 -4
Makefile
··· 431 431 HOSTCXX = g++ 432 432 endif 433 433 434 - export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \ 435 - -O2 -fomit-frame-pointer -std=gnu89 436 - export KBUILD_USERLDFLAGS := 434 + KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \ 435 + -O2 -fomit-frame-pointer -std=gnu89 436 + KBUILD_USERCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS) 437 + KBUILD_USERLDFLAGS := $(USERLDFLAGS) 437 438 438 - KBUILD_HOSTCFLAGS := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS) 439 + KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS) 439 440 KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS) 440 441 KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS) 441 442 KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS) ··· 531 530 export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX 532 531 export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD 533 532 export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE 533 + export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS 534 534 535 535 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS 536 536 export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+4 -4
init/Kconfig
··· 62 62 63 63 config CC_CAN_LINK 64 64 bool 65 - default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT 66 - default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag)) 65 + default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT 66 + default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag)) 67 67 68 68 config CC_CAN_LINK_STATIC 69 69 bool 70 - default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT 71 - default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static) 70 + default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT 71 + default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static) 72 72 73 73 config CC_HAS_ASM_GOTO 74 74 def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
+3
usr/include/Makefile
··· 12 12 # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64. 13 13 UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS)) 14 14 15 + # USERCFLAGS might contain sysroot location for CC. 16 + UAPI_CFLAGS += $(USERCFLAGS) 17 + 15 18 override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include 16 19 17 20 # The following are excluded for now because they fail to build.