{% set version = "15.0.7" %}
{% set major_ver = version.split(".")[0] %}

package:
  name: llvm-package
  version: {{ version }}

source:
  url: https://github.com/llvm/llvm-project/releases/download/llvmorg-{{ version.replace(".rc", "-rc") }}/llvm-project-{{ version.replace(".rc", "rc") }}.src.tar.xz
  sha256: 8b5fcb24b4128cf04df1b0b9410ce8b1a729cb3c544e6da885d234280dedeac6
  patches:
    # - patches/intel-D47188-svml-VF.patch    # Fixes vectorizer and extends SVML support
    # - patches/expect-fastmath-entrypoints-in-add-TLI-mappings.ll.patch # adjusts test added in 10.0.0 for intel-D47188-svml-VF.patch effects
    # - patches/amd-roc-2.7.0.diff
    - patches/0001-pass-through-QEMU_LD_PREFIX-SDKROOT.patch
    # backport of zlib patches, can be dropped for v>15.0.3, see
    # https://reviews.llvm.org/D135457 & https://reviews.llvm.org/D136065
    - patches/0002-CMake-Fix-Findzstd-module-for-shared-DLL-on-Windows.patch
    - patches/no-windows-symlinks.patch

build:
  number: 3
  merge_build_host: false

requirements:
  build:
    - {{ compiler('cxx') }}
    - cmake
    - ninja
    - python >=3
    - libcxx {{ cxx_compiler_version }}  # [osx]
  host:
    - libcxx {{ cxx_compiler_version }}  # [osx]
    # needs aarch/ppc/arm (re)build of conda-forge/backtrace-feedstock
    - backtrace                          # [unix and x86]
    - libxml2
    - zlib
    - zstd                               # [build_platform == target_platform]

outputs:
  # Contains everything
  - name: llvmdev
    script: install_llvm.sh  # [not win]
    script: install_llvm.bat  # [win]
    build:
      activate_in_script: true
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake
        - ninja
        - python >=3
        - m2-sed                             # [win]
        - libcxx {{ cxx_compiler_version }}  # [osx]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}
        - {{ pin_subpackage("llvm-tools", exact=True) }}
        - libxml2
        - zlib
        - zstd     # [build_platform == target_platform]
      run:
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}
        - {{ pin_subpackage("llvm-tools", exact=True) }}
        - libcxx >={{ cxx_compiler_version }}                           # [osx]
    test:
      requires:
        - ripgrep  # [win]
      commands:
        - $PREFIX/bin/llvm-config --libs                                # [not win]
        - test -f $PREFIX/include/llvm/Pass.h                           # [not win]
        - test -f $PREFIX/lib/libLLVMCore.a                             # [not win]
        - $PREFIX/libexec/llvm/not false                                # [not win]
        - $PREFIX/bin/llvm-nm --help                                    # [not win]
        - if not exist %LIBRARY_INC%\\llvm\\Pass.h exit 1               # [win]
        - if not exist "%LIBRARY_PREFIX%"\\libexec\llvm\not.exe exit 1  # [win]
        - if not exist "%LIBRARY_BIN%"\\llvm-nm.exe exit 1              # [win]
        - llvm-nm.exe --help                                            # [win]

        # ensure we've correctly inserted %VSINSTALLDIR% into the CMake metadata for LLVM;
        # we're looking for: `INTERFACE_LINK_LIBRARIES "$ENV{VSINSTALLDIR}/DIA SDK/lib/amd64/diaguids.lib;[...]`instead
        - rg -e "INTERFACE_LINK_LIBRARIES\s\"\$ENV\{VSINSTALLDIR\}[/\w\s]+/diaguids\.lib" %LIBRARY_LIB%\cmake\llvm\LLVMExports.cmake  # [win]

  # Contains the shared libraries. To make different LLVM libraries co-installable
  # soversion is appended to the package name.
  - name: libllvm{{ major_ver }}
    script: install_llvm.sh  # [not win]
    build:
      activate_in_script: true
      run_exports:   # [not win]
        - {{ pin_subpackage("libllvm" + major_ver, max_pin="x.x") }}  # [not win]
    requirements:
      build:
        - {{ compiler('cxx') }}
        - ninja                    # [not win]
        - cmake                    # [not win]
        - python >=3               # [not win]
        - libcxx {{ cxx_compiler_version }}  # [osx]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - libxml2
        - zlib
        - zstd                                 # [build_platform == target_platform]
      run:
        - libcxx >={{ cxx_compiler_version }}  # [osx]
    test:
      commands:
        - test -f $PREFIX/lib/libLLVM-{{ major_ver }}${SHLIB_EXT}     # [not win]

  # This is a meta package so that people can use the latest libllvm and also
  # for run_exports
  - name: llvm
    build:
      run_exports:   # [not win]
        - {{ pin_subpackage("libllvm" + major_ver, max_pin="x.x") }}  # [not win]
    requirements:
      build:
      host:
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}     # [not win]
        - libxml2
        - zlib
        - zstd                                                        # [build_platform == target_platform]
      run:   # [not win]
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}     # [not win]
      run_constrained:   # [not win]
        - llvmdev   {{ version }}                                     # [not win]
    test:
      commands:
        - echo "Hello World!"

  # Contains LLVM tools
  - name: llvm-tools
    script: install_llvm.sh  # [not win]
    script: install_llvm.bat  # [win]
    build:
      activate_in_script: true
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake
        - ninja
        - python >=3
        - libcxx {{ cxx_compiler_version }}  # [osx]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}
        - libxml2
        - zlib
        - zstd     # [build_platform == target_platform]
      run:
        - {{ pin_subpackage("libllvm" + major_ver, exact=True) }}
      run_constrained:
        - llvmdev   {{ version }}
    test:
      commands:
        - $PREFIX/bin/llc -version                               # [not win]
        - if not exist "%LIBRARY_BIN%"\\llc.exe exit 1           # [win]
        - llc -version                                           # [win]

  - name: lit
    build:
      noarch: python
      script: python -m pip install llvm/utils/lit --no-deps -vv
      activate_in_script: true
      skip: true  # [not linux64]
      entry_points:
        # upstream LLVM is inconsistent; there's one way specified in lit's setup.py...
        - lit = lit.main:main
        # ... and then the CMake files we install here (e.g. AddLLVM.cmake) look for another
        - llvm-lit = lit.main:main
    requirements:
      host:
        - python >=3
        - pip
      run:
        - python
    test:
      imports:
        - lit
      commands:
        - lit -h
        - llvm-lit -h

about:
  home: http://llvm.org/
  dev_url: https://github.com/llvm/llvm-project
  license: Apache-2.0 WITH LLVM-exception
  license_file: llvm/LICENSE.TXT
  license_family: Apache
  summary: Development headers and libraries for LLVM

extra:
  recipe-maintainers:
    - JohanMabille
    - inducer
    - jakirkham
    - mingwandroid
    - SylvainCorlay
    - isuruf
    - timsnyder
    - xhochy
    - h-vetinari
  feedstock-name: llvmdev
