# 
# Copyright (C) 2007-2009 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# Main makefile for the toolchain
#
# Steps:
# 1) toolchain/binutils/install
#    build & install binutils
# 2) toolchain/gcc/prepare
#    build & install a minimal gcc, needed for steps 3 & 4
# 3) toolchain/kernel-headers/install
#    install kernel headers, needed for step 4
# 4) toolchain/libc/prepare
#    build & install libc headers & support files, needed for step 5
# 5) toolchain/gcc/compile
#    build & install an initial gcc, needed for step 6
# 6) toolchain/libc/compile
#    build & install the final libc
# 7) toolchain/gcc/install
#    build & install the final gcc
# 8) toolchain/libc/install
#    build & install libc utilities
#

curdir:=toolchain

# subdirectories to descend into
$(curdir)/builddirs := $(if $(CONFIG_GDB),gdb) $(if $(CONFIG_INSIGHT),insight) $(if $(CONFIG_EXTERNAL_TOOLCHAIN),$(if $(CONFIG_NATIVE_TOOLCHAIN),,wrapper),kernel-headers binutils gcc/minimal gcc/initial gcc/final $(LIBC)/headers $(LIBC))
ifdef CONFIG_USE_UCLIBC
  $(curdir)/builddirs += $(LIBC)/utils
endif
$(curdir)/builddirs-compile:=$($(curdir)/builddirs-prepare)
$(curdir)/builddirs-install:=$($(curdir)/builddirs-compile)

# builddir dependencies
ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  $(curdir)/gcc/minimal/compile:=$(curdir)/binutils/install
  $(curdir)/kernel-headers/install:=$(curdir)/gcc/minimal/install
  $(curdir)/gcc/initial/prepare:=$(curdir)/gcc/minimal/prepare
  $(curdir)/gcc/final/prepare:=$(curdir)/gcc/initial/prepare

  $(curdir)/$(LIBC)/headers/compile:=$(curdir)/kernel-headers/install
  $(curdir)/gcc/initial/compile:=$(curdir)/$(LIBC)/headers/install
  $(curdir)/$(LIBC)/compile:=$(curdir)/gcc/initial/install
  $(curdir)/gcc/final/compile:=$(curdir)/$(LIBC)/install
  $(curdir)/$(LIBC)/utils/compile:=$(curdir)/gcc/final/install
  $(curdir)/$(LIBC)/prepare:=$(curdir)/$(LIBC)/headers/prepare
  $(curdir)/$(LIBC)/utils/prepare:=$(curdir)/$(LIBC)/headers/prepare
endif

ifdef CONFIG_GCC_VERSION_LLVM
  $(curdir)/builddirs += llvm
  $(curdir)/gcc/minimal/compile += $(curdir)/llvm/install
endif

ifndef DUMP_TARGET_DB
ifneq ($(ARCH),)
  $(TOOLCHAIN_DIR)/info.mk: .config
	@for dir in $(TOOLCHAIN_DIR); do ( \
		$(if $(QUIET),,set -x;) \
		mkdir -p "$$dir"; \
		cd "$$dir"; \
		ln -sf lib lib64; \
		mkdir -p stamp lib usr/include usr/lib ; \
	); done
	@grep GCC_VERSION $@ >/dev/null 2>&1 || $(INSTALL_DATA) $(TOPDIR)/toolchain/info.mk $@
	@touch $@
endif
endif

# prerequisites for the individual targets
$(curdir)/ := .config prereq
$(curdir)//prepare = $(STAGING_DIR)/.prepared $(TOOLCHAIN_DIR)/info.mk
$(curdir)//compile = $(1)/prepare
$(curdir)//install = $(1)/compile

ifndef DUMP_TARGET_DB
$(TOOLCHAIN_DIR)/stamp/.gcc-initial_installed:
endif

#modified by ChenQiang.
#$(eval $(call stampfile,$(curdir),toolchain,install,$(TOOLCHAIN_DIR)/stamp/.gcc-initial_installed,,$(TOOLCHAIN_DIR)))
#$(eval $(call subdir,$(curdir)))

#Define TOOLCHAIN backup variables.
TOOLCHAIN_BACKUP:=1
HOST_PLATFORM_BIT := $(shell getconf LONG_BIT)
TOOLCHAIN_DIR_NAME := $(notdir $(TOOLCHAIN_DIR))
TOOLCHAIN_BACKUP_FILENAME := $(TOOLCHAIN_DIR_NAME)_$(HOST_PLATFORM_BIT).tar.bz2
TOOLCHAIN_BACKUP_FILE_EXISTED := $(wildcard $(TOPDIR)/toolchain/$(TOOLCHAIN_BACKUP_FILENAME))
TOOLCHAIN_BACKUP_EMPTY :=

ifneq ($(HOST_OS),Linux)
  HOST_TAR:=trapret 2 $(TAR)
else
  HOST_TAR:=$(TAR)
endif
	
