# @configure_input@

# user variables

MORE_SCCFLAGS :=
SCCFLAGS += $(MORE_SCCFLAGS)

# get rid of some unused implicit rules to make debugging simpler
%:%,v
%:RCS/%,v
%:RCS/%
%:s.%
%:SCCS/s.%
%:%.o
%:%.p
%:%.S
%:%.web
%:%.c
%:%.cc
%:%.C
%:%.cpp
%:%.f
%:%.F
%:%.mod
%:%.r
%:%.s
%:%.sh
%:RCS/%,v
%:RCS/%
%.c:%.y
%.c:%.l
%.c:%.w

include ../../include/config.Makefile
include ./Makefile.files
VPATH = @srcdir@
# these can be overridden on the command line
DEPENDS = yes
############################## main targets
echo = $(shell echo "echo: $1" >&2) $1

ifneq (@ETAGS@,false)
all: tags
endif
all: .gdbinit
.gdbinit:; if [ -f @srcdir@/.gdbinit.$(USER) ]; then cp @srcdir@/.gdbinit.$(USER) $@; fi

choice2 = all: $(patsubst %.d,%$(1),$(patsubst %.dd,%$(2),$(3)))
choice1 = all: $(patsubst %.d,%$(1),$(patsubst %.dd,%$(1),$(2)))

$(foreach d,$(M2_DFILES),				\
	$(eval $(call choice2,-tmp.c,-tmp.cc,$d))	\
	$(eval $(call choice1,-exports.h.tmp,$d))	\
	$(eval $(call choice1,.sig.tmp,$d))		\
	$(eval $(call choice1,.sig,$d))			\
	$(eval $(call choice1,.dep,$d))			\
	$(eval $(call choice1,.o,$d))			\
	)
all: $(M2_OBJECTS)


# eventually, libd.a will allow stand-alone *.d programs to use just what they want to from the code in this directory:
# all: libd.a
# LIBFILES := $(patsubst M2mem.o,,$(patsubst scclib.o,,$(M2_OBJECTS))) $(patsubst %, %.o, $(M2_DNAMES))
LIBFILES :=

# to get getmem
LIBFILES += M2mem.o 
# to get trapchk()
LIBFILES += debug.o
LIBFILES += gmp_aux.o arithmetic.o atomic.o M2.o system.o strings.o varstrin.o errio.o vararray.o ctype.o nets.o varnets.o interrupts.o pthread0.o stdio.o stdiop.o err.o gmp.o
libd.a: $(LIBFILES) ; ar -r $@ $?
rm-libd.a:; rm libd.a

clean:: ; rm -f $(M2_MADECFILES)
xml-c.o: xml-c.h xml-exports.h
M2lib.o basic.o equality.o actors4.o actors5.o interface.o interface2.o engine.o : CPPFLAGS += -I@srcdir@/../e

pthread-tmp.c : SCCFLAGS += -noline

version.o : ../../include/M2/config.h distributed-packages.h
distributed-packages.h: @srcdir@/../packages/?distributed-packages Makefile
	echo '#define DISTRIBUTED_PACKAGES "'`cat $<`'"' >$@
clean::; rm -f distributed-packages.h

# these files depend on WITH_MYSQL, which is in config.h:
actors4.o actors2.o : ../../include/M2/config.h

ifeq "$(DEPENDS)" "yes"
# We include all the files twice, once in reverse order.  Example: "include a b c d d c b a"
# The reason for this is that some versions of gnu make build the needed files in reverse order, and some build in forward order.
# (Compare version 4.3 to version 4.4)
include $(patsubst %.d, %.dep, $(patsubst %.dd, %.dep, $(M2_DFILES) $(shell ../util/echoout -r $(M2_DFILES))))
endif

.SUFFIXES: .d .sig .dep .res .test .m2
.PHONY : clean all check tags

ifeq "@HAVE_WPARENTHESES_EQUALITY@" "yes"
DCFLAGS   += -Wno-parentheses-equality
DCXXFLAGS += -Wno-parentheses-equality
endif

ifeq "@DEVELOPMENT@" "yes"
# -- development --
CFLAGS    += -Werror
CXXFLAGS  += -Werror
ifeq "@HAVE_WDISCARDS_QUALIFIERS@" "yes"
CFLAGS += -Wincompatible-pointer-types-discards-qualifiers
CXXFLAGS += -Wincompatible-pointer-types-discards-qualifiers
endif
ifeq "@HAVE_WSOMETIMES_UNINITIALIZED@" "yes"
DCFLAGS   += -Wsometimes-uninitialized
DCXXFLAGS += -Wsometimes-uninitialized
endif
# -- development end --
else
# -- non-development --
ifeq "@HAVE_WSOMETIMES_UNINITIALIZED@" "yes"
DCFLAGS   += -Wno-sometimes-uninitialized
DCXXFLAGS += -Wno-sometimes-uninitialized
endif
# -- non-development end --
endif

DCFLAGS   += -Wno-unused-value
DCXXFLAGS += -Wno-unused-value

# scc1 produces switch statements without default cases, because it knows what
# all the possibilities are, so the compiler may think some cases are not handled.  Hence
# we may hush some warnings

ifeq "@HAVE_WUNUSED_BUT_SET_VARIABLE@" "yes"
DCFLAGS   += -Wno-unused-but-set-variable
DCXXFLAGS += -Wno-unused-but-set-variable
endif

ifeq "@HAVE_WMAYBE_UNINITIALIZED@" "yes"
DCFLAGS   += -Wno-maybe-uninitialized
DCXXFLAGS += -Wno-maybe-uninitialized
endif

