{% set version = "3.3.10" %}
{% set build = 8 %}


# ensure mpi is defined (needed for conda-smithy recipe-lint)
{% set mpi = mpi or 'nompi' %}

# prioritize nompi variant via build number
{% if mpi == 'nompi' %}
{% set build = build + 100 %}
{% endif %}

# add build string so packages can depend on
# mpi or nompi variants explicitly:
# `pkg * mpi_mpich_*` for mpich
# `pkg * mpi_*` for any mpi
# `pkg * nompi_*` for no mpi
{% if mpi != 'nompi' %}
{% set mpi_prefix = "mpi_" + mpi %}
{% else %}
{% set mpi_prefix = "nompi" %}
{% endif %}

package:
  name: fftw-split
  version: {{ version }}

source:
  url: http://www.fftw.org/fftw-{{ version }}.tar.gz
  sha256: 56c932549852cddcfafdab3820b0200c7742675be92179e59e6215b340e26467
  patches:
    - 0001-cmake-Set-SIMD-flags-on-appropriate-source-files-onl.patch

build:
  number: {{ build }}

outputs:
  - name: fftw
    script: build_fftw.sh   # [unix]
    script: bld_fftw.bat  # [win]
    build:
      string: {{ mpi_prefix }}_h{{ PKG_HASH }}_{{ PKG_BUILDNUM }}

      run_exports:
        - {{ pin_subpackage('fftw', max_pin='x') }}
      {% if mpi != 'nompi' %}
        - fftw * {{ mpi_prefix }}_*
      {% endif %}

    requirements:
      build:
        - perl 5.*  # [not win]
        - cmake
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - {{ compiler('fortran') }}  # [not win]
        - llvm-openmp  # [osx]
        - make      # [unix]
        - autoconf  # [unix]
        - automake  # [unix]
        - gettext   # [unix]
        - m4        # [unix]
        - libtool   # [unix]
        - {{ mpi }}  # [build_platform != target_platform and mpi == 'openmpi']
      host:
        - {{ mpi }}  # [mpi != 'nompi']
        - llvm-openmp  # [osx]
      run:
        - llvm-openmp  # [osx]

    test:
      files:                      # [not win]
        - test_cmake.sh           # [not win]
        - test_cmake/*            # [not win]
      requires:
        - python >=3
        - cmake                   # [not win]
        - make                    # [not win]
        - {{ compiler('c') }}     # [not win]
        - {{ compiler('cxx') }}   # [not win]

      commands:
        # Verify that static library files aren't present yet
        - test ! -f ${PREFIX}/lib/libfftw3f.a                     # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3.a                      # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3l.a                     # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3f_threads.a             # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3_threads.a              # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3l_threads.a             # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3f_omp.a                 # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3_omp.a                  # [not win]
        - test ! -f ${PREFIX}/lib/libfftw3l_omp.a                 # [not win]

        # Verify headers are installed
        - test -f ${PREFIX}/include/fftw3.h                     # [not win]
        - if not exist %LIBRARY_INC%\\fftw3.h exit 1            # [win]

        # Verify shared libraries are installed
        {% set fftw_libs = [
                "libfftw3",
                "libfftw3_threads",
                "libfftw3f",
                "libfftw3f_threads",
                "libfftw3l",
                "libfftw3l_threads",
        ] %}
        {% set fftw_omp_libs = [
                "libfftw3_omp",
                "libfftw3f_omp",
                "libfftw3l_omp",
        ] %}
        {% set fftw_mpi_libs = [
                "libfftw3_mpi",
                "libfftw3f_mpi",
                "libfftw3l_mpi",
        ] %}

        {% for lib in fftw_libs %}
        - python -c "import ctypes; ctypes.cdll[r'${PREFIX}/lib/{{ lib }}${SHLIB_EXT}']"  # [unix]
        {% endfor %}

        {% for lib in fftw_omp_libs %}
        - python -c "import ctypes; ctypes.cdll[r'${PREFIX}/lib/{{ lib }}${SHLIB_EXT}']"  # [unix]
        {% endfor %}

        {% if mpi != 'nompi' %}
        {% for lib in fftw_mpi_libs %}
        # you need to link to the mpi libs to load the dll, so we just test
        # if it exists
        - test -f ${PREFIX}/lib/{{ lib }}${SHLIB_EXT} || exit 1  # [unix]
        {% endfor %}
        {% endif %}

        {% set fftw_libs = ["fftw3f", "fftw3"] %}

        {% for base in fftw_libs %}
        - if not exist %LIBRARY_LIB%\\{{ base }}.lib exit 1  # [win]
        - if not exist %LIBRARY_BIN%\\{{ base }}.dll exit 1  # [win]
        {% endfor %}

        # verify cmake builds
        - bash test_cmake.sh    # [not win]

  - name: fftw-static
    script: build_fftw.sh     # [unix]
    build:
      skip: true           # [win]
      string: {{ mpi_prefix }}_h{{ PKG_HASH }}_{{ PKG_BUILDNUM }}
    requirements:
      build:
        - perl 5.*  # [not win]
        - cmake
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - {{ compiler('fortran') }}  # [not win]
        - llvm-openmp  # [osx]
        - make      # [unix]
        - autoconf  # [unix]
        - automake  # [unix]
        - gettext   # [unix]
        - m4        # [unix]
        - libtool   # [unix]
        - {{ mpi }}  # [build_platform != target_platform and mpi == 'openmpi']
      host:
        - {{ mpi }}  # [mpi != 'nompi']
        - llvm-openmp  # [osx]
        - {{ pin_subpackage('fftw', exact=True) }}
      run:
        - llvm-openmp  # [osx]
        - {{ pin_subpackage('fftw', exact=True) }}

    test:
      commands:
        # Verify library contains Fortran symbols
        - strings ${PREFIX}/lib/libfftw3.a | grep -q dfftw      # [not win]

        # Verify existence of static library files
        - test -f ${PREFIX}/lib/libfftw3f.a                     # [not win]
        - test -f ${PREFIX}/lib/libfftw3.a                      # [not win]
        - test -f ${PREFIX}/lib/libfftw3l.a                     # [not win]
        - test -f ${PREFIX}/lib/libfftw3f_threads.a             # [not win]
        - test -f ${PREFIX}/lib/libfftw3_threads.a              # [not win]
        - test -f ${PREFIX}/lib/libfftw3l_threads.a             # [not win]
        - test -f ${PREFIX}/lib/libfftw3f_omp.a                 # [not win]
        - test -f ${PREFIX}/lib/libfftw3_omp.a                  # [not win]
        - test -f ${PREFIX}/lib/libfftw3l_omp.a                 # [not win]

about:
  home: http://fftw.org
  license: GPL-2.0-or-later
  license_file: COPYING
  summary: The fastest Fourier transform in the west.

  dev_url: https://github.com/FFTW/fftw3

extra:
  recipe-maintainers:
    - alexbw
    - jakirkham
    - grlee77
    - jschueller
    - egpbos
