#!/usr/bin/make -f

export DH_VERBOSE = 1

# Upstream's makefile wants a sonLib checkout next to the source tree (and
# builds a CuTest test binary), 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
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 consumer
# supplies them from the static archive, exactly as it already has to for
# libpinchesandcacti.a.
LIBS = -l:libsonlib.a -lm -lpthread
LIB = libpinchesandcacti.so
SONAME = $(LIB).0
REAL = $(LIB).0.0.0

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

%:
	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)))
	$(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDES) debian/tests/consumer.c \
		libpinchesandcacti.a $(LIBS) -o obj/consumer
	obj/consumer
endif
