# Makefile for MSS version 1.2
# Written by Peter Palotas in 1998
#
# This makefile is written for use with GNU Make, GNU Fileutils, and GCC,
# under Linux or DJGPP.
#
# I know this makefile is all messy and	so, but	it works anyway.
#
# There are a few rules in this makefile, which are listed here.
#
# make all:
# This will compile everything, including the library and test-programs.
# The library will be placed in lib/[linux|djgpp]/libmss.a, and the
# testprograms will be placed in the ./samples/ directory.
#
# make lib:
# This will compile the library only.
#
# make test:
# This will compile the test programs, and the library if it is not present.
# (Same as make all)
#
# make install:
# This will install the library to /usr/local/lib/libmss.a under linux, or
# %DJDIR%/lib/libmss.a under DJGPP, and the headerfiles will go in
# /usr/local/include under linux, and %DJDIR%/include under DJGPP.
# You can change LIBDEST and INCLUDEDIR below (under either the DJGPP or
# linux section) if you want these files installed to another place.
#
# make clean:
# This will remove all objectfiles for the specific environment, i.e.
# if `make clean' is run under Linux it won't remove the DJGPP object
# files, and vice versa.
#
# make cleanall:
# This will remove all rebuildable files, excluding any installed ones.
#
# make uninstall:
# This will uninstall any installed files, i.e. remove them.
#
# make fixlibc:
# This is useful under DJGPP only.  If you are using version 2.01 of DJGPP
# (Or possibly also version 2.00) there is a bug in libc, which will make
# it impossible to write stuff to files from destructors, since all files
# will be closed before the destructors are called.
# This package includes the file `exit.c' from an alpha version of DJGPP,
# version 2.02, where this bug was fixed.  `make fixlibc' will compile this
# file and patch your libc, which it assumes to find under %DJDIR%/lib/libc.a
# Do make a backup of your libc before installing this patch just to be sure,
# especially if you are using a version other than 2.01 of DJGPP.
#
# If you are compiling on a system which lacks C++ support, you	want to
# uncomment the	line below. Otherwise you don't! =)
#NOCPP=.

# CC ofcourse should point to your compiler, preferrably gcc.
CC=gcc

# AR should point to your archiver. (Should probably be `ar')
AR=ar

# Compiler flags and stuff.
CFLAGS=-c -O2 -m486 -I. -ansi -pedantic -DMSS -o
LFLAGS=-m486 -o
WFLAGS=-W -Wall
ARFLAGS=rcs

#Uncomment the line below if you're debugging MSS. (if you are not a
#developer, you	don't want to uncomment	this, 'cause it	won't help you
#anything.  (This is not for debugging YOUR program, but for debugging MSS!
#DEBUG=.

#If you	don't have a GCC with support for stabs	debugging information,
#get it! (Or you could replace -gstabs+	below with ex. -g)
DEBUGFLAGS=-DMSS_DEBUG -gstabs+

# The version of MSS
VERSION=1.1

# -------- target OS (djgpp or linux) --------
ifdef DJDIR
TARGET=djgpp
else
TARGET=linux
endif

ifdef DEBUG
DFLAGS=$(DEBUGFLAGS)
else
DFLAGS=
endif

ifeq ($(TARGET),djgpp)
# -------- djgpp specific stuff	-----------
#
# Change this if you want to install the files in some other directory.
#
LIBDEST=$(DJDIR)/lib/libmss.a
INCLUDEDIR=$(DJDIR)/include

# Don't change anything below!
OBJDIR=objs/djgpp
LIBDIR=lib/djgpp
FIXLIBC=@ar rs $(DJDIR)/lib/libc.a $(OBJDIR)/exit.o
EXITDEP=objs/djgpp/exit.o
LIBCMESSAGE=@echo You should patch the libc if you have	not done so. Read the makefile for info.
FIXLIBCDEP=objs/djgpp/exit.o
EXE_SUFFIX=.exe

else
ifeq ($(TARGET),linux)
# -------- linux specific stuff	-----------
#
# Change this if you want to install the files in some other directory.
#
LIBDEST=/usr/local/lib/libmss.a
INCLUDEDIR=/usr/local/include

