From e846f0dc57f441e5e93194d39bc9b8ac2ab5e0a4 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Tue, 4 Jun 2019 15:42:48 +0300 Subject: kbuild: add support for ensuring headers are self-contained Sometimes it's useful to be able to explicitly ensure certain headers remain self-contained, i.e. that they are compilable as standalone units, by including and/or forward declaring everything they depend on. Add special target header-test-y where individual Makefiles can add headers to be tested if CONFIG_HEADER_TEST is enabled. This will generate a dummy C file per header that gets built as part of extra-y. Signed-off-by: Jani Nikula Reviewed-by: Sam Ravnborg Signed-off-by: Masahiro Yamada --- init/Kconfig | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'init') diff --git a/init/Kconfig b/init/Kconfig index 0e2344389501..df5bba27e3fe 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -96,6 +96,15 @@ config COMPILE_TEST here. If you are a user/distributor, say N here to exclude useless drivers to be distributed. +config HEADER_TEST + bool "Compile test headers that should be standalone compilable" + help + Compile test headers listed in header-test-y target to ensure they are + self-contained, i.e. compilable as standalone units. + + If you are a developer or tester and want to ensure the requested + headers are self-contained, say Y here. Otherwise, choose N. + config LOCALVERSION string "Local version - append to kernel release" help -- cgit v1.2.3 From 1a927fd347ebb3c02046150ee489d4fe4e6b9e81 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Mon, 1 Jul 2019 09:58:39 +0900 Subject: init/Kconfig: add CONFIG_CC_CAN_LINK Currently, scripts/cc-can-link.sh is run just for BPFILTER_UMH, but defining CC_CAN_LINK will be useful in other places. Signed-off-by: Masahiro Yamada --- init/Kconfig | 3 +++ 1 file changed, 3 insertions(+) (limited to 'init') diff --git a/init/Kconfig b/init/Kconfig index df5bba27e3fe..2e9813daa2c1 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -24,6 +24,9 @@ config CLANG_VERSION int default $(shell,$(srctree)/scripts/clang-version.sh $(CC)) +config CC_CAN_LINK + def_bool $(success,$(srctree)/scripts/cc-can-link.sh $(CC)) + config CC_HAS_ASM_GOTO def_bool $(success,$(srctree)/scripts/gcc-goto.sh $(CC)) -- cgit v1.2.3 From d6fc9fcbaa655cff2d2be05e16867d1918f78b85 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Mon, 1 Jul 2019 09:58:40 +0900 Subject: 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 , , , etc. directly or indirectly. You cannot use kernel.org pre-built toolchains [1] since they lack . 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 Reviewed-by: Sam Ravnborg --- init/Kconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'init') diff --git a/init/Kconfig b/init/Kconfig index 2e9813daa2c1..74192de8ada6 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -108,6 +108,16 @@ config HEADER_TEST If you are a developer or tester and want to ensure the requested headers are self-contained, say Y here. Otherwise, choose N. +config UAPI_HEADER_TEST + bool "Compile test UAPI headers" + depends on HEADER_TEST && HEADERS_INSTALL && CC_CAN_LINK + help + Compile test headers exported to user-space to ensure they are + self-contained, i.e. compilable as standalone units. + + If you are a developer or tester and want to ensure the exported + headers are self-contained, say Y here. Otherwise, choose N. + config LOCALVERSION string "Local version - append to kernel release" help -- cgit v1.2.3 From 43c78d88036e47949a5af2fac0067e7ebaaf7dd0 Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Mon, 1 Jul 2019 09:58:45 +0900 Subject: kbuild: compile-test kernel headers to ensure they are self-contained The headers in include/ are globally used in the kernel source tree to provide common APIs. They are included from external modules, too. It will be useful to make as many headers self-contained as possible so that we do not have to rely on a specific include order. There are more than 4000 headers in include/. In my rough analysis, 70% of them are already self-contained. With efforts, most of them can be self-contained. For now, we must exclude more than 1000 headers just because they cannot be compiled as standalone units. I added them to header-test-. The blacklist was mostly generated by a script, so the reason of the breakage should be checked later. Signed-off-by: Masahiro Yamada Tested-by: Jani Nikula Reviewed-by: Joel Fernandes (Google) --- init/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'init') diff --git a/init/Kconfig b/init/Kconfig index 74192de8ada6..e2e99544da8d 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -108,6 +108,17 @@ config HEADER_TEST If you are a developer or tester and want to ensure the requested headers are self-contained, say Y here. Otherwise, choose N. +config KERNEL_HEADER_TEST + bool "Compile test kernel headers" + depends on HEADER_TEST + help + Headers in include/ are used to build external moduls. + Compile test them to ensure they are self-contained, i.e. + compilable as standalone units. + + If you are a developer or tester and want to ensure the headers + in include/ are self-contained, say Y here. Otherwise, choose N. + config UAPI_HEADER_TEST bool "Compile test UAPI headers" depends on HEADER_TEST && HEADERS_INSTALL && CC_CAN_LINK -- cgit v1.2.3