#***************************************************************************#
#  Copyright (c) 2008-2011 Intel Corporation.
#
#  DISTRIBUTABLE AS SAMPLE SOURCE SOFTWARE
#
#  This Distributable As Sample Source Software is subject to the terms and
#  conditions of the Intel software License Agreement for the Intel(R) Media
#  Processor Software Development Kit.
#***************************************************************************#

#  The following forces /bin/bash as our shell.  This is because Ubuntu uses
#  dash, as the default shell (and has /bin/sh link to it), which does not
#  support the method we use to capture errors.txt during build.
ifneq ($(strip $(wildcard /bin/bash)),)
SHELL=/bin/bash
endif

tool_dirs:=$(patsubst %/Makefile,%,$(wildcard */Makefile))

export BUILD_BASE?=$(CURDIR)
export BUILD_MIRROR:= $(BUILD_BASE)/exports/lastbuilt

ifdef BUILD_DEST
dir_target_bin:=$(BUILD_DEST)/bin
dir_target_include:=$(BUILD_DEST)/include
dir_target_lib:=$(BUILD_DEST)/lib
dir_root:=$(BUILD_DEST)
 ifndef FSROOT
  ifneq ($(wildcard $(BUILD_DEST)../target/fsroot),)
FSROOT:=$(BUILD_DEST)../target/fsroot)
  endif
 endif
else
dir_target_bin:=$(BUILD_BASE)/exports/exported/bin
dir_target_include:=$(BUILD_BASE)/exports/exported/include
dir_target_lib:=$(BUILD_BASE)/exports/exported/lib
dir_root:=$(BUILD_BASE)/exports/exported
endif
ifndef FSROOT
FSROOT:=$(BUILD_BASE)/exports/fsroot
endif
dir_fs_bin:=$(FSROOT)/bin
dir_fs_include:=$(FSROOT)/include
dir_fs_lib:=$(FSROOT)/lib
dir_fs_root:=$(FSROOT)

ifndef SED
export SED=sed
endif

ifndef VER
export VER=<Unofficial Build>
export VER1=9
export VER2=9
export VER3=9
export VER4=9
endif

# build chain targets:
ifdef TARGETSIZE
export SIZE=$(TARGETSIZE)
endif
ifdef TARGETSTRIP
export STRIP=$(TARGETSTRIP)
endif
ifdef TARGETBISON
export BISON=$(TARGETBISON)
endif
ifdef TARGETCXXFILT
export CXXFILT=$(TARGETCXXFILT)
endif
ifdef TARGETYACC
export YACC=$(TARGETYACC)
endif
ifdef TARGETSTRINGS
export STRINGS=$(TARGETSTRINGS)
endif
ifdef TARGETGPROF
export GPROF=$(TARGETGPROF)
endif
ifdef TARGETAR
export AR=$(TARGETAR)
endif
ifdef TARGETCXX
export CXX=$(TARGETCXX)
endif
ifdef TARGETCC
export CC=$(TARGETCC)
endif
ifdef TARGETREADELF
export READELF=$(TARGETREADELF)
endif
ifdef TARGETM4
export M4=$(TARGETM4)
endif
ifdef TARGETGCC
export GCC=$(TARGETGCC)
endif
ifdef TARGETCPP
export CPP=$(TARGETCPP)
endif
ifdef TARGETOBJDUMP
export OBJDUMP=$(TARGETOBJDUMP)
endif
ifdef TARGETLD
export LD=$(TARGETLD)
endif
ifdef TARGETADDR2LINE
export ADDR2LINE=$(TARGETADDR2LINE)
endif
ifdef TARGETOBJCOPY
export OBJCOPY=$(TARGETOBJCOPY)
endif
ifdef TARGETRANLIB
export RANLIB=$(TARGETRANLIB)
endif
ifdef TARGETNM
export NM=$(TARGETNM)
endif
ifdef TARGETAS
export AS=$(TARGETAS)
endif

# defaults:
ifndef SIZE
export SIZE=size
endif
ifndef STRIP
export STRIP=strip
endif
ifndef BISON
export BISON=bison
endif
ifndef CXXFILT
export CXXFILT=c++filt
endif
ifndef YACC
export YACC=yacc
endif
ifndef STRINGS
export STRINGS=strings
endif
ifndef GPROF
export GPROF=gprof
endif
ifndef AR
export AR=ar
endif
ifndef CXX
export CXX=g++
endif
ifndef CC
export CC=gcc
endif
ifndef READELF
export READELF=readelf
endif
ifndef M4
export M4=m4
endif
ifndef GCC
export GCC=gcc
endif
ifndef CPP
export CPP=$(CC) -E
endif
ifndef OBJDUMP
export OBJDUMP=objdump
endif
ifndef LD
export LD=ld
endif
ifndef ADDR2LINE
export ADDR2LINE=addr2line
endif
ifndef OBJCOPY
export OBJCOPY=objcopy
endif
ifndef RANLIB
export RANLIB=ranlib
endif
ifndef NM
export NM=nm
endif
ifndef AS
export AS=as
endif

.PHONY:
.PHONY: all clean debug release doc install_dev install_target install test
.PHONY: tools deleteerrors $(tool_dirs)

#all: export CFLAGS+= -O1 -Wall -Werror
#all: export CFLAGS+= -O1 -Wall
#all: doc release test install_dev install_target
#	@echo "CC = " $(CC) " defined in " $(origin CC)
#	$(MAKE) install

# Note: the following export lines in target-specific variables seem to require GNU Make version 3.81 or later to work properly.
debug: export BUILD_VERSION:=debug
debug: tools test doc
# Note: the following export lines in target-specific variables seem to require GNU Make version 3.81 or later to work properly.
release: export BUILD_VERSION:=release
release: tools

clean: deleteerrors
	@$(foreach t,$(tool_dirs),$(MAKE) -C $(t) clean;)
	rm -rf $(BUILD_BASE)/exports

doc:

test:

install: install_dev install_target
install_dev:
	@echo "mkdir -p (dir_root)"
	mkdir -p $(dir_root)
ifneq ($(strip $(wildcard README_flash_encryption_signing_tools.txt)),)
	@cp -f README_flash_encryption_signing_tools.txt $(dir_root)/README_flash_encryption_signing_tools.txt
endif
	@echo "mkdir -p (dir_target_bin)"
	mkdir -p $(dir_target_bin)
	@$(foreach t,$(tool_dirs),$(MAKE) -C $(t) COPY_DESTINATION_BIN=$(dir_target_bin) copy;)

install_target:
	@echo "mkdir -p (dir_fs_root) [$(dir_fs_root)]"
	mkdir -p $(dir_fs_root)
	@echo "mkdir -p (dir_fs_bin) [$(dir_fs_bin)]"
	mkdir -p $(dir_fs_bin)
	@$(foreach t,$(tool_dirs),$(MAKE) -C $(t) COPY_DESTINATION_BIN=$(dir_fs_bin) copy;)

# Note: the following export lines in target-specific variables seem to require GNU Make version 3.81 or later to work properly.
tools: export CFLAGS+= -I$(dir_target_include)
tools: export LDFLAGS+= -L$(dir_target_lib)
tools: deleteerrors $(tool_dirs)

deleteerrors:
	@rm -f errors.txt

$(tool_dirs): deleteerrors
	@($(MAKE) -C $@ 3>&1 1>&2 2>&3- | tee -a errors.txt) 3>&1 1>&2 2>&3-
