# Makefile for example programs that demonstrate different features of
# the Adept library
#
# Note that this Makefile is hand-coded rather than being generated by
# automake
#
# The -DADEPT_RECORDING_PAUSABLE option enables the pause_recording
# and continue_recording functionality and is used by test_adept,
# although it will run correctly (but slightly more slowly) without
# this flag

# The configure script writes the following file, which contains
# variables controlling the compilation
include ../makefile_include

ADEPT_FLAGS = -DADEPT_RECORDING_PAUSABLE

# The objects to create
OBJECTS = algorithm.o algorithm_noad.o test_checkpoint.o \
	test_adept.o test_adept_with_and_without_ad.o \
	test_radiances.o simulate_radiances.o test_thread_safe.o \
	test_no_lib.o test_misc.o
GSL_OBJECTS = test_gsl_interface.o state.o rosenbrock_banana_function.o

GSL_LIBS = -lgsl -lgslcblas

COMPILE_FLAGS = $(CXXFLAGS) $(CPPFLAGS) $(ADEPT_FLAGS) -I../include

# Because we aren't going to install the test programs, and we want
# them to work even if Adept is not installed, it is easiest to use
# libtool to create statically-linked executables
top_builddir = ..
CXXLINK = $(LIBTOOL) --tag=CXX --mode=link $(CXX) $(CXXFLAGS) \
	-static -no-install -L../adept/.libs $(LDFLAGS) -o $@

# Dependency on the presence of the Adept static library
LIBADEPT = ../adept/.libs/libadept.a

MYLIBS = -ladept $(LIBS)

PROGRAMS = test_adept test_adept_with_and_without_ad test_radiances \
	test_gsl_interface test_misc test_checkpoint test_thread_safe \
	test_no_lib

all:
	@echo "********************************************************"
	@echo "*** To compile test programs in test/ and benchmark/ ***"
	@echo "*** type \"make check\"                                ***"
	@echo "********************************************************"

# Compile all four programs
check: $(PROGRAMS)

# Test program 1
test_adept: algorithm.o test_adept.o $(LIBADEPT)
	$(CXXLINK) algorithm.o test_adept.o $(MYLIBS)

# Test program 2
test_adept_with_and_without_ad: algorithm.o algorithm_noad.o test_adept_with_and_without_ad.o $(LIBADEPT)
	$(CXXLINK) algorithm.o algorithm_noad.o test_adept_with_and_without_ad.o $(MYLIBS)

# Test program 3
test_radiances: simulate_radiances.o test_radiances.o $(LIBADEPT)
	$(CXXLINK) simulate_radiances.o test_radiances.o $(MYLIBS)

ifeq "X$(USE_GSL)" "Xyes"
# Test program 4
test_gsl_interface: $(GSL_OBJECTS) $(LIBADEPT)
	$(CXXLINK) $(GSL_OBJECTS) $(GSL_LIBS) $(MYLIBS)
else
test_gsl_interface:
	@echo "The executable test_gsl_interface will not be created because GSL library was not found"
endif

# Test program 5
test_misc: test_misc.o algorithm.o $(LIBADEPT)
	$(CXXLINK) test_misc.o algorithm.o $(MYLIBS)

# Test program 6
test_checkpoint: test_checkpoint.o $(LIBADEPT)
	$(CXXLINK) test_checkpoint.o $(MYLIBS)

# Test program 7
test_thread_safe: test_thread_safe.o $(LIBADEPT)
	$(CXXLINK) test_thread_safe.o $(MYLIBS)

# Test program 8 (note that it is not linked against the Adept library
test_no_lib: test_no_lib.o algorithm.o
	$(CXXLINK) test_no_lib.o algorithm.o

# The no-automatic-differentiation version of the algorithm: uses the
# -DADEPT_NO_AUTOMATIC_DIFFERENTIATION to produce a version of the
# algorithm that takes double rather than adouble arguments
algorithm_noad.o: algorithm.cpp *.h ../include/adept.h
	$(CXX) $(COMPILE_FLAGS) $(INCLUDES) -c algorithm.cpp -DADEPT_NO_AUTOMATIC_DIFFERENTIATION -o $@

# All other object files created by compiling the corresponding source
# file without this flag
%.o: %.cpp *.h ../include/adept.h
	$(CXX) $(COMPILE_FLAGS) $(INCLUDES) -c $<

# Remove all object files and executables
clean:
	rm -f $(OBJECTS) $(GSL_OBJECTS) $(PROGRAMS)

mostlyclean: clean

# Null targets to satisfy autotools
EMPTY_AUTOMAKE_TARGETS = distdir install install-data install-exec uninstall \
	install-dvi install-html install-info install-ps install-pdf \
	installdirs installcheck distclean maintainer-clean \
	dvi pdf ps info html tags ctags
.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
$(EMPTY_AUTOMAKE_TARGETS):