################################################
## PolyLib Makefile.  Version : 4.15          ##
## Vincent Loechner, 1998/99.                 ##
################################################

VERSION = 4.15

###########################################################
## Type of integer to use (see ./ArithLib/ for details)
###########################################################

## Choose one of these 4 integer type:
## 1. 'long long int'  (recommended)
## 2. 'long int'
## 3. 'int'
## 4. 'char *'

## ------------------64 bits integers----------------------
## 1. On most systems, 'long long int' is the 64 bits integer definition.
## If this is your case define the following :
BITS= 64
AFLAGS = -DLINEAR_VALUE_IS_LONGLONG -DLINEAR_VALUE_PROTECT_MULTIPLY \
 -DLINEAR_VALUE_ASSUME_SOFTWARE_IDIV

## 2. On some systems/processors (Alpha 21164 for example) 'long int'
## is the type for 64 bits integers
## If this is your case, define the following :
#BITS= 64
#AFLAGS = -DLINEAR_VALUE_IS_LONG -DLINEAR_VALUE_PROTECT_MULTIPLY \
# -DLINEAR_VALUE_ASSUME_SOFTWARE_IDIV

## ------------------32 bits integers----------------------
## 3. to use standard 'int' as default integers, define :
#BITS= 32
#AFLAGS = -DLINEAR_VALUE_IS_INT

## --------------------------------------------------------
## 4. For the typechecking tests, this typedefs Value as 'char *' :
## (the executables won't run, this is just a compilation test)
#BITS= chars
#AFLAGS = -DLINEAR_VALUE_IS_CHARS -DLINEAR_VALUE_PROTECT_MULTIPLY

###########################################################
## GNU-MP stuff
###########################################################

## Define these 4 lines if you want to use the GNU-mp (multiple precision)
## library when there is an overflow
GFLAGS = -DGNUMP
EHRHART_MP = ehrhart_mp.c
MATRIX_MP = matrix_mp.c
EXTRA_LIBS += -lgmp

## Define these 5 lines if your GNU-MP lib is not in the standard directory
#GMPINSTALLDIR=/usr/local
#GMPLIB=$(GMPINSTALLDIR)/lib
#GMPINCLUDE=$(GMPINSTALLDIR)/include
#EXTRA_INCLUDES = -I$(GMPINCLUDE)
#EXTRA_LIBS += -L$(GMPLIB)

###########################################################
## Compiler stuff
###########################################################
CC = gcc
CFLAGS = -O4 -Wall -g
#CFLAGS = -O4
LDFLAGS = 

## In modern systems, just define ranlib as 'echo'
RANLIB = echo

## LD flag to generate a shared library.
## if you use GNU ld define:
#SHAREDLIB_FLAG = -shared
## else define:
SHAREDLIB_FLAG = -G

## set this if it is not defined by default (on HPUX for example)
#OSTYPE = HPUX

###########################################################
## Installation directories, name of executables.
###########################################################
## where to put intermediate objects and executables:
OBJ_DIR = Obj.$(BITS).$(OSTYPE)

## Define this to put a extra suffix on the executables
## This is usefull if you have multiple executables installed
## (32/64 bits or with/without GNU-MP for example)
#EXEC_EXTRA_SUFFIX = $(BITS)

## make install puts everything here:
## (if you choose method 2. 3. or 4. below)
INSTALLDIR = /usr/local
EXECDIR = $(INSTALLDIR)/bin
LIBDIR = $(INSTALLDIR)/lib
INCLUDEDIR = $(INSTALLDIR)/include
MANDIR = $(INSTALLDIR)/man
DOCSDIR = $(INSTALLDIR)/doc/packages/polylib-$(VERSION)

###########################################################
## Installing the library
###########################################################

## Choose one of these 4 installation methods:
## 1. don't install the lib (just test the polylib)
## 2. install the shared lib (recommended)
## 3. install the static lib
## 4. install both static and shared libs

## 1. don't install polylib anywhere (static linking).
## define these 3 lines if you just want to test the library.
## If you choose this, you won't be able to build other packages
## such as the VisuDomain tool (well indeed you can by making
## naughty changes in its makefile :-)
INSTALL_LIB = 
LIB = $(OBJ_DIR)/$(PSTATIC)
EXEC_EXTRA_LIBS = $(LIB)