# Don't change anything below!
OBJDIR=objs/linux
LIBDIR=lib/linux
FIXLIBC=@echo Linux libc is already ok.
EXITDEP=
LIBCMESSAGE=
FIXLIBCDEP=
EXE_SUFFIX=

else
.PHONY:	badtarget
badtarget:
	@echo Error: target operating system not properly set.
	@echo 	     try running `make TARGET=djgpp' or	`make TARGET=linux'
	@echo	     If you are using another operating system, you probably
	@echo	     need to tweak the makefile a whole lot.
	@echo	     Makefiles for other targets will be included in the
	@echo	     distribution if they are provided to us. (See the docs
	@echo	     for more info).
endif
endif

# Make neccessary modifications	if no c++ compiler is available.
ifndef NOCPP
CPPSPEC_O=$(OBJDIR)/cppspec.o
else
CPPSPEC_O=
endif

.PHONY:	all lib install clean cleanall fixlibc uninstall test

all: lib test

lib: $(LIBDIR)/libmss.a
	@echo The MSS library is compiled and placed in $(LIBDIR)/libmss.a
	@echo
	$(LIBCMESSAGE)
	@echo

test: lib
	@echo About to make the test programs in the ./sample/ directory.
	@echo
	@cd samples ; make ; cd ..


$(OBJDIR)/list.o: list.c list.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/list.o list.c

$(OBJDIR)/inifile.o: inifile.c list.h inifile.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/inifile.o inifile.c

$(OBJDIR)/alloc.o: internal.h mss.h alloc.c
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/alloc.o alloc.c

$(OBJDIR)/check.o: check.c internal.h mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/check.o check.c

$(OBJDIR)/config.o: internal.h config.c mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/config.o config.c

$(OBJDIR)/init.o: init.c internal.h mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/init.o init.c

$(OBJDIR)/internal.o: internal.c internal.h mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/internal.o internal.c

$(OBJDIR)/log.o: log.c internal.h mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/log.o log.c

$(OBJDIR)/user.o: user.c internal.h mss.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/user.o user.c

$(OBJDIR)/cppspec.o: cppspec.cc	mss.h internal.h
	$(CC) $(DFLAGS) $(WFLAGS) $(CFLAGS) $(OBJDIR)/cppspec.o cppspec.cc

$(LIBDIR)/libmss.a: $(CPPSPEC_O) $(OBJDIR)/alloc.o $(CPPSPEC_O) $(OBJDIR)/list.o \
 		    $(OBJDIR)/inifile.o $(OBJDIR)/check.o $(OBJDIR)/config.o \
		    $(OBJDIR)/init.o $(OBJDIR)/internal.o $(OBJDIR)/log.o \
		    $(OBJDIR)/user.o
	@echo Creating the library as $(LIBDEST)...
	$(AR) $(ARFLAGS) $(LIBDIR)/libmss.a $(OBJDIR)/alloc.o \
		$(OBJDIR)/check.o \
		$(CPPSPEC_O) \
		$(OBJDIR)/config.o \
		$(OBJDIR)/init.o \
		$(OBJDIR)/internal.o \
		$(OBJDIR)/log.o \
		$(OBJDIR)/user.o \
		$(OBJDIR)/list.o \
		$(OBJDIR)/inifile.o


objs/djgpp/exit.o: exit.c
	@echo Compiling the exit.c from DJGPP alpha version 2.02.
	@gcc -c -gstabs+ -o objs/djgpp/exit.o exit.c
	@echo Patching your libc.


install: lib
	@echo Installing the library and neccessary includefiles...
	@cp $(LIBDIR)/libmss.a $(LIBDEST)
	@cp mss.h $(INCLUDEDIR)
	@echo MSS version $(VERSION) installed.

fixlibc: $(FIXLIBCDEP)
	$(FIXLIBC)

clean:
	@echo Removing object files...
	@rm -f $(OBJDIR)/*.o
	@cd samples ; make clean ; cd ..

cleanall: clean
	@echo Removing all rebuildable files...
	@rm -f $(LIBDIR)/libmss.a
	@cd samples ; make cleanall ; cd ..

uninstall:
	@echo Removing any installed files...
	@rm -f $(LIBDEST)
	@rm -f $(INCLUDEDIR)/mss.h

