
### Change the variable below, so they match your system ###

#Compiler
CC?=g++

#Path to your SCV installation (not mandatory)
SCV?=/usr/local/SystemC/SCV

#Path where to install ocpip package
# Note, this must exactly match what you have chosen
#  for the OCP socket kit!
INSTALLPATH?=/usr/local/ocpip

#Path to your systemc installation
SYSTEMC?=/usr/local/SystemC

#Path to your TLM2 installation
TLM2?=/usr/local/SystemC/TLM-2009-07-15
TLM2_UTILS?=$(TLM2)/include/tlm/tlm_utils

#Special flags you wanna pass to the compiler, e.g. -DNDEBUG or something along that line
EXTRAFLAGS?=

#the target architecture of your system
#  the default value tries to guess it from your systemc installation
#  if that fails, set it manually
TARGET_ARCH ?= $(subst $(SYSTEMC)/lib-,,$(wildcard $(SYSTEMC)/lib-*))

#### DO NOT MODIFY BELOW THIS LINE !#####

VERSION=$(shell cat ../recent_version.txt)
SRC_EXT=cpp
SOURCES=$(wildcard ./src/ocpip_dev/*.$(SRC_EXT))
OBJECTS=$(SOURCES:%.$(SRC_EXT)=%.o)
DIR=$(shell pwd)

TPPS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard src/ocpip_dev/*.tpp), $(INSTALLPATH)/adapters/$(file)))
HPPS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard src/ocpip_dev/*.hpp), $(INSTALLPATH)/adapters/$(file)))
CPPS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard src/ocpip_dev/*.cpp), $(INSTALLPATH)/adapters/$(file)))
BENCHES=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard tst/ocpip_dev/tb_*/bench.cpp), $(INSTALLPATH)/adapters/$(file)))
B_HEADERS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard tst/ocpip_dev/inc/*.h), $(INSTALLPATH)/adapters/$(file)))
V_HEADERS=$(subst ocpip_dev,ocpip_$(VERSION),$(foreach file, $(wildcard include/ocpip_dev/*.h), $(INSTALLPATH)/adapters/$(file)))
TARGETLIB=$(INSTALLPATH)/lib-$(TARGET_ARCH)/$(FULL_LIBNAME)
EXAMPLE_MKF=$(wildcard $(INSTALLPATH)/examples/Makefile.conf)

all: install

debug | opt : make_dirs $(V_HEADERS) $(BENCHES) $(B_HEADERS) bench_makefile $(TPPS) $(HPPS) $(CPPS) Makefile $(INSTALLPATH)/adapters/include/ocpip_adapters_$(VERSION).h

install: opt
	@echo $(shell cd $(INSTALLPATH)/adapters/include/; \
	sed s/_dev/`ls ocpip_adapters_*.h | sort | tail -n1 | sed s/ocpip_adapters// | sed s/.h//`/ $(DIR)/include/ocpip_adapters.h >ocpip_adapters.hh;);
	mv $(INSTALLPATH)/adapters/include/ocpip_adapters.hh $(INSTALLPATH)/adapters/include/ocpip_adapters.h;
	@echo Installation done

make_dirs:
	mkdir -p $(INSTALLPATH)/adapters/include/ocpip_$(VERSION);
	mkdir -p $(INSTALLPATH)/adapters/src/ocpip_$(VERSION);
	mkdir -p $(INSTALLPATH)/adapters/tst/ocpip_$(VERSION);

bench_makefile : ./tst/ocpip_dev/Makefile ./tst/ocpip_dev/Makefile.generic
	cp ./tst/ocpip_dev/Makefile.generic $(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)
	cp ./tst/ocpip_dev/Makefile $(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)
	@rm -f $(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo CC=$(CC) >$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf
	@echo SYSTEMC=$(SYSTEMC) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo SCV=$(SCV) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo TLM2=$(TLM2) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo TLM2_UTILS=$(TLM2_UTILS) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo EXTRAFLAGS=$(EXTRAFLAGS) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo TARGET_ARCH=$(TARGET_ARCH) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo OCPIP=$(INSTALLPATH) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;
	@echo OCP_VERSION=$(VERSION) >>$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/Makefile.conf;

$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/tb_%/bench.cpp : ./tst/ocpip_dev/tb_%/bench.cpp
	mkdir -p $(dir $@)
	cp $< $@

$(INSTALLPATH)/adapters/src/ocpip_$(VERSION)/%.tpp : ./src/ocpip_dev/%.tpp
	cp $< $@

$(INSTALLPATH)/adapters/src/ocpip_$(VERSION)/%.hpp : ./src/ocpip_dev/%.hpp
	cp $< $@

$(INSTALLPATH)/adapters/src/ocpip_$(VERSION)/%.cpp : ./src/ocpip_dev/%.cpp
	sed 's/_dev/_$(VERSION)/' < $< > $@

$(INSTALLPATH)/adapters/include/ocpip_$(VERSION)/%.h : ./include/ocpip_dev/%.h
	cp $< $@

$(INSTALLPATH)/adapters/tst/ocpip_$(VERSION)/inc/%.h : ./tst/ocpip_dev/inc/%.h
	mkdir -p $(dir $@)
	cp $< $@

$(INSTALLPATH)/adapters/include/ocpip_adapters_$(VERSION).h : ./include/ocpip_adapters_dev.h
	sed s/__OCPIP_ADAPTERS_DEV_H__/__OCPIP_ADAPTERS_$(VERSION)_H__/ $< | sed s/ocpip_dev/ocpip_$(VERSION)/ > $@

