diff mbox series

[v3] kbuild: Add environment variables for userprogs flags

Message ID 20220201213542.2808035-1-quic_eberman@quicinc.com (mailing list archive)
State New, archived
Headers show
Series [v3] kbuild: Add environment variables for userprogs flags | expand

Commit Message

Elliot Berman Feb. 1, 2022, 9:35 p.m. UTC
Allow additional arguments be passed to userprogs compilation.
Reproducible clang builds need to provide a sysroot and gcc path to
ensure 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 13+ 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>
---

Changes since v2:
 - Incorporated Nick's suggestions:
   - Addressed docs and commit text comments
   - Introduced KBUILD_USERHOSTCFLAGS as suggested

 Documentation/kbuild/kbuild.rst    | 11 +++++++++++
 Documentation/kbuild/makefiles.rst |  2 ++
 Makefile                           | 10 ++++++----
 init/Kconfig                       |  8 ++++----
 usr/include/Makefile               |  3 +++
 5 files changed, 26 insertions(+), 8 deletions(-)

Comments

Nick Desaulniers Feb. 1, 2022, 9:38 p.m. UTC | #1
On Tue, Feb 1, 2022 at 1:36 PM Elliot Berman <quic_eberman@quicinc.com> wrote:
>
> Allow additional arguments be passed to userprogs compilation.
> Reproducible clang builds need to provide a sysroot and gcc path to
> ensure same toolchain is used across hosts. KCFLAGS is not currently

^ ensure the same (maybe Masahiro can fix that up locally when applying)

> used for any user programs compilation, so add new USERCFLAGS and
> USERLDFLAGS which serves similar purpose as HOSTCFLAGS/HOSTLDFLAGS.
>
> Clang 13+ 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>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>
> Changes since v2:
>  - Incorporated Nick's suggestions:
>    - Addressed docs and commit text comments
>    - Introduced KBUILD_USERHOSTCFLAGS as suggested
>
>  Documentation/kbuild/kbuild.rst    | 11 +++++++++++
>  Documentation/kbuild/makefiles.rst |  2 ++
>  Makefile                           | 10 ++++++----
>  init/Kconfig                       |  8 ++++----
>  usr/include/Makefile               |  3 +++
>  5 files changed, 26 insertions(+), 8 deletions(-)
>
> diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> index 2d1fc03d346e..ef19b9c13523 100644
> --- a/Documentation/kbuild/kbuild.rst
> +++ b/Documentation/kbuild/kbuild.rst
> @@ -77,6 +77,17 @@ HOSTLDLIBS
>  ----------
>  Additional libraries to link against when building host programs.
>
> +.. _userkbuildflags:
> +
> +USERCFLAGS
> +----------
> +Additional options used for $(CC) when compiling userprogs.
> +
> +USERLDFLAGS
> +-----------
> +Additional options used for $(LD) when linking userprogs. userprogs are linked
> +with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable.
> +
>  KBUILD_KCONFIG
>  --------------
>  Set the top-level Kconfig file to the value of this environment
> diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
> index b008b90b92c9..11a296e52d68 100644
> --- a/Documentation/kbuild/makefiles.rst
> +++ b/Documentation/kbuild/makefiles.rst
> @@ -982,6 +982,8 @@ The syntax is quite similar. The difference is to use "userprogs" instead of
>
>         When linking bpfilter_umh, it will be passed the extra option -static.
>
> +       From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
> +
>  5.4 When userspace programs are actually built
>  ----------------------------------------------
>
> diff --git a/Makefile b/Makefile
> index 45278d508d81..1d0172449355 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -431,11 +431,12 @@ HOSTCC    = gcc
>  HOSTCXX        = g++
>  endif
>
> -export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> -                             -O2 -fomit-frame-pointer -std=gnu89
> -export KBUILD_USERLDFLAGS :=
> +KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> +                        -O2 -fomit-frame-pointer -std=gnu89
> +KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
> +KBUILD_USERLDFLAGS := $(USERLDFLAGS)
>
> -KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> +KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
>  KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
>  KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
>  KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
> @@ -530,6 +531,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
>  export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
>  export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
>  export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
> +export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
>
>  export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
>  export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
> diff --git a/init/Kconfig b/init/Kconfig
> index f2ae41e6717f..164706c38e8b 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -62,13 +62,13 @@ config LLD_VERSION
>
>  config CC_CAN_LINK
>         bool
> -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT
> -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag))
> +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT
> +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag))
>
>  config CC_CAN_LINK_STATIC
>         bool
> -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT
> -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static)
> +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT
> +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
>
>  config CC_HAS_ASM_GOTO
>         def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
> diff --git a/usr/include/Makefile b/usr/include/Makefile
> index 1c2ae1368079..0322e567dc1e 100644
> --- a/usr/include/Makefile
> +++ b/usr/include/Makefile
> @@ -12,6 +12,9 @@ UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
>  # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
>  UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
>
> +# USERCFLAGS might contain sysroot location for CC.
> +UAPI_CFLAGS += $(USERCFLAGS)
> +
>  override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
>
>  # The following are excluded for now because they fail to build.
> --
> 2.25.1
>
Fangrui Song Feb. 1, 2022, 10:03 p.m. UTC | #2
On Tue, Feb 1, 2022 at 1:38 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
>
> On Tue, Feb 1, 2022 at 1:36 PM Elliot Berman <quic_eberman@quicinc.com> wrote:
> >
> > Allow additional arguments be passed to userprogs compilation.
> > Reproducible clang builds need to provide a sysroot and gcc path to
> > ensure same toolchain is used across hosts. KCFLAGS is not currently
>
> ^ ensure the same (maybe Masahiro can fix that up locally when applying)
>
> > used for any user programs compilation, so add new USERCFLAGS and
> > USERLDFLAGS which serves similar purpose as HOSTCFLAGS/HOSTLDFLAGS.
> >
> > Clang 13+ 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:

