# @configure_input@
include ../../include/config.Makefile
VPATH = @srcdir@
SRCDIR = @srcdir@
DEPENDS = yes

PRE.cc   = $(COMPILE.cc) -E 

include @srcdir@/Makefile.files
include Makefile.common
CPPFLAGS := -I@srcdir@ $(CPPFLAGS) -I../d -I@srcdir@/../c -I@srcdir@/../d -Wno-fatal-errors -Wno-unused-variable
CXXFLAGS += -Wno-unknown-pragmas -std=gnu++17 -Wno-macro-redefined

DOXYGEN_CONF_DIR :=$(SRCDIR)/doxygen-settings

.PHONY : tags

###################################################################
## Targets ##
#############

ifneq (@ETAGS@,false)
all: tags
endif
TAGS tags: @srcdir@/TAGS
@srcdir@/TAGS: Makefile.in @srcdir@/*.hpp @srcdir@/*.h @srcdir@/*.cpp @srcdir@/*.c \
	@srcdir@/NCAlgebras/*.hpp @srcdir@/NCAlgebras/*.cpp \
	@srcdir@/interface/*.h @srcdir@/interface/*.cpp \
	@srcdir@/schreyer-resolution/*.hpp @srcdir@/schreyer-resolution/*.cpp \
	@srcdir@/f4/*.hpp @srcdir@/f4/*.cpp \
	@srcdir@/bibasis/*.hpp @srcdir@/bibasis/*.cpp \
	@srcdir@/unit-tests/*.cpp
	@ echo " ** making TAGS file"
	@ cd @srcdir@ && @ETAGS@ $(patsubst @srcdir@/%, %, $^)
# note: ETAGS could be either ctags or etags, so any options given have to work with both

# overflows
LDLIBS = -lgc

check:: run-overflow-test
run-overflow-test : overflow-test Makefile
	@ echo "#warning: the following 6 tests are intended to throw uncaught overflow exceptions"
	./overflow-test throw || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	./overflow-test sub   || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	./overflow-test minus || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	./overflow-test mult  || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	./overflow-test div   || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	./overflow-test add   || if [ $$? = 1 ] ; then exit 1 ; else echo OVERFLOW TEST SUCCEEDED ; fi
	touch $@
clean :: ; rm -f run-overflow-test
overflow-test : overflow.o ../d/debug.o ../d/M2mem.o ../d/memdebug.o
clean :: ; rm -f overflow-test

#all:; $(MAKE) -C f4

ifeq "$(SHARED)" "yes"
all: @pre_librariesdir@/libengine.so
else
all: $(ENGINE_OFILES)
endif

%.ii: %.cpp; $(PRE.cc)        $< $(OUTPUT_OPTION)
%.s : %.cpp; $(COMPILE.cc) -S $< $(OUTPUT_OPTION)
%.ii: %.cc; $(PRE.cc)        $< $(OUTPUT_OPTION)
%.s : %.cc; $(COMPILE.cc) -S $< $(OUTPUT_OPTION)
%.ss : %.s ; demangle <$< >$@

%.lo : %.c  ; $(COMPILE.c)  -fPIC $< $(OUTPUT_OPTION)
%.lo : %.cc ; $(COMPILE.cc) -fPIC $< $(OUTPUT_OPTION)
%.lo : %.cpp; $(COMPILE.cc) -fPIC $< $(OUTPUT_OPTION)
@pre_librariesdir@/libengine.so : $(LOFILES); @CC@ -shared $^ $(OUTPUT_OPTION)

MORE_OPTIONS = -Wno-cast-qual
COMPILE.c += $(MORE_OPTIONS)
COMPILE.cc += $(MORE_OPTIONS)

ntl_interface.o: CXXFLAGS += -Wno-old-style-cast

clean::
	rm -f *.lo *.o *.rpo LOGFILE
	find . -name \*.o -delete
	find . -name \*.dep -delete
	find . -name dep-\*.tmp -delete

Makefile : Makefile.in; cd ../..; ./config.status Macaulay2/e/Makefile
all: Makefile.common
Makefile.common : Makefile.common.in; cd ../..; ./config.status Macaulay2/e/Makefile.common
Makefile.include : Makefile.include.in; cd ../..; ./config.status Macaulay2/e/Makefile.include
clean::; rm -f *.dep dep-*.tmp typecode.db TAGS
ifeq "$(DEPENDS)" "yes"
include $(ENGINE_CFILES:.c=.dep) $(ENGINE_CCFILES:.cpp=.dep)
endif

clean check::
	$(MAKE) -C unit-tests $@

# this requires gnu sort and gnu uniq commands
NMFLAGS = -o
ifeq (@NM_DEMANGLES@,yes)
NMFLAGS += --demangle
endif
dups-tmp: $(ENGINE_OBJFILES)
	nm $(NMFLAGS) $(ENGINE_OBJFILES) |grep ' T ' |sort -k 2 |uniq -2 -d |tee dups-tmp
dups-okay: dups-tmp
	@if [ -s $< ]; then echo "Multiple definitions found:"; cat $<; exit 1; \
	 else touch $@; fi
doxygen : ; cd @srcdir@ && doxygen
distclean: clean; rm -f Makefile @srcdir@/TAGS

SCCFLAGS += -I../d

# we like using "struct" and "class" interchangeably
CXXFLAGS += -Wno-mismatched-tags

# a stand-alone program made from test.dd
# test : ../c/scc-core.o
test : complex.o
test : LDFLAGS += -L../d -lmpfr -lmpir -lfac -lcf -lcfmem
test : LDLIBS += -ld

test.o : CXXFLAGS += -Wno-unused -Wno-uninitialized -Wno-cast-qual

clean :: ; rm -rf *.dSYM

DOXYGEN_OUTPUT_BASEDIR = doc

extended_doxygen:
	rm -rf ./$(DOXYGEN_OUTPUT_DIR)
	mkdir -p $(DOXYGEN_OUTPUT_DIR)
	DOXYGEN_INPUT_DIR=$(SRCDIR)				\
	  DOXYGEN_OUTPUT_DIR=$(DOXYGEN_OUTPUT_DIR)		\
	  DOXYGEN_OUTPUT_BASEDIR=$(DOXYGEN_OUTPUT_BASEDIR)	\
	  HAVE_DOT_SETTING=$(HAVE_DOT_SETTING)			\
	  CALL_GRAPH_SETTING=$(CALL_GRAPH_SETTING)		\
	  doxygen @srcdir@/doxygen.conf DOXYGEN_INPUT_DIR=asdfasdf

# doxygen -u ./Doxyconf
# doxygen -s  -u ./Doxyconf_copy ; # updates a config file and drops parameter documentation.

fulldoc smalldoc tinydoc: extended_doxygen
tinydoc: DOXYGEN_OUTPUT_DIR := $(DOXYGEN_OUTPUT_BASEDIR)/tinydoc
smalldoc: DOXYGEN_OUTPUT_DIR := $(DOXYGEN_OUTPUT_BASEDIR)/smalldoc
fulldoc: DOXYGEN_OUTPUT_DIR := $(DOXYGEN_OUTPUT_BASEDIR)/fulldoc
tinydoc: HAVE_DOT_SETTING=NO
smalldoc fulldoc: HAVE_DOT_SETTING=YES
tinydoc smalldoc: CALL_GRAPH_SETTING=NO
fulldoc: CALL_GRAPH_SETTING=YES

cleandoc: ; rm -rf $(DOXYGEN_OUTPUT_BASEDIR)

HPP_FILES := $(shell find @srcdir@ -name \*.hpp | sed -e 's=@srcdir@.==')
H_FILES   := $(shell find @srcdir@ -name \*.h   | sed -e 's=@srcdir@.==')

check-include-file-compilation: $(HPP_FILES:.hpp=-hpp.o) $(H_FILES:.h=-h.o)

reformat:; cd @srcdir@ && clang-format-3.8 -i $(ENGINE_SRCFILES)

help:
	@ echo "Macaulay2 @PACKAGE_VERSION@"
	@ echo ""
	@ echo "Current configuration:"
	@ echo "    OS=@OS@ REL=@REL@ ISSUE=@ISSUE@ ARCH=@ARCH@"
	@ echo "    DEBUG=@DEBUG@ OPTIMIZE=@OPTIMIZE@ ENCAP=@ENCAP@"
	@ echo ""
	@ echo "targets:"
	@ echo "  all               build/compile all files in Macaulay2/e [the default target]"
	@ echo "  clean             remove all generated files in Macaulay2/e except configured files"
	@ echo "  help              show this help"
	@ echo ""
	@ echo "  (documentation targets)"
	@ echo ""
	@ echo "  tinydoc    no diagrams"
	@ echo "  smalldoc   with diagrams but without function call diagrams"
	@ echo "  fulldoc    with almost all diagrams"
	@ echo "  cleandoc   remove documentation"
	@ echo ""

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/Macaulay2/e DEPENDS=no initialize && make -C $M2BUILDDIR/Macaulay2/e "
# End:

.NOTPARALLEL:
