# SMUG (TinyMUD 2.4.0) makefile 
CC = gcc 
AR = ar
# Uncomment these lines to use bison instead of yacc
YACC = bison
YFLAGS= -y -v
#YACC = yacc

# If you don't have or care to use lint/splint set this to nothing
LINT =
#LINT = splint

# The suffix appended to executables.  
# This should be set for Cygwin and Windows.
#EXE = .exe
EXE =

OPTIM= -g -O2 -pipe -Wall -W -Wno-parentheses -Wno-unused -Wno-sign-compare
DEFS= -DDEBUG -DNOISY_GC -DLOG_COMMANDS -DDUMP_CORE
CFLAGS= $(OPTIM) $(DEFS)
LFLAGS = -g 

# See LINT above - these flags for splint
LINTFLAGS = $(INCS) -weak -warnposixheaders +relaxtypes -retvalother

#LIBS may be defined to be any other system libraries you may require
LIBS = -lcrypt

HDR = alist.h  bytecode.h  config.h  db.h  externs.h  globals.h  interface.h \
    os.h  set.h

XTRA = language.pdf language.tex language.txt reserved.txt caveats.txt \
    INSTALL_NOTES COPYING TODO load.sh load.cmd init.db loadup.txt tiny.m4 \
    Makefile makefile.bor makefile.vc 

# Files needed to run the database 
DBSRC = db.c alist.c set.c utils.c vars.c predicates.c 
DBOBJ = $(DBSRC:.c=.o)

# Files needed to run the interpreter
INTERSRC = move.c tell.c create.c syscall.c encrypt.c bytecode.c \
        compiler.y match.c delay.c 
INTEROBJ = move.o tell.o create.o syscall.o encrypt.o bytecode.o \
        compiler.o match.o delay.o 
        
# Files needed to run the parser
PARSESRC = command.c
PARSEOBJ = $(PARSESRC:.c=.o)

# Files needed to run the game (everything except the driver)
GAMESRC = game.c
GAMEOBJ = $(GAMESRC:.c=.o)

# Files needed to run the network version
NETSRC = netdriver.c os.c
NETOBJ = $(NETSRC:.c=.o)

# Files needed to run the tty version
TTYSRC = ttydriver.c 
TTYOBJ = $(TTYSRC:.c=.o)

# Files needed for the loader utility
LDRSRC = loader.c 
LDROBJ = $(LDRSRC:.c=.o)

# Files needed for the massager utility
MSRC = massager.c 
MOBJ = $(MSRC:.c=.o)

# Files needed for the runtest program
RTSRC = runtest.c 
RTOBJ = $(RTSRC:.c=.o)

# Files needed for the comptest program
CTSRC = comptest.c 
CTOBJ = $(CTSRC:.c=.o)

OUTFILES= y.tab.h test.db y.output compiler.c

# Files in the standard distribution
DISTFILES = $(HDR) $(XTRA) $(DBSRC) $(INTERSRC) $(PARSESRC) $(GAMESRC) \
         $(NETSRC) $(TTYSRC) $(LDRSRC) $(MSRC) $(RTSRC) $(CTSRC)
PDIST = $(patsubst %,smug/%,$(DISTFILES))
RELEASE = dist

# Dependency information
OBJDEPENDS = $(DBOBJ) $(INTEROBJ) $(PARSEOBJ) $(GAMEOBJ) $(NETOBJ) \
         $(TTYOBJ) $(LDROBJ) $(MOBJ) $(RTOBJ) $(CTOBJ)

TARGETS = netmud$(EXE) ttymud$(EXE) loader$(EXE) test.db \
       massager$(EXE) runtest$(EXE) comptest$(EXE)

all: $(TARGETS)

netmud$(EXE): $(NETOBJ) $(GAMEOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

ttymud$(EXE): $(TTYOBJ) $(GAMEOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

loader$(EXE): $(LDROBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

# builds the test database
test.db: init.db loadup.txt tiny.m4 loader$(EXE)
	./load.sh init.db test.db < loadup.txt

# build parser
compiler.c y.tab.h : compiler.y 
	$(YACC) $(YFLAGS) compiler.y
	mv y.tab.c compiler.c

# utilities and test programs
massager$(EXE): $(MOBJ)
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

runtest$(EXE): $(RTOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ) 
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

comptest$(EXE): $(CTOBJ) compiler.o $(DBOBJ)
	$(CC) $(LFLAGS) -o $@ $^ $(LIBS) 

clean:
	@echo Cleaning...
	@-rm -f *.core gmon.out $(OUTFILES) $(TARGETS) $(OBJDEPENDS) $(OBJDEPENDS:.o=.d) 
	@echo Done.

dist : 
	@echo Building distribution...
	@ln -s ./ smug
	@tar czf smug-$(RELEASE).tar.gz $(PDIST) --exclude=.svn
	@rm smug
	@echo Done.

# pull in dependency info for *existing* .o files
-include $(OBJDEPENDS:.o=.d)
	
# compile and generate dependency info;
# more complicated dependency computation, so all prereqs listed
# will also become command-less, prereq-less targets
#   sed:    append directory to object target. (gcc bug?)
#   sed:    strip the target (everything before colon)
#   sed:    remove any continuation backslashes
#   fmt -1: list words one per line
#   sed:    strip leading spaces
#   sed:    add trailing colons
%.o: %.c
ifdef LINT
	-$(LINT) $(LINTFLAGS) $*.c
endif	
	$(CC) -c $(CFLAGS) $*.c -o $*.o 
	@$(CC) -MM $(CFLAGS) $*.c > $*.d
	@mv -f $*.d $*.d.tmp
	@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
	@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
	  sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
	@rm -f $*.d.tmp