#CPPFLAGS = -g -ggdb -Wall
CPPFLAGS = -Wall -O3
#CPPFLAGS = -pg -g -ggdb -Wall -O3

OBJFILES = decode.o sparse_memory.o execute.o pretty_print.o globals.o arch-listeners.o
ARCHOBJFILES = arch-model.o
PIPELINEDOBJFILES = pipelined-model.o circuit.o ifetch.o decode_stage.o \
	frontend_delay_stage.o rename_stage.o scheduler.o exec_unit.o \
	rob.o rename_tables.o free_list.o \
	lsq_pf.o lsq_packets.o

DUMPOBJFILES = objdump.o

SOURCES = $(patsubst %.o,%.cc, $(OBJFILES) $(ARCHOBJFILES) $(PIPELINEDOBJFILES) $(DUMPOBJFILES))

install: arch-model pipelined-model objdump

arch-model: $(ARCHOBJFILES) $(OBJFILES)
	g++ -o $@ $^

pipelined-model: $(PIPELINEDOBJFILES) $(OBJFILES)
	g++ -o $@ $^

objdump: $(DUMPOBJFILES) $(OBJFILES)
	g++ -o $@ $^

sim_endian.h: sim_endian.h.in show.endian
	sed -e "s,\@calculated_endianness\@,`./show.endian`," < $< > $@

.INTERMEDIATE: show.endian

show.endian: endian.c
	gcc -Wall -o $@ $<

%.html: %.texinfo
	makeinfo --no-split --html $<

# NOTE: To get good results from dvips one should create a ~/.dvipsrc
# file with the lines:
#
# p +psfonts.cmz
# p +psfonts.amz

%.dvi: %.tex
	latex $<
	latex $<

%.ps: %.dvi
	dvips -f $< > $@

%.pdf: %.ps
	ps2pdf $<

.PHONY: clean depend install

# We don't want Makefile.dep to depend on $(SOURCES) because then it
# would get remade every time we touched a .c file.  Instead we call
# make depend only when the Makefile itself changes (presumably
# because the user added another cc file to $(SRCS)).  We can't just
# call 'g++ -MM $(SOURCES)' because some of the sources might actually
# live in the VPATH. ...
Makefile.dep: Makefile
	make depend

# ... So instead "depend" depends on $(SOURCES) and we use $^ in the
# rule.  The nice attribute of $^ is that it is the list of
# dependencies, $(SOURCES) in this case, including the VPATH directory
# in which they were found.  NOTE: the sunpro compiler uses the flags
# -xM and -xM1 instead of -M and -MM.  (-MM doesn't follow system
# header files like -M does)
depend: $(SOURCES) sim_endian.h
	-g++ -MM $(CFLAGS) $(filter-out %.h,$^) > Makefile.dep

# if we are NOT running "make depend" then include Makefile.dep (which
# will be made if it does not exist).  If we are running "make depend"
# then we DON'T include Makefile.dep, we just generate it (see the two
# rules above).  Also don't include it for "make clean" because we're
# just going to blow it away as soon as we build it.
ifneq ($(MAKECMDGOALS),depend)
ifneq ($(MAKECMDGOALS),clean)
include Makefile.dep
endif
endif

clean:
	-rm $(OBJFILES) $(ARCHOBJFILES) $(PIPELINEDOBJFILES) $(DUMPOBJFILES) $(TESTTRACEFILES) sim_endian.h Makefile.dep

very-clean: clean
	-rm arch-model pipelined-model objdump