s/Clang 13+/Clang/.

Clang has been detecting GCC installation for a long time.
Clang 13 just improved some detection, but may not be worth mentioning
in the message.

> > $ 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>
>
> Thanks for the patch!
> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

LGTM.

Reviewed-by: Fangrui Song <maskray@google.com>

> > ---
> >
> > Changes since v2:
> >  - Incorporated Nick's suggestions:
> >    - Addressed docs and commit text comments
> >    - Introduced KBUILD_USERHOSTCFLAGS as suggested
> >
> >  Documentation/kbuild/kbuild.rst    | 11 +++++++++++
> >  Documentation/kbuild/makefiles.rst |  2 ++
> >  Makefile                           | 10 ++++++----
> >  init/Kconfig                       |  8 ++++----
> >  usr/include/Makefile               |  3 +++
> >  5 files changed, 26 insertions(+), 8 deletions(-)
> >
> > diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> > index 2d1fc03d346e..ef19b9c13523 100644
> > --- a/Documentation/kbuild/kbuild.rst
> > +++ b/Documentation/kbuild/kbuild.rst
> > @@ -77,6 +77,17 @@ HOSTLDLIBS
> >  ----------
> >  Additional libraries to link against when building host programs.
> >
> > +.. _userkbuildflags:
> > +
> > +USERCFLAGS
> > +----------
> > +Additional options used for $(CC) when compiling userprogs.
> > +
> > +USERLDFLAGS
> > +-----------
> > +Additional options used for $(LD) when linking userprogs. userprogs are linked
> > +with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable.
> > +
> >  KBUILD_KCONFIG
> >  --------------
> >  Set the top-level Kconfig file to the value of this environment
> > diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
> > index b008b90b92c9..11a296e52d68 100644
> > --- a/Documentation/kbuild/makefiles.rst
> > +++ b/Documentation/kbuild/makefiles.rst
> > @@ -982,6 +982,8 @@ The syntax is quite similar. The difference is to use "userprogs" instead of
> >
> >         When linking bpfilter_umh, it will be passed the extra option -static.
> >
> > +       From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
> > +
> >  5.4 When userspace programs are actually built
> >  ----------------------------------------------
> >
> > diff --git a/Makefile b/Makefile
> > index 45278d508d81..1d0172449355 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -431,11 +431,12 @@ HOSTCC    = gcc
> >  HOSTCXX        = g++
> >  endif
> >
> > -export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > -                             -O2 -fomit-frame-pointer -std=gnu89
> > -export KBUILD_USERLDFLAGS :=
> > +KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > +                        -O2 -fomit-frame-pointer -std=gnu89
> > +KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
> > +KBUILD_USERLDFLAGS := $(USERLDFLAGS)
> >
> > -KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> > +KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> >  KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
> >  KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
> >  KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
> > @@ -530,6 +531,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
> >  export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
> >  export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
> >  export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
> > +export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
> >
> >  export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
> >  export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
> > diff --git a/init/Kconfig b/init/Kconfig
> > index f2ae41e6717f..164706c38e8b 100644
> > --- a/init/Kconfig
> > +++ b/init/Kconfig
> > @@ -62,13 +62,13 @@ config LLD_VERSION
> >
> >  config CC_CAN_LINK
> >         bool
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag))
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag))
> >
> >  config CC_CAN_LINK_STATIC
> >         bool
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static)
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
> >
> >  config CC_HAS_ASM_GOTO
> >         def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
> > diff --git a/usr/include/Makefile b/usr/include/Makefile
> > index 1c2ae1368079..0322e567dc1e 100644
> > --- a/usr/include/Makefile
> > +++ b/usr/include/Makefile
> > @@ -12,6 +12,9 @@ UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
> >  # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
> >  UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
> >
> > +# USERCFLAGS might contain sysroot location for CC.
> > +UAPI_CFLAGS += $(USERCFLAGS)
> > +
> >  override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
> >
> >  # The following are excluded for now because they fail to build.
> > --
> > 2.25.1
> >
>
>
> --
> Thanks,
> ~Nick Desaulniers
Nick Desaulniers Feb. 8, 2022, 10:53 p.m. UTC | #3
On Tue, Feb 1, 2022 at 1:38 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
>
> On Tue, Feb 1, 2022 at 1:36 PM Elliot Berman <quic_eberman@quicinc.com> wrote:
> >
> > Allow additional arguments be passed to userprogs compilation.
> > Reproducible clang builds need to provide a sysroot and gcc path to
> > ensure same toolchain is used across hosts. KCFLAGS is not currently
>
> ^ ensure the same (maybe Masahiro can fix that up locally when applying)

