# 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

# Uncomment the following to check what happens if thread safety
# disabled
# ADEPT_FLAGS = -DADEPT_STACK_THREAD_UNSAFE

# 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 test_arrays.o test_arrays_active.o \
	test_array_speed.o test_arrays_active_pausable.o \
	test_fixed_arrays_active.o test_radiances_array.o \
	test_fixed_arrays.o test_constructors.o test_derivatives.o \
	test_array_derivatives.o test_thread_safe_arrays.o
GSL_OBJECTS = test_gsl_interface.o state.o rosenbrock_banana_function.o

GSL_LIBS = -lgsl

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) -ladept -o $@

# Link without the Adept library
CXXLINK_NOLIB = $(LIBTOOL) --tag=CXX --mode=link $(CXX) $(CXXFLAGS) \
	$(LDFLAGS) -o $@

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

MYLIBS = $(LIBS)

PROGRAMS = test_adept test_adept_with_and_without_ad test_radiances \
	test_gsl_interface test_misc test_checkpoint test_thread_safe \
	test_array_speed test_no_lib test_radiances_array test_constructors \
	test_arrays test_arrays_active test_arrays_active_pausable \
	test_fixed_arrays test_fixed_arrays_active test_derivatives \
	test_array_derivatives test_thread_safe_arrays

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

# Compile all four programs
check: $(PROGRAMS) run-tests

run-tests:
	./run_tests.sh $(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_NOLIB) test_no_lib.o algorithm.o $(MYLIBS)

# Test program 9a
test_arrays: test_arrays.o $(LIBADEPT)
	$(CXXLINK) test_arrays.o $(MYLIBS)

# Test program 9b
test_arrays_active: test_arrays_active.o $(LIBADEPT)
	$(CXXLINK) test_arrays_active.o $(MYLIBS)

# Test program 9c
test_arrays_active_pausable: test_arrays_active_pausable.o $(LIBADEPT)
	$(CXXLINK) test_arrays_active_pausable.o $(MYLIBS)

# Test program 10
test_array_speed: test_array_speed.o $(LIBADEPT)
	$(CXXLINK) test_array_speed.o $(MYLIBS)


# Test program 11
test_radiances_array: simulate_radiances.o test_radiances_array.o $(LIBADEPT)
	$(CXXLINK) simulate_radiances.o test_radiances_array.o $(MYLIBS)

# Test program 12a
test_fixed_arrays: test_fixed_arrays.o $(LIBADEPT)
	$(CXXLINK) test_fixed_arrays.o $(MYLIBS)

# Test program 12b
test_fixed_arrays_active: test_fixed_arrays_active.o $(LIBADEPT)
	$(CXXLINK) test_fixed_arrays_active.o $(MYLIBS)

# Test program 13
test_constructors: test_constructors.o $(LIBADEPT)
	$(CXXLINK) test_constructors.o $(MYLIBS)

# Test program 14
test_derivatives: test_derivatives.o $(LIBADEPT)
	$(CXXLINK) test_derivatives.o $(MYLIBS)

# Test program 15
test_array_derivatives: test_array_derivatives.o $(LIBADEPT)
	$(CXXLINK) test_array_derivatives.o $(MYLIBS)

# Test program 16
test_thread_safe_arrays: test_thread_safe_arrays.o $(LIBADEPT)
	$(CXXLINK) test_thread_safe_arrays.o $(MYLIBS)

# 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/*.h ../include/adept/*.h
	$(CXX) $(COMPILE_FLAGS) $(INCLUDES) -c $<

test_arrays_active.o: test_arrays.cpp  *.h ../include/*.h ../include/adept/*.h
	$(CXX) $(COMPILE_FLAGS) -DALL_ACTIVE $(INCLUDES) -c test_arrays.cpp -o test_arrays_active.o

test_arrays_active_pausable.o: test_arrays.cpp  *.h ../include/*.h ../include/adept/*h
	$(CXX) $(COMPILE_FLAGS) -DADEPT_RECORDING_PAUSABLE -DALL_ACTIVE $(INCLUDES) -c test_arrays.cpp -o test_arrays_active_pausable.o

test_fixed_arrays_active.o: test_fixed_arrays.cpp  *.h ../include/*.h ../include/adept/*.h
	$(CXX) $(COMPILE_FLAGS) -DALL_ACTIVE $(INCLUDES) -c test_fixed_arrays.cpp -o test_fixed_arrays_active.o

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

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):
