#!/usr/bin/make -f
# -*- makefile -*-

# Fails in cowbuilder too because this variable
# is not removed inside the chroot
unexport JAVA_HOME

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)

# error: this statement may fall through [-Werror=implicit-fallthrough=]
# with GCC 7
GCC_VERSION := $(shell gcc -dumpversion)
ifeq ($(shell dpkg --compare-versions $(GCC_VERSION) ge 7 && echo yes),yes)
  export DEB_CXXFLAGS_MAINT_APPEND := -Wimplicit-fallthrough=0
endif

%:
	dh $@ --with autoreconf,python2,python3 --parallel

override_dh_auto_build-arch:
## Chicken<->Egg problem: protobuf requires self-generated .pb.go files to
## be built. First we build it normally; then "generate_descriptor_proto.sh"
## generates .pb.go files and replaces 'em if they are different as well as
## invokes "make protoc" which re-builds "src/protoc" if required.
	dh_auto_build --arch
	bash -x ./generate_descriptor_proto.sh

	# Generate the manpage.
	xmlto man debian/protoc.xml

	# Python and Python3 build.
	cp -rv python python3
	cd python && python setup.py build --cpp_implementation
	cd python3 && python3 setup.py build --cpp_implementation

override_dh_auto_build-indep:
	dh_auto_build --indep

	# Java build.
	ant -f debian/java-build.xml jar

override_dh_clean:
	$(RM) -rv gmock
	$(RM) -rvf php/src/Google/Protobuf/Internal/ExtensionRangeOptions.php
	dh_clean

override_dh_autoreconf: gmock
	dh_autoreconf

gmock:
	cp -rv debian/gmock ./
	cp -rv /usr/src/gmock/src gmock/
	cp -rv /usr/src/gtest/src gmock/gtest/

override_dh_auto_test-arch: gmock
	dh_auto_test --arch

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Python test.
	set -e; \
	export LD_LIBRARY_PATH=$(CURDIR)/src/.libs; \
	cd python && for PYTHON in $(shell pyversions -r); do \
		$$PYTHON setup.py test --cpp_implementation; \
	done
    # Python3 test.
	set -e; \
	export LD_LIBRARY_PATH=$(CURDIR)/src/.libs; \
	cd python3 && for PYTHON in $(shell py3versions -r); do \
		$$PYTHON setup.py test --cpp_implementation; \
	done
endif

override_dh_auto_test-indep:
	:
override_dh_auto_clean-arch:
	dh_auto_clean --arch

	# Python clean.
	set -e; \
	cd python && for python in $(shell pyversions -r); do \
		$$python setup.py clean --all; \
	done
	rm -rf python/protobuf.egg-info
	# Python3 clean.
	rm -rf python3

override_dh_auto_clean-indep:
	dh_auto_clean --indep

	# Java clean.
	ant -f debian/java-build.xml clean || true
	mh_clean || true

override_dh_auto_install-arch:
	dh_auto_install --arch

	# Python install.
	set -e; \
	cd python && for python in $(shell pyversions -r); do \
		$$python setup.py install --cpp_implementation \
			--install-layout=deb --no-compile \
			--root=$(CURDIR)/debian/python-protobuf; \
	done
	find $(CURDIR)/debian/python-protobuf -name 'protobuf-*-nspkg.pth' -delete

	# Python3 install.
	cd python3 && for python in $(shell py3versions -r); do \
		$$python setup.py install --cpp_implementation \
			--install-layout=deb --no-compile \
			--root=$(CURDIR)/debian/python3-protobuf; \
	done
	find $(CURDIR)/debian/python3-protobuf -name 'protobuf-*-nspkg.pth' -delete

override_dh_auto_install-indep:
	dh_auto_install --indep

	# Java install.
	mh_install

override_dh_install-arch:
	dh_install --arch

	# Convert doc dir to symlink for the -dev packages.
	rm -rf $(CURDIR)/debian/libprotobuf-dev/usr/share/doc/libprotobuf-dev
	ln -s libprotobuf14 $(CURDIR)/debian/libprotobuf-dev/usr/share/doc/libprotobuf-dev
	rm -rf $(CURDIR)/debian/libprotoc-dev/usr/share/doc/libprotoc-dev
	ln -s libprotoc14 $(CURDIR)/debian/libprotoc-dev/usr/share/doc/libprotoc-dev

	# Remove compiler headers from libprotobuf-dev.
	rm -rf $(CURDIR)/debian/libprotobuf-dev/usr/include/google/protobuf/compiler

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION)+$(DIST_NAME)-$(DIST_RELEASE)-$(DIST_VERSION)"
