# *****************************************************************
#
# LAB05C Makefile
#
# X86 makefile for lab05ab/c Contains rules for:
#
# - .x (x86 executable target)
# - .o (x86 object file)
# - .PHONY (all, clean and test)
# - Configuro
#
#
# Uses the following variables (built-in and user-defined):
#
# CC = C compiler (gcc)
# CFLAGS = compiler options (e.g. -g)
# LINKER_FLAGS = specify libraries to link in
# $@ = target
# $^ = all dependencies
# $< = first dependency only (not used in this solution)
#
# Generic "rule" terminology:
#
# target:dependency
# [TAB] <CMD1>
# [TAB] <CMD2>
#
# GCC options used in this makefile:
#
# -c = "compile only"
# -o = "output filename"
# -g = "debug mode"
#
# Note1: if you use a filename other than "makefile", use the
# 	 -f option to enable make to use it:
# 	 e.g.  make -f my_make_file.mak
#
# Note2: .PHONY tells gMake that the rule's target is not 
#        actually a file (to be created or searched for).
#
# Note3: When gMake runs without any rules specified on the
#        command line, it will make (by default) the FIRST
#        rule found in the makefile. Hence, it is common
#        to have a rule named "all" listed first (as below).
# 
# ***************************************************************


# -----------------------
# ------ includes -------
# -----------------------

-include ../../setpaths.mak


# --------------------------------
# ------ user-defined vars -------
# --------------------------------

CC := $(LINUX86_GCC)
CC_ROOT := $(LINUX86_DIR)
CFLAGS := -g
LINKER_FLAGS := -lstdc++

# --------------------------------
# ------ Configuro vars ----------
# --------------------------------
CONFIG := app_cfg
XDCROOT := $(XDC_INSTALL_DIR)
CONFIGURO := $(XDCROOT)/xs xdc.tools.configuro -b $(PWD)/../../config.bld
export XDCPATH:=/home/user/rtsc_primer/examples;$(XDCROOT)/packages
TARGET   := gnu.targets.Linux86
PLATFORM := host.platforms.PC

# ------------------------------------------------
# ----- always keep these intermediate files -----
# ------------------------------------------------
.PRECIOUS : $(CONFIG)/linker.cmd $(CONFIG)/compiler.opt


# -----------------------
# ------ make all -------
# -----------------------

# The "all" rule commonly specifies all executable targets to
# be built. Note, the user has full control over what the "all"
# rule builds. In this solution, we only have one target.
# However, in the final DaVinci makefile, you'll see more
# targets listed. 

.PHONY : all
all : app.x86U

# -------------------------------------------------
# --- delete the implicit rules for object files --
# -------------------------------------------------
%.o : %.c



# -----------------------------------
# ------ executable rule (.x) -------
# -----------------------------------

app.x86U : app.o $(CONFIG)/linker.cmd
	$(CC) $(CFLAGS) $(LINKER_FLAGS) $^ -o $@
	@echo; echo $@ successfully created; echo


# ---------------------------------------------------
# ------ intermeditate object files rule (.o) -------
# ---------------------------------------------------

%.o : %.c $(CONFIG)/compiler.opt
	$(CC) $(CFLAGS) $(shell cat $(CONFIG)/compiler.opt) -c $< -o $@


# ----------------------------------
# ----- Configuro Rule (.cfg) ------
# ----------------------------------

%/linker.cmd %/compiler.opt : %.cfg 
	@$(CONFIGURO) -c $(CC_ROOT) -t $(TARGET) -p $(PLATFORM) -o $(CONFIG) $<
	@echo "Configuro has completed; it's results are in $(CONFIG) " ; echo

# following rule is needed if no app_cfg.cfg file is avaialable - otherwise gMake crashes
%.cfg :
	touch $(CONFIG).cfg


# ----------------------
# ----- clean all ------
# ----------------------

# The "clean" rule should remove all files created by
# the makefile (e.g. the executables and intermediate
# files).

.PHONY : clean
clean :
	rm -rf app.x86U
	rm -rf app.o
	rm -rf $(CONFIG)
	


# -------------------------------------
# ----- basic debug for makefile ------
#
# -----     example only         ------
# -------------------------------------

# We use test to print out the user-defined variables to
# make sure they are set properly - this helps with 
# debugging makefile errors.

.PHONY : test
test:
	@echo CC = $(CC)	

	