#Define compress command.
define TOOLCHAIN_BACKUP_COMPRESS_COMMAND
	cd $(TOPDIR)/staging_dir;\
	$(HOST_TAR) -jcvf $(TOOLCHAIN_BACKUP_FILENAME) $(TOOLCHAIN_DIR_NAME);\
	mv $(TOOLCHAIN_BACKUP_FILENAME) $(TOPDIR)/toolchain
endef

#Define decompress command.
define TOOLCHAIN_BACKUP_DECOMPRESS_COMMAND
	$(HOST_TAR) -jxvf $(TOPDIR)/toolchain/$(TOOLCHAIN_BACKUP_FILENAME) -C $(TOPDIR)/staging_dir 
endef
 
toolchain/stamp-install := $(TOOLCHAIN_DIR)/stamp/.toolchain_install
ifeq ($(TOOLCHAIN_RECOMPILE), 1) #TOOLCHAIN_RECOMPILE == 1.
TOOLCHAIN_BACKUP_FILE_EXISTED :=
TOOLCHAIN_COMPILE_FLAG := 1
else
TOOLCHAIN_BACKUP_FILE_EXISTED := $(wildcard $(TOPDIR)/toolchain/$(TOOLCHAIN_BACKUP_FILENAME))
#If 0, means that .toolchain_install is the newest file; otherwise, it isn't the newest file.
TOOLCHAIN_COMPILE_FLAG := $(shell $(SCRIPT_DIR)/timestamp.pl -n $(toolchain/stamp-install) \
                toolchain $(TOOLCHAIN_DIR)/stamp/.gcc-initial_installed > /dev/null;echo $$?)
endif

$(toolchain/stamp-install): $(TMP_DIR)/.build $(TOOLCHAIN_DIR)/stamp/.gcc-initial_installed
ifeq ($(TOOLCHAIN_RECOMPILE), 1) #[A] TOOLCHAIN_RECOMPILE == 1.
	rm -rf $(TOOLCHAIN_DIR) $(BUILD_DIR_TOOLCHAIN)
ifeq ($(TOOLCHAIN_BACKUP), 1) #[B] Open toochain optimization switch.
	rm -f $(TOPDIR)/toolchain/$(TOOLCHAIN_BACKUP_FILENAME)
endif #[B] End of ifeq ($(TOOLCHAIN_BACKUP), 1)
endif #[A] TOOLCHAIN_RECOMPILE == 1.
	
ifneq ($(TOOLCHAIN_BACKUP), 1) #[1] Close toochain optimization switch.
ifneq ($(TOOLCHAIN_COMPILE_FLAG), 0) #[2] Toolchain need to compile all tools.
	$(MAKE) $(if $(QUIET),--no-print-directory) $(toolchain/flags-install) toolchain/install
endif #[2] End of ifeq ($(TOOLCHAIN_COMPILE_FLAG), )
	
else #[1] Open toochain optimization switch.
ifeq ($(TOOLCHAIN_BACKUP_FILE_EXISTED), ) #[2] The backup file doesn't exist.
ifneq ($(TOOLCHAIN_COMPILE_FLAG), 0) #[3] Toolchain need to compile all tools.
	$(MAKE) $(if $(QUIET),--no-print-directory) $(toolchain/flags-install) toolchain/install
else #[3] Toolchain doesn't need to compile any tool.
	#@echo "TOOLCHAIN_BACKUP_OPEN, TOOLCHAIN_BACKUP_FILE_NOT_EXISTED, TOOLCHAIN_NOT_COMPILE."
endif #[3] End of ifeq ($(TOOLCHAIN_COMPILE_FLAG), )
	$(TOOLCHAIN_BACKUP_COMPRESS_COMMAND)
	 
else #[2] The backup file existes.
ifneq ($(TOOLCHAIN_COMPILE_FLAG), 0) #[3] Toolchain need to update all tools.
	$(TOOLCHAIN_BACKUP_DECOMPRESS_COMMAND) 
else #[3] Toolchain has been installed in the staging_dir dircetory.
	#@echo "TOOLCHAIN_BACKUP_OPEN, TOOLCHAIN_BACKUP_FILE_EXISTED, DON'T DECOMPRESS."
endif #[3] End of ifneq ($(TOOLCHAIN_COMPILE_FLAG), 0)
endif #[2] End of ifeq ($(TOOLCHAIN_BACKUP_FILE_EXISTED), )
endif #[1] End of ifneq ($(TOOLCHAIN_BACKUP), 1)
	mkdir -p $$(dirname $(toolchain/stamp-install))
	touch $(toolchain/stamp-install)


$(if $(call debug,toolchain,v),,.SILENT: $(toolchain/stamp-install))

.PRECIOUS: $(toolchain/stamp-install) # work around a make bug

toolchain/clean:=toolchain/stamp-install/clean
toolchain/stamp-install/clean: FORCE
	@rm -f $(toolchain/stamp-install)

#Compile all the toolchain subdirs.
$(eval $(call subdir,$(curdir)))
