{% set name = "cuda-nvcc-impl" %}
{% set version = "12.8.93" %}
{% set cuda_version = "12.8" %}
{% set cuda_version_next_major = (cuda_version.split(".")[0]|int + 1)|string + ".0a0" %}
{% set gcc_min_constraint = ">=6" %}
{% set gcc_constraint = ">=6,<15.0a0" %}
{% set platform = "linux-x86_64" %}  # [linux64]
{% set platform = "linux-sbsa" %}  # [aarch64 and arm_variant_type == "sbsa"]
#{% set platform = "linux-aarch64" %}  # [aarch64 and arm_variant_type == "tegra"]
{% set platform = "windows-x86_64" %}  # [win]
{% set target_name = "x86_64-linux" %}  # [linux64]
{% set target_name = "sbsa-linux" %}  # [aarch64 and arm_variant_type == "sbsa"]
#{% set target_name = "aarch64-linux" %}  # [aarch64 and arm_variant_type == "tegra"]
{% set target_name = "x64" %}  # [win]
{% set extension = "tar.xz" %}  # [not win]
{% set extension = "zip" %}  # [win]
{% set exists = "which" %}  # [not win]
{% set exists = "where" %}  # [win]
{% set sha256 = "9961b3484b6b71314063709a4f9529654f96782ad39e72bf1e00f070db8210d3" %}  # [linux64]
{% set sha256 = "dc0b713ce69fd921aa53ac68610717d126fc273a3c554b0465cf44d7e379f467" %}  # [aarch64 and arm_variant_type == "sbsa"]
{% set sha256 = "9fdc70b4271ed9aad4d64cd7076a7d96ec36512d074b9995fe638de669197391" %}  # [win]
#{% set sha256 = "2eb267e6ebbe17e5ebc593bae8b83fa927c3e53cbf43ef5c614eff6c94053d10" %}  # [aarch64 and arm_variant_type == "tegra"]

package:
  name: cuda-nvcc-impl-split
  version: {{ version }}

source:
  url: https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvcc/{{ platform }}/cuda_nvcc-{{ platform }}-{{ version }}-archive.{{ extension }}
  sha256: {{ sha256 }}
  patches:
    - nvcc.profile.patch      # [linux]
    - nvcc.profile.patch.win  # [win]

build:
  number: 3
  binary_relocation: false
  skip: true  # [osx]

requirements:
  build:
    - patchelf             # [linux]
    - cf-nvidia-tools 1.*  # [linux]

