# generic makefile for atoms for (gcc)
# runs under DOS, Windows and OS/2
#
TARGET_LIB_NAME = libatoms.a

GCC=gcc
GXX=gpp
FLAGS=-Wall -O2 -I./include/ -fno-rtti -fno-exceptions
GXXFLAGS=-fno-rtti -fno-exceptions
#FLAGS+=-DERROR_HANDLER
OBJ=.o

OBJECTS=struct$(OBJ)

%OBJECTS=containe$(OBJ)
OBJECTS+=dbllist$(OBJ)
OBJECTS+=sets$(OBJ)
OBJECTS+=std_str$(OBJ)
OBJECTS+=struct$(OBJ)
OBJECTS+=lists$(OBJ)
OBJECTS+=strings$(OBJ)
OBJECTS+=stack$(OBJ)

OBJECTS+=raster$(OBJ)
OBJECTS+=rasterc$(OBJ) 
OBJECTS+=rasterut$(OBJ)
OBJECTS+=xlat_swp$(OBJ) 

OBJECTS+=utf8$(OBJ)
OBJECTS+=mtx_impl$(OBJ)


COMMONDEP=include/common.h $(MAKEFILE_LIST)


#### OS autodetection part of makefile ####


#Nothing is an indentifier for DOS? Too bad.
ifeq ($(OSTYPE),)
ifeq ($(HOSTTYPE),iris4d)
OSTYPE = iris4d
else
OSTYPE = dos
endif
endif


# for Linux - debian
ifeq ($(OSTYPE),linux-gnu)
GXX = g++
endif

# for 'linux' - Slackware; 'Linux' - Red Hat
#ifeq ($(OSTYPE),$(filter $(OSTYPE),linux Linux))
#endif
#end of Linux


# for MAC
ifeq ($(OSTYPE),darwin)
GXX = g++
endif

ifeq ($(OSTYPE),msys)
GXX = g++
GXXFLAGS+=-fpermissive
endif

ifeq ($(OSTYPE),cygwin)
GXX = g++
GXXFLAGS+=-fpermissive
endif


# for sun
#ifeq ($(OSTYPE),solaris)
#LIBS=-lm
#endif
#ifeq ($(OSTYPE),$(filter $(OSTYPE),solaris2.9 solaris2.10))
#endif

#ifeq ($(OSTYPE),sunos4)
#endif
#end of sun

#for OS/2
ifneq "$(OS2_SHELL)" ""
OSTYPE = os2
endif

# for aix
#ifeq ($(OSTYPE),aix)
#endif
#end of aix

# for MSDOS under djgpp
#ifeq ($(OSTYPE),msdosdjgpp)
#endif
#end of MSDOS under DJGPP


.SUFFIXES: .c .cc $(OBJ) .exe .a
.PHONY: all default distclean generic clean test


.cc$(OBJ):  include/*.h
	$(GXX) $(FLAGS) $(GXXFLAGS) $(DEBUG) -c $*.cc $(ERROR) -o $*$(OBJ) $(GETTEXT)

.c$(OBJ):  include/*.h
	$(GCC) $(FLAGS) $(DEBUG) -c $*.c $(ERROR) -o $*$(OBJ) $(GETTEXT)


$(TARGET_LIB_NAME): $(OBJECTS)
	ar rcs $(TARGET_LIB_NAME) $(OBJECTS)


struct$(OBJ):	struct.c $(COMMONDEP) include/struct.h
utf8$(OBJ):	utf8.c $(COMMONDEP)

%complex$(OBJ):	complex.cc $(COMMONDEP)
containe$(OBJ):	containe.cc $(COMMONDEP)
interval$(OBJ):	interval.cc $(COMMONDEP) include/interval.h
raster$(OBJ):	stack.cc $(COMMONDEP) include/ras_prot.h include/raster.h 
std_str$(OBJ):	std_str.cc $(COMMONDEP) include/std_str.h
stack$(OBJ):	stack.cc $(COMMONDEP) include/stacks.h
sets$(OBJ):	sets.cc $(COMMONDEP) include/sets.h
strings$(OBJ):	strings.cc $(COMMONDEP) include/stringa.h

all:     $(TARGET_LIB_NAME)
#	cp -f strings/*.h include/*.h
#	cp -f sets/*.h include/*.h


default: $(TARGET_LIB_NAME)


clean:
	rm -f $(TARGET_LIB_NAME) *$(OBJ) *.obj *.o *.bak core *.bak *.bkp *.a *.tmp
	rm -f include/*.bak include/*.bkp
	rm -f test/*$(OBJ)
	rm -f TestLists/*$(OBJ) TestLists/*.exe
	rm -f TestRaster/*$(OBJ) TestRaster/*.exe
	rm -f TestSets/*$(OBJ) TestSets/*.exe
	rm -f TestStack/*$(OBJ) TestStack/*.exe
	rm -f TestString/*$(OBJ) TestString/*.exe
	rm -f TestStruct/*$(OBJ) TestStruct/*.exe

distclean: clean
	cp include/common.h.gen include/common.h
	cp makefile.gen makefile

generic:
	cp include/common.h.gen include/common.h
	rm makefile
	cp makefile.gen makefile


#################### TEST ONLY #######################

test/bench$(OBJ): test/bench.c $(MAKEFILE_LIST)

TestLists/test$(OBJ): TestLists/test.cc include/*.h $(MAKEFILE_LIST)

TestLists/test.exe: TestLists/test$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GXX) -o $@ $*$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

TestString/test$(OBJ): TestString/test.cc $(MAKEFILE_LIST)

TestString/test.exe: TestString/test$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GXX) -o $@ $*$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

TestSets/test$(OBJ): TestSets/test.cc $(MAKEFILE_LIST)

TestSets/test.exe: TestSets/test$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GXX) -o $@ $*$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

TestStack/test$(OBJ): TestStack/test.cc $(MAKEFILE_LIST)

TestStack/test.exe: TestStack/test$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GXX) -o $@ $*$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

TestStruct/test$(OBJ): TestStruct/test.c $(MAKEFILE_LIST)

TestStruct/test.exe: TestStruct/test$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GCC) -o $@ $*$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

TestRaster/test.exe: TestRaster/test$(OBJ) TestRaster/error$(OBJ) test/bench$(OBJ) $(TARGET_LIB_NAME)
	$(GXX) -o $@ $*$(OBJ) TestRaster/error$(OBJ) test/bench$(OBJ) $(LDFLAGS) $(LIBS) $(TARGET_LIB_NAME) $(ERROR)

test: TestString/test.exe TestSets/test.exe TestStack/test.exe TestStruct/test.exe TestRaster/test.exe TestLists/test.exe
	TestLists/test.exe -test
	TestString/test.exe -test
	TestSets/test.exe -test
	TestStack/test.exe -test
	TestStruct/test.exe -test
	TestRaster/test.exe -test
