###############################################################################
# Copyright (c) 2012-2012 Integrated Device Technology, Inc.
# All Rights Reserved
#
# This file is provided for use with IDT products only, and does not convey any
# license, whether express or implied, to any intellectual property rights of
# Integrated Device Technology, Inc.  Distribution, copying or modification of
# the contents of this file and/or any derivatives thereto is not permitted,
# except as specifically authorized in writing. This notice shall be included
# in its entirety on all copies made from this file.
#
#
# The contents herein are the proprietary and confidential information
# of Integrated Device Technology Inc. and shall not be distributed or
# furnished to third parties or made public without prior written
# permission by Integrated Device Technology Inc.  Disclosures of this
# file in any form to the United States Government may not be made
# without a written predetermination that it will be protected by
# limited or restricted rights as set forth in the applicable Federal
# Acquisition Regulations or other equivalent governmental rule or
# regulation.
#
# The contents herein are provided "as is" and without any express or implied
# warranties, including, without limitation, the implied warranties of
# merchantability and fitness for a particular purpose.
#
# Last modified: <lastCommitDate>
# Release: <releaseName>
# Id: <gitCommitHash>
#
###############################################################################

#
# This is top-level device driver unit test Makefile
#

##############################################################################
# B U I L D    E N V I R O N M E N T
##############################################################################
HOST_PLATFORM := $(shell uname | cut -d _ -f 1 | tr [:upper:] [:lower:])

# If HARDWARE_TARGET is defined then we'll append it to the host platform 
# that we are running on.
ifdef HARDWARE_TARGET
TARGET := $(HOST_PLATFORM).$(HARDWARE_TARGET)
else
TARGET := $(HOST_PLATFORM)
endif

# Include host/target specific options
OSADAPT_PLATFORMDIR := ../osAdaptation/$(TARGET)
include ../osAdaptation/$(TARGET)/osConfig.mk

# Device
DEVICE := 82V3910

VERBOSE := @

##############################################################################
# Include directories 
INCL_DIRS += \
             ../$(DEVICE)/include \
             $(OSADAPT_PLATFORMDIR)/include \
             ../common/include \
             ../common/hlapi/include \
             ../common/test \
             ../common/access/sim \
             ../apll/include \
             ../apll/access/sim \
             ../$(DEVICE)/dev/include \
             ../sample/include

# Compiler Options
CFLAGS += \
          $(foreach i,$(INCL_DIRS),-I$(i)) \
          -g \
          -Wall \
          -ansi \
          -pedantic \
          -fprofile-arcs \
          -ftest-coverage

TEMP_CFLAGS += \
          -Werror

##############################################################################
# Static Libraries
STATIC_LIBS = \

# Shared Libraries
SHARED_LIBS = \
               check

# Library make targets 
LIB_MKTGTS := \
              $(foreach i,$(STATIC_LIBS), lib$(i)) \
              $(foreach i,$(SHARED_LIBS), lib$(i))

# LIbrary search paths
LD_DIRS = \
          lib

# Linker Options
LDFLAGS := \
           $(foreach i,$(LD_DIRS),-L$(i)) \
           -Wall \
           -fprofile-arcs

##############################################################################
# I N P U T    A R T I F A C T S
##############################################################################

VPATH += \
         . \
         $(OSADAPT_PLATFORMDIR)/src \
         ../common/src \
         ../common/hlapi/src \
         ../common/test \
         ../common/access/sim \
         ../apll/src \
         ../apll/test \
         ../apll/access/sim \
         ../$(DEVICE)/dev/src \
         ../sample/src

DRV_SOURCE := \
              General.c \
              Hal.c \
              Input.c \
              Output.c \
              DpllCnfg.c \
              inputFreq.c \
              Apll.c \
              ApllApi.c \
              ApllFreqProfile.c \
              osAdapt.c \
              ReadWrite_sim.c \
              ApllReadWrite_sim.c \
              82V3910.c

SOURCE:= \
         $(DRV_SOURCE) \
         check_General.c \
         check_Output.c \
         check_Input.c \
         check_DpllCnfg.c \
         check_ApllApi.c \
         check_inputFreq.c \
         check_main.c

##############################################################################
# O U T P U T    A R T I F A C T S
##############################################################################
OBJ_DIR := obj
OBJS := $(addprefix $(OBJ_DIR)/,$(notdir $(SOURCE:.c=.o)))

BIN_DIR := bin
PROGNAME := $(BIN_DIR)/test$(PROG_EXT)

LOG_FILENAME := test.log

GCOV := gcov
GCOV_DIR := cov
GCOV_FILES := $(notdir $(SOURCE:.c=.c.gcov))
GCNO_FILES := $(addprefix $(OBJ_DIR)/,$(notdir $(SOURCE:.c=.gcno)))
GNDA_FILES := $(addprefix $(OBJ_DIR)/,$(notdir $(SOURCE:.c=.gcda)))
##############################################################################
# M A K E F I L E    T A R G E T S
##############################################################################
.PHONY: all clean gcov

all: $(PROGNAME)
	@$(RM) $(GNDA_FILES)

$(OBJ_DIR)/%.o : %.c
	@echo "Compiling $(notdir $<)"
	$(VERBOSE)$(CC) $(CFLAGS) -c -o $@ $<

$(PROGNAME): $(OBJS)
	@echo "Linking with Shared"
	$(VERBOSE)$(LD) $(LDFLAGS) $(OBJS) $(foreach i,$(SHARED_LIBS),-l$(i)) -o $(PROGNAME)

$(GNDA_FILES) : $(PROGNAME)
	$(RM) $(GNDA_FILES)
	$(PROGNAME)

%.c.gcov : $(OBJ_DIR)/%.gcda
	$(GCOV) -o $(OBJ_DIR) $(basename $<).c

profile: $(GCOV_FILES)

clean:
	@echo "Removing build artifacts"
	$(VERBOSE)$(RM) $(OBJS) $(PROGNAME) 
	$(VERBOSE)$(RM) $(LOG_FILENAME) *~
	$(VERBOSE)$(RM) $(GCOV_FILES) $(GCNO_FILES) $(GNDA_FILES)
