# @configure_input@
.NOTPARALLEL:
include include/config.Makefile
VPATH = @srcdir@
.PRECIOUS:@srcdir@/configure config.status
.PHONY:unconfigure-libs reconfigure configure-help report-M2-location help scripts
all install:										\
	check-make config.status configured check-for-undefined-configure-variables	\
	optional-git-submodule-update							\
	srcdir protect-configs configured-files check-machine				\
	M2 all-in-subdirs report-M2-location
ifeq "@DEVELOPMENT@" "yes"
optional-git-submodule-update:
	: skipping automatic updating of submodules, because --enable-development was specified
else ifeq "@BUILDSUBLIST@" ""
optional-git-submodule-update:
	: skipping automatic updating of submodules, because no submodules are being built
else ifeq "@DOWNLOAD@" "yes"
optional-git-submodule-update:
	cd @srcdir@/.. && git submodule update
else
optional-git-submodule-update:
	: skipping automatic updating of submodules, because --enable-download not provided to configure script
endif
configured-files: @CONFIGURED_FILES@
$(foreach f,@CONFIGURED_FILES@,$(eval $f: @srcdir@/$f.in; ./config.status $f))
M2: GNUmakefile
	rm -f M2
	(echo '#! /bin/sh'; echo 'exec @pre_bindir@/M2 "$$@"') >M2
	chmod a+x M2
define package
all install-$1: all-$1
install: install-$1
install-$1 all-$1:; $(MAKE) -C Macaulay2/packages $$@
endef
$(foreach i, $(PACKAGES), $(eval $(call package,$i)))

check-make:; @ echo "$(.FEATURES)" | grep order-only >/dev/null || (echo error: version >= 3.81 of GNU make is required >&2; false)
which-tools:
	for i in m4 autoconf automake; do which $$i ; $$i --version | head -1 ; done
	printenv ACLOCAL_PATH
configured:include/configuration
check:configured check-in-Macaulay2
ifeq "@ENABLE_STRIP@" "yes"
check: check-stripped
endif
check:; : +++ all Macaulay2 tests succeeded +++
check-machine:; @[ "@UNAME_INFO@" = "` @UNAME_INFO_COMMAND@ `" ] || (echo "error: reconfiguration required (uname output seems different)" >&2; false)

define cdw
set +x && cd "$1" && echo make: Entering directory \'`pwd`\' && set -x
endef

# terminology:
#   subdirectories of the source tree
#     usr-build  -- install tools needed for rebuilding the configure script here, if needed
#   subdirectories of the build tree
#     usr-host   -- install the results of compilation of libraries, programs, and submodules, here, for the target machine
#     usr-dist   -- place copies of everything needed in the final Macaulay2 distribution here, for the target machine

