#
# Copyright (c) 2013 Qualcomm Atheros, Inc.
# Copyright (C) 2006-2011 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# Main makefile for the host tools
#
curdir:=tools

# subdirectories to descend into
tools-y :=
ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN)$(CONFIG_GCC_LLVM),)
tools-y += gmp mpfr mpc libelf
endif
tools-y += m4 libtool autoconf automake flex bison pkg-config sed mklibs
tools-y += sstrip ipkg-utils genext2fs e2fsprogs mtd-utils mkimage
tools-y += firmware-utils patch-cmdline quilt yaffs2 flock padjffs2
tools-y += mm-macros xorg-macros xfce-macros missing-macros xz cmake scons
tools-$(CONFIG_TARGET_orion_generic) += wrt350nv2-builder upslug2
tools-$(CONFIG_powerpc) += upx
# tools-$(CONFIG_TARGET_x86) += qemu
tools-$(CONFIG_TARGET_brcm2708) += mtools dosfstools
ifneq ($(CONFIG_TARGET_ar71xx),)
tools-y += lzma-old squashfs
endif
tools-y += lzma squashfs4
tools-y += makefun
tools-$(CONFIG_KERNEL_SPARSE) += sparse
tools-$(CONFIG_USE_NAND_FLASH) += mstarbin

ifneq ($(CONFIG_PACKAGE_firmwarehotplug),)
tools-y += sdcc
endif

ifdef CONFIG_GCC_USE_GRAPHITE
  ifeq ($(CONFIG_GCC_USE_SYSTEM_PPL_CLOOG),)
	tools-y += ppl cloog
	$(curdir)/cloog/compile := $(curdir)/ppl/install
  endif
endif

# builddir dependencies
$(curdir)/bison/compile := $(curdir)/flex/install
$(curdir)/flex/compile := $(curdir)/m4/install
$(curdir)/pkg-config/compile := $(curdir)/sed/install
$(curdir)/libtool/compile := $(curdir)/sed/install $(curdir)/m4/install $(curdir)/autoconf/install $(curdir)/automake/install $(curdir)/missing-macros/install
$(curdir)/squashfs/compile := $(curdir)/lzma-old/install
$(curdir)/squashfs4/compile := $(curdir)/xz/install
$(curdir)/quilt/compile := $(curdir)/sed/install $(curdir)/autoconf/install
$(curdir)/dtc/compile := $(curdir)/bison/install
$(curdir)/autoconf/compile := $(curdir)/m4/install
$(curdir)/automake/compile := $(curdir)/m4/install $(curdir)/autoconf/install $(curdir)/pkg-config/install $(curdir)/xz/install
$(curdir)/gmp/compile := $(curdir)/automake/install
$(curdir)/mpc/compile := $(curdir)/mpfr/install $(curdir)/gmp/install
$(curdir)/mpfr/compile := $(curdir)/gmp/install
$(curdir)/ppl/compile := $(curdir)/gmp/install
$(curdir)/cloog/compile := $(curdir)/ppl/install
$(curdir)/mtd-utils/compile := $(curdir)/e2fsprogs/install $(curdir)/xz/install
$(curdir)/mkimage/compile := $(curdir)/sed/install
$(curdir)/qemu/compile := $(curdir)/e2fsprogs/install
$(curdir)/upslug2/compile := $(curdir)/automake/install
$(curdir)/mm-macros/compile := $(curdir)/automake/install
$(curdir)/xorg-macros/compile := $(curdir)/automake/install
$(curdir)/xfce-macros/compile := $(curdir)/automake/install
$(curdir)/missing-macros/compile := $(curdir)/autoconf/install
$(curdir)/e2fsprogs/compile := $(curdir)/automake/install
$(curdir)/libelf/compile := $(curdir)/automake/install
$(curdir)/sdcc/compile := $(curdir)/bison/install

ifneq ($(CONFIG_CCACHE),)
$(foreach tool, $(tools-y), $(eval $(curdir)/$(tool)/compile += $(curdir)/ccache/install))
tools-y += ccache
endif

$(curdir)/builddirs := $(tools-y) $(tools-dep) $(tools-)
$(curdir)/builddirs-default := $(tools-y)

