# -*- Makefile -*-
LIBRARY_MODE = yes
include ../../include/config.Makefile
TARFILE_DIR = @abs_top_srcdir@/BUILD/tarfiles
# some libraries (gdbm, mpfr, readline, gmp, and gc) use libtool, which 
# insists on installing its files to a directory whose trailing part is the
# prefix that was used at compile time, i.e., the only change allowed to the prefix
# between compile time and install time is to prepend a string
PREFIX = $(BUILTLIBPATH)
LIBNAME ?= $(shell basename `pwd`)
UNTARDIR = build
OLDUNTARDIR = build-old
TARDIR ?= $(LIBNAME)-$(VERSION)
TARFILE ?= $(LIBNAME)-$(VERSION).tar.gz
CHECKTARGET ?= check
CHECKOPTIONS ?= 
CHECKCMD ?= time $(MAKE) -C $(BUILDDIR) $(CHECKOPTIONS) $(CHECKTARGET)
LIBRARIESDIR := $(PREFIX)
BIN_DIR := $(LIBRARIESDIR)/bin
LICENSE_DIR := $(LIBRARIESDIR)/licenses/$(LIBNAME)
PRECONFIGURE  ?= :
ifneq (@build_alias@,)
# it was ../configure --build=@build_alias@
CONFIGOPTIONS += --build=@build_alias@
endif
ifneq (@host_alias@,)
# it was ../configure --build=@host_alias@ (for cross compiling)
CONFIGOPTIONS += --host=@host_alias@
endif
CONFIGOPTIONS += --cache-file=/dev/null
COMPILEFLAGS  =					\
	CXXFLAGS="$(CXXFLAGS)"			\
	CFLAGS="$(CFLAGS)"			\
	FFLAGS="$(FCFLAGS)"			\
	FCFLAGS="$(FCFLAGS)"
ALLFLAGS = \
	$(COMPILEFLAGS)				\
	CPPFLAGS="$(CPPFLAGS)"			\
	CC="$(CC)"				\
	CXX="$(CXX)"				\
	LDFLAGS="$(LDFLAGS)"			\
	LOADLIBES="$(LOADLIBES)"		\
	LDLIBS="$(LDLIBS)"
CONFIGENV     = env $(ALLFLAGS)
CONFIGURESCRIPT ?= configure
CONFIGURECMD  ?= $(PRECONFIGURE) && $(CONFIGENV) ./$(CONFIGURESCRIPT) --prefix=$(LIBRARIESDIR) $(CONFIGOPTIONS)
BUILDTARGET   ?= 
BUILDCMD      ?= $(MAKE) prefix=$(LIBRARIESDIR) $(BUILDOPTIONS) $(BUILDTARGET)
INSTALLTARGET ?= install
INSTALLCMD    ?= $(MAKE) $(INSTALLOPTIONS) prefix=$(LIBRARIESDIR) $(INSTALLTARGET)
STRIPFILES    ?= 
ifeq ($(STRIPFILES),)
  STRIPCMD = true
else
  STRIPCMD = strip $(STRIPFILES)
endif
BUILDDIR      ?= $(UNTARDIR)/$(TARDIR)
TAROPTIONS    ?= --gzip
UNTARCMD      ?= @TAR@ xf $(TARFILE_DIR)/$(TARFILE) $(TAROPTIONS)
%.E: %.c; $(COMPILE.c) -E $(OUTPUT_OPTION) $<
export PATH := @abs_top_srcdir@/autoconf/final/bin:$(PATH)
all:prereq install post-install
prereq:
post-install:
show-flags:; $(CONFIGENV) :
install: .installed-$(VERSION) config-chk
unconfigure:; rm -f .configured-$(VERSION) .compiled-$(VERSION) .installed-$(VERSION)
reconfigure: unconfigure configure
uncompile:; rm -f .compiled-$(VERSION) .installed-$(VERSION)
recompile: uncompile compile
uninstall::; rm -f .installed-$(VERSION)
reinstall: uninstall install
always:
diffs: .untarred2-$(VERSION) always
	D=`pwd`; cd $(UNTARDIR) && diff -ur --exclude=configure $$D/tmp/$(TARDIR) $(TARDIR) |egrep -v '^Only in ' >$$D/$@ || echo diffs: `pwd`/$@
unmark:; rm -f .configured-$(VERSION) .compiled-$(VERSION)
package-clean: unmark ; if [ -d $(BUILDDIR) ]; then $(MAKE) -C $(BUILDDIR) clean ; fi
PACKAGE-DISTCLEAN-TARGET := distclean
package-distclean: unmark ; if [ -d $(BUILDDIR) ]; then $(MAKE) -C $(BUILDDIR) $(PACKAGE-DISTCLEAN-TARGET) ; fi
fetch: $(TARFILE_DIR)/$(TARFILE)
patch: .patched-$(VERSION)
license-dir: $(LICENSE_DIR)
$(LICENSE_DIR):; $(MKDIR_P) $(LICENSE_DIR)
compile: .compiled-$(VERSION) config-chk
configure: .configured-$(VERSION) config-chk 
config-chk:

PROGRAMS ?=
LICENSEFILES ?=
.installed-$(VERSION)  : .compiled-$(VERSION)
	( cd $(BUILDDIR) && set -x ; $(INSTALLCMD)  )
ifneq ($(PROGRAMS),)
	$(MKDIR_P) $(BIN_DIR)
	cd $(BUILDDIR) && for p in $(PROGRAMS) ; do (set -x ; @INSTALL@ -s $$p $(BIN_DIR)) ; done
