# these values filled in by    yorick -batch make.i
Y_MAKEDIR=
Y_EXE=
Y_EXE_PKGS=
Y_EXE_HOME=
Y_EXE_SITE=
Y_HOME_PKG=

# ----------------------------------------------------- optimization flags

# options for make command line, e.g.-   make COPT=-g TGT=exe
COPT=$(COPT_DEFAULT) -L../lib/.libs -I../include
TGT=$(DEFAULT_TGT)

# ------------------------------------------------ macros for this package

#include ../local_settings

vpath %.C @srcdir@

CXXFLAGS=@CXXFLAGS@
CPPFLAGS=@CPPFLAGS@
XERCES_LIBS=@XERCES_LIBS@
XERCES_CFLAGS=@XERCES_CFLAGS@
UDUNITS_CFLAGS=@UDUNITS_CFLAGS@
UDUNITS_LIBS=@UDUNITS_LIBS@
CFITSIO_CFLAGS=@CFITSIO_CFLAGS@
CFITSIO_LIBS=@CFITSIO_LIBS@
BOOST_CPPFLAGS=@BOOST_CPPFLAGS@
DYLIB_VAR=@DYLIB_VAR@
PTHREAD_CFLAGS= @PTHREAD_CFLAGS@
PTHREAD_LIBS= @PTHREAD_LIBS@

