#!/usr/bin/env bash
name='fftw'
release='1'
version='3.3.10'
url='https://www.fftw.org/'
description='Fast C library for the Discrete Fourier Transform '
email='bayramk@gmail.com'
maintainer='bk'
license=('GPLv3')
source=("https://github.com/FFTW/fftw3/archive/refs/tags/fftw-$version.tar.gz")
depends=()
makedepends=()
md5sums=('20fc019b3dc717b29c234590cdf4cc47')
group=(media.libs)
uses=()
arch=('x86_64')

cd ${name}3-fftw-$version
_build_types=(single double long-double quad)

prepare() {
  local _i

  # fix wrong soname in FFTW3LibraryDepends.cmake
  sed -e 's/3.6.9/3.6.10/' -i ./CMakeLists.txt

}

build() {
  local _name
  local _configure=(
    ./configure
    --prefix=/usr
    --enable-shared
    --enable-threads
    --enable-mpi
    --enable-openmp
  )
  local _configure_single=(
    --enable-sse
    --enable-avx
    --enable-single
  )
  local _configure_double=(
    --enable-sse2
    --enable-avx
  )
  local _configure_long_double=(
    --enable-long-double
  )
  local _configure_quad=(
    --disable-mpi
    --enable-quad-precision
  )
  local _cmake_options=(
    -B build
    -S .
    -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
  )

  # create missing FFTW3LibraryDepends.cmake
  # https://bugs.archlinux.org/task/67604
  cmake "${_cmake_options[@]}"
  # 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

  export F77='gfortran'
  # use upstream default CFLAGS while keeping our -march/-mtune
  CFLAGS+=" -O3 -fomit-frame-pointer -malign-double -fstrict-aliasing -ffast-math"

  

  
}

check() {
  local _name

  for _name in "${_build_types[@]}"; do
    make check-local -C $name-$version-$_name/tests
  done
}

package() {
  local _name

  
  
  # install missing FFTW3LibraryDepends.cmake
  install -vDm 644 build/FFTW3LibraryDepends.cmake -t "$pkgdir/usr/lib/cmake/fftw3/"
}

