-include $(call sorted-wildcard,$(TOOLCHAIN_DIR)/make/kernel/*/Makefile.in)
-include $(call sorted-wildcard,$(TOOLCHAIN_DIR)/make/target/*/Makefile.in)

KERNEL_ARCH:=$(call qstrip,$(FREETZ_KERNEL_ARCH))

TARGET_ARCH:=$(call qstrip,$(FREETZ_TARGET_ARCH))
TARGET_ARCH_ENDIANNESS_DEPENDENT:=$(call qstrip,$(FREETZ_TARGET_ARCH_ENDIANNESS_DEPENDENT))
TARGET_GNU_TRIPLET:=$(call qstrip,$(FREETZ_TARGET_GNU_TRIPLET))
TARGET_TRIPLET_GNU_ABI:=$(call qstrip,$(FREETZ_TARGET_TRIPLET_GNU_ABI))

REAL_GNU_KERNEL_NAME:=$(TARGET_GNU_TRIPLET)

REAL_GNU_TARGET_NAME:=$(call qstrip,$(FREETZ_TARGET_UCLIBC_TRIPLET))
GNU_TARGET_NAME:=$(call GET_STRING_COMPONENTS,$(REAL_GNU_TARGET_NAME),-,2)

KERNEL_VERSION_MAJOR:=$(call qstrip,$(FREETZ_KERNEL_VERSION_MAJOR))
KERNEL_TOOLCHAIN_GCC_VERSION:=$(call qstrip,$(FREETZ_KERNEL_GCC_VERSION))
KERNEL_TOOLCHAIN_BINUTILS_VERSION:=$(call qstrip,$(FREETZ_KERNEL_BINUTILS_VERSION))

KERNEL_TOOLCHAIN_SYMLINK:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_DIR)/kernel
KERNEL_TOOLCHAIN_SYMLINK_DOT_FILE:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_DIR)/.kernel
KERNEL_TOOLCHAIN_COMPILER:=$(TARGET_ARCH_ENDIANNESS_DEPENDENT)_gcc-$(KERNEL_TOOLCHAIN_GCC_VERSION)
KERNEL_TOOLCHAIN_DIR:=$(FREETZ_BASE_DIR)/$(SOURCE_DIR_ROOT)/toolchain-$(KERNEL_TOOLCHAIN_COMPILER)
KERNEL_TOOLCHAIN_STAGING_DIR:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_BUILD_DIR)/$(KERNEL_TOOLCHAIN_COMPILER)/$(REAL_GNU_KERNEL_NAME)
KERNEL_CROSS_COMPILER:=$(KERNEL_TOOLCHAIN_STAGING_DIR)/bin/$(REAL_GNU_KERNEL_NAME)-gcc

KERNEL_TOOLCHAIN_PATH:=$(KERNEL_TOOLCHAIN_STAGING_DIR)/bin:/bin:/sbin:/usr/bin:/usr/sbin

ifeq ($(strip $(FREETZ_KERNEL_GCC_3_4)),y)
KERNEL_TOOLCHAIN_NO_MPFR:=y
endif

TARGET_TOOLCHAIN_GCC_VERSION:=$(call qstrip,$(FREETZ_TARGET_GCC_VERSION))
TARGET_TOOLCHAIN_GCC_MAJOR_VERSION:=$(call qstrip,$(FREETZ_TARGET_GCC_MAJOR_VERSION))
TARGET_TOOLCHAIN_UCLIBC_VERSION:=$(call qstrip,$(FREETZ_TARGET_UCLIBC_VERSION))
TARGET_TOOLCHAIN_UCLIBC_MAJOR_VERSION:=$(call qstrip,$(FREETZ_TARGET_UCLIBC_MAJOR_VERSION))
TARGET_TOOLCHAIN_BINUTILS_VERSION:=$(call qstrip,$(FREETZ_TARGET_BINUTILS_VERSION))

