#!/usr/bin/make -f

export DH_VERBOSE = 1

# Upstream's makefile wants a sonLib checkout next to the source tree, so
# compile the library directly.  Its DPkg::Buildsystem is therefore disabled
# and the *.install files take the build products.
SOURCES = impl/stPinchGraphs.c impl/stCactusGraphs.c \
	  impl/stPinchPhylogeny.c impl/stThreeEdgeConnected.c
INCLUDES = -Iinc -I/usr/include/sonLib
# libsonlib.so.1 is linked with -z now and leaves the quicktree symbols it
# needs undefined, so it cannot be loaded at all.  Do not link the shared
# library against it: the sonLib symbols stay undefined there and the caller
# supplies them from the static archive, exactly as it already has to for
# libpinchesandcacti.a.
LIB = libpinchesandcacti.so
SONAME = $(LIB).0
REAL = $(LIB).0.0.0

override_dh_auto_clean:
	rm -rf obj libpinchesandcacti.a libpinchesandcacti.so*
	rm -f *.o

%:
	dh $@ --buildsystem=none

override_dh_auto_build:
	mkdir -p obj
	for s in $(SOURCES); do \
		$(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDES) -fPIC -c $$s \
			-o obj/`basename $$s .c`.o; \
	done
	$(AR) rcs libpinchesandcacti.a obj/*.o
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -Wl,-z,now \
		-Wl,-soname,$(SONAME) -o $(REAL) obj/*.o -lm -lpthread
	ln -sf $(REAL) $(SONAME)
	ln -sf $(SONAME) $(LIB)

execute_after_dh_install:
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-la \
		    --movedev "inc/*.h" usr/include/pinchesAndCacti \
		    *.so

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p obj/sonLib
	: > obj/sonLib/include.mk
	$(MAKE) test \
		sonLibRootDir=obj/sonLib \
		BINDIR=obj LIBDIR=obj \
		CC="$(CC)" AR="$(AR)" RANLIB=ranlib \
		CPPFLAGS="$(CPPFLAGS) -Iinc -I/usr/include/sonLib -I/usr/include/cutest" \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		LIBDEPENDS= \
		LDLIBS="-lcutest -l:libsonlib.a -lquicktree -lm -lpthread"
endif
