1# SPDX-License-Identifier: GPL-2.0
2ifdef 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
9
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
13    DISABLE_LATENT_ENTROPY_PLUGIN			+= -fplugin-arg-latent_entropy_plugin-disable
14  endif
15
16  ifdef CONFIG_GCC_PLUGIN_SANCOV
17    ifeq ($(CFLAGS_KCOV),)
18      # It is needed because of the gcc-plugin.sh and gcc version checks.
19      gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV)           += sancov_plugin.so
20
21      ifneq ($(PLUGINCC),)
22        CFLAGS_KCOV := $(SANCOV_PLUGIN)
23      else
24        $(warning warning: cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler)
25      endif
26    endif
27  endif
28
29  gcc-plugin-$(CONFIG_GCC_PLUGIN_STRUCTLEAK)	+= structleak_plugin.so
30  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_VERBOSE)	+= -fplugin-arg-structleak_plugin-verbose
31  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL)	+= -fplugin-arg-structleak_plugin-byref-all
32  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK)	+= -DSTRUCTLEAK_PLUGIN
33
34  gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)	+= randomize_layout_plugin.so
35  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)	+= -DRANDSTRUCT_PLUGIN
36  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE)	+= -fplugin-arg-randomize_layout_plugin-performance-mode
37
38  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
39
40  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
41  export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN
42
43  ifneq ($(PLUGINCC),)
44    # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
45    GCC_PLUGINS_CFLAGS := $(filter-out $(SANCOV_PLUGIN), $(GCC_PLUGINS_CFLAGS))
46  endif
47
48  KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
49  GCC_PLUGIN := $(gcc-plugin-y)
50  GCC_PLUGIN_SUBDIR := $(gcc-plugin-subdir-y)
51endif
52
53# If plugins aren't supported, abort the build before hard-to-read compiler
54# errors start getting spewed by the main build.
55PHONY += gcc-plugins-check
56gcc-plugins-check: FORCE
57ifdef CONFIG_GCC_PLUGINS
58  ifeq ($(PLUGINCC),)
59    ifneq ($(GCC_PLUGINS_CFLAGS),)
60      # Various gccs between 4.5 and 5.1 have bugs on powerpc due to missing
61      # header files. gcc <= 4.6 doesn't work at all, gccs from 4.8 to 5.1 have
62      # issues with 64-bit targets.
63      ifeq ($(ARCH),powerpc)
64        ifeq ($(call cc-ifversion, -le, 0501, y), y)
65	  @echo "Cannot use CONFIG_GCC_PLUGINS: plugin support on gcc <= 5.1 is buggy on powerpc, please upgrade to gcc 5.2 or newer" >&2 && exit 1
66        endif
67      endif
68      ifeq ($(call cc-ifversion, -ge, 0405, y), y)
69	$(Q)$(srctree)/scripts/gcc-plugin.sh --show-error "$(__PLUGINCC)" "$(HOSTCXX)" "$(CC)" || true
70	@echo "Cannot use CONFIG_GCC_PLUGINS: your gcc installation does not support plugins, perhaps the necessary headers are missing?" >&2 && exit 1
71      else
72	@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
73      endif
74    endif
75  endif
76endif
77	@:
78
79# Actually do the build, if requested.
80PHONY += gcc-plugins
81gcc-plugins: scripts_basic gcc-plugins-check
82ifdef CONFIG_GCC_PLUGINS
83	$(Q)$(MAKE) $(build)=scripts/gcc-plugins
84endif
85	@:
86