# Authors			: Rosière Mathieu
# Modif.   (yyyy/mm/dd)		: 2005/04/04
# Creation (yyyy/mm/dd) 	: 2005/04/04

SIMULATOR			= systemc

#--------------------------------------
# Commands
#--------------------------------------
RM				= rm -fr
ECHO				= echo
GTKWAVE				= gtkwave
TIME				= time
MKDIR				= mkdir -p
DATE				= `date +%Y%m%d-%H%M%S`
PID				= $$$$
CXXFILT				= c++filt
CCACHE				=

#--------------------------------------
# Compilator
#--------------------------------------
CXX				= $(CCACHE) g++
#CXX				= g++ -fprofile-arcs -ftest-coverage

#DEBUG_LVL			= DEBUG_NONE
#DEBUG_LVL			= DEBUG_INFO
#DEBUG_LVL			= DEBUG_STAT
#DEBUG_LVL			= DEBUG_TRACE
#DEBUG_LVL			= DEBUG_MAX
#DEBUG_LVL			= DEBUG_ALL

DEBUG_TIME			= 
#-DDEBUG_TIME_MIN=45000 -DDEBUG_TIME_MAX=50000
DEBUG				= -g3
# -DDEBUG=$(DEBUG_LVL) $(DEBUG_TIME)
#				  -DM_CPU_COUNT_CALL_GENMEALY

STAT				= 
#-DM_CPU_STAT

OPT		 		= -O3

OTHER_systemc			=

OTHER_systemcass		= -DNONAME_RENAME		\
				  -ansi                       	\
		                  -Wno-long-long              	\
				  -DCHECK_MULTIWRITING2REGISTER

OTHER_SPECIFIC			= $(OTHER_$(SIMULATOR))

OTHER_COMMON			= -Wall				\
				  -Wno-deprecated 		\
				  -Wno-non-template-friend	\
				  -Wno-pmf-conversions 		\
				  -Wunused        		\
				  -DKANE			\
				  -D$(SIMULATOR)

#				  -Wfatal-errors 		\

# 		                  -pedantic                   	\
# 				  -Werror			\

OTHER				= $(OTHER_COMMON) $(OTHER_SPECIFIC)

#--------------------------------------
# variable
#--------------------------------------
REP_SRC				= src

REP_OBJ				= obj
REP_BIN				= bin
REP_INC				= include

# Directory to include
INCDIR	 			= -I. -I.. 					\
				  -I$(TOOLS)/$(SIMULATOR)/include 		\
				  -I$(IPS) 					\
				  -I$(REP_INC) 					\
				  -I$(M_CPU_CONFIGURATION_DIRECTORY)

LIBDIR 				= -L. -L.. 					\
				  -L$(TOOLS)/$(SIMULATOR)/lib-$(TARGET_ARCH) 	\
				  -L$(TOOLS)/soclib/lib 			\
				  -L$(TOOLS)/or1k/lib 				

LIBS		   		= -lm -lsystemc -lsoclib -lbfd -liberty -ldl
# target architecture
TARGET_ARCH	 		= linux

MODULE 				= system
EXE    				= $(REP_BIN)/$(MODULE)_$(ID)

# source files
MAIN				= main_sc
OBJS 				= $(patsubst $(REP_SRC)/%.cpp,$(REP_OBJ)/%_$(ID).o,$(wildcard $(REP_SRC)/*.cpp))

CXX_OPT				= $(DEBUG) $(STAT) $(OPT) $(OTHER)           $(INCDIR) 
LXX_OPT                         = $(DEBUG) $(STAT) $(OPT) $(OTHER) -rdynamic $(LIBDIR)

#--------------------------------------
# Règles
#--------------------------------------
.SUFFIXES			: .cc .cpp .o

# 2 règles : La première pour tester la présence de la date
#            La seconde  pour générer l'executable

all	 			: id

all2				: test_env $(EXE)

# Test créer la variable d'environnement
id				:
ifeq ($(origin M_CPU_CONFIGURATION_DIRECTORY), undefined)
				$(error "variable M_CPU_CONFIGURATION_DIRECTORY is undefined");
endif
ifeq ($(origin M_CPU_CONFIGURATION_FILENAME), undefined)
				$(error "variable M_CPU_CONFIGURATION_FILENAME is undefined");
endif
ifeq ($(origin ID), undefined)
				@make all2 ID=$(DATE)"-"$(PID)
else
				@make all2
endif

# Test des variables d'environnements
test_env			: $(M_CPU_CONFIGURATION_DIRECTORY)/$(M_CPU_CONFIGURATION_FILENAME)
ifeq ($(origin IPS), undefined)
				$(error "variable IPS     is undefined");
endif
ifeq ($(origin TOOLS), undefined)
				$(error "variable TOOLS   is undefined");
endif
				@$(ECHO) "$(ID) : Create directory"
				@$(MKDIR) $(REP_OBJ) $(REP_BIN)

# Création du fichier exe

$(EXE)				: $(OBJS)
				@$(ECHO) "$(ID) : Compilation of execute"
				$(CXX) $(LXX_OPT) -o $@    $< $(LIBS) 2>&1 | $(CXXFILT)

# Fichier objet
$(REP_OBJ)/%_$(ID).o		: $(REP_SRC)/%.cpp
				@$(ECHO) "$(ID) : Compilation of files $@"
				$(CXX) $(CXX_OPT) -o $@ -c $<         2>&1 | $(CXXFILT)

# Maintenance
clean 				:
				@$(ECHO) "Delete temporary files"
				@$(RM) $(REP_OBJ) *~ $(REP_SRC)/*~ $(REP_INC)/*~ *.trace tty*
				@$(RM) $(REP_BIN)
