#!/usr/bin/make -f
# -*- makefile -*-
# Package building rules for the SimGrid Debian Package.
# Strongly inspirated from the sample debhelper file, originally written by Joey Hess and Craig Small.
# Adapted to SimGrid by Lucas Nussbaum and Martin Quinson.

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

# See 'man dpkg-architecture'
include /usr/share/dpkg/architecture.mk

CFLAGS += $(shell dpkg-buildflags --get CFLAGS)
# build with -O3 everywhere since it is upstream policy to do so.
CFLAGS += -O3

export DEB_BUILD_MAINT_OPTIONS += hardening=+all

ifeq ($(DEB_HOST_GNU_CPU),arm)
  CFLAGS += -funwind-tables
endif

LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

%:
	dh $@ --with python3 --parallel

# CMAKE_BUILD_TYPE= (unset CMAKE_BUILD_TYPE) required, because:
# - dh magic runs cmake with -DCMAKE_BUILD_TYPE=RelWithDebInfo
# - this adds -DNDEBUG to CFLAGS (maybe a bug, see #711515)
# - this breaks simgrid (or at least some tests).
override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	  dh_auto_configure -Scmake -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE= \
		-Denable_smpi_MPICH3_testsuite=ON \
		-Denable_java=ON -Denable_lib_in_jar=OFF

override_dh_auto_build:
	dh_auto_build -- all documentation

# FIXME renable tests at some point
override_dh_auto_test:
	dh_auto_test || true

# Make install and prepare package building
override_dh_auto_install:
	dh_auto_install
	chrpath -d debian/tmp/usr/bin/graphicator
	mv debian/tmp/usr/bin/graphicator debian/tmp/usr/bin/simgrid-graphicator
	# move doc to correct place
	mkdir -p debian/tmp/usr/share/doc/simgrid
	mv debian/tmp/usr/doc/simgrid/* debian/tmp/usr/share/doc/simgrid/
	mv debian/tmp/usr/share/doc/simgrid/html/javadoc debian/tmp/usr/share/doc/simgrid/
	rmdir debian/tmp/usr/doc/simgrid
	# move jarfile to the right location
	mkdir -p debian/tmp/usr/share/java
	mv debian/tmp/usr/java/simgrid.jar debian/tmp/usr/share/java
	rm -rf debian/tmp/usr/java
	# Don't embeed jquery
	rm debian/tmp/usr/share/doc/simgrid/html/jquery.js
	ln -s /usr/share/javascript/jquery/jquery.js debian/tmp/usr/share/doc/simgrid/html/jquery.js

# Ensure that no file where left over when building the package
override_dh_install:
	dh_install --fail-missing
