# SPDX-License-Identifier: GPL-2.0
GCC_PLUGINS_DIR := $(shell $(CC) -print-file-name=plugin)

HOST_EXTRACXXFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(srctree)/$(src)
HOST_EXTRACXXFLAGS += -include include/generated/autoconf.h
ifeq ($(call gcc-min-version, 110000, y), y)
HOST_EXTRACXXFLAGS += -std=c++11
else
HOST_EXTRACXXFLAGS += -std=gnu++98
endif
HOST_EXTRACXXFLAGS += -ggdb -fvisibility=hidden
HOST_EXTRACXXFLAGS += -fno-rtti -fno-exceptions -fasynchronous-unwind-tables
HOST_EXTRACXXFLAGS += -Wall -W
HOST_EXTRACXXFLAGS += $(call hostcxx-disable-warning,format-diag)
ifeq ($(call gcc-max-version, 110000, y), y)
HOST_EXTRACXXFLAGS += $(call hostcxx-disable-warning,c++11-compat)
endif
HOST_EXTRACXXFLAGS += -Wno-unused-parameter -Wno-narrowing
#HOST_EXTRACXXFLAGS += -Wno-unused-variable
HOST_EXTRACXXFLAGS += -include $(srctree)/include/linux/compiler-version.h
export HOST_EXTRACXXFLAGS

type_canary_h := include/generated/type_canary.h

HOSTCXXFLAGS_type_canary_plugin.o := -include $(type_canary_h)

KBUILD_HOSTLDFLAGS += -Wl,--as-needed

# ensure every plugin has an soname
$(foreach p,$(GCC_PLUGIN),\
	$(eval export HOSTLDLIBS_$(notdir $(p)) += -Wl,-soname=$(notdir $(p))))

# add utilities_plugin.so as link dependency to every other plugin
$(foreach p,$(filter-out utilities_plugin/utilities_plugin.so,$(GCC_PLUGIN)),$(if $(findstring /,$(p)),\
	$(eval export HOSTLDLIBS_$(notdir $(p)) += -Wl,-rpath='$$$$$$$$ORIGIN/../utilities_plugin' $(obj)/utilities_plugin/utilities_plugin.so),\
	$(eval        HOSTLDLIBS_$(notdir $(p)) += -Wl,-rpath='$$$$ORIGIN/utilities_plugin'        $(obj)/utilities_plugin/utilities_plugin.so)))

# ensure utilities_plugin.so is built first
$(addprefix $(obj)/,$(filter-out utilities_plugin/utilities_plugin.so,$(GCC_PLUGIN)) $(filter-out utilities_plugin,$(GCC_PLUGIN_SUBDIR))): $(obj)/utilities_plugin

$(obj)/type_canary_plugin.o: $(type_canary_h)
$(type_canary_h):
	$(call cmd,type_canary)

quiet_cmd_type_canary = CANARY  $@
      cmd_type_canary = mkdir -p $(dir $@); test -f $@ || \
	$(CONFIG_SHELL) $(srctree)/scripts/gcc-plugins/gen-type_canary.sh > $@

$(obj)/randomize_layout_plugin.o: $(obj)/randomize_layout_seed.h
quiet_cmd_create_randomize_layout_seed = SEEDHDR $@
cmd_create_randomize_layout_seed = \
	SEED=$$(cat $(filter-out FORCE,$^) </dev/null); \
	echo '/*' > $@; \
	echo ' * This file is automatically generated. Keep it private.' >> $@; \
	echo ' * Exposing this value will expose the layout of randomized structures.' >> $@; \
	echo ' */' >> $@; \
	echo "const char randstruct_seed[] = \"$$SEED\";" >> $@
$(obj)/randomize_layout_seed.h: $(objtree)/scripts/basic/randstruct.seed FORCE
	$(call if_changed,create_randomize_layout_seed)
targets += randomize_layout_seed.h randomize_layout_hash.h

hostcxxlibs-y := $(foreach p,$(GCC_PLUGIN),$(if $(findstring /,$(p)),,$(p)))
always-y += $(hostcxxlibs-y)

$(foreach p,$(hostcxxlibs-y:%.so=%),$(eval $(p)-objs := $(p).o))

subdir-y := $(GCC_PLUGIN_SUBDIR)
subdir-  += $(GCC_PLUGIN_SUBDIR)

clean-files += *.so
