{% set name = "cuda-nvcc-impl" %}
{% set version = "12.4.131" %}
{% set cuda_version = "12.4" %}
{% set cuda_version_next_major = (cuda_version.split(".")[0]|int + 1)|string + ".0a0" %}
{% set platform = "linux-x86_64" %}  # [linux64]
{% set platform = "linux-ppc64le" %}  # [ppc64le]
{% set platform = "linux-sbsa" %}  # [aarch64]
{% set platform = "windows-x86_64" %}  # [win]
{% set target_name = "x86_64-linux" %}  # [linux64]
{% set target_name = "ppc64le-linux" %}  # [ppc64le]
{% set target_name = "sbsa-linux" %}  # [aarch64]
{% set target_name = "x64" %}  # [win]
{% set extension = "tar.xz" %}  # [not win]
{% set extension = "zip" %}  # [win]

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: 7ffba1ada0e4b8c17e451ac7a60d386aa2642ecd08d71202a0b100c98bd74681  # [linux64]
  sha256: 2934e83a4df2e0f4182e148753cfd1f29af226a280ea459008819531e9edb5b9  # [ppc64le]
  sha256: 83f130dab0325e12b90fdf1279c0cbbd88acf638ef0a7e0cad72d50855a4f44a  # [aarch64]
  sha256: 3b14cf8dd9dda4a3b1a9682270d46eef775f018e17650187a8a448a06111f2b8  # [win]
  patches:
    - nvcc.profile.patch      # [linux]
    - nvcc.profile.patch.win  # [win]

build:
  number: 0
  skip: true  # [osx or (linux and s390x)]

outputs:
  - name: cuda-nvcc-tools
    files:                                  # [linux]
      - bin/*                               # [linux]
    requirements:
      build:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        - sysroot_{{ target_platform }} {{ conda_glibc_ver }}.*    # [linux]
      host:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
        - {{ pin_subpackage("cuda-crt-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-tools", exact=True) }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
        - {{ pin_subpackage("cuda-crt-tools", exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-tools", exact=True) }}
      run_constrained:
        - gcc_impl_{{ target_platform }} >=6,<13  # [linux]
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -f $PREFIX/bin/nvcc                                # [linux]
        - test -f $PREFIX/bin/ptxas                               # [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:
        - sysroot_{{ target_platform }} {{ conda_glibc_ver }}.*    # [linux]
      host:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
        - {{ pin_subpackage("cuda-nvcc-tools", exact=True) }}
        - {{ pin_subpackage("cuda-crt-dev_" + target_platform, exact=True) }}
        - {{ pin_subpackage("cuda-nvvm-dev_" + target_platform, exact=True) }}
        - libgcc-ng >=6                         # [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) }}
        - libgcc-ng >=6                         # [linux]
      run_constrained:
        - gcc_impl_{{ target_platform }} >=6,<13  # [linux]
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    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:
      build:
        - sysroot_{{ target_platform }} {{ conda_glibc_ver }}.*    # [linux]
      host:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
        - cuda-cudart-dev
        - {{ pin_subpackage("cuda-nvvm-impl", exact=True) }}
      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") }}
        - cuda-cudart-dev
      run_constrained:
        - gcc_impl_{{ target_platform }} >=6,<13  # [linux]
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
        - vc >={{ VCVER }}                        # [win]
    test:
      requires:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
        #- gxx              # [linux]
        #- gcc              # [linux]
        - vs{{ VSYEAR }}_win-64 =={{ CL_VERSION }}  # [win]
        - cmake
        - cuda-driver-dev  # [linux]
        - git              # [linux]
        - ninja
      files:
        - test.cu
        - CMakeLists.txt
        - run_cmake_cuda_tests.sh  # [linux]
      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]
        - nvcc --version
        - nvcc --verbose test.cu
        - cmake -S . -B ./build -G=Ninja && cmake --build ./build -v
        - bash ./run_cmake_cuda_tests.sh  # [linux and (x86_64 or aarch64)]
    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:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
      run_constrained:
        - 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:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
      run_constrained:
        - 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") }}
      host:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
      run_constrained:
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -d $PREFIX/nvvm                                    # [linux]
        - test -f $PREFIX/nvvm/bin/cicc                           # [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:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
      run_constrained:
        - 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
    files:
      - nvvm/include          # [linux]
      - nvvm/lib64            # [linux]
      - Library\nvvm\include  # [win]
      - Library\nvvm\lib      # [win]
    requirements:
      build:
        - {{ compiler("c") }}
        - {{ compiler("cxx") }}
      host:
        #- arm-variant * {{ arm_variant_type }}  # [aarch64]
        - cuda-version {{ cuda_version }}
      run:
        - {{ pin_compatible("cuda-version", max_pin="x.x") }}
      run_constrained:
        - arm-variant * {{ arm_variant_type }}    # [aarch64]
    test:
      commands:
        - test -d $PREFIX/nvvm/include  # [linux]
        - test -d $PREFIX/nvvm/lib64    # [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
