#!/usr/bin/make -f
# -*- makefile -*-
#export DH_VERBOSE=1

# Reduce size of debug symbols to fix FTBFS due to the
# 2GB/3GB address space limits on 32bit
DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)
ifeq (32,$(DEB_HOST_ARCH_BITS))
       export DEB_CFLAGS_MAINT_APPEND = -g1
       export DEB_CXXFLAGS_MAINT_APPEND = -g1
endif

# minimise needless linking and link to libatomic
# The last is needed because long long atomic operations are not directly
# supported by all processor architectures
export DEB_LDFLAGS_MAINT_APPEND= -Wl,--as-needed -latomic

# Enable hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

export JAVA_HOME=/usr/lib/jvm/default-java
## Set JAVAC to prevent FTBFS due to incorrect use of 'gcj' if found (see "m4/ac_prog_javac.m4").
export JAVAC=javac

extraopts += -DWITH_OCF=ON -DWITH_NSS=ON -DWITH_PYTHON3=ON -DWITH_DEBUG=ON
extraopts += -DWITH_CEPHFS_JAVA=ON
extraopts += -DWITH_LTTNG=OFF -DWITH_EMBEDDED=OFF
extraopts += -DCMAKE_INSTALL_LIBEXECDIR=/usr/lib
ifneq ($(DEB_HOST_ARCH), armhf)
extraopts += -DCMAKE_BUILD_TYPE=RelWithDebInfo
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  extraopts += -DBOOST_J=$(NUMJOBS)
endif

ifeq ($(DEB_HOST_ARCH), armel)
  # armel supports ARMv4t or above instructions sets.
  # libatomic-ops is only usable with Ceph for ARMv6 or above.
  extraopts += -DWITH_ATOMIC_OPS=OFF
endif

ifeq ($(DEB_HOST_ARCH), s390x)
  # beast depends on libboost_{context,coroutine} which is not supported on s390x
  extraopts += -DWITH_BOOST_CONTEXT=OFF
else
  extraopts += -DWITH_BOOST_CONTEXT=ON
endif

ifeq ($(DEB_HOST_ARCH), arm64)
  # Use gcc-7 toolchain for arm64 until
  # https://bugs.launchpad.net/ubuntu/+source/gcc-6/+bug/1702044 is resolved
  extraopts += -DCMAKE_C_COMPILER=gcc-7 -DCMAKE_CXX_COMPILER=g++-7
endif

%:
	dh $@ --buildsystem=cmake --with javahelper,python2,python3,systemd --max-parallel=2

override_dh_auto_configure:
	dh_auto_configure --buildsystem=cmake -- $(extraopts)

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp --buildsystem=cmake
	# NOTE: manually install python modules to avoid virtualenvs
	#       during the package build process.
	set -e; for comp in ceph-detect-init ceph-disk; do \
		(set -e; cd src/$$comp; python setup.py install --root=$(CURDIR)/debian/tmp \
			--install-script=/usr/sbin --install-layout=deb); \
	done
	# NOTE: ensure that any versioned erasure coding test code is dropped
	#       from the package install - package ships unversioned modules.
	rm -f $(CURDIR)/debian/tmp/usr/lib/*/ceph/erasure-code/libec_*.so.*
	find $(CURDIR)/debian/tmp/usr/lib/*/ceph/erasure-code -type l -delete || :
	install -D -m 644 src/etc-rbdmap $(CURDIR)/debian/tmp/etc/ceph/rbdmap
	install -D -m 644 etc/sysctl/90-ceph-osd.conf $(CURDIR)/debian/tmp/etc/sysctl.d/30-ceph-osd.conf

