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

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

FC:=$(shell basename $(shell readlink /usr/bin/f95))
F77:=$(shell basename $(shell readlink /usr/bin/f77))
export F77 FC

# see #957440
GF10_FLAGS:=$(if $(filter $(shell readlink /usr/bin/gfortran), gfortran-10), \
                 -fallow-invalid-boz -fallow-argument-mismatch, )
FFLAGS=$(shell dpkg-buildflags --get FFLAGS) $(GF10_FLAGS)
FCFLAGS=$(shell dpkg-buildflags --get FCFLAGS) $(GF10_FLAGS)
export FFLAGS FCFLAGS

DO_FLANG:=$(if $(wildcard /usr/bin/flang),true, false)

# Use --as-needed with gfortran, not flang
ifneq ($(filter gfortran,$(FC)),)
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
endif

PY3VERS=$(shell py3versions -vr)

%:
	dh $@ 

override_dh_auto_configure:
	dh_auto_configure -Bbuild-main -- --disable-python --with-perl-dir=vendor
	cp src/*.h build-main/src/
	for v in $(PY3VERS); do \
	  dh_auto_configure -Bbuild-py$$v -- --disable-bindings --enable-python --with-python=/usr/bin/python$$v; \
	  cp src/*.h build-py$$v/src/; \
	done

#trick from python-ldns
# magic is: PYTHON_VERSION=$$v dh_auto_configure -Bbuild-py$$v 
# and later dh_auto_build -Bbuild-py$$v

override_dh_auto_build:
	dh_auto_build -Bbuild-main
	for v in $(PY3VERS); do \
	  dh_auto_build -Bbuild-py$$v; \
	done

override_dh_auto_install:
	dh_auto_install -Bbuild-main
	for v in $(PY3VERS); do \
	  dh_auto_install -Bbuild-py$$v; \
	done
	dh_numpy3

override_dh_auto_clean:
	dh_auto_clean -Bbuild-main
	for v in $(PY3VERS); do \
	  dh_auto_clean -Bbuild-py$$v; \
	done
