CC ?= cc
MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGS ?= -O2 -g -Wall
CPPFLAGS ?=

# Libraries sonLib itself depends on.  libquicktree is not listed here: the
# shared libsonlib.so already links it, and for the static archive it is
# added separately below.
SONLIB_LIBS = -ltokyocabinet -lz -lbz2 -lpthread -lm -lstdc++

# libsonlib.a leaves the quicktree symbols (neighbour_joining_buildtree and
# friends) unresolved, so static consumers have to link it explicitly.
STATIC_SONLIB_LIBS = -lquicktree

.PHONY: all check clean

all: sonlib-example sonlib-example-static sonlib-cutest-example

sonlib-example: sonlib-example.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< -lsonlib $(SONLIB_LIBS)

sonlib-example-static: sonlib-example.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< \
		/usr/lib/$(MULTIARCH)/libsonlib.a $(STATIC_SONLIB_LIBS) $(SONLIB_LIBS)

sonlib-cutest-example: sonlib-cutest-example.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< -lsonlib -lcutest $(SONLIB_LIBS)

check: all
	./sonlib-example
	./sonlib-example-static
	./sonlib-cutest-example

clean:
	rm -f sonlib-example sonlib-example-static sonlib-cutest-example