ifndef DUMP_TARGET_DB
define PrepareStaging
	@for dir in $(1); do ( \
		$(if $(QUIET),,set -x;) \
		mkdir -p "$$dir"; \
		cd "$$dir"; \
		mkdir -p bin lib include stamp; \
	); done
endef

# preparatory work
$(STAGING_DIR)/.prepared: $(TMP_DIR)/.build
	$(call PrepareStaging,$(STAGING_DIR))
	mkdir -p $(BUILD_DIR)/stamp
	touch $@

$(STAGING_DIR_HOST)/.prepared: $(TMP_DIR)/.build
	$(call PrepareStaging,$(STAGING_DIR_HOST))
	mkdir -p $(BUILD_DIR_HOST)/stamp $(STAGING_DIR_HOST)/include/sys
	$(INSTALL_DATA) $(TOPDIR)/tools/include/*.h $(STAGING_DIR_HOST)/include/
	$(INSTALL_DATA) $(TOPDIR)/tools/include/sys/*.h $(STAGING_DIR_HOST)/include/sys/
	ln -sf lib $(STAGING_DIR_HOST)/lib64
	touch $@


define PrepareCommand
$(STAGING_DIR_HOST)/bin/$(1): $(STAGING_DIR)/.prepared
	@mkdir -p "$$(dir $$@)"; rm -f "$$@"
	@export FILE="$$$$(which $(2) 2>/dev/null | grep -v 'not found' | head -n1)"; [ -n "$$$$FILE" ] || { \
		echo "Command $(1) not found."; false; \
	}; ln -s "$$$$FILE" "$$@"

endef
endif

$(STAGING_DIR_HOST)/bin/stat: $(STAGING_DIR)/.prepared
	@rm -f $@
	@if stat --version > /dev/null 2>&1; then \
		ln -s `which stat` $@; \
	elif gstat --version > /dev/null 2>&1; then \
		ln -s `which gstat` $@; \
	else \
		echo "GNU stat not found"; \
		false; \
	fi

$(eval $(call PrepareCommand,find,gfind find))
$(eval $(call PrepareCommand,md5sum,gmd5sum md5sum $(SCRIPT_DIR)/md5sum))
$(eval $(call PrepareCommand,cp,gcp cp))
$(eval $(call PrepareCommand,seq,gseq seq))
$(eval $(call PrepareCommand,python,python2 python))

$(curdir)/cmddeps = $(patsubst %,$(STAGING_DIR_HOST)/bin/%,find md5sum cp stat seq python)
$(curdir)//prepare = $(STAGING_DIR)/.prepared $(STAGING_DIR_HOST)/.prepared $($(curdir)/cmddeps)
$(curdir)//compile = $(STAGING_DIR)/.prepared $(STAGING_DIR_HOST)/.prepared $($(curdir)/cmddeps)

# prerequisites for the individual targets
$(curdir)/ := .config prereq
$(curdir)//install = $(1)/compile

# Modified by ChenQiang
#$(eval $(call stampfile,$(curdir),tools,install,,CONFIG_CCACHE CONFIG_powerpc CONFIG_GCC_VERSION_4_5 CONFIG_GCC_USE_GRAPHITE CONFIG_TARGET_orion_generic))
#$(eval $(call subdir,$(curdir)))

#Define TOOLS backup variables.
TOOLS_BACKUP:=1
HOST_PLATFORM_BIT := $(shell getconf LONG_BIT)
TOOLS_DIR_NAME := $(notdir $(STAGING_DIR_HOST))
TOOLS_BACKUP_FILENAME := $(TOOLS_DIR_NAME)_$(HOST_PLATFORM_BIT).tar.bz2
TOOLS_BACKUP_FILE_EXISTED := $(wildcard $(TOPDIR)/tools/$(TOOLS_BACKUP_FILENAME))

PARAM5 := CONFIG_CCACHE CONFIG_powerpc CONFIG_GCC_VERSION_4_5 CONFIG_GCC_USE_GRAPHITE CONFIG_TARGET_orion_generic
tools/stamp-install := $(STAGING_DIR)/stamp/.tools_install$(if $(PARAM5),_$(call confvar,$(PARAM5)))
ifneq ($(HOST_OS),Linux)
  HOST_TAR:=trapret 2 $(TAR)
else
  HOST_TAR:=$(TAR)
endif

#Define tools compress command.
define TOOLS_BACKUP_COMPRESS_COMMAND
	cd $(TOPDIR)/staging_dir;\
	$(HOST_TAR) -jcvf $(TOPDIR)/tools/$(TOOLS_BACKUP_FILENAME) $(TOOLS_DIR_NAME);
endef

#Define tools decompress command.
define TOOLS_BACKUP_DECOMPRESS_COMMAND
	$(HOST_TAR) -jxvf $(TOPDIR)/tools/$(TOOLS_BACKUP_FILENAME) -C $(TOPDIR)/staging_dir 
endef
 
ifeq ($(TOOLS_RECOMPILE), 1) # TOOLS_RECOMPILE == 1.
TOOLS_BACKUP_FILE_EXISTED :=
TOOLS_COMPILE_FLAG := 1
else
TOOLS_BACKUP_FILE_EXISTED := $(wildcard $(TOPDIR)/tools/$(TOOLS_BACKUP_FILENAME))
#If 0, means that .tools_install_... is the newest file; otherwise, it isn't the newest file.
TOOLS_COMPILE_FLAG := $(shell $(SCRIPT_DIR)/timestamp.pl -n $(tools/stamp-install) tools > /dev/null;echo $$?)
#.tools_install_nnnnn isn't in $(STAGING_DIR_HOST), so it need to judge whether $(STAGING_DIR_HOST) existes.
ifeq ($(wildcard $(STAGING_DIR_HOST)), )
TOOLS_COMPILE_FLAG := 1
endif
endif

$(tools/stamp-install): $(TMP_DIR)/.build
ifeq ($(TOOLS_RECOMPILE), 1) #[A] TOOLS_RECOMPILE == 1.
	rm -rf $(BUILD_DIR_HOST) $(STAGING_DIR_HOST)
ifeq ($(TOOLS_BACKUP), 1) #[B] Open toochain optimization switch.
	rm -f $(TOPDIR)/tools/$(TOOLS_BACKUP_FILENAME)
endif #[B] End of ifeq ($(TOOLS_BACKUP), 1)
endif #[A] End of TOOLS_RECOMPILE == 1.
	
ifneq ($(TOOLS_BACKUP), 1) #[1] Close tools optimization switch.
ifneq ($(TOOLS_COMPILE_FLAG), 0) #[2] TOOLS need to compile all tools.
	$(MAKE) $(if $(QUIET),--no-print-directory) $(tools/flags-install) tools/install
endif #[2] End of ifeq ($(TOOLS_COMPILE_FLAG), )
	
else #[1] Open toochain optimization switch.
ifeq ($(TOOLS_BACKUP_FILE_EXISTED), ) #[2] The backup file doesn't exist.
ifneq ($(TOOLS_COMPILE_FLAG), 0) #[3] TOOLS need to compile all tools.
	echo "TOOLS_BACKUP_OPEN, TOOLS_BACKUP_FILE_NOT_EXISTED, TOOLS_COMPILE."
	$(MAKE) $(if $(QUIET),--no-print-directory) $(TOOLS/flags-install) tools/install
else #[3] Tools doesn't need to compile any tool.
	echo "TOOLS_BACKUP_OPEN, TOOLS_BACKUP_FILE_NOT_EXISTED, TOOLS_NOT_COMPILE."
endif #[3] End of ifeq ($(TOOLS_COMPILE_FLAG), )
	$(TOOLS_BACKUP_COMPRESS_COMMAND)
	 
else #[2] The backup file existes.
ifneq ($(TOOLS_COMPILE_FLAG), 0) #[3] TOOLS need to update all tools.
	$(TOOLS_BACKUP_DECOMPRESS_COMMAND) 
else #[3] Tools has been installed in the staging_dir dircetory.
	#@echo "TOOLS_BACKUP_OPEN, TOOLS_BACKUP_FILE_EXISTED, DON'T DECOMPRESS."
endif #[3] End of ifneq ($(TOOLS_COMPILE_FLAG), 0)
endif #[2] End of ifeq ($(TOOLS_BACKUP_FILE_EXISTED), )
endif #[1] End of ifneq ($(TOOLS_BACKUP), 1)
	mkdir -p $$(dirname $(tools/stamp-install))
	touch $(tools/stamp-install)


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

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

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

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