# -*- Makefile -*-
LIBRARY_MODE = yes
include ../../include/config.Makefile
TARFILE_DIR = @abs_top_srcdir@/BUILD/tarfiles
export LD_LIBRARY_PATH:=$(BUILTLIBPATH)/lib:$(LD_LIBRARY_PATH)
# 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)
.NOTPARALLEL:
ifneq ($(PARALLEL),yes)
NOTPARALLEL = -j1
endif
LIBNAME ?= $(shell basename `pwd`)
UNTARDIR = build
OLDUNTARDIR = build-old
TARDIR ?= $(LIBNAME)-$(VERSION)
TARFILE ?= $(LIBNAME)-$(VERSION).tar.gz
CHECKTARGET ?= check
CHECKOPTIONS ?= 
CHECKCMD ?= time $(MAKE) $(NOTPARALLEL) $(CHECKOPTIONS) $(CHECKTARGET)
LIBRARIESDIR := $(PREFIX)
BIN_DIR := $(LIBRARIESDIR)/bin
LICENSE_DIR := $(LIBRARIESDIR)/licenses/$(LIBNAME)
PRECONFIGURE  ?= :

export PKG_CONFIG_PATH := $(BUILTLIBPATH)/lib/pkgconfig:$(PKG_CONFIG_PATH)

# suppress warning messages while compiling libraries, because it's not our code
CXXFLAGS += -w
CFLAGS += -w

ifeq ($(OPTIMIZE),no)
CXXFLAGS += -O0
CFLAGS += -O0
FCFLAGS += -O0
endif

ifeq (@OS@,Darwin)
## this option gives space in the binary so 'install_name_tool' can edit it and tell it where to find its dynamic libraries
## the editing is done in distributions/dmg/Makefile
LDFLAGS += -headerpad_max_install_names
endif

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
TOOLNAMES =					\
	AR=@AR@					\
	AS=@AS@					\
	DLLTOOL=@DLLTOOL@			\
	OBJDUMP=@OBJDUMP@			\
	STRIP=@STRIP@
COMPILEFLAGS  =					\
	CXXFLAGS="$(CXXFLAGS)"			\
	CFLAGS="$(CFLAGS)"			\
	FFLAGS="$(FCFLAGS)"			\
	FCFLAGS="$(FCFLAGS)"			\
	TARGET_ARCH=$(TARGET_ARCH)
ALLFLAGS =					\
	$(TOOLNAMES)				\
	$(COMPILEFLAGS)				\
	CPPFLAGS="$(CPPFLAGS)"			\
	CC="$(CC)"				\
	CXX="$(CXX)"				\
	LDFLAGS="$(LDFLAGS)"			\
	LOADLIBES="$(LOADLIBES)"		\
	LDLIBS="$(LDLIBS)"
