# This helper makefile is used for creating
#  - symbolic links (arch/$ARCH/include/asm/arch
#  - include/autoconf.mk, {spl,tpl}/include/autoconf.mk
#  - include/config.h
#
# When our migration to Kconfig is done
# (= When we move all CONFIGs from header files to Kconfig)
# this makefile can be deleted.
#
# SPDX-License-Identifier:	GPL-2.0
#

__all: include/autoconf.mk include/autoconf.mk.dep

ifeq ($(shell grep -q '^CONFIG_SPL=y' include/config/auto.conf 2>/dev/null && echo y),y)
__all: spl/include/autoconf.mk
endif

ifeq ($(shell grep -q '^CONFIG_TPL=y' include/config/auto.conf 2>/dev/null && echo y),y)
__all: tpl/include/autoconf.mk
endif

include include/config/auto.conf

include scripts/Kbuild.include

# Need to define CC and CPP again here in case the top Makefile did not
# include config.mk.  Some architectures expect CROSS_COMPILE to be defined
# in arch/$(ARCH)/config.mk
CC		= $(CROSS_COMPILE)gcc
CPP		= $(CC) -E

include config.mk

UBOOTINCLUDE    := \
		-Iinclude \
		$(if $(KBUILD_SRC), -I$(srctree)/include) \
		-I$(srctree)/arch/$(ARCH)/include \
		-include $(srctree)/include/linux/kconfig.h

c_flags := $(KBUILD_CFLAGS) $(KBUILD_CPPFLAGS) $(PLATFORM_CPPFLAGS) \
					$(UBOOTINCLUDE) $(NOSTDINC_FLAGS)

quiet_cmd_autoconf_dep = GEN     $@
      cmd_autoconf_dep = $(CC) -x c -DDO_DEPS_ONLY -M -MP $(c_flags) \
	-MQ include/config/auto.conf $(srctree)/include/common.h > $@ || {	\
		rm $@; false;							\
	}
include/autoconf.mk.dep: FORCE
	$(call cmd,autoconf_dep)

# We are migrating from board headers to Kconfig little by little.
# In the interim, we use both of
#  - include/config/auto.conf (generated by Kconfig)
#  - include/autoconf.mk      (used in the U-Boot conventional configuration)
# The following rule creates autoconf.mk
# include/config/auto.conf is grepped in order to avoid duplication of the
# same CONFIG macros
quiet_cmd_autoconf = GEN     $@
      cmd_autoconf = \
	$(CPP) $(c_flags) $2 -DDO_DEPS_ONLY -dM $(srctree)/include/common.h > $@.tmp && { \
		sed -n -f $(srctree)/tools/scripts/define2mk.sed $@.tmp |		\
		while read line; do							\
			if [ -n "${KCONFIG_IGNORE_DUPLICATES}" ] ||			\
			   ! grep -q "$${line%=*}=" include/config/auto.conf; then	\
				echo "$$line";						\
			fi								\
		done > $@;								\
		rm $@.tmp;								\
	} || {										\
		rm $@.tmp; false;							\
	}

include/autoconf.mk: FORCE
	$(call cmd,autoconf)

spl/include/autoconf.mk: FORCE
	$(Q)mkdir -p $(dir $@)
	$(call cmd,autoconf,-DCONFIG_SPL_BUILD)

tpl/include/autoconf.mk: FORCE
	$(Q)mkdir -p $(dir $@)
	$(call cmd,autoconf,-DCONFIG_SPL_BUILD -DCONFIG_TPL_BUILD)

include/autoconf.mk include/autoconf.mk.dep \
	spl/include/autoconf.mk tpl/include/autoconf.mk: include/config.h

# include/config.h
# Prior to Kconfig, it was generated by mkconfig. Now it is created here.
define filechk_config_h
	(echo "/* Automatically generated - do not edit */";		\
	for i in $$(echo $(CONFIG_SYS_EXTRA_OPTIONS) | sed 's/,/ /g'); do \
		echo \#define CONFIG_$$i				\
		| sed '/=/ {s/=/	/;q; } ; { s/$$/	1/; }'; \
	done;								\
	echo \#define CONFIG_BOARDDIR board/$(if $(VENDOR),$(VENDOR)/)$(BOARD);\
	echo \#include \<config_defaults.h\>;				\
	echo \#include \<config_uncmd_spl.h\>;				\
	echo \#include \<configs/$(CONFIG_SYS_CONFIG_NAME).h\>;		\
	echo \#include \<asm/config.h\>;				\
	echo \#include \<config_fallbacks.h\>;)
endef

include/config.h: scripts/Makefile.autoconf create_symlink FORCE
	$(call filechk,config_h)

# symbolic links
# If arch/$(ARCH)/mach-$(SOC)/include/mach exists,
# make a symbolic link to that directory.
# Otherwise, create a symbolic link to arch/$(ARCH)/include/asm/arch-$(SOC).
PHONY += create_symlink
create_symlink:
ifdef CONFIG_CREATE_ARCH_SYMLINK
ifneq ($(KBUILD_SRC),)
	$(Q)mkdir -p include/asm
	$(Q)if [ -d $(KBUILD_SRC)/arch/$(ARCH)/mach-$(SOC)/include/mach ]; then	\
		dest=arch/$(ARCH)/mach-$(SOC)/include/mach;			\
	else									\
		dest=arch/$(ARCH)/include/asm/arch-$(if $(SOC),$(SOC),$(CPU));	\
	fi;									\
	ln -fsn $(KBUILD_SRC)/$$dest include/asm/arch
else
	$(Q)if [ -d arch/$(ARCH)/mach-$(SOC)/include/mach ]; then	\
		dest=../../mach-$(SOC)/include/mach;			\
	else								\
		dest=arch-$(if $(SOC),$(SOC),$(CPU));			\
	fi;								\
	ln -fsn $$dest arch/$(ARCH)/include/asm/arch
endif
endif

PHONY += FORCE
FORCE:

.PHONY: $(PHONY)