kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
# Unlike the kernel space, exported headers are written in standard C.
|
|
|
|
# - Forbid C++ style comments
|
|
|
|
# - Use '__inline__', '__asm__' instead of 'inline', 'asm'
|
|
|
|
#
|
|
|
|
# -std=c90 (equivalent to -ansi) catches the violation of those.
|
|
|
|
# We cannot go as far as adding -Wpedantic since it emits too many warnings.
|
|
|
|
UAPI_CFLAGS := -std=c90 -Wall -Werror=implicit-function-declaration
|
|
|
|
|
2020-04-29 03:45:13 +00:00
|
|
|
# In theory, we do not care -m32 or -m64 for header compile tests.
|
|
|
|
# It is here just because CONFIG_CC_CAN_LINK is tested with -m32 or -m64.
|
2022-03-05 12:56:05 +00:00
|
|
|
UAPI_CFLAGS += $(filter -m32 -m64 --target=%, $(KBUILD_CFLAGS))
|
2020-04-29 03:45:13 +00:00
|
|
|
|
2022-02-01 21:35:42 +00:00
|
|
|
# USERCFLAGS might contain sysroot location for CC.
|
|
|
|
UAPI_CFLAGS += $(USERCFLAGS)
|
2020-04-29 03:45:13 +00:00
|
|
|
|
kbuild: prevent exported headers from including <stdlib.h>, <stdbool.h>
Some UAPI headers included <stdlib.h>, like this:
#ifndef __KERNEL__
#include <stdlib.h>
#endif
As it turned out, they just included it for no good reason.
After some fixes, now I can compile-test UAPI headers
(CONFIG_UAPI_HEADER_TEST=y) without including <stdlib.h> from the
system header search paths.
To avoid somebody getting it back again, this commit adds the dummy
header, usr/dummy-include/stdlib.h
I added $(srctree)/usr/dummy-include to the header search paths.
Because it is searched before the system directories, if someone
tries to include <stdlib.h>, they will see the error message.
While I am here, I also replaced $(objtree)/usr/include with $(obj),
but it has no functional change.
If we can make kernel headers self-contained (that is, none of exported
kernel headers includes system headers), we will be able to add the
-nostdinc flag, but that is much far from where we stand now.
As a realistic solution, we can ban header inclusion individually by
putting a dummy header into usr/dummy-include/.
Currently, no header include <stdbool.h>. I put it as well before somebody
attempts to use it.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2022-04-04 06:19:42 +00:00
|
|
|
override c_flags = $(UAPI_CFLAGS) -Wp,-MMD,$(depfile) -I $(obj) -I $(srctree)/usr/dummy-include
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
|
|
|
|
# The following are excluded for now because they fail to build.
|
|
|
|
#
|
|
|
|
# Do not add a new header to the blacklist without legitimate reason.
|
|
|
|
# Please consider to fix the header first.
|
|
|
|
#
|
|
|
|
# Sorted alphabetically.
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += asm/ucontext.h
|
|
|
|
no-header-test += drm/vmwgfx_drm.h
|
|
|
|
no-header-test += linux/am437x-vpfe.h
|
|
|
|
no-header-test += linux/coda.h
|
2022-01-27 07:33:04 +00:00
|
|
|
no-header-test += linux/cyclades.h
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += linux/errqueue.h
|
|
|
|
no-header-test += linux/hdlc/ioctl.h
|
|
|
|
no-header-test += linux/ivtv.h
|
|
|
|
no-header-test += linux/matroxfb.h
|
|
|
|
no-header-test += linux/omap3isp.h
|
|
|
|
no-header-test += linux/omapfb.h
|
|
|
|
no-header-test += linux/patchkey.h
|
|
|
|
no-header-test += linux/phonet.h
|
|
|
|
no-header-test += linux/sctp.h
|
|
|
|
no-header-test += linux/sysctl.h
|
|
|
|
no-header-test += linux/usb/audio.h
|
|
|
|
no-header-test += linux/v4l2-mediabus.h
|
|
|
|
no-header-test += linux/v4l2-subdev.h
|
|
|
|
no-header-test += linux/videodev2.h
|
|
|
|
no-header-test += linux/vm_sockets.h
|
|
|
|
no-header-test += sound/asequencer.h
|
|
|
|
no-header-test += sound/asoc.h
|
|
|
|
no-header-test += sound/asound.h
|
|
|
|
no-header-test += sound/compress_offload.h
|
|
|
|
no-header-test += sound/emu10k1.h
|
|
|
|
no-header-test += sound/sfnt_info.h
|
|
|
|
no-header-test += xen/evtchn.h
|
|
|
|
no-header-test += xen/gntdev.h
|
|
|
|
no-header-test += xen/privcmd.h
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
|
|
|
|
# More headers are broken in some architectures
|
|
|
|
|
|
|
|
ifeq ($(SRCARCH),arc)
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += linux/bpf_perf_event.h
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(SRCARCH),ia64)
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += asm/setup.h
|
|
|
|
no-header-test += asm/sigcontext.h
|
|
|
|
no-header-test += linux/if_bonding.h
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(SRCARCH),powerpc)
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += linux/bpf_perf_event.h
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
ifeq ($(SRCARCH),sparc)
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += asm/uctx.h
|
|
|
|
no-header-test += asm/fbio.h
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
# asm-generic/*.h is used by asm/*.h, and should not be included directly
|
2019-12-18 15:34:22 +00:00
|
|
|
no-header-test += asm-generic/%
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
|
2022-02-27 09:03:35 +00:00
|
|
|
always-y := $(patsubst $(obj)/%.h,%.hdrtest, $(shell find $(obj) -name '*.h' 2>/dev/null))
|
2019-11-07 07:14:40 +00:00
|
|
|
|
2020-04-08 18:29:19 +00:00
|
|
|
# Include the header twice to detect missing include guard.
|
2019-11-07 07:14:40 +00:00
|
|
|
quiet_cmd_hdrtest = HDRTEST $<
|
2019-11-07 07:14:41 +00:00
|
|
|
cmd_hdrtest = \
|
2022-03-30 18:34:06 +00:00
|
|
|
$(CC) $(c_flags) -fsyntax-only -x c /dev/null \
|
2019-12-18 15:34:22 +00:00
|
|
|
$(if $(filter-out $(no-header-test), $*.h), -include $< -include $<); \
|
2021-12-06 02:35:06 +00:00
|
|
|
$(PERL) $(srctree)/$(src)/headers_check.pl $(obj) $(SRCARCH) $<; \
|
2019-11-07 07:14:41 +00:00
|
|
|
touch $@
|
2019-11-07 07:14:40 +00:00
|
|
|
|
|
|
|
$(obj)/%.hdrtest: $(obj)/%.h FORCE
|
|
|
|
$(call if_changed_dep,hdrtest)
|
kbuild: compile-test exported headers to ensure they are self-contained
Multiple people have suggested compile-testing UAPI headers to ensure
they can be really included from user-space. "make headers_check" is
obviously not enough to catch bugs, and we often leak unresolved
references to user-space.
Use the new header-test-y syntax to implement it. Please note exported
headers are compile-tested with a completely different set of compiler
flags. The header search path is set to $(objtree)/usr/include since
exported headers should not include unexported ones.
We use -std=gnu89 for the kernel space since the kernel code highly
depends on GNU extensions. On the other hand, UAPI headers should be
written in more standardized C, so they are compiled with -std=c90.
This will emit errors if C++ style comments, the keyword 'inline', etc.
are used. Please use C style comments (/* ... */), '__inline__', etc.
in UAPI headers.
There is additional compiler requirement to enable this test because
many of UAPI headers include <stdlib.h>, <sys/ioctl.h>, <sys/time.h>,
etc. directly or indirectly. You cannot use kernel.org pre-built
toolchains [1] since they lack <stdlib.h>.
I reused CONFIG_CC_CAN_LINK to check the system header availability.
The intention is slightly different, but a compiler that can link
userspace programs provide system headers.
For now, a lot of headers need to be excluded because they cannot
be compiled standalone, but this is a good start point.
[1] https://mirrors.edge.kernel.org/pub/tools/crosstool/index.html
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
2019-07-01 00:58:40 +00:00
|
|
|
|
2021-12-06 02:35:06 +00:00
|
|
|
# Since GNU Make 4.3, $(patsubst $(obj)/%/,%,$(wildcard $(obj)/*/)) works.
|
|
|
|
# To support older Make versions, use a somewhat tedious way.
|
|
|
|
clean-files += $(filter-out Makefile headers_check.pl, $(notdir $(wildcard $(obj)/*)))
|