## (2. 3. and 4. -needed) general (static/shared) defines
## define this to link polylib with the executables:
#EXEC_EXTRA_LIBS += -lpolylib$(BITS)
## -optional: if the library is not installed in a standard path, define:
#EXEC_EXTRA_LIBS += -L$(LIBDIR)

## 2. (shared)
## Install the shared library:
#INSTALL_LIB = install-shared
## this is in most cases not necessary since it is the default:
#EXEC_EXTRA_LIBS += -Bdynamic
## On linux define this to run ldconfig after building the shared library:
#LDCONFIG = ldconfig

## 3. (static)
## Install the static library:
#INSTALL_LIB = install-static

## 4. (static AND shared)
## Install both static and shared libs:
#INSTALL_LIB = install-static install-shared
## this is in most cases not necessary since it is the default:
#EXEC_EXTRA_LIBS += -Bdynamic
## On linux define this to run ldconfig after building the shared library:
#LDCONFIG = ldconfig


## (2. and 4. -optional)
## Using shared libraries in non-standard paths.
## It is strongly discouraged to use this directory hard-coding
## in the executables. Anyway, it's not very usefull to have a
## shared object in a private directory. This can be used for
## testing purpose.
## On SVR4 (solaris, ...) to avoid defining LD_LIBRARY_PATH define:
#EXEC_LDFLAGS += -R$(LIBDIR)
## or using GNU ld:
#EXEC_LDFLAGS += -r$(LIBDIR)

#############################################################
##-------------------END OF USER DEFINES-------------------##
#############################################################
##  you shouldn't need to change anything below this line  ##
#############################################################

## more defines
POLYLIB_INC = ./include/polylib
POLYLIB_SRC = ./source
EXTRA_INCLUDES += -I ./include
ARITH_DIR = ./ArithLib
CFLAGS += $(EXTRA_INCLUDES) $(AFLAGS) $(GFLAGS)

PSTATIC = libpolylib$(BITS).a.$(VERSION)
PSHARED =  libpolylib$(BITS).so.$(VERSION)

PEXEC = \
	testlib \
	polytest \
	c2p \
	r2p \
	findv \
	pp \
	union_disjointe \
	union_convex \
	ehrhart
POLY_EXEC= $(PEXEC:%=$(OBJ_DIR)/%$(EXEC_EXTRA_SUFFIX))

CFILES= \
	errormsg.c \
	vector.c \
	matrix.c \
	polyhedron.c \
	polyparam.c \
	ehrhart.c \
	eval_ehrhart.c \
	$(EHRHART_MP) \
	$(MATRIX_MP)

LIB_CFILES = $(CFILES:%=$(POLYLIB_SRC)/%)

PHEADERS= $(CFILES:%.c=$(POLYLIB_INC)/%.h) \
	$(POLYLIB_INC)/polylib.h $(POLYLIB_INC)/types.h \
	$(POLYLIB_INC)/types_mp.h \
	$(POLYLIB_INC)/arithmetique.h $(POLYLIB_INC)/arithmetic_errors.h

LIB_OBJECTS= $(CFILES:%.c=$(OBJ_DIR)/%.o) 

###########################################################
## default : make lib, install it, and build executables
###########################################################
all:: libs
	@echo "---------------------------------------------------"
	@echo "Successfully built Library."
	@echo "The lib now needs to be installed in order to build executables"
	@echo "---------------------------------------------------"
all:: $(INSTALL_LIB)
	@echo "---------------------------------------------------"
	@echo "Successfully installed Library."
	@echo "---------------------------------------------------"
all:: exec
	@echo "---------------------------------------------------"
	@echo "Type 'make install' to install everything"
	@echo "---------------------------------------------------"

###########################################################
## Install/UnInstall
###########################################################
install:: install-libs install-exec install-man install-docs
install-libs: $(INSTALL_LIB) install-include