Masahiro,
What are your thoughts on this patch?  I think this would help Android
define a musl or bionic sysroot for CC_CAN_LINK and ultimately
UAPI_HEADER_TEST support.

>
> > used for any user programs compilation, so add new USERCFLAGS and
> > USERLDFLAGS which serves similar purpose as HOSTCFLAGS/HOSTLDFLAGS.
> >
> > Clang 13+ 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>
>
> Thanks for the patch!
> Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
>
> > ---
> >
> > Changes since v2:
> >  - Incorporated Nick's suggestions:
> >    - Addressed docs and commit text comments
> >    - Introduced KBUILD_USERHOSTCFLAGS as suggested
> >
> >  Documentation/kbuild/kbuild.rst    | 11 +++++++++++
> >  Documentation/kbuild/makefiles.rst |  2 ++
> >  Makefile                           | 10 ++++++----
> >  init/Kconfig                       |  8 ++++----
> >  usr/include/Makefile               |  3 +++
> >  5 files changed, 26 insertions(+), 8 deletions(-)
> >
> > diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> > index 2d1fc03d346e..ef19b9c13523 100644
> > --- a/Documentation/kbuild/kbuild.rst
> > +++ b/Documentation/kbuild/kbuild.rst
> > @@ -77,6 +77,17 @@ HOSTLDLIBS
> >  ----------
> >  Additional libraries to link against when building host programs.
> >
> > +.. _userkbuildflags:
> > +
> > +USERCFLAGS
> > +----------
> > +Additional options used for $(CC) when compiling userprogs.
> > +
> > +USERLDFLAGS
> > +-----------
> > +Additional options used for $(LD) when linking userprogs. userprogs are linked
> > +with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable.
> > +
> >  KBUILD_KCONFIG
> >  --------------
> >  Set the top-level Kconfig file to the value of this environment
> > diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
> > index b008b90b92c9..11a296e52d68 100644
> > --- a/Documentation/kbuild/makefiles.rst
> > +++ b/Documentation/kbuild/makefiles.rst
> > @@ -982,6 +982,8 @@ The syntax is quite similar. The difference is to use "userprogs" instead of
> >
> >         When linking bpfilter_umh, it will be passed the extra option -static.
> >
> > +       From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
> > +
> >  5.4 When userspace programs are actually built
> >  ----------------------------------------------
> >
> > diff --git a/Makefile b/Makefile
> > index 45278d508d81..1d0172449355 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -431,11 +431,12 @@ HOSTCC    = gcc
> >  HOSTCXX        = g++
> >  endif
> >
> > -export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > -                             -O2 -fomit-frame-pointer -std=gnu89
> > -export KBUILD_USERLDFLAGS :=
> > +KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > +                        -O2 -fomit-frame-pointer -std=gnu89
> > +KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
> > +KBUILD_USERLDFLAGS := $(USERLDFLAGS)
> >
> > -KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> > +KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> >  KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
> >  KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
> >  KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
> > @@ -530,6 +531,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
> >  export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
> >  export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
> >  export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
> > +export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
> >
> >  export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
> >  export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
> > diff --git a/init/Kconfig b/init/Kconfig
> > index f2ae41e6717f..164706c38e8b 100644
> > --- a/init/Kconfig
> > +++ b/init/Kconfig
> > @@ -62,13 +62,13 @@ config LLD_VERSION
> >
> >  config CC_CAN_LINK
> >         bool
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag))
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag))
> >
> >  config CC_CAN_LINK_STATIC
> >         bool
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT
> > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static)
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT
> > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
> >
> >  config CC_HAS_ASM_GOTO
> >         def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
> > diff --git a/usr/include/Makefile b/usr/include/Makefile
> > index 1c2ae1368079..0322e567dc1e 100644
> > --- a/usr/include/Makefile
> > +++ b/usr/include/Makefile
> > @@ -12,6 +12,9 @@ UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
> >  # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
> >  UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
> >
> > +# USERCFLAGS might contain sysroot location for CC.
> > +UAPI_CFLAGS += $(USERCFLAGS)
> > +
> >  override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
> >
> >  # The following are excluded for now because they fail to build.
> > --
> > 2.25.1
> >
>
>
> --
> Thanks,
> ~Nick Desaulniers
Masahiro Yamada Feb. 10, 2022, 12:29 a.m. UTC | #4
On Wed, Feb 9, 2022 at 7:53 AM Nick Desaulniers <ndesaulniers@google.com> wrote:
>
> On Tue, Feb 1, 2022 at 1:38 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
> >
> > On Tue, Feb 1, 2022 at 1:36 PM Elliot Berman <quic_eberman@quicinc.com> wrote:
> > >
> > > Allow additional arguments be passed to userprogs compilation.
> > > Reproducible clang builds need to provide a sysroot and gcc path to
> > > ensure same toolchain is used across hosts. KCFLAGS is not currently
> >
> > ^ ensure the same (maybe Masahiro can fix that up locally when applying)
>
> Masahiro,
> What are your thoughts on this patch?  I think this would help Android
> define a musl or bionic sysroot for CC_CAN_LINK and ultimately
> UAPI_HEADER_TEST support.