ifeq "@__INTEL_COMPILER@" "no"
CFLAGS += -Wno-unused
endif

ifeq "@__INTEL_COMPILER@" "yes"
DCFLAGS += -diag-disable 111,411,177,593,1572
ifeq "@DEBUG@" "yes"
CFLAGS += -diag-disable 1418
endif
endif

.SECONDARY:

M2INCLUDES += -I.
M2INCLUDES += -I@srcdir@
M2INCLUDES += -I@srcdir@/../e
M2INCLUDES += -I@srcdir@/../system
M2INCLUDES += -I@srcdir@/../../include

# these are the files that need to find include files built in the current build directory
$(M2_LASTCFILES:.c=.o) : CPPFLAGS += -I.

# to get scc-core.h:
M2INCLUDES += -I@srcdir@/../c

# it's important that *our* directories get searched first:
CPPFLAGS := $(M2INCLUDES) $(CPPFLAGS) $(M2_CPPFLAGS)

CPPFLAGS += -Wno-unknown-pragmas

CFLAGS += $(M2_CFLAGS)
ifeq "@__INTEL_COMPILER@" "no"
CFLAGS += -Wno-unused-label
endif

CXXFLAGS += $(M2_CXXFLAGS)

CFLAGS   += -Wreturn-type
CXXFLAGS += -Wreturn-type

CFLAGS   += -Wunused-function
CXXFLAGS += -Wunused-function

CFLAGS   += -Wfatal-errors
CXXFLAGS += -Wfatal-errors

version.o : CPPFLAGS += -I@srcdir@/../e/memtailor -I@srcdir@/../e/mathic -I@srcdir@/../e/mathicgb
version.o : CXXFLAGS += -Wno-unused-local-typedefs

# the following is included since g++-8 and gcc-8 give many warnings arising from "pragma omp"
# and apparently it can't be silenced in source code.
CXXFLAGS += -Wno-unknown-pragmas

# This hushes warnings about "struct" and "class" both being used.
CXXFLAGS += -Wno-mismatched-tags

python-c.o : | python-exports.h.tmp

# see https://github.com/Macaulay2/M2/issues/430
# adding this prevents an error when gcc 6 compiles stack_trace in M2lib.c
ifeq (@HAVE_WFRAME_ADDRESS@,yes)
M2lib.o : M2_CFLAGS += -Wno-frame-address
endif

M2lib.o scclib.o : | interp-exports.h.tmp
scclib.o : expr-exports.h M2mem.h debug.h types.h ../c/compat.h ../e/engine.h
M2lib.o : types.h ../c/compat.c debug.h M2mem.h ../c/compat.h ../e/engine.h
M2lib.o scclib.o gdbm_interface.o gc_cpp.o debug.o : ../../include/M2/config.h
M2.o : ../../include/M2/config.h @srcdir@/../c/scc-core.h

clean::; rm -f startup.c
SSTRING := -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/'
BSTRING := -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/\(.*\)/"\1"/'
../m2/startup.m2: ../m2/startup.m2.in ; cd ../.. ; ./config.status Macaulay2/m2/startup.m2
startup.c: startup-header.h ../m2/startup.m2 @srcdir@/../m2/basictests/*.m2 Makefile
	: making $@
	@(\
	 cat @srcdir@/startup-header.h; \
	 echo 'cached_file startupFile = {' ; \
	     echo Macaulay2/Core/startup.m2.in | sed $(BSTRING) ; \
	     echo  ',' ; \
	     cat ../m2/startup.m2 | sed $(SSTRING) ; \
	 echo  '};' ; \
	 echo 'cached_file testStrings[] = {' ; \
	 for i in @abs_top_srcdir@/Macaulay2/m2/basictests/*.m2 ; \
	 do echo '{' ; echo Macaulay2/Core/basictests/`basename $$i` | \
	     sed $(BSTRING) ; echo  ',' ; cat $$i | sed $(SSTRING) ; \
	     echo  '},' ; \
	 done ; \
	 echo '};' ; \
	 cat @srcdir@/startup-trailer.h; \
        ) >$@
clean:: ; rm -f startup.c
clean-startup.c:; rm -f startup.c
cat-startup.c:; cat startup.c
redo-startup:clean-startup.c startup.c cat-startup.c

all: $(M2_LIBDEPS)
tags: @srcdir@/TAGS
@srcdir@/TAGS: $(M2_SRCFILES); cd @srcdir@ && @ETAGS@ $(M2_SRCFILES)
clean-bin:; rm -f *.o *-tmp.c *-tmp.cc *.sig.tmp *-exports.h.tmp
clean::; rm -f typecode.db *-exports.h *-exports.h.tmp *-tmp.c *-tmp.cc *.log a.out *.o *.sym *.out *.a *.oo *.sig *.sig.tmp *.sg *.dep *.dp *.dep.tmp core gmon.out mapfile restart.tmp TAGS
distclean: clean; rm -f Makefile ../m2/startup.m2 @srcdir@/TAGS
Makefile: Makefile.in; cd ../..; ./config.status Macaulay2/d/Makefile
Makefile.files: Makefile.files.in; cd ../..; ./config.status Macaulay2/d/Makefile.files
clean::; rm -f .gdbinit .gdb-directories

# for experimenting with stand-alone programs
LOADLIBES = -lgc

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/Macaulay2/d MORE_SCCFLAGS= "
# End:
.NOTPARALLEL:
