# gpimg C library: exact O(n) 2D Gaussian processes for multi-band light curves.
#
#   make                 shared + static library in build/ (libgpimg.so / .dylib, libgpimg.a)
#   make example         build/example (fit + image of a synthetic light curve)
#   make test            build/test_gpimg, run it (likelihood vs dense Cholesky, gradients, smoothers)
#   make bitlock         Matern-3/2 results bit-identical to the committed fingerprint (tests/bitlock_m32.txt)
#   make install         PREFIX=/usr/local by default: lib/, include/gpimg.h, lib/pkgconfig/gpimg.pc
#
# Variables: CC (default cc), ARCH (e.g. ARCH=-march=native for the fastest build on this machine),
# OPENMP=0 to build without OpenMP (gp_batch then runs serially), PREFIX, DESTDIR.
CC      ?= cc
ARCH    ?=
OPENMP  ?= 1
PREFIX  ?= /usr/local
VERSION := 0.2.0

UNAME := $(shell uname -s)
ifeq ($(UNAME),Darwin)
  SHLIB := libgpimg.dylib
  SHFLAGS := -dynamiclib -install_name $(PREFIX)/lib/$(SHLIB)
else
  SHLIB := libgpimg.so
  SHFLAGS := -shared -Wl,-soname,libgpimg.so.0
endif

# IEEE semantics are kept (no -ffast-math); FMA contraction is allowed.
CFLAGS_BASE := -O3 $(ARCH) -fno-math-errno -ffp-contract=fast -fPIC -std=gnu11 -Wall -Wextra -Iinclude
ifeq ($(OPENMP),1)
  OMPFLAGS := -fopenmp
endif
CFLAGS_ALL := $(CFLAGS_BASE) $(OMPFLAGS) $(CFLAGS)
LDLIBS_ALL := $(OMPFLAGS) -lm

B := build

all: $(B)/$(SHLIB) $(B)/libgpimg.a

$(B):
	mkdir -p $(B)

$(B)/gpimg.o: src/gpimg.c include/gpimg.h | $(B)
	$(CC) $(CFLAGS_ALL) -c $< -o $@

$(B)/$(SHLIB): $(B)/gpimg.o
	$(CC) $(SHFLAGS) $< -o $@ $(LDLIBS_ALL) $(LDFLAGS)

$(B)/libgpimg.a: $(B)/gpimg.o
	$(AR) rcs $@ $<

$(B)/example: examples/example.c $(B)/libgpimg.a
	$(CC) $(CFLAGS_ALL) $< $(B)/libgpimg.a -o $@ $(LDLIBS_ALL) $(LDFLAGS)

$(B)/test_gpimg: tests/test_gpimg.c $(B)/libgpimg.a
	$(CC) $(CFLAGS_ALL) $< $(B)/libgpimg.a -o $@ $(LDLIBS_ALL) $(LDFLAGS)

$(B)/bitlock: tests/bitlock.c $(B)/libgpimg.a
	$(CC) $(CFLAGS_ALL) $< $(B)/libgpimg.a -o $@ $(LDLIBS_ALL) $(LDFLAGS)

# Matern-3/2 must stay bit-identical: compare every public result with the committed fingerprint
bitlock: $(B)/bitlock
	./$(B)/bitlock | cmp - tests/bitlock_m32.txt && echo "bitlock: Matern-3/2 results bit-identical"

example: $(B)/example
	./$(B)/example

test: $(B)/test_gpimg
	./$(B)/test_gpimg

$(B)/gpimg.pc: | $(B)
	printf 'prefix=$(PREFIX)\nlibdir=$${prefix}/lib\nincludedir=$${prefix}/include\n\nName: gpimg\nDescription: Exact O(n) 2D Gaussian processes for multi-band light curves\nVersion: $(VERSION)\nLibs: -L$${libdir} -lgpimg\nLibs.private: $(OMPFLAGS) -lm\nCflags: -I$${includedir}\n' > $@

install: all $(B)/gpimg.pc
	install -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include $(DESTDIR)$(PREFIX)/lib/pkgconfig
	install -m 644 include/gpimg.h $(DESTDIR)$(PREFIX)/include/
	install -m 755 $(B)/$(SHLIB) $(DESTDIR)$(PREFIX)/lib/
	install -m 644 $(B)/libgpimg.a $(DESTDIR)$(PREFIX)/lib/
	install -m 644 $(B)/gpimg.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig/
ifneq ($(UNAME),Darwin)
	ln -sf $(SHLIB) $(DESTDIR)$(PREFIX)/lib/libgpimg.so.0
endif

uninstall:
	rm -f $(DESTDIR)$(PREFIX)/include/gpimg.h $(DESTDIR)$(PREFIX)/lib/$(SHLIB) $(DESTDIR)$(PREFIX)/lib/libgpimg.so.0 \
	      $(DESTDIR)$(PREFIX)/lib/libgpimg.a $(DESTDIR)$(PREFIX)/lib/pkgconfig/gpimg.pc

clean:
	rm -rf $(B)

.PHONY: all example test install uninstall clean