CONFIGENV     = env $(ALLFLAGS)
CONFIGURESCRIPT ?= configure
CONFIGURECMD  ?= $(PRECONFIGURE) && ./$(CONFIGURESCRIPT) --prefix=$(LIBRARIESDIR) $(ALLFLAGS) $(CONFIGOPTIONS)
BUILDTARGET   ?= 
BUILDCMD      ?= $(MAKE) $(NOTPARALLEL) prefix=$(LIBRARIESDIR) $(BUILDOPTIONS) $(BUILDTARGET)
INSTALLTARGET ?= install
INSTALLCMD    ?= $(MAKE) $(NOTPARALLEL) $(INSTALLOPTIONS) prefix=$(LIBRARIESDIR) $(INSTALLTARGET) && rm -f $(LIBRARIESDIR)/lib/*.la
STRIPFILES    ?= 
ifeq ($(STRIPFILES),)
  STRIPCMD = :
else
 ifeq (@ENABLE_STRIP@,yes)
  STRIPCMD = @STRIP@ $(STRIPFILES)
 else
  STRIPCMD = :
 endif
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@/usr-build/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: uncompile-basic uncompile-extra
uncompile-basic:; rm -f .compiled-$(VERSION) .installed-$(VERSION)
uncompile-extra:
recompile: uncompile compile
uninstall::; rm -f .installed-$(VERSION)
reinstall: uninstall install
always:
diffs: .untarred2-$(VERSION) always
	D=`pwd`; cd $(UNTARDIR) && diff -ur $(DIFF_OPTIONS) $$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) $(NOTPARALLEL) -C $(BUILDDIR) clean ; fi
PACKAGE-DISTCLEAN-TARGET := distclean
package-distclean: unmark ; if [ -d $(BUILDDIR) ]; then $(MAKE) $(NOTPARALLEL) -C $(BUILDDIR) $(PACKAGE-DISTCLEAN-TARGET) ; fi
fetch: $(TARFILE_DIR)/$(TARFILE)
patch: .patched-$(VERSION)
license-dir: $(LICENSE_DIR)
$(LICENSE_DIR):; $(MKDIR_P) $(LICENSE_DIR)
compile: config-chk .compiled-$(VERSION) 
configure: .configured-$(VERSION) config-chk 
config-chk:

PROGRAMS ?=
LICENSEFILES ?=
LIBRARIES ?=
INCLUDEFILES ?=

CDBUILDDIR = set +x && cd $(BUILDDIR) && echo make: Entering directory \'`pwd`\' && set -x

.installed-$(VERSION)  : .compiled-$(VERSION)
	+ ( $(CDBUILDDIR) && set -x ; $(INSTALLCMD)  )
ifneq ($(PROGRAMS),)
	$(MKDIR_P) $(BIN_DIR)
#	Note: /usr/bin/install on Mac OS X is not gnu install, and doesn't know about --strip-program
#	Stop stripping, anyway, so users can debug.
	set -e ; $(CDBUILDDIR) && for p in $(PROGRAMS) ; do (set -x ; @INSTALL@ $$p $(BIN_DIR)) ; done
endif
ifneq ($(LIBRARIES),)
	$(MKDIR_P) $(LIBRARIESDIR)/lib
	set -e ; $(CDBUILDDIR) && for p in $(LIBRARIES) ; do (set -x ; @INSTALL@ $$p $(LIBRARIESDIR)/lib) ; done
endif
ifneq ($(INCLUDEFILES),)
	$(MKDIR_P) $(LIBRARIESDIR)/include
	set -e ; $(CDBUILDDIR) && for p in $(INCLUDEFILES) ; do (set -x ; @INSTALL_DATA@ $$p $(LIBRARIESDIR)/include) ; done
endif
ifeq ($(LICENSEFILES),)
	@ echo "warning: *** no license files to be installed"
else
	$(MKDIR_P) $(LICENSE_DIR)
	set -e ; cd $(UNTARDIR)/$(TARDIR) && for p in $(LICENSEFILES) ; do (set -x ; @INSTALL_DATA@ "$$p" $(LICENSE_DIR)) ; done
endif
	for l in $(LIBRARIESDIR)/lib/*.dylib $(LIBRARIESDIR)/lib/*.so $(LIBRARIESDIR)/lib/*.so.* ;	\
	do if [ -f "$$l" ] ;										\
	   then echo "copying $$l to @pre_exec_prefix@/@tail_librariesdir@" ;				\
		$(MKDIR_P) "@pre_exec_prefix@/@tail_librariesdir@" ;					\
		@INSTALL_DATA@ "$$l" "@pre_exec_prefix@/@tail_librariesdir@" ;				\
	   fi												\
	done
	touch $@
## Here's why we copy the libraries above.
# Macaulay2 includes many third party "libraries", such as gdbm, mpir, mpfr, and
# factory.  Some of these create shared library files (with names such as
# libgdbm.so.4.0.0), and we are ready to link with them, but then we have to
# distribute them along with the executable file M2-binary.  On the other hand,
# if the library makes a static library file, with a name like libgdb.a, then we
# don't have to distribute that.  So now as the last step after building and
# installing a library, we copy the shared library files from the "usr-host"
# directory tree in the build directory to the Macaulay2 part of the "usr-dist"
# directory tree in the build directory.  In that location, they will be found
# when running M2 or its third party programs, and they will be
# distributed along with Macaulay2.

.compiled-$(VERSION)   : .configured-$(VERSION)
	$(WHY)
	@+ set -x ; ( $(CDBUILDDIR) && $(BUILDCMD) && $(STRIPCMD) ) && touch $@
.configured-$(VERSION) : .patched-$(VERSION)
	$(WHY)
	@ set -x ; ( $(CDBUILDDIR) && $(CONFIGURECMD) ) && touch $@

PATCHCMD = cd $(UNTARDIR) && for i in $(PATCHFILE) ; do patch --batch -p0 < $$i ; done
.patched-$(VERSION) : $(PATCHFILE) $(TARFILE_DIR)/$(TARFILE)
	$(WHY)
	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)
	$(WHY)
	mkdir tmp ; (cd tmp && $(UNTARCMD)) && touch $@
ifeq ($(FETCHER),)
ifneq (@WGET@,false)
FETCHER = @WGET@
else
ifneq (@CURL@,false)
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 = --location -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) :
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 $(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
ifeq (@ULIMIT_T@,yes)
LIMIT += ; ulimit -t
endif
ifeq (@ULIMIT_M@,yes)
LIMIT += ; ulimit -m
endif
ifeq (@ULIMIT_V@,yes)
LIMIT += ; ulimit -v
endif
ifeq (@ULIMIT_S@,yes)
LIMIT += ; ulimit -s
endif
recheck:
	rm -f .checked-$(VERSION)
	$(MAKE) check
check: .checked-$(VERSION)
.checked-$(VERSION) : .compiled-$(VERSION)
	$(WHY)
	+ set -x; $(LIMIT); $(CDBUILDDIR) && $(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 ""
autotool-versions:
	whereis autoconf
	autoconf --version | head -1
	whereis aclocal
	aclocal --version | head -1
	whereis automake
	automake --version | head -1

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