TARGET_TOOLCHAIN_SYMLINK:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_DIR)/target
TARGET_TOOLCHAIN_SYMLINK_DOT_FILE:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_DIR)/.target
TARGET_TOOLCHAIN_COMPILER:=$(TARGET_ARCH_ENDIANNESS_DEPENDENT)_gcc-$(TARGET_TOOLCHAIN_GCC_VERSION)_uClibc-$(TARGET_TOOLCHAIN_UCLIBC_VERSION)$(if $(FREETZ_AVM_UCLIBC_NPTL_ENABLED),-nptl)$(if $(FREETZ_KERNEL_VERSION_3_10_MIN),_kernel-$(KERNEL_VERSION_MAJOR))
TARGET_TOOLCHAIN_DIR:=$(FREETZ_BASE_DIR)/$(SOURCE_DIR_ROOT)/toolchain-$(TARGET_TOOLCHAIN_COMPILER)
TARGET_TOOLCHAIN_STAGING_DIR:=$(FREETZ_BASE_DIR)/$(TOOLCHAIN_BUILD_DIR)/$(TARGET_TOOLCHAIN_COMPILER)/$(REAL_GNU_TARGET_NAME)
TARGET_CROSS_COMPILER:=$(TARGET_TOOLCHAIN_STAGING_DIR)/usr/bin/$(REAL_GNU_TARGET_NAME)-gcc
TARGET_CXX_CROSS_COMPILER_SYMLINK:=$(TARGET_TOOLCHAIN_STAGING_DIR)/usr/bin/$(REAL_GNU_TARGET_NAME)-g++-wrapper
TARGET_CXX_CROSS_COMPILER_CCACHE_SYMLINK:=$(TARGET_TOOLCHAIN_STAGING_DIR)/usr/bin-ccache/$(REAL_GNU_TARGET_NAME)-g++-wrapper
TARGET_CXX_CROSS_COMPILER_SYMLINK_TIMESTAMP:=$(TARGET_CXX_CROSS_COMPILER_SYMLINK).timestamp
TARGET_TOOLCHAIN_KERNEL_VERSION_HEADER:=$(TARGET_TOOLCHAIN_STAGING_DIR)/usr/include/linux/version.h
TARGET_UTILS_DIR:=$(TARGET_TOOLCHAIN_STAGING_DIR)/target-utils

STDCXXLIB:=$(call qstrip,$(FREETZ_STDCXXLIB))

# Use this paths to search for our compilers
KERNEL_MAKE_PATH:=$(KERNEL_TOOLCHAIN_STAGING_DIR)/bin
TARGET_MAKE_PATH:=$(TARGET_TOOLCHAIN_STAGING_DIR)/bin

ifeq ($(strip $(FREETZ_BUILD_TOOLCHAIN)),y)
TOOLCHAIN:=kernel-toolchain target-toolchain
else
TOOLCHAIN:=download-toolchain
endif

$(KERNEL_TOOLCHAIN_SYMLINK_DOT_FILE): $(TOPDIR)/.config
	@$(RM) $(KERNEL_TOOLCHAIN_SYMLINK)
	@ln -fs $(BUILD_DIR)/$(KERNEL_TOOLCHAIN_COMPILER)/$(REAL_GNU_KERNEL_NAME) $(KERNEL_TOOLCHAIN_SYMLINK)
	@touch $@

$(TARGET_TOOLCHAIN_SYMLINK_DOT_FILE): $(TOPDIR)/.config
	@$(RM) $(TARGET_TOOLCHAIN_SYMLINK)
	@ln -fs $(BUILD_DIR)/$(TARGET_TOOLCHAIN_COMPILER)/$(REAL_GNU_TARGET_NAME) $(TARGET_TOOLCHAIN_SYMLINK)
	@touch $@

$(TARGET_CXX_CROSS_COMPILER_SYMLINK_TIMESTAMP): $(TOPDIR)/.config | gcc $(if $(FREETZ_TOOLCHAIN_CCACHE),ccache)
	@$(RM) $(TARGET_CXX_CROSS_COMPILER_SYMLINK) $(TARGET_CXX_CROSS_COMPILER_CCACHE_SYMLINK)
	@ln -fs $(REAL_GNU_TARGET_NAME)-g++$(if $(FREETZ_STDCXXLIB_USE_UCLIBCXX),-uc) $(TARGET_CXX_CROSS_COMPILER_SYMLINK)
	@$(if \
		$(and $(FREETZ_STDCXXLIB_USE_GNULIBSTDCXX),$(FREETZ_TOOLCHAIN_CCACHE)), \
		ln -fs $(REAL_GNU_TARGET_NAME)-g++ $(TARGET_CXX_CROSS_COMPILER_CCACHE_SYMLINK), \
		true \
	)
	@touch $@

kernel-toolchain-clean: ccache-kernel-clean
kernel-toolchain-dirclean: ccache-kernel-dirclean

target-toolchain-clean: ccache-clean libtool-host-clean
target-toolchain-dirclean: ccache-dirclean libtool-host-dirclean

toolchain-dirclean: kernel-toolchain-dirclean target-toolchain-dirclean

toolchain-distclean: kernel-toolchain-distclean target-toolchain-distclean
	$(RM) -r $(TOOLCHAIN_BUILD_DIR)

kernel-toolchain-distclean: kernel-toolchain-dirclean
	$(RM) -r $(SOURCE_DIR_ROOT)/toolchain/kernel
	$(RM) $(TOOLCHAIN_DIR)/kernel
	$(RM) $(KERNEL_TOOLCHAIN_SYMLINK_DOT_FILE)

target-toolchain-distclean: target-toolchain-dirclean
	$(RM) -r $(SOURCE_DIR_ROOT)/toolchain/target
	$(RM) $(TOOLCHAIN_DIR)/target
	$(RM) $(TARGET_TOOLCHAIN_SYMLINK_DOT_FILE)

include $(TOOLCHAIN_DIR)/make/toolchain-common.in
