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

CC 	= gcc-9
#ALT_CC 	= gcc-9
PYTHON	= python
p3	= gcc-python3-plugin
d3	= debian/$(p3)
p3dbg	= gcc-python3-dbg-plugin
d3dbg	= debian/$(p3dbg)
pdoc	= gcc-python-plugin-doc
ddoc	= debian/$(pdoc)

INTERP	= python3 python3-dbg # python python-dbg

export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

build-arch: $(INTERP:%=build-stamp-%)
build-indep: doc-stamp
build: $(INTERP:%=build-stamp-%) $(INTERP:%=check-stamp-%)
build-stamp-%:
	dh_testdir
	rm -rf build-$*
	mkdir -p build-$*
	tar -c -f - --exclude=debian --exclude=build-* . | tar -C build-$* -x -f -
	$(MAKE) -C build-$* \
	  CC=$(CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  plugin print-gcc-version
ifneq (,$(ALT_CC))
	rm -rf build-alt-$*
	mkdir -p build-alt-$*
	tar -c -f - --exclude=debian --exclude=build-* . | tar -C build-alt-$* -x -f -
	$(MAKE) -C build-alt-$* \
	  CC=$(ALT_CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  plugin print-gcc-version
endif
	touch $@

check-stamp-%: build-stamp-%
	dh_testdir
	: # TODO: see the spec file for XFAIL tests
	-$(MAKE) -C build-$* \
	  CC=$(CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  all
ifneq (,$(ALT_CC))
	-$(MAKE) -C build-alt-$* \
	  CC=$(ALT_CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  all
endif
	touch $@

doc-stamp:
	dh_testdir
	$(MAKE) -C docs html
	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build*-stamp* check*-stamp* doc-stamp
	rm -rf build-*
	$(MAKE) -C docs clean
	find -type d -name __pycache__ | xargs -r rm -rf
	find -type f -name '*.py[co]'  | xargs -r rm -f
	dh_clean 

pre-install:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

install-%: build-stamp-%
	dh_testdir
	dh_testroot
ifneq (,$(ALT_CC))
	$(MAKE) -C build-alt-$* \
	  CC=$(ALT_CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  DESTDIR=$(CURDIR)/debian/gcc-$*-plugin \
	  install
	set -x; \
	p="gcc-$*-plugin"; \
	src="$(shell $(ALT_CC) --print-file-name=plugin)/$(subst -,_,$*)"; \
	dst="usr/lib/$(if $(findstring python3, $*),python3,python2.7)/dist-packages"; \
	mkdir -p debian/$$p/$$dst; \
	for d in gccutils libcpychecker; do \
	  tar -C debian/$$p/$$src -c -f - $$d \
	    | tar -C debian/$$p/$$dst -x -f -; \
	  rm -rf debian/$$p/$$src/$$d; \
	  dh_link -p$$p /$$dst/$$d /$$src/$$d; \
	done; \
	$(if $(findstring dbg,$*), \
	  rm -f debian/$$p/$$src/../libgcc-c-api.so; \
	  rm -rf debian/$$p/$$dst; \
	)
endif
	$(MAKE) -C build-$* \
	  CC=$(CC) \
	  PYTHON=$* PYTHON_CONFIG=$*-config \
	  PLUGIN_NAME=$(subst -,_,$*) \
	  PLUGIN_DSO=$(subst -,_,$*.so) \
	  PLUGIN_PYTHONPATH=$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*) \
	  DESTDIR=$(CURDIR)/debian/gcc-$*-plugin \
	  install
	set -x; \
	p="gcc-$*-plugin"; \
	src="$(shell $(CC) --print-file-name=plugin)/$(subst -,_,$*)"; \
	dst="usr/lib/$(if $(findstring python3, $*),python3,python2.7)/dist-packages"; \
	mkdir -p debian/$$p/$$dst; \
	for d in gccutils libcpychecker; do \
	  tar -C debian/$$p/$$src -c -f - $$d \
	    | tar -C debian/$$p/$$dst -x -f -; \
	  rm -rf debian/$$p/$$src/$$d; \
	  dh_link -p$$p /$$dst/$$d /$$src/$$d; \
	done; \
	$(if $(findstring dbg,$*), \
	  rm -f debian/$$p/$$src/../libgcc-c-api.so; \
	  rm -rf debian/$$p/$$dst; \
	)

install: pre-install $(INTERP:%=install-%)
	dh_testdir
	dh_testroot
	find $(d) -type d -name __pycache__ | xargs -r rm -rf
	find $(d) -type f -name '*.py[co]'  | xargs -r rm -f

# Build architecture-independent files here.
binary-indep: install doc-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -p$(pdoc) docs/_build/html
	cp -a examples $(ddoc)/usr/share/doc/$(pdoc)/.
	dh_sphinxdoc -i
	dh_lintian -i
	dh_compress -i -X.py
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a
#	dh_python2 -a
	dh_python3 -a --no-dbg-cleaning
	dh_installman -a
	dh_link -a
#	dh_strip -p$(p2) --dbg-package=$(p2dbg)
	dh_strip -p$(p3) --dbg-package=$(p3dbg)
	rm -rf debian/$(p3dbg)/usr/share/doc/$(p3dbg)
	ln -s $(p3) debian/$(p3dbg)/usr/share/doc/$(p3dbg)
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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