#!/usr/bin/make -f

# Build with flang first this release
export FC_DEFAULT=flang-21
# export FC_OPTIONAL=gfortran-15


export DH_VERBOSE=1

# Hack until dh-dfortran fix in 0.70 
FPM_FFLAGS=$(shell dh_fortran get_env --fc flang-21 | grep FPM_FFLAGS | sed -e s/FPM_FFLAGS=// ) -I/usr/include
FPM_LDFLAGS=$(shell dh_fortran get_env --fc flang-21 | grep FPM_LDFLAGS | sed -e s/FPM_LDFLAGS=// )
FPM_FC=flang-21
export FPM_FFLAGS FPM_FC FPM_LDFLAGS

# Necessary to build shared libraries
export DEB_LDFLAGS_MAINT_APPEND=-fPIC

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

execute_after_dh_auto_clean:
	rm -fr doc/html

override_dh_auto_build:
	fpm build --compiler flang-21 --profile release	--verbose
	ford ford.md

# FTB on error: loc("/home/alastair/git/deb-packages/fortran-julienne/fortran-julienne-3.6.2/test/idiomatic_assertion_failure_test.F90":13:3): flang/lib/Optimizer/Builder/IntrinsicCall.cpp:2033: not yet implemented: coarray: intrinsic this_image
override_dh_auto_test:
	# fpm test --compiler flang-21 --profile release	
	:

override_dh_fortran_lib:
	:

override_dh_auto_install:
	# fpm install --compiler gfortran --profile release  --prefix debian/tmp/usr
	fpm install --compiler flang-21 --profile release  --prefix debian/tmp/usr
	patchelf --set-soname libfortran-julienne.so.0 debian/tmp/usr/lib/libjulienne.so
	mv debian/tmp/usr/lib/libjulienne.so debian/tmp/usr/lib/libjulienne.so.0
	# Need to rename to avoid conflict
	mv debian/tmp/usr/bin/scaffold debian/tmp/usr/bin/fpm-scaffold