install-shared: lib-shared
	mkdirhier $(LIBDIR)
	cp $(OBJ_DIR)/$(PSHARED) $(LIBDIR)/
	/bin/rm -f $(LIBDIR)/libpolylib$(BITS).so
	ln -s $(LIBDIR)/$(PSHARED) $(LIBDIR)/libpolylib$(BITS).so
	$(LDCONFIG)
install-static: lib-static
	mkdirhier $(LIBDIR)
	cp $(OBJ_DIR)/$(PSTATIC) $(LIBDIR)/
	/bin/rm -f $(LIBDIR)/libpolylib$(BITS).a
	ln -f -s $(LIBDIR)/$(PSTATIC) $(LIBDIR)/libpolylib$(BITS).a
install-include:
	mkdirhier $(INCLUDEDIR)/polylib
	cp ./include/polylib/* $(INCLUDEDIR)/polylib
install-exec: $(POLY_EXEC)
	mkdirhier $(EXECDIR)
	cp $(POLY_EXEC) $(EXECDIR)
install-man:
# to be done...
install-docs:
	mkdirhier $(DOCSDIR)
	cp README CHANGES INSTALL LICENSING polylib-doc.ps.gz $(DOCSDIR)/
	mkdirhier $(DOCSDIR)/examples
	cp Test/*.in $(DOCSDIR)/examples/

uninstall:: uninstall-libs uninstall-exec uninstall-man uninstall-docs
uninstall-libs:
	/bin/rm -f $(LIBDIR)/$(PSHARED)
	/bin/rm -f $(LIBDIR)/libpolylib.so
	/bin/rm -f $(LIBDIR)/$(PSTATIC)
	/bin/rm -f $(LIBDIR)/libpolylib.a
	/bin/rm -rf $(INCLUDEDIR)/polylib
uninstall-exec:
	( cd $(EXECDIR) ; /bin/rm -f $(POLY_EXEC) )
uninstall-man:

uninstall-docs:
	/bin/rm -rf $(DOCSDIR)

###########################################################
## Libs : static and shared
###########################################################
libs: lib-shared lib-static

lib-shared:: $(OBJ_DIR)
lib-shared:: $(OBJ_DIR)/$(PSHARED)
lib-static:: $(OBJ_DIR)
lib-static:: $(OBJ_DIR)/$(PSTATIC)

$(OBJ_DIR)/$(PSTATIC): $(PHEADERS) $(LIB_OBJECTS)  
	(cd $(ARITH_DIR) ; $(MAKE) "CFLAGS=$(CFLAGS)" "OBJ_DIR=$(OBJ_DIR)" )
	/bin/rm -f $(OBJ_DIR)/$(PSTATIC)
	cp $(ARITH_DIR)/$(OBJ_DIR)/arithmetique.a $(OBJ_DIR)/$(PSTATIC)
	ar -q $(OBJ_DIR)/$(PSTATIC) $(LIB_OBJECTS)
# (ranlib is not necessary in modern systems)
	@$(RANLIB) $(OBJ_DIR)/$(PSTATIC)

$(OBJ_DIR)/$(PSHARED): $(LIB_OBJECTS)
	(cd $(ARITH_DIR) ; $(MAKE) "CFLAGS=$(CFLAGS)" "OBJ_DIR=$(OBJ_DIR)" )
	gcc $(SHAREDLIB_FLAG) -o $(OBJ_DIR)/$(PSHARED) $(LIB_OBJECTS) \
		$(ARITH_DIR)/$(OBJ_DIR)/arithmetique.a

###########################################################
## Cleans
###########################################################
clean:
	(cd $(ARITH_DIR) ; $(MAKE) "OBJ_DIR=$(OBJ_DIR)" clean)
	/bin/rm -rf $(OBJ_DIR)

distclean:
	(cd $(ARITH_DIR) ; $(MAKE) distclean)
	/bin/rm -rf Obj.*

###########################################################
## Tests
###########################################################
test:
	(cd Test ; $(MAKE) \
		"OBJ_DIR=../$(OBJ_DIR)" "EXEC_EXTRA_SUFFIX=$(EXEC_EXTRA_SUFFIX)" )
64test:
	(cd Test ; $(MAKE) \
		"OBJ_DIR=../$(OBJ_DIR)" "EXEC_EXTRA_SUFFIX=$(EXEC_EXTRA_SUFFIX)" \
		64_tests )

########################################################################
##  Lib Objects
######################################################################## 
$(OBJ_DIR):
	mkdir $(OBJ_DIR)

$(LIB_OBJECTS): $(PHEADERS) $(LIB_CFILES)
	$(CC) -c $(CFLAGS) $(*:$(OBJ_DIR)/%=$(POLYLIB_SRC)/%.c) -o $*.o

#$(PHEADERS): $(LIB_CFILES)
#	-mv -f $@ $@.old
#	cextract +a +w72 $(EXTRA_INCLUDES) -H_$*_H_ -o $@ $(@:.h=.c)

########################################################################
## Executables
########################################################################

exec:: $(OBJ_DIR)
exec:: $(POLY_EXEC)
	@echo "---------------------------------------------------"
	@echo "Successfully built executables."
	@echo "---------------------------------------------------"
	@echo "To run the test, type: make test and/or make 64test"

# constraints2polyhedron
$(OBJ_DIR)/c2p$(EXEC_EXTRA_SUFFIX): $(OBJ_DIR)/c2p.o $(LIB)
	$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/c2p.o \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/c2p.o: $(POLYLIB_SRC)/c2p.c $(PHEADERS)
	$(CC) -c $(CFLAGS) $(POLYLIB_SRC)/c2p.c -o $(OBJ_DIR)/c2p.o

# rays2polyhedron
$(OBJ_DIR)/r2p$(EXEC_EXTRA_SUFFIX): $(OBJ_DIR)/r2p.o $(LIB)
	$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/r2p.o \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/r2p.o: $(POLYLIB_SRC)/r2p.c $(PHEADERS)
	$(CC) -c $(CFLAGS) $(POLYLIB_SRC)/r2p.c -o $(OBJ_DIR)/r2p.o

# find vertices
$(OBJ_DIR)/findv$(EXEC_EXTRA_SUFFIX): $(OBJ_DIR)/findv.o $(LIB)
	$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/findv.o \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/findv.o: $(POLYLIB_SRC)/findv.c $(PHEADERS)
	$(CC) -c $(CFLAGS) $(POLYLIB_SRC)/findv.c -o $(OBJ_DIR)/findv.o

# find validity domains+vertices
$(OBJ_DIR)/pp$(EXEC_EXTRA_SUFFIX): $(OBJ_DIR)/pp.o $(LIB)
	$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/pp.o \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/pp.o: $(POLYLIB_SRC)/pp.c $(PHEADERS)
	$(CC) -c $(CFLAGS) $(POLYLIB_SRC)/pp.c -o $(OBJ_DIR)/pp.o

# misc : disjoint and convex union of polyhedra (cf. sources)
$(OBJ_DIR)/union_disjointe$(EXEC_EXTRA_SUFFIX): $(PHEADERS) $(LIB)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ \
		$(POLYLIB_SRC)/union_disjointe.c $(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/union_convex$(EXEC_EXTRA_SUFFIX): $(PHEADERS) $(LIB)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ \
		$(POLYLIB_SRC)/union_convex.c $(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)

# computes vertices, validity domains and ehrhart polynomial
$(OBJ_DIR)/ehrhart$(EXEC_EXTRA_SUFFIX): $(POLYLIB_SRC)/ehrhart.c $(LIB)
	$(CC) $(LDFLAGS) $(CFLAGS) -DEMAIN -DEP_EVALUATION  -o $@ \
		$(EHRHART_MP:%=$(POLYLIB_SRC)/%) $(POLYLIB_SRC)/ehrhart.c \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)

$(OBJ_DIR)/testlib$(EXEC_EXTRA_SUFFIX): $(POLYLIB_SRC)/testlib.c $(LIB)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(POLYLIB_SRC)/testlib.c \
		$(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)
$(OBJ_DIR)/polytest$(EXEC_EXTRA_SUFFIX): $(POLYLIB_SRC)/polytest.c $(LIB)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(POLYLIB_SRC)/polytest.c \
		 $(EXEC_EXTRA_LIBS) $(EXTRA_LIBS)

########################################################################
## END
########################################################################
