# Template file for 'fftw'
pkgname=fftw
version=3.3.11
revision=1
hostmakedepends="cmake-bootstrap perl"
makedepends="libgomp-devel"
short_desc="Library for computing the discrete Fourier transform (DFT)"
maintainer="Gonzalo Tornaría <tornaria@cmat.edu.uy>"
license="GPL-2.0-or-later"
homepage="http://www.fftw.org/"
changelog="https://raw.githubusercontent.com/FFTW/fftw3/master/NEWS"
distfiles="${homepage}/fftw-${version}.tar.gz"
checksum=5630c24cdeb33b131612f7eb4b1a9934234754f9f388ff8617458d0be6f239a1

do_configure() {
	PATH=$PATH:/usr/libexec/xbps-src/bin
	local CARGS="--enable-shared --enable-threads --enable-openmp"

	case "$XBPS_TARGET_MACHINE" in
		i686*|x86_64*) _sse="--enable-sse"; _sse2="--enable-sse2";;
	esac

	# double precision
	mkdir build-double
	cd ${wrksrc}/build-double
	../configure ${configure_args} ${CARGS} ${_sse2}

	# long double precision
	mkdir ${wrksrc}/build-long-double
	cd ${wrksrc}/build-long-double
	../configure ${configure_args} ${CARGS} --enable-long-double

	# single precision
	mkdir ${wrksrc}/build-single
	cd ${wrksrc}/build-single
	../configure ${configure_args} ${CARGS} --enable-float ${_sse}

	cd ${wrksrc}
	# fix wrong soname in FFTW3LibraryDepends.cmake
	vsed -e 's/3.6.9/3.7.11/' -i CMakeLists.txt

	# create missing FFTW3LibraryDepends.cmake
	cmake -B build -D CMAKE_INSTALL_PREFIX=/usr -D CMAKE_BUILD_TYPE=None \
			-D ENABLE_OPENMP=ON -D ENABLE_THREADS=ON -D ENABLE_FLOAT=ON \
			-D ENABLE_LONG_DOUBLE=ON -D ENABLE_QUAD_PRECISION=ON -D ENABLE_SSE=ON \
			-D ENABLE_SSE2=ON -D ENABLE_AVX=ON -D ENABLE_AVX2=ON \
			-D CMAKE_POLICY_VERSION_MINIMUM=3.5
	# fix broken IMPORTED_LOCATION: https://github.com/FFTW/fftw3/issues/130#issuecomment-1030280157
	sed -e 's|\(IMPORTED_LOCATION_NONE\).*|\1 "/usr/lib/libfftw3.so.3"|' -i build/FFTW3LibraryDepends.cmake
}

do_build() {
	for f in double long-double single; do
		cd ${wrksrc}/build-${f}
		make ${makejobs}
	done
}

do_check() {
	for f in double long-double single; do
		cd ${wrksrc}/build-${f}
		make DESTDIR=${DESTDIR} check
	done
}

do_install() {
	for f in double long-double single; do
		cd ${wrksrc}/build-${f}
		make DESTDIR=${DESTDIR} install
	done
	vinstall ${wrksrc}/build/FFTW3LibraryDepends.cmake 644 usr/lib/cmake/fftw3/
}

libfftw_package() {
	short_desc+=" - runtime libraries"
	pkg_install() {
		vmove "usr/lib/*.so.*"
	}
}
fftw-devel_package() {
	depends="libfftw>=${version}_${revision}"
	short_desc+=" - Development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/pkgconfig
		vmove usr/lib/cmake
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
		vmove usr/share/info
	}
}