Sorry for delay.


Applied with two fixups in the commit log:

 ensure same -> ensure the same
 Clang 13+ might detect  ->  Clang might detect













> >
> > > used for any user programs compilation, so add new USERCFLAGS and
> > > USERLDFLAGS which serves similar purpose as HOSTCFLAGS/HOSTLDFLAGS.
> > >
> > > Clang 13+ 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>
> >
> > Thanks for the patch!
> > Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
> >
> > > ---
> > >
> > > Changes since v2:
> > >  - Incorporated Nick's suggestions:
> > >    - Addressed docs and commit text comments
> > >    - Introduced KBUILD_USERHOSTCFLAGS as suggested
> > >
> > >  Documentation/kbuild/kbuild.rst    | 11 +++++++++++
> > >  Documentation/kbuild/makefiles.rst |  2 ++
> > >  Makefile                           | 10 ++++++----
> > >  init/Kconfig                       |  8 ++++----
> > >  usr/include/Makefile               |  3 +++
> > >  5 files changed, 26 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
> > > index 2d1fc03d346e..ef19b9c13523 100644
> > > --- a/Documentation/kbuild/kbuild.rst
> > > +++ b/Documentation/kbuild/kbuild.rst
> > > @@ -77,6 +77,17 @@ HOSTLDLIBS
> > >  ----------
> > >  Additional libraries to link against when building host programs.
> > >
> > > +.. _userkbuildflags:
> > > +
> > > +USERCFLAGS
> > > +----------
> > > +Additional options used for $(CC) when compiling userprogs.
> > > +
> > > +USERLDFLAGS
> > > +-----------
> > > +Additional options used for $(LD) when linking userprogs. userprogs are linked
> > > +with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable.
> > > +
> > >  KBUILD_KCONFIG
> > >  --------------
> > >  Set the top-level Kconfig file to the value of this environment
> > > diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
> > > index b008b90b92c9..11a296e52d68 100644
> > > --- a/Documentation/kbuild/makefiles.rst
> > > +++ b/Documentation/kbuild/makefiles.rst
> > > @@ -982,6 +982,8 @@ The syntax is quite similar. The difference is to use "userprogs" instead of
> > >
> > >         When linking bpfilter_umh, it will be passed the extra option -static.
> > >
> > > +       From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
> > > +
> > >  5.4 When userspace programs are actually built
> > >  ----------------------------------------------
> > >
> > > diff --git a/Makefile b/Makefile
> > > index 45278d508d81..1d0172449355 100644
> > > --- a/Makefile
> > > +++ b/Makefile
> > > @@ -431,11 +431,12 @@ HOSTCC    = gcc
> > >  HOSTCXX        = g++
> > >  endif
> > >
> > > -export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > > -                             -O2 -fomit-frame-pointer -std=gnu89
> > > -export KBUILD_USERLDFLAGS :=
> > > +KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
> > > +                        -O2 -fomit-frame-pointer -std=gnu89
> > > +KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
> > > +KBUILD_USERLDFLAGS := $(USERLDFLAGS)
> > >
> > > -KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> > > +KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
> > >  KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
> > >  KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
> > >  KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
> > > @@ -530,6 +531,7 @@ export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
> > >  export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
> > >  export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
> > >  export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
> > > +export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
> > >
> > >  export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
> > >  export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
> > > diff --git a/init/Kconfig b/init/Kconfig
> > > index f2ae41e6717f..164706c38e8b 100644
> > > --- a/init/Kconfig
> > > +++ b/init/Kconfig
> > > @@ -62,13 +62,13 @@ config LLD_VERSION
> > >
> > >  config CC_CAN_LINK
> > >         bool
> > > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT
> > > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag))
> > > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT
> > > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag))
> > >
> > >  config CC_CAN_LINK_STATIC
> > >         bool
> > > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT
> > > -       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static)
> > > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT
> > > +       default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
> > >
> > >  config CC_HAS_ASM_GOTO
> > >         def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
> > > diff --git a/usr/include/Makefile b/usr/include/Makefile
> > > index 1c2ae1368079..0322e567dc1e 100644
> > > --- a/usr/include/Makefile
> > > +++ b/usr/include/Makefile
> > > @@ -12,6 +12,9 @@ UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
> > >  # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
> > >  UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
> > >
> > > +# USERCFLAGS might contain sysroot location for CC.
> > > +UAPI_CFLAGS += $(USERCFLAGS)
> > > +
> > >  override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
> > >
> > >  # The following are excluded for now because they fail to build.
> > > --
> > > 2.25.1
> > >
> >
> >
> > --
> > Thanks,
> > ~Nick Desaulniers
>
>
>
> --
> Thanks,
> ~Nick Desaulniers
diff mbox series

