{% set version = "8.9.7.29" %}
{% set platform = "linux-x86_64" %}  # [linux64]
{% set platform = "linux-ppc64le" %}  # [ppc64le]
{% set platform = "linux-sbsa" %}  # [aarch64]
{% set platform = "windows-x86_64" %}  # [win]
{% set extension = "tar.xz" %}  # [not win]
{% set extension = "zip" %}  # [win]

package:
  name: cudnn
  version: {{ version }}

source:
  url: https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/{{ platform }}/cudnn-{{ platform }}-{{ version }}_cuda11-archive.{{ extension }}  # [(cuda_compiler_version or "").startswith("11")]
  url: https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/{{ platform }}/cudnn-{{ platform }}-{{ version }}_cuda12-archive.{{ extension }}  # [(cuda_compiler_version or "").startswith("12")]
  sha256: a3e2509028cecda0117ce5a0f42106346e82e86d390f4bb9475afc976c77402e  # [linux64 and (cuda_compiler_version or "").startswith("11")]
  sha256: 475333625c7e42a7af3ca0b2f7506a106e30c93b1aa0081cd9c13efb6e21e3bb  # [linux64 and (cuda_compiler_version or "").startswith("12")]
  sha256: 91c37cfb458f541419e98510f13aaf5975c0232c613e18b776385490074eea17  # [aarch64 and (cuda_compiler_version or "").startswith("11")]
  sha256: e98b7c80010785e5d5ca01ee4ce9b5b0c8c73587ea6f8648be34d3f8d1d47bd1  # [aarch64 and (cuda_compiler_version or "").startswith("12")]
  sha256: f23fd7d59f9d4f743fa926f317dab0d37f6ea21edb2726ceb607bea45b0f9f36  # [ppc64le and (cuda_compiler_version or "").startswith("11")]
  sha256: 8574d291b299f9cc0134304473c9933bd098cc717e8d0876f4aba9f9eebe1b76  # [ppc64le and (cuda_compiler_version or "").startswith("12")]
  sha256: 5e45478efe71a96329e6c0d2a3a2f79c747c15b2a51fead4b84c89b02cbf1671  # [win and (cuda_compiler_version or "").startswith("11")]
  sha256: 94fc17af8e83a26cc5d231ed23981b28c29c3fc2e87b1844ea3f46486f481df5  # [win and (cuda_compiler_version or "").startswith("12")]

build:
  number: 3
  skip: True  # [osx]
  skip: True  # [cuda_compiler_version == "11.2"]
  # Disable binary relocation to workaround patchelf issue
  # xref: https://github.com/NixOS/patchelf/issues/492
  binary_relocation: false
  script:
    - mkdir -p $PREFIX/include               # [linux]
    - cp include/cudnn*.h $PREFIX/include/   # [linux]
    - mkdir -p $PREFIX/lib                   # [linux]
    - mv lib/libcudnn*.so* $PREFIX/lib/      # [linux]

    - mkdir %LIBRARY_INC%                                   # [win]
    - copy %SRC_DIR%\\include\\cudnn*.h %LIBRARY_INC%\\     # [win]
    - mkdir %LIBRARY_LIB%                                   # [win]
    - copy %SRC_DIR%\\lib\\x64\\cudnn*.lib %LIBRARY_LIB%\\  # [win]
    - mkdir %LIBRARY_BIN%                                   # [win]
    - copy %SRC_DIR%\\bin\\cudnn*.dll %LIBRARY_BIN%\\       # [win]
  ignore_run_exports:
    - cudatoolkit
  run_exports:
    - {{ pin_subpackage('cudnn') }}

requirements:
  build:
    - {{ compiler('c') }}
    - {{ compiler('cxx') }}
    - {{ compiler('cuda') }}
    - arm-variant * {{ arm_variant_type }}  # [aarch64]
    - sysroot_{{ target_platform }} 2.17  # [linux]
    # TODO: Update aarch64 to use sysroot 2.28 when compatible CI images are
    # available on conda-forge
    #- sysroot_{{ target_platform }} 2.28  # [aarch64]
  host:
    - cuda-version 11.0  # [(cuda_compiler_version or "").startswith("11")]
    - cuda-version 12.0  # [(cuda_compiler_version or "").startswith("12")]
    - patchelf >=0.12  # [linux]
    # to suppress ".so not found" errors
    - libzlib {{ zlib }}       # [linux]
    - libzlib-wapi {{ zlib }}  # [win64]
  run:
    - {{ pin_compatible("cuda-version", max_pin="x") }}
    - cudatoolkit 11.*  # [(cuda_compiler_version or "").startswith("11")]
    - cuda-nvrtc        # [(cuda_compiler_version or "").startswith("12")]
    - libcublas         # [(cuda_compiler_version or "").startswith("12")]
  run_constrained:
    - arm-variant * {{ arm_variant_type }}  # [aarch64]

test:
  requires:
    - {{ compiler('c') }}  # [linux]
  files:
    - test_load_elf.c      # [linux]
  commands:
    - if not exist %LIBRARY_INC%/cudnn.h exit 1                       # [win]
    - if not exist %LIBRARY_INC%/cudnn_adv_train.h exit 1             # [win]
    - if not exist %LIBRARY_LIB%/cudnn.lib exit 1                     # [win]
    - if not exist %LIBRARY_LIB%/cudnn_adv_train.lib exit 1           # [win]
    - if not exist %LIBRARY_BIN%/cudnn64_8.dll exit 1                 # [win]
    - if not exist %LIBRARY_BIN%/cudnn_adv_train64_8.dll exit 1       # [win]
    - test -f $PREFIX/include/cudnn.h                                 # [linux]
    - test -f $PREFIX/include/cudnn_adv_train.h                       # [linux]
    - test -f $PREFIX/lib/libcudnn.so                                 # [linux]
    - test -f $PREFIX/lib/libcudnn_adv_train.so                       # [linux]
    - ${GCC} test_load_elf.c -std=c99 -Werror -ldl -o test_load_elf   # [linux]
    - for f in $PREFIX/lib/libcudnn*.so; do ./test_load_elf $f; done  # [linux]

about:
  home: https://developer.nvidia.com/cudnn
  license: LicenseRef-cuDNN-Software-License-Agreement
  license_file: LICENSE
  license_url: https://docs.nvidia.com/deeplearning/cudnn/sla/index.html
  summary: "NVIDIA's cuDNN deep neural network acceleration library"
  description: |
    NVIDIA CUDA Deep Neural Network (cuDNN) is a GPU-accelerated library of
    primitives for deep neural networks. It provides highly tuned
    implementations of routines arising frequently in DNN applications.

    License Agreements:- The packages are governed by the NVIDIA cuDNN
    Software License Agreement (EULA). By downloading and using the packages,
    you accept the terms and conditions of the NVIDIA cuDNN EULA -
    https://docs.nvidia.com/deeplearning/cudnn/sla/index.html
  doc_url: https://docs.nvidia.com/deeplearning/cudnn/
  dev_url: https://developer.nvidia.com/rdp/cudnn-download

extra:
  recipe-maintainers:
    - adibbley
    - bdice
    - jakirkham
    - vyasr
