{% set version = hdf5_version|default("1.10.5") %}

{% set maj_min_ver = ".".join(version.split(".")[:2]) %}
{% set build = 1014 %}

# recipe-lint fails if mpi is undefined
{% set mpi = mpi or 'nompi' %}
{% if mpi == "nompi" %}
# prioritize nompi via build number
{% set build = build + 100 %}
{% endif %}

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

# mpi builds require the right mpi
# non-mpi builds *do not* appear to require non-mpi builds
# at least not always

{% if mpi != 'nompi' %}
{% set build_pin = mpi_prefix + '_*' %}
{% else %}
{% set build_pin = '' %}
{% endif %}

package:
  name: hdf5_split
  version: {{ version }}

source:
  url: https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-{{ maj_min_ver }}/hdf5-{{ version }}/src/hdf5-{{ version }}.tar.gz
  sha256: {{ hdf5_sha }}
  patches:
    # Patches the test suite to skip the cache, cache_image, and fheap tests
    # This test has been found to rather resource intensive.
    # In particular, it caused Travis CI's Mac builds to hang.
    # Given that we simply skip the test on all platforms.
    - patches/test_Makefile.in.patch       # [hdf5_version == "1.10.5"]
    - patches/test_Makefile.in_1106.patch  # [hdf5_version == "1.10.6"]
    # same thing, but for parallel tests:
    - patches/testpar_Makefile.in.patch
    # ref: http://hdf-forum.184993.n3.nabble.com/HDF5-1-8-14-15-16-with-OpenMPI-1-10-1-and-Intel-16-1-td4028533.html
    - patches/testpar_pflush.sh.in.openmpi.patch  # [mpi == 'openmpi']
    # h5diff help output test is flaky with mpi
    - patches/testh5diff.sh.in.patch  # [mpi != 'nompi']
    # Disable shared Fortran API building on OSX
    - patches/osx_configure.patch  # [osx]
    # Enable cross-compiling on osx
    - patches/osx_cross_configure.patch  # [osx and build_platform != target_platform]
    - patches/osx_cross_src_makefile.patch  # [osx and build_platform != target_platform]
    - patches/osx_cross_fortran_src_makefile.patch  # [osx and build_platform != target_platform]
    - patches/osx_cross_hl_fortran_src_makefile.patch  # [osx and build_platform != target_platform]
    # Fix for curl>7.69
    # See https://github.com/conda-forge/hdf5-feedstock/issues/125
    - patches/fix_curl_nobody.patch  # [hdf5_version == "1.10.6"]

build:
  number: {{ build }}
  skip: True  # [win and vc<14]
  skip: True  # [osx and arm64 and hdf5_version == "1.10.5"]
  string: "{{ mpi_prefix }}_h{{ PKG_HASH }}_{{ build }}"

requirements:
  build:
    - gnuconfig  # [unix]
    - {{ compiler('c') }}
    - {{ compiler('cxx') }}
    - {{ compiler('fortran') }}  # [not win]
    - cmake >=3.2.2              # [win]
    - make      # [unix]
    - libtool   # [unix]
    - ninja                      # [win]
    - {{ mpi }}  # [build_platform != target_platform and mpi != 'nompi']
    - libgfortran-devel_osx-64  # [build_platform != target_platform and build_platform == 'osx-64']
  host:
    - {{ mpi }}  # [mpi != 'nompi']
    - zlib
    - libcurl
    - openssl
  run:
    - {{ mpi }}  # [mpi != 'nompi']
    - zlib
    - libcurl
    - openssl