Patch

diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
index 2d1fc03d346e..ef19b9c13523 100644
--- a/Documentation/kbuild/kbuild.rst
+++ b/Documentation/kbuild/kbuild.rst
@@ -77,6 +77,17 @@  HOSTLDLIBS
 ----------
 Additional libraries to link against when building host programs.
 
+.. _userkbuildflags:
+
+USERCFLAGS
+----------
+Additional options used for $(CC) when compiling userprogs.
+
+USERLDFLAGS
+-----------
+Additional options used for $(LD) when linking userprogs. userprogs are linked
+with CC, so $(USERLDFLAGS) should include "-Wl," prefix as applicable.
+
 KBUILD_KCONFIG
 --------------
 Set the top-level Kconfig file to the value of this environment
diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
index b008b90b92c9..11a296e52d68 100644
--- a/Documentation/kbuild/makefiles.rst
+++ b/Documentation/kbuild/makefiles.rst
@@ -982,6 +982,8 @@  The syntax is quite similar. The difference is to use "userprogs" instead of
 
 	When linking bpfilter_umh, it will be passed the extra option -static.
 
+	From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
+
 5.4 When userspace programs are actually built
 ----------------------------------------------
 
diff --git a/Makefile b/Makefile
index 45278d508d81..1d0172449355 100644
--- a/Makefile
+++ b/Makefile
@@ -431,11 +431,12 @@  HOSTCC	= gcc
 HOSTCXX	= g++
 endif
 
