#!/usr/bin/make -f
# -*- makefile -*-
#
# Makefile to build the mCRL2 package using debhelper.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS_RELEASE = -O0 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O0 -g $(CFLAGS)
else 
	CFLAGS_RELEASE = -O2 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O2 -g $(CFLAGS)
endif

BUILD_TYPE = Release
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	BUILD_TYPE = Debug
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))  
    MAKEFLAGS += -j$(NUMJOBS)
endif

BUILD_FLAGS = -DCMAKE_INSTALL_PREFIX="/usr" \
              -DCMAKE_INSTALL_RPATH="/usr/lib/mcrl2" \
	      -DCMAKE_BUILD_TYPE="$(BUILD_TYPE)" \
	      -DCMAKE_C_FLAGS_RELEASE="$(CFLAGS_RELEASE)" \
	      -DCMAKE_VERBOSE_MAKEFILE="ON" \
	      -DMCRL2_ENABLE_EXPERIMENTAL="OFF" \
	      -DMCRL2_ENABLE_DEPRECATED="OFF"	 \
	      -DMCRL2_ENABLE_TEST_TARGETS="OFF" \
	      -DMCRL2_MAN_PAGES="ON" \
	      -DMCRL2_PACKAGE_RELEASE="ON"
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	BUILD_FLAGS += -DCMAKE_SYSTEM_NAME=$(shell dpkg-architecture -qDEB_BUILD_ARCH_OS) \
		       -DCMAKE_SYSTEM_PROCESSOR=$(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU) \
		       -DCMAKE_C_COMPILER=$(DEB_BUILD_GNU_TYPE)-gcc
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), armel armhf))
	BUILD_FLAGS += -DMCRL2_ENABLE_GUI_TOOLS="OFF"
endif

SRC	:= $(CURDIR)
BUILD	:= $(CURDIR)/debian/build
TARGET	:= $(CURDIR)/debian/mcrl2

configure: configure-stamp

configure-stamp:
	dh_testdir
	[ -d $(BUILD) ] || mkdir $(BUILD)
	cd $(BUILD) && \
	  cmake $(BUILD_FLAGS) $(CURDIR) 
	touch $@

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: configure
	dh_testdir
	$(MAKE) -C $(BUILD) $(MAKEFLAGS)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf $(BUILD)
	rm -f configure-stamp build-stamp 
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) -C $(BUILD) install DESTDIR=$(TARGET)
	# Generate and install the man pages.
	export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(BUILD)/usr/lib/mcrl2
	$(MAKE) -C $(BUILD) $(MAKEFLAGS)
	$(MAKE) -C $(BUILD) install DESTDIR=$(TARGET)

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V --exclude=/usr/lib/mcrl2
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build build-arch build-indexp clean binary-indep binary-arch binary install 
