Skip to content

Commit 59f5385

Browse files
committed
gcc-plugins: test plugin support in Kconfig and clean up Makefile
Run scripts/gcc-plugin.sh from Kconfig so that users can enable GCC_PLUGINS only when the compiler supports building plugins. Kconfig defines a new symbol, PLUGIN_HOSTCC. This will contain the compiler (g++ or gcc) used for building plugins, or empty if the plugin can not be supported at all. This allows us to remove all ugly testing in Makefile.gcc-plugins. Signed-off-by: Masahiro Yamada <[email protected]> Acked-by: Kees Cook <[email protected]>
1 parent 8034c2f commit 59f5385

File tree

4 files changed

+36
-57
lines changed

4 files changed

+36
-57
lines changed

arch/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ config SECCOMP_FILTER
398398

399399
See Documentation/prctl/seccomp_filter.txt for details.
400400

401+
preferred-plugin-hostcc := $(if-success,[ $(gcc-version) -ge 40800 ],$(HOSTCXX),$(HOSTCC))
402+
403+
config PLUGIN_HOSTCC
404+
string
405+
default "$(shell,$(srctree)/scripts/gcc-plugin.sh "$(preferred-plugin-hostcc)" "$(HOSTCXX)" "$(CC)")"
406+
help
407+
Host compiler used to build GCC plugins. This can be $(HOSTCXX),
408+
$(HOSTCC), or a null string if GCC plugin is unsupported.
409+
401410
config HAVE_GCC_PLUGINS
402411
bool
403412
help
@@ -407,6 +416,7 @@ config HAVE_GCC_PLUGINS
407416
menuconfig GCC_PLUGINS
408417
bool "GCC plugins"
409418
depends on HAVE_GCC_PLUGINS
419+
depends on PLUGIN_HOSTCC != ""
410420
depends on !COMPILE_TEST
411421
help
412422
GCC plugins are loadable modules that provide extra features to the

scripts/Kconfig.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ cc-option = $(success,$(CC) -Werror $(1) -E -x c /dev/null -o /dev/null)
2525
# $(ld-option,<flag>)
2626
# Return y if the linker supports <flag>, n otherwise
2727
ld-option = $(success,$(LD) -v $(1))
28+
29+
# gcc version including patch level
30+
gcc-version := $(shell,$(srctree)/scripts/gcc-version.sh -p $(CC) | sed 's/^0*//')

scripts/Makefile.gcc-plugins

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,37 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
ifdef CONFIG_GCC_PLUGINS
3-
__PLUGINCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC))
4-
PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)")
5-
6-
SANCOV_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so
7-
8-
gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so
2+
gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so
93

10-
gcc-plugin-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += latent_entropy_plugin.so
11-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += -DLATENT_ENTROPY_PLUGIN
12-
ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
4+
gcc-plugin-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += latent_entropy_plugin.so
5+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) += -DLATENT_ENTROPY_PLUGIN
6+
ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
137
DISABLE_LATENT_ENTROPY_PLUGIN += -fplugin-arg-latent_entropy_plugin-disable
14-
endif
15-
16-
ifdef CONFIG_GCC_PLUGIN_SANCOV
17-
# It is needed because of the gcc-plugin.sh and gcc version checks.
18-
gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so
19-
20-
ifeq ($(PLUGINCC),)
21-
$(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler)
22-
endif
23-
endif
24-
25-
gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += structleak_plugin.so
26-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_VERBOSE) += -fplugin-arg-structleak_plugin-verbose
27-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL) += -fplugin-arg-structleak_plugin-byref-all
28-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += -DSTRUCTLEAK_PLUGIN
8+
endif
299

30-
gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
31-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
32-
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) += -fplugin-arg-randomize_layout_plugin-performance-mode
10+
gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so
11+
gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += structleak_plugin.so
12+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_VERBOSE) += -fplugin-arg-structleak_plugin-verbose
13+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL) += -fplugin-arg-structleak_plugin-byref-all
14+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += -DSTRUCTLEAK_PLUGIN
3315

34-
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
16+
gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
17+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
18+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) += -fplugin-arg-randomize_layout_plugin-performance-mode
3519

36-
export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
37-
export DISABLE_LATENT_ENTROPY_PLUGIN
20+
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
3821

39-
ifneq ($(PLUGINCC),)
40-
# SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
41-
GCC_PLUGINS_CFLAGS := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGINS_CFLAGS))
42-
endif
22+
export GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
23+
export DISABLE_LATENT_ENTROPY_PLUGIN
4324

44-
KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
45-
GCC_PLUGIN := $(gcc-plugin-y)
46-
GCC_PLUGIN_SUBDIR := $(gcc-plugin-subdir-y)
47-
endif
25+
# sancov_plugin.so can be only in CFLAGS_KCOV because avoid duplication.
26+
GCC_PLUGINS_CFLAGS := $(filter-out %/sancov_plugin.so, $(GCC_PLUGINS_CFLAGS))
4827

49-
# If plugins aren't supported, abort the build before hard-to-read compiler
50-
# errors start getting spewed by the main build.
51-
PHONY += gcc-plugins-check
52-
gcc-plugins-check: FORCE
53-
ifdef CONFIG_GCC_PLUGINS
54-
ifeq ($(PLUGINCC),)
55-
ifneq ($(GCC_PLUGINS_CFLAGS),)
56-
ifeq ($(call cc-ifversion, -ge, 0405, y), y)
57-
$(Q)$(srctree)/scripts/gcc-plugin.sh --show-error "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)" || true
58-
@echo "Cannot use CONFIG_GCC_PLUGINS: your gcc installation does not support plugins, perhaps the necessary headers are missing?" >&2 && exit 1
59-
else
60-
@echo "Cannot use CONFIG_GCC_PLUGINS: your gcc version does not support plugins, you should upgrade it to at least gcc 4.5" >&2 && exit 1
61-
endif
62-
endif
63-
endif
64-
endif
65-
@:
28+
KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
29+
GCC_PLUGIN := $(gcc-plugin-y)
30+
GCC_PLUGIN_SUBDIR := $(gcc-plugin-subdir-y)
6631

6732
# Actually do the build, if requested.
6833
PHONY += gcc-plugins
69-
gcc-plugins: scripts_basic gcc-plugins-check
34+
gcc-plugins: scripts_basic
7035
ifdef CONFIG_GCC_PLUGINS
7136
$(Q)$(MAKE) $(build)=scripts/gcc-plugins
7237
endif

scripts/gcc-plugins/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
PLUGINCC := $(CONFIG_PLUGIN_HOSTCC:"%"=%)
23
GCC_PLUGINS_DIR := $(shell $(CC) -print-file-name=plugin)
34

45
ifeq ($(PLUGINCC),$(HOSTCC))

0 commit comments

Comments
 (0)