Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit f67695c

Browse files
eberman-quicmasahir0y
authored andcommitted
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>
1 parent a5575df commit f67695c

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

Documentation/kbuild/kbuild.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ HOSTLDLIBS
7777
----------
7878
Additional libraries to link against when building host programs.
7979

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+
8091
KBUILD_KCONFIG
8192
--------------
8293
Set the top-level Kconfig file to the value of this environment

Documentation/kbuild/makefiles.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,8 @@ The syntax is quite similar. The difference is to use "userprogs" instead of
982982

983983
When linking bpfilter_umh, it will be passed the extra option -static.
984984

985+
From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
986+
985987
5.4 When userspace programs are actually built
986988
----------------------------------------------
987989

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,12 @@ HOSTCC = gcc
431431
HOSTCXX = g++
432432
endif
433433

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)
437438

438-
KBUILD_HOSTCFLAGS := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
439+
KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
439440
KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
440441
KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
441442
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
@@ -530,6 +531,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
530531
export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
531532
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
532533
export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
534+
export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
533535

534536
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
535537
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE

init/Kconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ config LLD_VERSION
6262

6363
config CC_CAN_LINK
6464
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))
6767

6868
config CC_CAN_LINK_STATIC
6969
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)
7272

7373
config CC_HAS_ASM_GOTO
7474
def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))

usr/include/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
1212
# It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
1313
UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
1414

15+
# USERCFLAGS might contain sysroot location for CC.
16+
UAPI_CFLAGS += $(USERCFLAGS)
17+
1518
override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
1619

1720
# The following are excluded for now because they fail to build.

0 commit comments

Comments
 (0)