outputs:
  - name: hdf5
    build:
      skip: True  # [win and vc<14]
      run_exports:
        - {{ pin_subpackage('hdf5', max_pin='x.x.x') }} {{ build_pin }}
      string: "{{ mpi_prefix }}_h{{ PKG_HASH }}_{{ build }}"
    script: install.sh  # [not win]
    script: install.bat  # [win]

    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - {{ compiler('fortran') }}  # [not win]
        - cmake >=3.2.2              # [win]
        - libtool                    # [not win]
        - make                       # [not win]
        - ninja                      # [win]
        - {{ mpi }}  # [build_platform != target_platform and mpi != 'nompi']
      host:
        - {{ mpi }}  # [mpi != 'nompi']
        - zlib
        - libcurl    # [hdf5_version == "1.10.6"]
        - openssl    # [hdf5_version == "1.10.6"]
      run:
        - {{ mpi }}  # [mpi != 'nompi']
        - zlib
        - libcurl    # [hdf5_version == "1.10.6"]
        - openssl    # [hdf5_version == "1.10.6"]

    test:
      requires:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - {{ compiler('fortran') }}  # [not win]
        - pkg-config                 # [not win]
        - m2-grep                    # [win]
      files:
        - h5_cmprss.c
        - h5tutr_cmprss.cpp
        - h5_cmprss.f90
        - compound_fortran2003.f90
      commands:
        # Verify UNIX CLI tools.
        {% if mpi == 'nompi' %}
        {% set h5_compilers = [
            "h5c++",
            "h5cc",
            "h5fc",
        ] %}
        {% else %}
        # no p on parallel h5c++!
        {% set h5_compilers = [
            "h5c++",
            "h5pcc",
            "h5pfc",
        ] %}
        {% endif %}
        {% set hdf5_unix_cmds = h5_compilers + [
            "h5redeploy",
        ] %}
        {% if build_platform == target_platform %}
        {% set hdf5_unix_cmds = hdf5_unix_cmds + [
            "h5perf_serial",
        ] %}
        {% endif %}
        {% for each_hdf5_unix_cmd in hdf5_unix_cmds %}
        - command -v {{ each_hdf5_unix_cmd }}   # [not win]
        {% endfor %}
        # Verify CLI tools.
        {% set hdf5_cmds = [
            "gif2h5",
            "h52gif",
            "h5copy",
            "h5debug",
            "h5diff",
            "h5dump",
            "h5import",
            "h5jam",
            "h5ls",
            "h5mkgrp",
            "h5repack",
            "h5repart",
            "h5stat",
            "h5unjam"
        ] %}
        {% for each_hdf5_cmd in hdf5_cmds %}
        - command -v {{ each_hdf5_cmd }}  # [not win]
        - where {{ each_hdf5_cmd }}  # [win]
        {% endfor %}
        # Verify libraries.
        {% set hdf5_libs = [
            "hdf5",
            "hdf5_cpp",
            "hdf5_hl",
            "hdf5_hl_cpp"
        ] %}
        {% for each_hdf5_lib in hdf5_libs %}
        - test -f $PREFIX/lib/lib{{ each_hdf5_lib }}${SHLIB_EXT}  # [not win]
        - if not exist %PREFIX%\\Library\\lib\\{{ each_hdf5_lib }}.lib exit 1  # [win]
        - if not exist %PREFIX%\\Library\\bin\\{{ each_hdf5_lib }}.dll exit 1  # [win]
        {% endfor %}
        # Check ros3 driver
        - h5dump --filedriver=ros3 "http://s3.amazonaws.com/hdfgroup/data/hdf5demo/tall.h5" | grep '^HDF5'  # [hdf5_version == "1.10.6"]

  - name: hdf5-static                                   # [not win]
    build:                                              # [not win]
      string: "{{ mpi_prefix }}_h{{ PKG_HASH }}_{{ build }}"  # [not win]
    requirements:                                       # [not win]
      build:                                            # [not win]
        - {{ compiler('c') }}                           # [not win]
        - {{ compiler('cxx') }}                         # [not win]
        - {{ compiler('fortran') }}                     # [not win]
      run:                                              # [not win]
        - {{ pin_subpackage('hdf5', exact=True) }}      # [not win]
    script: install-static.sh                           # [not win]
    test:                                               # [not win]
      commands:                                         # [not win]
        {% set hdf5_libs = ["hdf5", "hdf5_cpp", "hdf5_hl", "hdf5_hl_cpp"] %}  # [not win]
        {% for each_hdf5_lib in hdf5_libs %}            # [not win]
        - test -f $PREFIX/lib/lib{{ each_hdf5_lib }}.a  # [not win]
        {% endfor %}                                    # [not win]

about:
  home: http://www.hdfgroup.org/HDF5/
  license: LicenseRef-HDF5
  license_family: BSD
  license_file: COPYING
  summary: HDF5 is a data model, library, and file format for storing and managing data

extra:
  recipe-maintainers:
    - jakirkham
    - gillins
    - groutr
    - ocefpaf
    - astrofrog-conda-forge
    - marqh
    - marcelotrevisani
    - scopatz
    - davidbrochart
    - SylvainCorlay