YGYOTO_C = $(shell ls @srcdir@/*.C)
GYOTO_C = $(shell ls @top_srcdir@/lib/*.C)
GYOTO_OBJ = $(GYOTO_C:.C=.o)
GYOTO_H = $(shell ls @top_srcdir@/include/*.h)

# Works with openmpi 3
MPIRUN = mpirun -np 1 -oversubscribe

PKG_NAME=gyoto
PKG_I=@srcdir@/gyoto.i

OBJS=gyoto_utils.o gyoto_Metric.o gyoto_Photon.o \
	gyoto_Scenery.o gyoto_Screen.o gyoto_Idx.o gyoto_Spectrum.o \
	gyoto_Astrobj.o gyoto_ThinDisk.o \
	gyoto_Spectrometer.o gyoto_SpectroComplex.o gyoto_SpectroUniform.o 

# gyoto_KerrBL.o gyoto_KerrKS.o gyoto_Star.o gyoto_FixedStar.o gyoto_PolishDoughnut.o gyoto_ThinInfiniteDiskBL.o gyoto_ThinInfiniteDiskKS.o 

# change to give the executable a name other than yorick
PKG_EXENAME=yorick

# PKG_DEPLIBS=-Lsomedir -lsomelib   for dependencies of this package
PKG_DEPLIBS=-L../lib/.libs -lgyoto@FEATURES@ $(XERCES_LIBS) $(PTHREAD_LIBS) $(CFITSIO_LIBS) $(UDUNITS_LIBS)
# set compiler (or rarely loader) flags specific to this package
PKG_CFLAGS= -I@top_srcdir@/include $(XERCES_CFLAGS) $(UDUNITS_CFLAGS) $(BOOST_CPPFLAGS) $(PTHREAD_CFLAGS) $(CFITSIO_CFLAGS)
PKG_LDFLAGS= $(PTHREAD_CFLAGS)

# list of additional package names you want in PKG_EXENAME
# (typically Y_EXE_PKGS should be first here)
EXTRA_PKGS=$(Y_EXE_PKGS)

# list of additional files for clean
PKG_CLEAN=

# autoload file for this package, if any
PKG_I_START=
# non-pkg.i include files for this package, if any
PKG_I_EXTRA=@srcdir@/gyoto_constants.i @srcdir@/gyotoy.i @srcdir@/gyoto_namespace.i

# -------------------------------- standard targets and rules (in Makepkg)

# set macros Makepkg uses in target and dependency names
# DLL_TARGETS, LIB_TARGETS, EXE_TARGETS
# are any additional targets (defined below) prerequisite to
# the plug-in library, archive library, and executable, respectively
PKG_I_DEPS=$(PKG_I)
Y_DISTMAKE=distmake

include $(Y_MAKEDIR)/Make.cfg
# Yorick-distributed Makepkg is not reliable for out-of-tree builds.
include @top_srcdir@/yorick/Makepkg
include $(Y_MAKEDIR)/Make$(TGT)

# override macros Makepkg sets for rules and other macros
# Y_HOME and Y_SITE in Make.cfg may not be correct (e.g.- relocatable)
Y_HOME=$(Y_EXE_HOME)
Y_SITE=$(Y_EXE_SITE)
# use the compiler selected with the configure script
CC=@CC@
CXX=@CXX@

# Actual install directories set with ./configure --with-yorick-*
prefix=@prefix@
exec_prefix=@exec_prefix@
DEST_Y_HOME=$(DESTDIR)@Y_INST_HOME@
DEST_Y_SITE=$(DESTDIR)@Y_INST_SITE@
abs_top_builddir = @abs_top_builddir@

# ------------begin C++ source hacks
# must use C++ to load yorick with this C++ package
# this assumes make default CXX macro points to C++ compiler
CXXFLAGS+=$(CFLAGS) $(GYOTO_FLAGS)
LD_DLL=$(CXX) $(LDFLAGS) $(PLUG_SHARED)
LD_EXE=$(CXX) $(LDFLAGS) $(PLUG_EXPORT)

# C++ has no standard file extension, supply default make rule(s)
.C.o:
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<
# --------------end C++ source hacks

# simple example:
#myfunc.o: myapi.h
# more complex example (also consider using PKG_CFLAGS above):
#myfunc.o: myapi.h myfunc.c
#	$(CC) $(CPPFLAGS) $(CFLAGS) -DMY_SWITCH -o $@ -c myfunc.c

DEST_PKG_INSTALLED_DIR=$(DEST_Y_SITE)/packages/installed

all: gyoto.so gyoto_std.so

gyoto_std.so: stdplug/gyoto_std.so
	cp stdplug/gyoto_std.so ./

stdplug/gyoto_std.so: @srcdir@/stdplug/*.C
	cd stdplug; $(MAKE)

gyoto.so: gyoto.i $(OBJS)

custom.i: @srcdir@/custom1.i
	cp $< $@

check-dll: $(PKG_DLL) check.i custom.i
	unset GYOTO_PLUGINS && \
	GYOTO_CHECK_NODISPLAY=true \
	./yorick1 -i check.i

check-mpi: $(PKG_DLL) check-mpi.i custom.i
	unset GYOTO_PLUGINS && \
	GYOTO_CHECK_NODISPLAY=true \
	PATH=$(abs_top_builddir)/bin:$$PATH \
	$(MPIRUN) ./yorick1 -i check-mpi.i

install::
	cd stdplug; make install
	$(YNSTALL) @srcdir@/ygyoto.h @srcdir@/ygyoto_idx.h @srcdir@/ygyoto_supplier.C $(DEST_Y_SITE)/include
	$(YNSTALL) @srcdir@/gyotoy.xml $(DEST_Y_SITE)/glade
	$(YNSTALL) @srcdir@/gyotoy.png $(DEST_Y_SITE)/data
	$(YNSTALL) @srcdir@/gyotoy $(DEST_Y_SITE)/bin
	-ln $(DEST_Y_SITE)/bin/gyotoy $(DESTDIR)$(exec_prefix)/bin/
	$(YNSTALL) @srcdir@/gyotoy.1 $(DESTDIR)$(prefix)/share/man/man1
	$(YNSTALL) gyoto.info $(DEST_PKG_INSTALLED_DIR)

uninstall::
	cd stdplug; make uninstall
	cd $(DEST_Y_SITE)/include ; rm -f ygyoto.h ygyoto_idx.h ygyoto_supplier.C
	rm -f $(DEST_Y_SITE)/glade/gyotoy.xml
	rm -f $(DEST_Y_SITE)/data/gyotoy.png
	rm -f $(DEST_Y_HOME)/bin/gyotoy
	rm -f $(DESTDIR)$(exec_prefix)/bin/gyotoy
	rm -f $(DESTDIR)$(prefix)/share/man/man1/gyotoy.1
	-rm -f $(DEST_PKG_INSTALLED_DIR)/gyoto.info

clean:: 
	cd stdplug; $(Y_EXE) -batch make.i; make clean
	rm -f gyoto_std.so example-patterndisk.fits.gz example-patterndisk.xml

distclean:: $(Y_EXE)
	cd stdplug; $(Y_EXE) -batch make.i; make distclean
	-rm custom.i

%: ;

.PHONY: standalone test-cppprog test-standalone test test-plugin check

# -------------------------------------------------------- end of Makefile
