#***************************************************************************#
#  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.
#***************************************************************************#

#***************************************************************************#
#  Optional Input Environment Variables:
#    CFLAGS, LDFLAGS   - any input CFLAGS or LDFLAGS you want to add
#    VER               - program version info (defaults to <Unofficial Build>)
#    BUILD_VERSION     - release or debug (defaults to debug)
#    BUILD_BASE        - base directory at which outputs generated
#                          - $BUILD_BASE/exports/$BUILD_VERSION/bin for binaries
#                          - $BUILD_BASE/exports/$BUILD_VERSION/objs for object files
#    BUILD_MIRROR      - optional additional place to copy compiled binaries to
#***************************************************************************#

.PHONY:
.PHONY: all tools clean copy

tool_name:=cssinput
tool_source:=CreateCSSInput.c

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

CFLAGS+= -DVER="\"$(VER)\""
CFLAGS+= -DVER1=$(VER1) -DVER2=$(VER2) -DVER3=$(VER3) -DVER4=$(VER4)
LDFLAGS+= -ldl -lcrypto

export BUILD_BASE?=$(CURDIR)

BUILD_EXPORTS:= $(BUILD_BASE)/exports

ifeq ($(BUILD_VERSION),)
BUILD_VERSION:=debug
endif
ifeq ($(BUILD_VERSION),debug)
CFLAGS+= -O0 -g3 -DDEBUG
LDFLAGS+= -g
OUTPUT_DIR:=$(BUILD_EXPORTS)/debug
else
CFLAGS+= -O1 -Wall -Werror
LDFLAGS+= -s
OUTPUT_DIR:=$(BUILD_EXPORTS)/release
endif

$(OUTPUT_DIR)/objs/%.o: %.c
	mkdir -p $(@D)
	$(CC) -c $(CFLAGS) $< -o $@

all: tools

clean:
	@rm -f $(OUTPUT_DIR)/bin/$(tool_name) $(addprefix $(OUTPUT_DIR)/objs/,$(tool_source:.c=.o))
ifdef BUILD_MIRROR
	@rm -f $(BUILD_MIRROR)/bin/$(tool_name) $(addprefix $(BUILD_MIRROR)/objs/,$(tool_source:.c=.o))
endif
	@rmdir --ignore-fail-on-non-empty $(OUTPUT_DIR)/bin $(OUTPUT_DIR)/objs; true
ifdef BUILD_MIRROR
	@rmdir --ignore-fail-on-non-empty $(BUILD_MIRROR)/bin $(BUILD_MIRROR)/objs; true
	@rmdir --ignore-fail-on-non-empty $(BUILD_MIRROR); true
endif
	@rmdir --ignore-fail-on-non-empty $(OUTPUT_DIR); true
ifdef BUILD_MIRROR
	@rmdir --ignore-fail-on-non-empty $(BUILD_MIRROR); true
endif
	@rmdir --ignore-fail-on-non-empty $(BUILD_EXPORTS); true
ifdef BUILD_MIRROR
	@rmdir --ignore-fail-on-non-empty $(BUILD_MIRROR); true
endif

copy:
ifdef BUILD_MIRROR
	@cp -f $(BUILD_MIRROR)/bin/$(tool_name) $(COPY_DESTINATION_BIN)
else
	@cp -f $(OUTPUT_DIR)/bin/$(tool_name) $(COPY_DESTINATION_BIN)
endif

tools: $(OUTPUT_DIR)/bin/$(tool_name)
$(OUTPUT_DIR)/bin/$(tool_name): $(addprefix $(OUTPUT_DIR)/objs/,$(tool_source:.c=.o))
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
ifdef BUILD_MIRROR
	@mkdir -p $(BUILD_MIRROR)
	@mkdir -p $(BUILD_MIRROR)/bin
	@cp -f $@ $(BUILD_MIRROR)/bin
endif

