# Generic makefile stuff.  This file is included by all 
# the makefiles in the distribution.

# The following variable is automatically filled on by "make depend".
SRCDIR= /users/sys/empire/EMP
GAMEDIR= $(SRCDIR)

NROFF := nroff
TROFF := troff

LIBDIR := $(SRCDIR)/lib
MAKEDIR := $(SRCDIR)/make
RANLIB := ranlib

include $(MAKEDIR)/UtilMakefile

ifdef SYSTYPE
include $(MAKEDIR)/Systems/$(SYSTYPE)
endif

# The following variables are configurable.
#-----------------------------------#
GLOBALCFLAGS := 
GLOBALIFLAGS := -I$(SRCDIR)/h
GLOBALLFLAGS := -O
GLOBALSFLAGS :=

LINTFLAGS := -bhuz -I$(SRCDIR)/h

CC := cc
BSD := 4.3
BINDIR := $(GAMEDIR)/bin
DEPENDFILE := GNUdepend
#-----------------------------------#

# Other variables
CFLAGS := $(LOCALCFLAGS) $(GLOBALCFLAGS)
IFLAGS := $(LOCALIFLAGS) $(GLOBALIFLAGS)
LFLAGS := $(LOCALLFLAGS) $(GLOBALLFLAGS)
SFLAGS := $(LOCALSFLAGS) $(GLOBALSFLAGS)

ifdef ARCH
DEPENDENCIES := $(ARCH)/$(DEPENDFILE)
else
DEPENDENCIES := $(DEPENDFILE)
endif

# Rules
$(DEPENDFILE):
	cp $(MAKEDIR)/DependTmpl $@
	@echo "Remember to run $(MAKE) depend!"

ifdef ARCH
$(ARCH)/%.o: %.c
	$(CC) -c $(IFLAGS) $(CFLAGS) $<
	@mv $*.o $@

$(ARCH)/$(DEPENDFILE): $(DEPENDFILE)
	@-if [ ! -d $(ARCH) ]; then mkdir $(ARCH); fi
	@echo "Updating $@..."
	@awk '{ if ($$1 ~ /\.o:/) print "$(ARCH)/"$$0; else print; }' $< > $@
endif # ARCH

%.o: %.c
	$(CC) -c $(IFLAGS) $(CFLAGS) $<

# Productions
#	depend: create dependencies file for thie directory.
#	lintlib: create a lint library (LINTNAME must be set).
#	lint: run lint on the srcs (LINTLIBS may be set).

.PHONY: depend lintlib lint 

depend:
	@echo "Making depend.  The errors can be ignored."
	@mkmf -f $(DEPENDFILE) 'CFLAGS=$(IFLAGS)'

ifeq "$(origin LINTNAME)" "file"
LINTLIB := llib-l$(LINTNAME).ln
lintlib: $(LINTLIB)
$(LINTLIB): $(SRCS)
	lint -I$(SRCDIR)/h -C$(LINTNAME) $(SRCS)
	@rm -f $(LIBDIR)/$@
	@ln $@ $(LIBDIR)/$@
endif

lint: $(SRCS)
	lint $(LINTFLAGS) $(SRCS) \
		$(patsubst %,$(LIBDIR)/llib-l%.ln,$(LINTLIBS))

