#*******************************************************************************
#                               makefile.rules
# Description:
#   gnu make makefile rules for elftosb executable.  make needs to be called
#   with the following command:
#
#   make -C ${BUILD_DIR} -f ${SRC_DIR}/makefile.rules SRC_DIR=${SRC_DIR} $@;
#
#   SRC_DIR needs to be passed in.  It is assumed that make is running in
#   the build directory.

#*******************************************************************************
#                               Environment

# UNAMES is going to be set to either "Linux" or "CYGWIN_NT-5.1"
UNAMES = $(shell uname -s)

#*******************************************************************************
#                               Directories

#*******************************************************************************
#                               Paths

# search path for source files. make finds them automatically.
VPATH = \
	${SRC_DIR}/common	\
	${SRC_DIR}/elftosb2

# include directories
INC_PATH =                      \
    -I${SRC_DIR}/elftosb2        \
	-I${SRC_DIR}/common

#*******************************************************************************
#                               Build flags
# gcc Compiler flags
#    -g						: Produce debugging information.

#CFLAGS	    = -g $(INC_PATH) -D${UNAMES} -Wno-multichar -Wno-format-extra-args
CFLAGS	    = $(INC_PATH) -D${UNAMES} -Wno-multichar -Wno-format-extra-args -std=gnu++11

#*******************************************************************************
#                               File lists

OBJ_FILES_COMMON =			\
	AESKey.o	\
	Blob.o	\
	crc.o	\
	DataSource.o	\
	DataTarget.o	\
	ELFSourceFile.o \
	EncoreBootImage.o	\
	EvalContext.o	\
	GHSSecInfo.o	\
	GlobMatcher.o	\
	HexValues.o \
	Logging.o	\
	Operation.o \
	OptionDictionary.o	\
	options.o	\
	OutputSection.o \
	Random.o	\
	RijndaelCBCMAC.o	\
	RijndaelCTR.o	\
	rijndael.o	\
	SHA1.o	\
	SourceFile.o	\
	SRecordSourceFile.o \
	stdafx.o	\
	StELFFile.o \
	StExecutableImage.o \
	StSRecordFile.o \
	Value.o \
	Version.o \
	format_string.o \
	ExcludesListMatcher.o \
	SearchPath.o	\
	DataSourceImager.o \
	IVTDataSource.o \
	EncoreBootImageReader.o

OBJ_FILES_ELFTOSB2 = 		\
	${OBJ_FILES_COMMON} \
	BootImageGenerator.o	\
	ConversionController.o	\
	ElftosbAST.o	\
	elftosb.o	\
	elftosb_lexer.o \
	ElftosbLexer.o	\
	elftosb_parser.tab.o	\
	EncoreBootImageGenerator.o \
	Keyblob.o \
	bytes_aes.o \
	aes128_key_wrap_unwrap.o


LIBS =     -lstdc++ -lm


ifeq ("${UNAMES}", "Linux")
EXEC_FILE_ELFTOSB2 = elftosb
else 
ifeq ("${UNAMES}", "CYGWIN_NT-5.1")
EXEC_FILE_ELFTOSB2 = elftosb.exe
endif # ifeq ("${UNAMES}", "CYGWIN_NT-5.1")
endif # ifeq ("${UNAMES}", "Linux")


#*******************************************************************************
#                                 Targets

all: elftosb

# Uncomment the next line to print out the environment variables.
all: exec_always

exec_always:
	@echo "SRC_DIR = ${SRC_DIR}"
	@echo "OBJ_FILES = ${OBJ_FILES_ELFTOSB2}"
	@echo "LIBS = ${LIBS}"
	@echo "EXEC_FILE = ${EXEC_FILE}"
	@echo "BUILD_DIR = ${BUILD_DIR}"

clean:
	rm -f ${OBJ_FILES_ELFTOSB2} \
		${EXEC_FILE_ELFTOSB2}

elftosb: ${OBJ_FILES_ELFTOSB2}
	gcc ${OBJ_FILES_ELFTOSB2} ${LIBS} -o ${EXEC_FILE_ELFTOSB2}


#ifeq ("${UNAMES}", "Linux")
# Use default rules for creating all the .o files from the .c files.  Only
# for linux
.SUFFIXES : .c .cpp

.c.o :
	gcc ${CFLAGS} -c $<

.cpp.o :
	gcc ${CFLAGS} -c $<

#endif

#*******************************************************************************
#                       Automatic dependency generation

%.d: %.c
	@set -e; \
	$(CC) -MM $(CFLAGS) $< | \
	sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ]  || rm -f $@

%.d: %.cpp
	@set -e; \
	$(CC) -MM $(CFLAGS) $< | \
	sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ]  || rm -f $@

#*******************************************************************************
