#!/usr/bin/make -f


export DH_VERBOSE=1
export FC_DEFAULT=gfortran
export FC_OPTIONAL=flang


# The magic debhelper  rule
%:
	dh $@ --buildsystem=cmake  --with fortran

# KNOWN LIMITATION (as of dh-fortran 0.85ubuntu1): dh_fortran_cmake's
# CMakeFileHelper.process_file() in dhfortran/cmake.py is an unimplemented
# stub, so it never actually installs or rewrites the CMake target files it
# claims to handle -- and even if it did, the hipfort-config-shim.cmake it
# would relocate looks up the real per-toolchain config by the literal
# Fortran compiler binary name (e.g. "f95"), which has no relation to
# dh-fortran's own version-independent flavour tags (e.g. "gnu"). Both
# issues make the upstream multitoolchain/shim mechanism non-functional
# under dh-fortran.
#
# Workaround: debian/patches/0001-disable-multitoolchain-layout-by-default.patch
# defaults HIPFORT_MULTITOOLCHAIN_LAYOUT to OFF (flat, upstream default
# install layout, no shim), and the CMake config/target files and static
# libraries are installed verbatim via debian/libhipfort-dev.install
# instead of being routed through dh_fortran_cmake/dh_fortran_lib. This
# keeps libhipfort-dev's *-targets.cmake internally consistent, but means
# only ONE Fortran compiler's archive can occupy the plain
# usr/lib/<triplet>/libhipfort-*.a path -- FC_OPTIONAL=flang below is
# effectively unsupported for CMake consumers until dh-fortran's
# process_file() is implemented upstream (and the shim is made to use
# dh-fortran's flavour tags).
#
# Deliberately NOT done via override_dh_auto_configure: dh_auto_configure
# and dh_auto_build are intercepted by dh-fortran via a PATH-shadowed
# wrapper (dhfortran/intercept.py) that, for a multi-compiler build
# (FC_OPTIONAL set), loops dh_auto_configure once per Fortran compiler into
# separate obj-*-<compiler> directories. That PATH-shadowing did not
# reliably survive being invoked from inside a debian/rules override target
# on Launchpad's buildd (it worked in one local test, but the buildd fell
# through to a single unlooped configure into a plain, unsuffixed obj dir,
# which then failed outright) -- so the CMake flag is set via the source
# patch instead, leaving dh_auto_configure/dh_auto_build completely
# unmodified.
#
# The exported *-targets.cmake files also hardcode their
# INTERFACE_INCLUDE_DIRECTORIES to this same flat usr/include/hipfort/*
# layout (again unrewritten by dh_fortran_cmake), so
# debian/libhipfort-dev.fortran-mod pins its .mod destinations to that exact
# absolute path too -- dh_fortran_mod still inspects each .mod file and
# tracks its compiler/version for ${fortran:Depends}, it just no longer
# relocates the files into its own per-compiler directory.
#
# Filed upstream as https://bugs.debian.org/1147390.
#
# TODO: once dh-fortran #1147390 is fixed, drop
# 0001-disable-multitoolchain-layout-by-default.patch, restore
# debian/libhipfort-dev.fortran-cmake and .fortran-lib, revert
# .fortran-mod's destinations to relative (amdgcn / nvptx), and remove the
# cmake config/target and .a entries from debian/libhipfort-dev.install so
# dh_fortran_cmake/dh_fortran_lib/dh_fortran_mod handle them again with
# proper per-compiler isolation.
override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp

