# ------------------------------------------------------------------------
# Makefile: module 'sim' of the SpecC system
#	    (run-time simulation library)
# ------------------------------------------------------------------------
#
# Modifications: (most recent first)
#
# 06/15/04 PC   Adjustments for scrc 2.0
# 11/19/02 RD	fixed SCCOPT definition
# 10/03/02 RD	separated 'clean' target into 'clean' and 'distclean'
# 08/14/02 RD	added par7, pipe12, try32
# 07/10/03 RD	added fsm0, fsm1, fsm2, fsm3, fsm4, fsm5, fsm6
# 06/27/02 RD	added notifyone8, notifyone9, notifyone10
# 06/26/02 RD	added par5, par6, pipe10, pipe11, try30, try31, wait5, wait6
# 04/09/02 RD	replaced sim.sed with sim.h and removed preprocessing step
# 02/27/02 RD	added signals.h (support of 'buffered' and 'signal' types)
# 12/07/01 RD	added Win32Thread files and support for thread selection
# 08/02/01 RD	removed hardcoding of pthread by use of $(THREAD_BASE)
# 06/04/01 RD	added README file in testbench directory
# 05/25/01 RD	renamed 'scc' to 'scrc'
# 05/16/01 RD	added thread.cc for separation of common/specific threads
# 05/15/01 RD	added waitfor6
# 05/12/01 RD	added par4, pipe8, pipe9, try29, waitfor4, waitfor5
# 05/11/01 RD	added try24, try25, try26, try27, try28
# 05/10/01 RD	added try22, try23
# 05/09/01 RD	added try20, try21
# 05/08/01 RD	added try13, try14, try15, try16, try17, try18, try19
# 05/07/01 RD	added notifyone6, notifyone7, try0, try1, try2, try3, try4,
#		try5, try6, try7, try8, try9, try10, try11, try12
# RD 05/01/01	added missing copy-rule for sim.h under Windows
# RD 04/23/01	fix for allowing spaces in $(LLONG) definition
# RD 04/17/01	introduced sim.sed, preprocessed to sim.h in order to allow
#		switching between native and non-native 'long long' types
# RD 04/16/01	added liblonlong to be linked against
# RD 04/06/01	added linking against libpthread for sim_debug
# RD 04/06/01	added wait2, wait3, wait4
# RD 04/05/01	added the examples in the testbench directory
# RD 03/28/01	added template file "piped.h"
# RD 03/27/01	made 'make test' independent from $SPECC in the shell
# RD 03/26/01	added PosixThread sources
# RD 03/22/01	extensions, refinement
# RD 03/21/01	initial version
# ------------------------------------------------------------------------

# --- macros

include ../../Makefile.macros

MODULE	=	$(SIM_LIB_NAME)
BINARY	=	$(MODULE)_debug
BINFILE	=	$(BINARY)$(EXE_SUFFIX)
PREFIX	=	SIM_
LIBRARY	=	lib$(MODULE)
LIBOBJ	=	$(MODULE).o specc.o thread.o $(THREAD_NAME)Thread.o
OBJECTS =	specc.o thread.o $(THREAD_NAME)Thread.o
LIBDEP	=	$(SPECC_LIB)/lib$(LLONG_LIB_NAME).$(LIBSFX)
LIBS	=	-l$(LLONG_LIB_NAME) -l$(THREAD_BASE)
INCLUDE	=	$(MODULE).h specc.h piped.h signals.h thread.h		\
		$(THREAD_NAME)Thread.h
EXAMPLE	=	par0 par1 par2 par3 par4 par5 par6 par7			\
		waitfor0 waitfor1 waitfor2 waitfor3 waitfor4 waitfor5	\
		waitfor6						\
		wait0 wait1 wait2 wait3 wait4 wait5 wait6		\
		notify0 notify1 notify2 notify3 notify4			\
		notifyone0 notifyone1 notifyone2 notifyone3 notifyone4	\
		notifyone5 notifyone6 notifyone7 notifyone8 notifyone9	\
		notifyone10						\
		pipe0 pipe1 pipe2 pipe3 pipe4 pipe5 pipe6 pipe7 pipe8	\
		pipe9 pipe10 pipe11 pipe12				\
		piped0 piped1 piped2					\
		try0 try1 try2 try3 try4 try5 try6 try7 try8 try9	\
		try10 try11 try12 try13 try14 try15 try16 try17 try18	\
		try19 try20 try21 try22 try23 try24 try25 try26 try27	\
		try28 try29 try30 try31 try32				\
		fsm0 fsm1 fsm2 fsm3 fsm4 fsm5 fsm6
DIST	=	$(MODULE).cc $(MODULE).h				\
		specc.cc specc.h piped.h signals.h			\
		thread.cc thread.h					\
		PosixThread.cc PosixThread.h				\
		Win32Thread.cc Win32Thread.h				\
		$(MODULE).sh Makefile .purify testbench/Makefile	\
		testbench/README