check-stripped:
	@ echo "-- checking for unstripped binaries"
	file @pre_programsdir@/* $(BUILTLIBPATH)/bin/* @pre_bindir@/M2-binary usr-dist/bin/M2-binary usr-host/bin/* | (! grep "not stripped")

## submodules

ifeq (@SHARED@,no)
SUBMODULE_CONFIGOPTIONS += --disable-shared
else
# Our current strategy with submodules is to use them in place, so they don't have to be
# installed.  We like avoiding the installation step, because running "make install" a second time
# causes files to be copied again.  With shared libraries, though, the libraries have to be installed
# for use at run-time and then distributed as part of binary distributions.  So we disable the building of
# shared libraries here.
SUBMODULE_CONFIGOPTIONS += --disable-shared
endif

ifneq (@build_alias@,)
# it was ../configure --build=@build_alias@
SUBMODULE_CONFIGOPTIONS += --build=@build_alias@
endif
ifneq (@host_alias@,)
# it was ../configure --build=@host_alias@ (for cross compiling)
SUBMODULE_CONFIGOPTIONS += --host=@host_alias@
endif
copy-license-files-from-memtailor: LICENSE_FILES = README.md license.txt
copy-license-files-from-mathic: LICENSE_FILES = README.md lgpl-*
copy-license-files-from-mathicgb: LICENSE_FILES = README.md gpl-*

# the Makefile is the target used to run "configure" in the submodule:
submodules/mathicgb/Makefile : SUBMODULE_CONFIGOPTIONS += --with-gtest=yes \
	--disable-cli # possibly add this option? --with-tbb
submodules/mathic/Makefile   : SUBMODULE_CONFIGOPTIONS += --with-gtest=yes
submodules/memtailor/Makefile: SUBMODULE_CONFIGOPTIONS += --with-gtest=yes
submodules/fflas_ffpack/Makefile: LIBS += @LINALGLIBS@

# Note: we don't necessarily use the "install" target of submodules, even
# though we did so for libraries.  The problem with "install" targets is that
# they *always* copy their files, which is annoying.  Moreover, our submodules
# don't even come with "uninstall" targets, typically.
# include/config.Makefile.in for lines that add the locations of the the
# include files and libraries to CPPFLAGS and LDFLAGS.
BUILDSUBTARGETS = check all run-configure check install

# Even for submodules not scheduled to be built, we might as well arrange
# for these targets to fire also for them when doing them for all submodules:
SUBTARGETS = clean distclean
GITSUBTARGETS = git-clean git-update git-status git-describe

ifneq (,$(findstring memtailor,@BUILDSUBLIST@))
all-in-mathicgb all-in-mathic:all-in-memtailor
endif

ifneq (,$(findstring mathic ,@BUILDSUBLIST@))
all-in-mathicgb:all-in-mathic
endif

all-in-mathicgb:all-in-mathic all-in-memtailor

$(foreach t,$(BUILDSUBTARGETS),$(eval $t:$t-in-submodules))

$(foreach t,$(BUILDSUBTARGETS),$(eval $t-in-submodules:))
define build-submodule-rules
$(foreach t,$(BUILDSUBTARGETS),$(eval $t-in-submodules:$t-in-$1))
endef

# disable these ffpack checks, because they fail, and they're compile time only:
no-check-fflas_ffpack = 1

$(foreach t,$(SUBTARGETS) $(GITSUBTARGETS),$(eval $t-in-submodules:))
define submodule-rules
$(foreach t,$(SUBTARGETS) $(GITSUBTARGETS),$(eval $t-in-submodules:$t-in-$1))
all-in-$1: run-configure-in-$1 build-in-$1 copy-license-files-from-$1
copy-license-files-from-submodules: copy-license-files-from-$1
copy-license-files-from-$1:
	@$(MKDIR_P) $(BUILTLIBPATH)/licenses/$1
	@for i in $$(LICENSE_FILES); do                                     \
	  if ! ls $(BUILTLIBPATH)/licenses/$1/$$$$i > /dev/null 2>&1; then  \
	    cp -v @srcdir@/submodules/$1/$$$$i $(BUILTLIBPATH)/licenses/$1; \
	  fi ;                                                              \
	done
build-in-$1: submodules/$1/Makefile
	$(MAKE) -C submodules/$1 all
check-in-$1: run-configure-in-$1
ifeq ($(no-check-$1),)
	$(MAKE) -C submodules/$1 check
endif

install-in-$1: build-in-$1

# it may look odd to make submodules/$1/Makefile, but this allows recovery if the submodule
# has been updated or cleaned, and needing scripts (such as "missing") installed by libtool 
# are no longer there:
$(foreach c, clean distclean,
    $(eval $c-in-$1:; + if [ -f submodules/$1/Makefile ]; then $(MAKE) submodules/$1/Makefile && $(MAKE) -C submodules/$1 $c; fi)
    $(eval $c: $c-in-$1))

git-checkout-in-$1:; cd @srcdir@/.. && git submodule update --init M2/submodules/$1
git-update-in-$1:; cd @srcdir@/submodules/$1 && git checkout master && git pull
git-status-in-$1:; cd @srcdir@/submodules/$1 && git status
git-clean-in-$1:; cd @srcdir@/submodules/$1 && git clean -Xdf
git-describe-in-$1:; cd @srcdir@/submodules/$1 && git describe --dirty --long --always --abbrev=40 --all
run-configure-in-submodules: run-configure-in-$1
run-configure-in-$1: submodules/$1/Makefile
unconfigure-in-submodules: unconfigure-in-$1
unconfigure-in-$1:; rm -f submodules/$1/Makefile
submodules/$1/Makefile:@srcdir@/submodules/$1/configure @srcdir@/submodules/$1/Makefile.in
	$(MKDIR_P) submodules/$1
	@ $(call cdw,submodules/$1) &&							\
	 @abs_srcdir@/submodules/$1/configure						\
		--prefix=$(BUILTLIBPATH)						\
		PKG_CONFIG_PATH="$$(BUILTLIBPATH)/lib/pkgconfig:$$(PKG_CONFIG_PATH)"	\
		GTEST_PATH=@GTEST_PATH@							\
		AR=@AR@									\
		AS=@AS@									\
		DLLTOOL=@DLLTOOL@							\
		OBJDUMP=@OBJDUMP@							\
		STRIP=@STRIP@								\
		CXXFLAGS="$$(CXXFLAGS)"							\
		CFLAGS="$$(CFLAGS)"							\
		FFLAGS="$$(FCFLAGS)"							\
		FCFLAGS="$$(FCFLAGS)"							\
		TARGET_ARCH=$$(TARGET_ARCH)						\
		CPPFLAGS="$$(CPPFLAGS)"							\
		CC="$$(CC)"								\
		CXX="$$(CXX)"								\
		LDFLAGS="$$(LDFLAGS)"							\
		LOADLIBES="$$(LOADLIBES)"						\
		LDLIBS="$$(LDLIBS)"							\
		LIBS="$$(LIBS)"								\
		$$(SUBMODULE_CONFIGOPTIONS)

@srcdir@/submodules/$1/configure.ac: $(if $(or $(filter yes, @DOWNLOAD@), \
	$(filter $1, memtailor mathic mathicgb)), \
	git-checkout-in-$1, git-checkout-warning-for-$1)

git-checkout-warning-for-$1:
	@ echo "error: for the submodule \"$1\"" >&2
	@ echo "       the source code is not present in the directory \"submodules/$1\"" >&2
	@ echo "       so either run 'git submodule update --init' in the source tree" >&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
	@ echo "       To clone a repository together with its submodules, add the option" >&2
	@ echo "       '--recursive' to the 'git clone' command." >&2
	@ false

@srcdir@/submodules/$1/configure @srcdir@/submodules/$1/Makefile.in: @srcdir@/submodules/$1/configure.ac @srcdir@/submodules/$1/Makefile.am
	cd @srcdir@/submodules/$1 && NOCONFIGURE=1 ./autogen.sh
endef
$(foreach s,@SUBLIST@,$(eval $(call submodule-rules,$s)))
$(foreach s,@BUILDSUBLIST@,$(eval $(call build-submodule-rules,$s)))

# installation into usr-dist
## It's annoying that fflas_ffpack will copy the files during installation every time.
all-in-fflas_ffpack: install-in-fflas_ffpack
all-in-memtailor:    install-in-memtailor
all-in-mathic:       install-in-mathic
all-in-mathicgb:     install-in-mathicgb

install-in-fflas_ffpack: $(BUILTLIBPATH)/include/fflas-ffpack/fflas-ffpack.h
install-in-memtailor:    $(BUILTLIBPATH)/include/memtailor.h
install-in-mathic:       $(BUILTLIBPATH)/include/mathic.h
install-in-mathicgb:     $(BUILTLIBPATH)/include/mathicgb.h

$(BUILTLIBPATH)/include/fflas-ffpack/fflas-ffpack.h:
	$(MAKE) -C submodules/fflas_ffpack install

$(BUILTLIBPATH)/include/memtailor.h:
	$(MAKE) -C submodules/memtailor install

$(BUILTLIBPATH)/include/mathic.h:
	$(MAKE) -C submodules/mathic install

$(BUILTLIBPATH)/include/mathicgb.h:
	$(MAKE) -C submodules/mathicgb install

ifeq (@BUILD_givaro@,yes)
# fflas_ffpack depends on givaro, so if they both get built, then build givaro first.
# This is a counterintuitive way to specify a dependence of one submodule on another.
submodules/fflas_ffpack/Makefile: install-in-givaro
endif

all-in-Macaulay2: all-in-submodules

## 
distclean:clean distclean-in-subdirs distclean-this-dir
distclean-this-dir:
	rm -f @CONFIGURED_FILES@ include/M2/config.h include/config.Makefile config.cache config.log configure.options config.status config.args
	rm -f include/M2/synchronization.h
	find . -type d -empty -delete
$(foreach t,all check clean distclean,					\
	$(foreach d,libraries Macaulay2 distributions check-configure,	\
	   $(eval .PHONY: $t-in-subdirs)				\
	   $(eval .PHONY $t $t-in-subdirs:$t-in-$d)			\
	   $(eval $t-in-$d:; $$(MAKE) -C $d $t)))
clean:
	rm -rf .completed
	rm -f M2 srcdir .link-test srcdir confdefs.h configure.lineno conftest.* include/config.h
	rm -rf usr-dist conf_* usr-host
	rm -f pre-install.sh post-install.sh normal-install.sh
	if [ -f Makefile ] ; then $(MAKE) -f Makefile clean-tools ; fi

install: configured; $(MAKE) -C distributions $@
@srcdir@/configure : @srcdir@/configure.ac @srcdir@/config/files; $(MAKE) -C @srcdir@ -f Makefile
recheck config.status: @srcdir@/configure @srcdir@/Macaulay2/packages/=distributed-packages
	$(WHY)
	./config.status --recheck
	./config.status
status: @srcdir@/configure
	@echo '-----------------------------------------------------------------------------'
	@echo '#  Here is the way you ran $^ before:'
	@echo '#'
	@echo "# `if test -f config.args ; then cat config.args ; else echo arguments not available ; fi`"
	@echo '#'
	@echo '#  Type'
	@echo '        make reconfigure'
	@echo '#  to run it that way again.'
	@echo '#'
	@echo '-----------------------------------------------------------------------------'
srcdir: GNUmakefile
	: creating or removing symbolic link to common staging area, if necessary,
	: based on comparison of these directories:
	:                      pre_prefix : @pre_prefix@
	: abs_builddir/usr-dist/common : @abs_builddir@/usr-dist/common
	@ if [ "@abs_builddir@"/usr-dist/common != "@pre_prefix@" ] ;\
	then (set -x ;\
	     rm -rf usr-dist/common ;\
	     $(MKDIR_P) usr-dist ;\
	     ln -s "@pre_prefix@" usr-dist/common) ;\
	else \
	     if [ -L "@abs_builddir@"/usr-dist/common ] ;\
	     then (set -x ;\
		   rm usr-dist/common ;\
		  ) ;\
	     fi ;\
	fi
	rm -f $@ .link-test
	echo "@srcdir@/" >$@
subst: config.status ; ./config.status
show: config.status
	<$< sed -e 's/\\\n//' |egrep '^[SD]\["' | sed -e 's/^S."\(.*\)"\]="\(.*\)"$$/\1=\2/' -e 's/^D."\(.*\)"\]="\(.*\)"$$/#define \1 \2/' -e 's/\\"/"/g'
CONFIG_FILES = @srcdir@/configure @srcdir@/config/install-sh @srcdir@/config/config.sub @srcdir@/config/config.guess @srcdir@/config/files
reconfigure-top-only: recheck check-for-undefined-configure-variables protect-configs
protect-configs:
	 @ chmod a-w $(shell cat @srcdir@/config/files)
check-for-undefined-configure-variables:
	: "checking for strings that look like undefined configure variables in all *.in files..."
	@ if egrep -nH '@[A-Za-z_]+@' $(shell cat @srcdir@/config/files) | sed -e 's=^\([^:]*\):=@srcdir@/\1.in:=' | egrep . ;	\
	  then exit 1;														\
	  fi
configure-help: $(CONFIG_FILES) ; @ @srcdir@/configure --help
relink:; rm -f @pre_exec_prefix@/bin/M2@EXE@ && $(MAKE) -C Macaulay2/d @pre_exec_prefix@/bin/M2@EXE@
relink-nostrip:; rm -f @pre_exec_prefix@/bin/M2@EXE@ && $(MAKE) -C Macaulay2/d ENABLE_STRIP=no @pre_exec_prefix@/bin/M2@EXE@
unmark-packages:; $(MAKE) -C Macaulay2/packages $@
reconfigure: reconfigure-top-only unconfigure-libs 
unconfigure-libs:; $(MAKE) -C libraries unconfigure
remove-deps:; find . -name \*.dep -delete
find-conflicts:; grep -r -nH --exclude-dir BUILD -e '^<<<<<<< ' @srcdir@ || true
log-archive:; find . -name config.log |xargs tar xzf config-logs.tgz
shell:; PKG_CONFIG_PATH=$(M2_PKG_CONFIG_PATH) bash
report-M2-location:
	: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
	: Macaulay2 executable location:
	:    @pre_bindir@/M2@EXE@
	: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :

validate-html:
	$(MAKE) -C Macaulay2/packages $@

announce:
	@ echo "================================================================="
	@ echo "    Macaulay2 version @PACKAGE_VERSION@"
	@ echo "    OS=@OS@ ISSUE=@ISSUE@ ARCH=@ARCH@"
	@ echo "    DEBUG=@DEBUG@ OPTIMIZE=@OPTIMIZE@ ENCAP=@ENCAP@"
	@ echo "================================================================="

help:
	@ echo "Macaulay2 version @PACKAGE_VERSION@"
	@ echo ""
	@ echo "make files configured with:"
	@ echo "    OS=@OS@ ISSUE=@ISSUE@ ARCH=@ARCH@"
	@ echo "    DEBUG=@DEBUG@ OPTIMIZE=@OPTIMIZE@ ENCAP=@ENCAP@"
	@ echo ""
	@ echo "Macaulay2 executable location:"
	@ echo "   @pre_bindir@/M2@EXE@"
	@ echo ""
	@ echo "usage: make ..."
	@ echo ""
	@ echo "targets:"
	@ echo "  all               build/compile all files and distributions [the default target]"
	@ echo "  install -o all    install files (without re-making them)"
	@ echo "  relink            remove M2@EXE@ and rebuild it"
	@ echo "  relink-nostrip    remove M2@EXE@ and rebuild it, unstripped"
	@ echo "  install           make and install files"
	@ echo "  check             run the tests"
	@ echo "  clean             remove all generated files except configured files"
	@ echo "  distclean         remove all generated files"
	@ echo "  help              show this help"
	@ echo ""
	@ echo "targets for all libraries:"
	@ echo "  all-in-libraries"
	@ echo "  check-in-libraries"
	@ echo "  install-in-libraries"
	@ echo "  clean-in-libraries"
	@ echo ""
	@ echo "targets for all submodules:"
	@ echo "  all-in-submodules"
	@ echo "  build-in-submodules"
	@ echo "  check-in-submodules"
	@ echo "  install-in-submodules"
	@ echo "  clean-in-submodules"
	@ echo ""
	@ echo "targets for a submodule FOO:"
	@ echo "  all-in-FOO"
	@ echo "  build-in-FOO"
	@ echo "  check-in-FOO"
	@ echo "  clean-in-FOO"
	@ echo ""
	@ echo "configuration display:"
	@ echo "  status                    show options used with 'configure'"
	@ echo "  configure-help            show help for 'configure'"
	@ echo "  reconfigure               re-run configure, set libraries for later reconfiguration and thus recompilation"
	@ echo "  reconfigure-top-only      re-run configure"
	@ echo "  unconfigure-libs          set libraries for later reconfiguration"
	@ echo "  subst                     re-run config.status, remaking the makefiles"
	@ echo "  show                      show configuration variable values"
	@ echo "  find-conflicts            find conflict markers in source files"
	@ echo ""
	@ echo "package options:"
	@ echo "  PACKAGES='Foo Bar'             the list of packages to install"
	@ echo "  ReinstallPackages=true         reinstall the packages"
	@ echo ""
	@ echo "package options, when installing:"
	@ echo "  IgnoreExampleErrors=true       continue even after errors in example code"
	@ echo "  RerunExamples=true             rerun example output files"
	@ echo "  RemakeAllDocumentation=true    remake all documentation"
	@ echo "  CheckDocumentation=false       don't check documentation for completeness"
	@ echo "  debugLevel=1                   turn on further M2 debugging output"
	@ echo ""
	@ echo "make install options:"
	@ echo "  prefix=...          installation prefix"
	@ echo "                      (currently ${prefix})"
	@ echo "  exec_prefix=...     installation prefix for architecture dependent files"
	@ echo "                      (currently ${exec_prefix})"
	@ echo "  ENCAP=yes           install in ${prefix}/@package@ instead"
	@ echo "  MAPFILE=yes         create a mapfile when linking M2"
	@ echo "  DESTDIR=/Foo/Bar    install in /Foo/Bar${prefix} instead"
# Local Variables:
# compile-command: "make -C $M2BUILDDIR "
# End:
