# This file has been generated automatically.  Modify at your own risk!

# Makefile for system powerloom-extensions
# Uses various constructs only supported by GNU make!

# Template version: $Id$

# TO DO:
# - `make' shouldn't remake the system if the library exists (and is younger
#   than all of the source files) but some/all object files are missing.  That
#   would allow us to remove the object files after building to save space.

SYSTEMS-ROOT = ../..
STELLA-ROOT = ../../stella

# g++ settings:
CXX    = g++
CC     = gcc
CFLAGS = -g -O2 -fPIC

# Some versions of Unix and/or gcc need special switches; see what we have:
OS = $(shell if gcc --version | grep -iq "CYGWIN"; then echo "CYGWIN"; \
             elif gcc --version | grep -iq "MINGW"; then echo "MINGW"; \
             elif uname | grep -iq "DARWIN"; then echo "DARWIN"; \
             elif uname | grep -iq "SUNOS"; then echo "SUNOS"; \
             else echo "UNIX"; fi)

# Should be one of `static' or `dynamic'.  Dynamic linking under CygWin &
# MinGW doesn't seem to work (easily), so we force static linking there:
LINK-MODE =  $(shell if [ $(OS) = CYGWIN -o $(OS) = MINGW ]; \
                   then echo static; \
                   else echo dynamic; fi)

# Comment GC-SWITCHES and GC-LIBS (or reset them from the `make' command line)
# to not use the garbage collector.  This also requires a recompilation of
# all systems using and required by this one with the same settings.
GC-DIR = $(STELLA-ROOT)/cpp-lib/gc
GC-SWITCHES = -DSTELLA_USE_GC
GC-CFLAGS = $(GC-SWITCHES) -I$(GC-DIR)/include
GC-LIBS = -lgc $(shell if [ $(LINK-MODE) = dynamic ]; then echo -ldl; fi)
GC-LIB-DIR = $(GC-DIR)/.libs
GC-LDFLAGS = -L$(GC-LIB-DIR)

override INCLUDE += -I$(SYSTEMS-ROOT)

LDFLAGS = # -pg
MINGW-LIBS = $(shell if [ $(OS) = MINGW ]; then echo -liberty; fi)
PTHREAD-LIB = -lpthread
OTHER-LIBS = -lm $(MINGW-LIBS) $(PTHREAD-LIB)

# Leave this as is if one of the system source files has a `defmain' entry.
# Otherwise, or if you want to use your own `main' function, supply the .cc
# file here that defines `main':
MAIN =    main.cc
PROG =    powerloom-extensions

LIB-DIR  = $(SYSTEMS-ROOT)/lib
# Would need .dll for dynamic libraries under Windows: 
LIB-TYPE = $(shell if [ $(LINK-MODE) = static ]; \
	           then echo .a; \
                   elif [ $(OS) = "DARWIN" ]; \
                   then echo .dylib; \
                   else echo .so; fi)
LIB-FILE = $(LIB-DIR)/libpowerloom-extensions$(LIB-TYPE)
LIB      = -L$(LIB-DIR) -lpowerloom-extensions

# Linker flags to generate a shared library.
# The incantation below works with g++ on Linux, MacOS X and SunOS.
LIB-SHARED-FLAGS = $(shell if [ $(OS) = "SUNOS" ]; \
                           then echo -Xlinker -G; \
                           elif [ $(OS) = "DARWIN" ]; \
		           then echo -dynamiclib; \
                           else echo -shared; fi)

# For dynamic linking, tell the runtime linker where the libraries are via an
# rpath option; we use a relative and an absolute location to be able to move
# around a bit (for the absolute version we have to worry about spaces, etc.);
# if everything fails we can still set LD_LIBRARY_PATH at runtime:
RPATH  = $(LIB-DIR):$(shell cd $(LIB-DIR); pwd):$(GC-LIB-DIR):$(shell cd $(GC-LIB-DIR); pwd)
# Linker flags to define shared library path (covers Linux, MacOS X and SunOS):
RPATH-FLAGS = $(shell if [ $(OS) = "SUNOS" ]; \
                      then echo -Xlinker -R -Xlinker \'$(RPATH)\'; \
	              elif [ $(OS) = "DARWIN" ]; \
	              then echo ""; \
                      else echo -Xlinker -rpath -Xlinker \'$(RPATH)\'; fi)

LIB-SRCS =   extensions.cc unit-support.cc unit-kb.cc timepoint-support.cc timepoint-kb.cc startup-system.cc 
LIB-OBJS =   $(LIB-SRCS:.cc=.o)

PROG-SRCS =  $(MAIN)
PROG-OBJS =  $(MAIN:.cc=.o)

HEADERS =  $(PROG-SRCS:.cc=.hh) $(LIB-SRCS:.cc=.hh)

# Assumes each system has an associated <system>.{so,a} file in $(LIB-DIR):
REQUIRED-SYSTEMS =     stella logic utilities 
REQUIRED-SYSTEM-DIRS = stella logic utilities 
ALL-REQUIRED-SYSTEMS = utilities logic stella 
REQUIRED-LIBS =        -L$(LIB-DIR) $(ALL-REQUIRED-SYSTEMS:%=-l%)


all: $(PROG)
lib: $(LIB-FILE)

$(PROG): $(PROG-OBJS) $(LIB-FILE) update-required-systems
	$(CXX) -$(LINK-MODE) $(LDFLAGS) $(GC-LDFLAGS) $(RPATH-FLAGS) \
	       $(PROG-OBJS) -o $(PROG) \
	       $(LIB) $(REQUIRED-LIBS) $(GC-LIBS) $(OTHER-LIBS)

$(LIB-FILE): $(LIB-OBJS) $(LIB-DIR) update-required-systems
	if [ $(LINK-MODE) = dynamic ]; \
	then $(CXX) $(LIB-SHARED-FLAGS) $(LDFLAGS) $(GC-LDFLAGS) $(LIB-OBJS) \
                    -o $(LIB-FILE) $(REQUIRED-LIBS) $(GC-LIBS) $(OTHER-LIBS); \
	else ar r $(LIB-FILE) $(LIB-OBJS); \
             ranlib $(LIB-FILE); \
	fi

$(LIB-DIR):
	mkdir -p $(LIB-DIR)

# We don't know dependencies of required systems, so make sure they are up-to-date:
update-required-systems:
	for system in $(REQUIRED-SYSTEM-DIRS) ; do \
	  (cd $(SYSTEMS-ROOT)/$${system} ; \
	  $(MAKE) $(MFLAGS) CC=$(CC) CXX=$(CXX) LINK-MODE=$(LINK-MODE) lib;) \
	done

#
# Default rules
# 

.cc.o: $(HEADERS)
	$(CXX) $(CFLAGS) $(GC-CFLAGS) $(OTHER-CFLAGS) -c $(INCLUDE) -o $@ $<

#
# Manual
#

.PHONY: manual

manual:
	cd doc; $(MAKE) $(MFLAGS) all

#
# Cleaning up
#

.PHONY: clean extraclean

clean:
	/bin/rm -f *.o */*.o core \#*

extraclean: clean
	/bin/rm -f $(LIB-FILE) $(PROG) *~ .*~ \#* .\#*