ORIG	=	$(MODULE).cc $(MODULE).h				\
		specc.cc specc.h piped.h signals.h			\
		thread.cc thread.h					\
		PosixThread.cc PosixThread.h				\
		Win32Thread.cc Win32Thread.h				\
		$(MODULE).sh Makefile testbench/Makefile		\
		testbench/README

SCC	=	SPECC=$(SPECC) $(SPECC_BIN)/scrc
#SCCOPT	=	-vv -ww -g -I. -L. -xl "-Xlinker -R -Xlinker ."
SCCOPT  =	-vv -ww -g
SCCPURE	=	-xld '$(PURIFY) g++ %i -o %o %l'


# --- compilation rules

all:		$(LIBRARY).$(LIBSFX) $(SPECC_LIB)/$(LIBRARY).$(LIBSFX)


$(BINFILE):	$(BINARY).o $(OBJECTS) $(LIBDEP)
	$(LN) -o $(BINFILE) $(BINARY).o $(OBJECTS) $(LIBS) $(STDLIB)

$(BINARY)_pure:	$(BINARY).o $(OBJECTS) $(LIBDEP)
	$(LNPURE) -o $(BINARY)_pure $(BINARY).o $(OBJECTS) $(LIBS) $(STDLIB)

$(LIBRARY).a:	$(LIBOBJ)
	$(RM) $(LIBRARY).a
	$(AR) $(LIBRARY).a $(LIBOBJ)
	$(RAN) $(LIBRARY).a
	if [ "$(ADDLIB)" = "so" ]; then					\
		$(MAKE) $(LIBRARY).so $(SPECC_LIB)/$(LIBRARY).so ;	\
		fi;

$(LIBRARY).so:	$(LIBOBJ)
	$(LNSO) -o $(LIBRARY).so $(LIBOBJ)
	if [ "$(ADDLIB)" = "a" ]; then					\
		$(MAKE) $(LIBRARY).a $(SPECC_LIB)/$(LIBRARY).a ;	\
		fi;

$(MODULE).o:	$(MODULE).cc sim.h specc.h piped.h signals.h
	$(CC) $(MODULE).cc

$(BINARY).o:	$(MODULE).cc sim.h specc.h piped.h signals.h		\
		$(SPECC_INC)/Global.h
	$(CC) $(DEBUG) -o $(BINARY).o $(MODULE).cc


# if there is no symbolic link already, create a copy in the public directory
$(SPECC_LIB)/$(LIBRARY).a:	$(LIBRARY).a
	$(CP) $(LIBRARY).a $(SPECC_LIB)

$(SPECC_LIB)/$(LIBRARY).so:	$(LIBRARY).so
	$(CP) $(LIBRARY).so $(SPECC_LIB)


# --- submodules

specc.o:	specc.cc $(INCLUDE)
	$(CC) -DTHREAD_NAME_$(THREAD_NAME) specc.cc

thread.o:	thread.cc $(INCLUDE)
	$(CC) -DTHREAD_NAME_$(THREAD_NAME) thread.cc

$(THREAD_NAME)Thread.o:	$(THREAD_NAME)Thread.cc $(INCLUDE)
	$(CC) -DTHREAD_NAME_$(THREAD_NAME) $(THREAD_NAME)Thread.cc


# --- service rules

clean:
	-$(RM) *.bak *.BAK *~
	-$(RM) *.o core
	-$(RM) *.pure* *_pure* .pure
	cd testbench; $(MAKE) clean

distclean:	clean
	-$(RM) $(BINFILE) $(LIBRARY).*


dist:
	for file in $(DIST); do echo $(DISTPREFIX)/$$file >>$(DISTLIST); done
	for file in $(EXAMPLE);						\
		do echo $(DISTPREFIX)/testbench/$$file.sc >>$(DISTLIST); done

orig:
	for file in $(ORIG); do echo $(ORIGPREFIX)/$$file >>$(ORIGLIST); done
	for file in $(EXAMPLE);						\
		do echo $(ORIGPREFIX)/testbench/$$file.sc >>$(ORIGLIST); done

test:		$(BINFILE)
	./$(BINFILE)
	set -e;							\
	cd testbench;						\
	for ex in $(EXAMPLE); do				\
		$(SCC) $$ex -sc2out $(SCCOPT);			\
		./$$ex;						\
	done

pure:		$(BINARY)_pure

puretest:	$(BINARY)_pure
	./$(BINARY)_pure
	set -e;							\
	cd testbench;						\
	for ex in $(EXAMPLE); do				\
		$(SCC) $$ex -sc2out $(SCCOPT) $(SCCPURE);	\
		./$$ex;						\
	done


# --- EOF Makefile ---