outputs:
  - name: cuda-nvcc-tools
    files:   # [linux]
      - bin/*                               # [linux]
    build:
      binary_relocation: false
    requirements:
      build:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        - {{ stdlib("c") }}
      host:
        - {{ pin_subpackage("cuda-crt-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-tools", exact=True) }}
        - arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - {{ pin_subpackage("cuda-crt-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-tools", exact=True) }}
        - arm-variant * {{ arm_variant_type }}  # [aarch64]
      run_constrained:
        - gcc_impl_{{ target_platform }} {{ gcc_constraint }}  # [linux]
    test:
      requires:
        - patchelf                                                # [linux]
      files:
        - patchelf_exclude.txt                                    # [linux]
        - test-rpath-nvcc.sh                                      # [linux]
      commands:
        - test -f $PREFIX/bin/nvcc                                # [linux]
        - test -f $PREFIX/bin/ptxas                               # [linux]
        - bash test-rpath-nvcc.sh                                 # [linux]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Architecture independent part of CUDA NVCC compiler.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-nvcc-dev_{{ target_platform }}
    build:
      noarch: generic
      missing_dso_whitelist:                     # [win]
        - "*/api-ms-win-core-winrt-*.dll"        # [win]
    run_exports:
      strong:
        - cuda-version >={{ cuda_version }},<{{ cuda_version_next_major }}
    files:
      # targets/{{ target_name }}/bin/nvcc is only a symlink
      - targets/{{ target_name }}/bin          # [linux]
      - targets/{{ target_name }}/include/*.h  # [linux]
      - targets/{{ target_name }}/lib         # [linux]
      - Library\bin\*.exe                     # [win]
      - Library\bin\nvcc.profile              # [win]
      - Library\include\fatbinary_section.h   # [win]
      - Library\include\nvPTXCompiler.h       # [win]
      - Library\lib\nvptxcompiler_static.lib  # [win]
    requirements:
      build:
        - {{ stdlib("c") }}
      host:
        - {{ pin_subpackage("cuda-crt-dev_" + target_platform, exact=True) }}
        - {{ pin_subpackage("cuda-nvcc-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-dev_" + target_platform, exact=True) }}
        - arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
        # Use libgcc-ng instead of libgcc
        - libgcc-ng {{ gcc_min_constraint }}  # [linux]
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - {{ pin_subpackage("cuda-crt-dev_" + target_platform, exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-dev_" + target_platform, exact=True) }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
        # Use libgcc-ng instead of libgcc
        - libgcc-ng {{ gcc_min_constraint }}  # [linux]
      run_constrained:
        - gcc_impl_{{ target_platform }} {{ gcc_constraint }}  # [linux]
    test:
      commands:
        - test -L $PREFIX/targets/{{ target_name }}/bin/nvcc      # [linux]
        - test -f $PREFIX/targets/{{ target_name }}/lib/libnvptxcompiler_static.a     # [linux]
        - test -f $PREFIX/targets/{{ target_name }}/include/fatbinary_section.h       # [linux]
        - test -f $PREFIX/targets/{{ target_name }}/include/nvPTXCompiler.h           # [linux]
        - if not exist %LIBRARY_BIN%\nvcc.exe exit 1                  # [win]
        - if not exist %LIBRARY_BIN%\nvcc.profile exit 1              # [win]
        - if not exist %LIBRARY_LIB%\nvptxcompiler_static.lib exit 1  # [win]
        - if not exist %LIBRARY_INC%\nvPTXCompiler.h exit 1           # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Target architecture dependent parts of CUDA NVCC compiler.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-nvcc-impl
    build:
      # libnvvm.so gets corrupted by patchelf. No need to relocate as it is already relocatable
      binary_relocation: false
    run_exports:
      strong:
        - cuda-version >={{ cuda_version }},<{{ cuda_version_next_major }}
    files:   # [linux]
      - lib/libnvptxcompiler_static.a  # [linux]
    requirements:
      host:
        - {{ pin_subpackage("cuda-nvvm-impl", exact=True) }}
        - arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-cudart-dev
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_subpackage("cuda-nvcc-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvcc-dev_" + target_platform, exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-impl", exact=True) }}
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-cudart-dev
      run_constrained:
        - gcc_impl_{{ target_platform }} {{ gcc_constraint }}  # [linux]
    test:
      requires:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        - {{ stdlib("c") }}
        - cmake
        - cuda-driver-dev  # [linux]
        - git              # [linux]
        - ninja
      files:
        - test.cu
        - CMakeLists.txt
        - run_nvcc_tests.sh   # [linux]
        - run_nvcc_tests.bat  # [win]
      commands:
        # these symlinks are provided by the gxx and gcc packages on conda-forge
        - ln -sf ${PREFIX}/bin/x86_64-conda-linux-gnu-g++ ${PREFIX}/bin/g++     # [linux and x86_64]
        - ln -sf ${PREFIX}/bin/x86_64-conda-linux-gnu-gcc ${PREFIX}/bin/gcc     # [linux and x86_64]
        - ln -sf ${PREFIX}/bin/aarch64-conda-linux-gnu-g++ ${PREFIX}/bin/g++     # [linux and aarch64]
        - ln -sf ${PREFIX}/bin/aarch64-conda-linux-gnu-gcc ${PREFIX}/bin/gcc     # [linux and aarch64]
        # cmake has trouble finding ar on linux-aarch64; so this needs to be added
        # compared to conda-forge. NB conda-forge isn't building this for linux-aarch64 currently.
        - if [ ! -f "${PREFIX}/bin/ar" ]; then ln -s "${AR}" "${PREFIX}/bin/ar"; fi # [linux and aarch64]
        - test -L $PREFIX/lib/libnvptxcompiler_static.a  # [linux]
        - {{ exists }} nvcc
        - ./run_nvcc_tests.sh   # [linux]
        - .\run_nvcc_tests.bat  # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Compiler for CUDA applications.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-crt-tools
    files:
      - bin/crt          # [linux]
      - Library\bin\crt  # [win]
    requirements:
      host:
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -d $PREFIX/bin/crt            # [linux]
        - test -f $PREFIX/bin/crt/link.stub  # [linux]
        - if not exist %LIBRARY_BIN%\crt\link.stub exit 1  # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: CUDA internal tools.
      description: |
        CUDA internal tools.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-crt-dev_{{ target_platform }}
    build:
      noarch: generic
    run_exports:
      strong:
        - cuda-version >={{ cuda_version }},<{{ cuda_version_next_major }}
    files:
      - targets/{{ target_name }}/include/crt  # [linux]
      - Library\include\crt                    # [win]
    requirements:
      host:
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -f $PREFIX/targets/{{ target_name }}/include/crt/common_functions.h  # [linux]
        - if not exist %LIBRARY_INC%\crt\common_functions.h exit 1  # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: CUDA internal headers.
      description: |
        CUDA internal headers.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-nvvm-tools
    files:
      - nvvm/bin                # [linux]
      - nvvm/libdevice          # [linux]
      - Library\nvvm\bin        # [win]
      - Library\nvvm\libdevice  # [win]
    requirements:
      build:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        - {{ stdlib("c") }}
      host:
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      requires:
        - patchelf                                                # [linux]
      files:
        - test-rpath-nvvm.sh                                      # [linux]
      commands:
        - test -d $PREFIX/nvvm                                    # [linux]
        - test -f $PREFIX/nvvm/bin/cicc                           # [linux]
        - bash test-rpath-nvvm.sh                                 # [linux]
        - if not exist %LIBRARY_PREFIX%\nvvm\bin\cicc.exe exit 1  # [win]
        - if not exist %LIBRARY_PREFIX%\nvvm\libdevice exit 1     # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Compiler for CUDA applications.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-nvvm-dev_{{ target_platform }}
    build:
      noarch: generic
    files:                              # [linux]
      - targets/{{ target_name }}/nvvm  # [linux]
    requirements:
      host:
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -L $PREFIX/targets/{{ target_name }}/nvvm          # [linux]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Compiler for CUDA applications.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

  - name: cuda-nvvm-impl
    build:
      # libnvvm.so gets corrupted by patchelf. No need to relocate as it is already relocatable.
      binary_relocation: false
      # This is a workaround to ignore the overlinking error for problematic *.so files.
      # The reason is that in the Amazon Linux 2-based Docker container, there's py-lief version 0.12.3. 
      # In the Rocky 8 container, there's 0.16.4, which seems to have this bug. 
      # See also:
      # https://github.com/anaconda/docker-images/blob/main/anaconda-pkg-build/linux/rocky8/Dockerfile
      # https://github.com/conda/conda-build/issues/5665
      # https://github.com/conda-forge/conda-build-feedstock/pull/251
      # https://github.com/conda-forge/lief-feedstock/pull/57#issuecomment-2770881269
      # This workaround should be removed when the conda-build fix is merged/py-lief pinned and the rocky 8 docker image rebuilt.
      overlinking_ignore_patterns: # [linux and aarch64]
        - "nvvm/lib64/libnvvm.so*"  # [linux and aarch64]
    files:
      - nvvm/include          # [linux]
      - nvvm/lib64            # [linux]
      - Library\nvvm\include  # [win]
      - Library\nvvm\lib      # [win]
    requirements:
      build:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        - {{ stdlib("c") }}
      host:
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      requires:
        - patchelf                                                # [linux]
      files:
        - test-rpath-nvvm.sh                                      # [linux]
      commands:
        - test -d $PREFIX/nvvm/include  # [linux]
        - test -d $PREFIX/nvvm/lib64    # [linux]
        - bash test-rpath-nvvm.sh       # [linux]
        - if not exist %LIBRARY_PREFIX%\nvvm\include exit 1  # [win]
        - if not exist %LIBRARY_PREFIX%\nvvm\lib exit 1      # [win]
    about:
      home: https://developer.nvidia.com/cuda-toolkit
      license_file: LICENSE
      license: LicenseRef-NVIDIA-End-User-License-Agreement
      license_url: https://docs.nvidia.com/cuda/eula/index.html
      summary: Compiler for CUDA applications.
      description: |
        Compiler for CUDA applications.
      doc_url: https://docs.nvidia.com/cuda/index.html

about:
  home: https://developer.nvidia.com/cuda-toolkit
  license_file: LICENSE
  license: LicenseRef-NVIDIA-End-User-License-Agreement
  license_url: https://docs.nvidia.com/cuda/eula/index.html
  summary: Compiler for CUDA applications.
  description: |
    Compiler for CUDA applications.
  doc_url: https://docs.nvidia.com/cuda/index.html

extra:
  feedstock-name: cuda-nvcc-impl
  recipe-maintainers:
    - conda-forge/cuda