endif
ifeq ($(LICENSEFILES),)
	@ echo warning: no license files to be installed
else
	$(MKDIR_P) $(LICENSE_DIR)
	cd $(BUILDDIR) && for p in $(LICENSEFILES) ; do (set -x ; @INSTALL_DATA@ $$p $(LICENSE_DIR)) ; done
endif
	touch $@

.compiled-$(VERSION)   : .configured-$(VERSION)
	@ set -x ; ( cd $(BUILDDIR) && $(BUILDCMD) && $(STRIPCMD) ) && touch $@
.configured-$(VERSION) : .patched-$(VERSION)
	@ set -x ; ( cd $(BUILDDIR) && $(CONFIGURECMD) ) && touch $@

ifneq ($(PATCHFILE),)
.patched-$(VERSION) : $(PATCHFILE)
PATCHCMD = cd $(UNTARDIR) && patch -p0 $(PATCHOPTIONS) <$(PATCHFILE)
else
PATCHCMD = :
endif
.patched-$(VERSION) : $(TARFILE_DIR)/$(TARFILE)
	if [ -d $(UNTARDIR) -a -d $(OLDUNTARDIR)/$(UNTARDIR) ] ; \
	    then echo please remove or move $(OLDUNTARDIR)/$(UNTARDIR) out of the way ; \
	         exit 1 ; \
	    fi
	if [ -d $(UNTARDIR) ] ; then mv $(UNTARDIR) $(OLDUNTARDIR) ; fi
	mkdir $(UNTARDIR)
	cd $(UNTARDIR) && $(UNTARCMD)
	$(PATCHCMD)
	touch $@
.untarred2-$(VERSION) : $(TARFILE_DIR)/$(TARFILE) ; mkdir tmp ; (cd tmp && $(UNTARCMD)) && touch $@
ifeq ($(FETCHER),)
ifneq (@WGET@,)
FETCHER = @WGET@
else
ifneq (@CURL@,)
FETCHER = @CURL@
else
ifeq (@DOWNLOAD@,yes)
$(error either curl or wget is needed for downloading library source code, please install one of them and reconfigure)
else
FETCHER = false
endif
endif
endif
endif
ifeq ($(FETCHER),curl)
FETCHOPTS = -o "$(TARFILE)"
else
ifneq ($(FETCHER),wget)
FETCHOPTS = 
endif
endif
CHECKFETCHED = case "$(URL)" in									\
	http:*)											\
	    if file $(TARFILE_DIR)/$(TARFILE) | grep HTML ;					\
	    then echo tried to fetch file, but HTML file returned instead >&2 ; exit 1 ;	\
	    fi ;;										\
	esac
$(TARFILE_DIR)/$(TARFILE) :
	$(MKDIR_P) $(TARFILE_DIR)
ifeq (@DOWNLOAD@,yes)
	(cd $(TARFILE_DIR) && $(FETCHER) $(URL)/$(TARFILE) $(FETCHOPTS) && $(CHECKFETCHED))
else
	@ echo "error: for the third-party library or program source \"$(LIBNAME)\"" >&2
	@ echo "       the source code is not present in the file \"$@\"" >&2
	@ echo "       so either download a \"fat\" tar file of the Macaulay2 source code" >&2
	@ echo "       or rerun the Macaulay2 \"configure\" command with the added option \"--enable-download\"" >&2
	@ echo "       to enable automatic downloading of the source code over the internet" >&2
	@ false
endif
distclean: package-distclean
clean::; rm -rf .patched* .untarred* $(LIBNAME)* .checked* .compiled* .configured* .installed* .untarred2* diffs tmp $(TARDIR) $(UNTARDIR) $(OLDUNTARDIR)
distclean: clean ; rm -rf Makefile

SLIMIT ?= 8192
TLIMIT ?= 100
MLIMIT ?= 400000
VLIMIT ?= 400000
LIMIT := :
ifeq (@ULIMIT_T@,yes)
LIMIT += ; ulimit -t $(TLIMIT)
endif
ifeq (@ULIMIT_M@,yes)
LIMIT += ; ulimit -m $(MLIMIT)
endif
ifeq (@ULIMIT_V@,yes)
LIMIT += ; ulimit -v $(VLIMIT)
endif
ifeq (@ULIMIT_S@,yes)
LIMIT += ; ulimit -s $(SLIMIT)
endif
recheck:
	rm -f .checked-$(VERSION)
	$(MAKE) check
check: .checked-$(VERSION)
.checked-$(VERSION) : .compiled-$(VERSION)
	$(LIMIT); $(CHECKCMD)
	touch $@

ls:; ls -la . tarfiles
help:;
	@ echo ""
	@ echo "usage: make [option...] [target...]"
	@ echo ""
	@ echo "targets:"
	@ echo ""
	@ echo "  all                         build/compile all files [the default target]"
	@ echo "  reconfigure                 remake configuration files for library"
	@ echo "  diffs                       create diff file for changes to library sources"
	@ echo "  clean                       remove most created files, except Makefiles and dowloaded tarfiles"
	@ echo "  distclean                   remove all created files"
	@ echo "  package-clean               remove most created files in the package's directory tree, except Makefiles"
	@ echo "  package-distclean           remove all created files in the package's directory tree"
	@ echo "  check                       check whether library compiled correctly"
	@ echo "  recheck                     check, even if we've checked before"
	@ echo ""

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/libraries Makefile.library"
# End:
