#*******************************************************************************
#                               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 \
	${SRC_DIR}/sbtool	\
	${SRC_DIR}/keygen

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

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

CFLAGS	    = -g $(INC_PATH) -D${UNAMES}

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

OBJ_FILES_ELFTOSB2 = 		\
	${OBJ_FILES_COMMON} \
	StKeySet.o	\
	BootImageGenerator.o	\
	ConversionController.o	\
	ElftosbAST.o	\
	elftosb.o	\
	elftosb_lexer.o \
	ElftosbLexer.o	\
	elftosb_parser.tab.o	\
	EncoreBootImageGenerator.o \
	St3600IPL.o		\
	StLFSREncrypter.o	\
	crypto.o	\
	default_rom_key.o \
	SB36xxBootImageGenerator.o

OBJ_FILES_SBTOOL =			\
	${OBJ_FILES_COMMON} \
	EncoreBootImageReader.o \
	sbtool.o

OBJ_FILES_KEYGEN =			\
	${OBJ_FILES_COMMON} \
	keygen.o


LIBS =     -lstdc++


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


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

all: elftosb sbtool keygen

# 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} ${OBJ_FILES_SBTOOL} ${OBJ_FILES_KEYGEN} \
		${EXEC_FILE_ELFTOSB2} ${EXEC_FILE_SBTOOL} ${EXEC_FILE_KEYGEN}

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

sbtool: ${OBJ_FILES_SBTOOL}
	gcc ${OBJ_FILES_SBTOOL} ${LIBS} -o ${EXEC_FILE_SBTOOL}

keygen: ${OBJ_FILES_KEYGEN}
	gcc ${OBJ_FILES_KEYGEN} ${LIBS} -o ${EXEC_FILE_KEYGEN}


#ifeq ("${UNAMES}", "Linux")
#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 $@

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