override_dh_installinit:
	dh_installinit --no-start
	dh_installinit -pceph-common --name=rbdmap --no-start
	dh_installinit -pceph-base --name ceph --no-start
	# install the systemd stuff manually since we have funny service names
	# and need to update the paths in all of the files post install
	# systemd:ceph-common
	install -d -m0755 debian/ceph-common/lib/systemd/system
	install -m0644 systemd/ceph.target debian/ceph-common/lib/systemd/system
	install -d -m0755 debian/ceph-common/usr/lib/tmpfiles.d
	install -m 0644 -D systemd/ceph.tmpfiles.d debian/ceph-common/usr/lib/tmpfiles.d/ceph.conf
	# systemd:ceph
	install -d -m0755 debian/ceph-base/lib/systemd/system
	install -d -m0755 debian/ceph-mon/lib/systemd/system
	install -d -m0755 debian/ceph-osd/lib/systemd/system
	install -m0644 systemd/ceph-mon@.service debian/ceph-mon/lib/systemd/system
	install -m0644 systemd/ceph-osd@.service debian/ceph-osd/lib/systemd/system
	install -m0644 systemd/ceph-disk@.service debian/ceph-osd/lib/systemd/system
	install -m0644 systemd/ceph-volume@.service debian/ceph-osd/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-mon/lib/systemd/system/ceph-mon@.service
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-osd/lib/systemd/system/ceph-osd@.service
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-osd/lib/systemd/system/ceph-disk@.service
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-osd/lib/systemd/system/ceph-volume@.service
	install -m0644 systemd/ceph-mon.target debian/ceph-mon/lib/systemd/system
	install -m0644 systemd/ceph-osd.target debian/ceph-osd/lib/systemd/system
	# NOTE(jamespage): Install previous ceph-mon service from packaging for upgrades
	install -m0644 debian/lib-systemd/system/ceph-mon.service debian/ceph-mon/lib/systemd/system
	# systemd:ceph-mds
	install -d -m0755 debian/ceph-mds/lib/systemd/system
	install -m0644 systemd/ceph-mds@.service debian/ceph-mds/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-mds/lib/systemd/system/ceph-mds@.service
	install -m0644 systemd/ceph-mds.target debian/ceph-mds/lib/systemd/system
	# NOTE(jamespage): Install previous ceph-mds service from packaging for upgrades
	install -m0644 debian/lib-systemd/system/ceph-mds.service debian/ceph-mds/lib/systemd/system
	install -d -m0755 debian/ceph-fuse/lib/systemd/system
	# systemd:ceph-fuse
	install -m0644 systemd/ceph-fuse@.service debian/ceph-fuse/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-fuse/lib/systemd/system/ceph-fuse@.service
	install -m0644 systemd/ceph-fuse.target debian/ceph-fuse/lib/systemd/system
	# systemd:ceph-mgr
	install -d -m0755 debian/ceph-mgr/lib/systemd/system
	install -m0644 systemd/ceph-mgr@.service debian/ceph-mgr/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/ceph-mgr/lib/systemd/system/ceph-mgr@.service
	install -m0644 systemd/ceph-mgr.target debian/ceph-mgr/lib/systemd/system
	# systemd:radosgw
	install -d -m0755 debian/radosgw/lib/systemd/system
	install -m0644 systemd/ceph-radosgw@.service debian/radosgw/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/radosgw/lib/systemd/system/ceph-radosgw@.service
	install -m0644 systemd/ceph-radosgw.target debian/radosgw/lib/systemd/system
	# systemd:rbd-mirror
	install -d -m0755 debian/rbd-mirror/lib/systemd/system
	install -m0644 systemd/ceph-rbd-mirror@.service debian/rbd-mirror/lib/systemd/system
	sed -i s./etc/sysconfig/./etc/default/.g debian/rbd-mirror/lib/systemd/system/ceph-rbd-mirror@.service
	install -m0644 systemd/ceph-rbd-mirror.target debian/rbd-mirror/lib/systemd/system
	# Ensure Debian/Ubuntu specific systemd units are NOT automatically enabled and started
	# Enable systemd targets only
	dh_systemd_enable -Xceph-mon.service -Xceph-osd.service -X ceph-mds.service
	# Start systemd targets only
	dh_systemd_start --no-stop-on-upgrade --no-restart-after-upgrade

override_dh_systemd_enable:
	# systemd enable done as part of dh_installinit

override_dh_systemd_start:
	# systemd start done as part of dh_installinit

override_dh_makeshlibs:
	# exclude jni libraries in libcephfs-jni to avoid pointless ldconfig
	# calls in maintainer scripts; exclude private erasure-code plugins.
	dh_makeshlibs -V -X/usr/lib/jni -X/usr/lib/$(DEB_HOST_MULTIARCH)/ceph/erasure-code

override_dh_auto_test:
	# do not run tests

override_dh_python2:
	for binding in rados cephfs rbd rgw; do \
		dh_python2 -p python-$$binding;       \
	done
	dh_python2 -p ceph-common
	dh_python2 -p ceph-base
	dh_python2 -p ceph-osd
	dh_python2 -p ceph-mgr

override_dh_python3:
	for binding in rados cephfs rbd rgw; do \
		dh_python3 -p python3-$$binding;      \
        done
	dh_python3 -p python3-ceph-argparse

.PHONY: override_dh_auto_configure override_dh_installinit override_dh_makeshlibs override_dh_auto_test