-export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
-			      -O2 -fomit-frame-pointer -std=gnu89
-export KBUILD_USERLDFLAGS :=
+KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
+			 -O2 -fomit-frame-pointer -std=gnu89
+KBUILD_USERCFLAGS  := $(KBUILD_USERHOSTCFLAGS) $(USERCFLAGS)
+KBUILD_USERLDFLAGS := $(USERLDFLAGS)
 
-KBUILD_HOSTCFLAGS   := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
+KBUILD_HOSTCFLAGS   := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
 KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
 KBUILD_HOSTLDFLAGS  := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
 KBUILD_HOSTLDLIBS   := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
@@ -530,6 +531,7 @@  export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AW
 export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
 export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
 export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE
+export KBUILD_USERCFLAGS KBUILD_USERLDFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
 export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
diff --git a/init/Kconfig b/init/Kconfig
index f2ae41e6717f..164706c38e8b 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -62,13 +62,13 @@  config LLD_VERSION
 
 config CC_CAN_LINK
 	bool
-	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag)) if 64BIT
-	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag))
+	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag)) if 64BIT
+	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag))
 
 config CC_CAN_LINK_STATIC
 	bool
-	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m64-flag) -static) if 64BIT
-	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(m32-flag) -static)
+	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m64-flag) -static) if 64BIT
+	default $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(m32-flag) -static)
 
 config CC_HAS_ASM_GOTO
 	def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC))
diff --git a/usr/include/Makefile b/usr/include/Makefile
index 1c2ae1368079..0322e567dc1e 100644
--- a/usr/include/Makefile
+++ b/usr/include/Makefile
@@ -12,6 +12,9 @@  UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
 # It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
 UAPI_CFLAGS += $(filter -m32 -m64, $(KBUILD_CFLAGS))
 
+# USERCFLAGS might contain sysroot location for CC.
+UAPI_CFLAGS += $(USERCFLAGS)
+
 override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I$(objtree)/usr/include
 
 # The following are excluded for now because they fail to build.