{% set version = "15.0.7" %}
{% set major_version = version.split(".")[0] %}
{% set build_number = 3 %}

{% set minor_aware_ext = major_version %}
{% if version.split(".")[1] | int > 0 %}
{% set minor_aware_ext = major_version + "." + version.split(".")[1] %}
{% endif %}

{% if libclang_soversion is not defined %}
{% set libclang_soversion = "13" %}
{% endif %}

package:
  name: clang_packages
  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/0001-Fix-sysroot-detection-for-linux.patch
      - patches/0002-clang-add-conda-specific-env-var-CONDA_BUILD_SYSROOT.patch
      - patches/0003-Fix-normalizeProgramName-s-handling-of-dots-outside-.patch
      - patches/0004-Set-VERSION-in-osx-as-well.patch
      - patches/0005-Fix-crosscompiling-LLVM-tools.patch
      # Disable -Werror,-Wundef-prefix=TARGET_OS as they are not yet defined in the 10.9 SDK used for osx-64
      # Only enable it for TARGET_OS_OSX.
      - patches/0006-Only-error-on-undefined-TARGET_OS_OSX.patch  # [osx and x86_64]
      - patches/0007-set-libclang-SOVERSION-unconditionally.patch
      # - patches/amd-roc-2.7.0.diff   # [variant != "hcc"]
      # - patches/amd-roc-hcc-2.7.0.diff  # [variant == "hcc"]
    # name folder for easier deletion; we do the equivalent of downloading
    # the subproject sources, so the work folder then has clang in it;
    # for details see build scripts
    folder: llvm-project

build:
  number: {{ build_number }}
  skip: true  # [variant == "hcc"]

requirements:
  build:
    - {{ compiler('cxx') }}
    - cmake >=3.4.3
    # Needed to unpack the source tarball
    - m2w64-xz  # [win]
    # "compiling .pyc files" fails without this
    - python >3
    # Building with ninja on windows for parallel builds
    - ninja  # [win]
    - make   # [unix]
    - llvmdev =={{ version }}    # [build_platform != target_platform]
  host:
    - libcxx {{ cxx_compiler_version }}  # [osx]
    - llvmdev =={{ version }}
    - zlib                               # [linux or win]

test:
  requires:
    - {{ compiler('cxx') }}
    - cmake >=3.4.3
  files:
    - mytest.c
  commands:
    - $PREFIX/bin/clang --version        # [unix]
    - $PREFIX/bin/clang++ --version      # [unix]
    - $PREFIX/bin/clang-check --version  # [unix]

    - $PREFIX/bin/clang -v -c mytest.c      # [unix]
    - '%LIBRARY_BIN%/clang -v -c mytest.c'  # [win]

    - if not exist %LIBRARY_INC%\\clang\\Lex\\Lexer.h exit 1  # [win]
    - test -f $PREFIX/include/clang/Lex/Lexer.h      # [unix]
    - test -f $PREFIX/lib/libclangBasic.a            # [unix]


outputs:
  - name: clangdev
    script: install_clangdev.sh  # [unix]
    script: install_clangdev.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("clangxx", exact=True) }}
        - {{ pin_subpackage("clang-tools", exact=True) }}
        - {{ pin_subpackage("libclang", exact=True) }}
        - {{ pin_subpackage("libclang-cpp", exact=True) }}
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - zlib                               # [linux or win]
      run:
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("clangxx", exact=True) }}
        - {{ pin_subpackage("clang-tools", exact=True) }}
        - {{ pin_subpackage("libclang", exact=True) }}
        - {{ pin_subpackage("libclang-cpp", exact=True) }}
        - {{ pin_compatible("libcxx", max_pin=None) }}  # [osx]
        - llvmdev =={{ version }}
    test:
      commands:
        - test -f "$PREFIX/include/clang/Config/config.h"   # [unix]
        - test -f "$PREFIX/lib/libclangAST.a"               # [unix]
        - if not exist %LIBRARY_INC%\\clang/Config/config.h exit 1   # [win]
        - if not exist %LIBRARY_LIB%\\clangAST.lib exit 1            # [win]

  - name: libclang-cpp{{ minor_aware_ext }}
    script: install_libclang_cpp.sh  # [unix]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
      run_exports:
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, max_pin="x.x") }}   # [unix]
      skip: true  # [win]
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib   # [linux]
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}  # [osx]
    test:
      commands:
        - test ! -f "$PREFIX/lib/libclang-cpp.so"                             # [linux]
        - test -f "$PREFIX/lib/libclang-cpp.so.{{ minor_aware_ext }}"         # [linux]
        - test ! -f "$PREFIX/lib/libclang-cpp.dylib"                          # [osx]
        - test -f "$PREFIX/lib/libclang-cpp.{{ minor_aware_ext }}.dylib"      # [osx]

  - name: libclang-cpp
    # note that this is just an empty wrapper on windows
    script: install_libclang_cpp.sh  # [unix]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
      run_exports:   # [unix]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, max_pin="x.x") }}   # [unix]
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                                                                # [linux]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, exact=True) }}  # [unix]
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}                        # [osx]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, exact=True) }}  # [unix]
    test:
      commands:
        - test -f "$PREFIX/lib/libclang-cpp.so"                     # [linux]
        - test -f "$PREFIX/lib/libclang-cpp.dylib"                  # [osx]
        - if exist %LIBRARY_BIN%\\libclang-cpp.dll exit 1           # [win]

  - name: libclang{{ libclang_soversion }}
    script: install_libclang.sh  # [unix]
    script: install_libclang.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
      run_exports:
        # note that the name (e.g. libclang13) does not necessarily
        # match the version lower bound (e.g. >=14.0.0)
        - {{ pin_subpackage("libclang" + libclang_soversion, max_pin="x.x") }}
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja                      # [win]
        - make                       # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - libcxx {{ cxx_compiler_version }}             # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                                          # [linux or win]
        - {{ pin_subpackage("clang", exact=True) }}
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}  # [osx]
    test:
      commands:
        # presence of versioned libraries
        - test -f "$PREFIX/lib/libclang.so.{{ libclang_soversion }}"                # [linux]
        - test -f "$PREFIX/lib/libclang.{{ libclang_soversion }}.dylib"             # [osx]
        - if not exist %LIBRARY_BIN%\\libclang-{{ libclang_soversion }}.dll exit 1  # [win]

        # absence of unversioned libraries
        - test ! -f "$PREFIX/lib/libclang.so"                              # [linux]
        - test ! -f "$PREFIX/lib/libclang.dylib"                           # [osx]
        - if exist %LIBRARY_BIN%\\libclang.dll exit 1                      # [win]
        - if exist %LIBRARY_LIB%\\libclang.lib exit 1                      # [win]

        {% if libclang_soversion != major_version %}
        # absence of major version if soversion is different
        - test ! -f "$PREFIX/lib/libclang.so.{{ major_version }}"          # [linux]
        - test ! -f "$PREFIX/lib/libclang.{{ major_version }}.dylib"       # [osx]
        - if exist %LIBRARY_BIN%\\libclang-{{ major_version }}.dll exit 1  # [win]

        # same for full version (i.e. with major that doesn't match soversion)
        - test ! -f "$PREFIX/lib/libclang.so.{{ version }}"                # [linux]
        - test ! -f "$PREFIX/lib/libclang.{{ version }}.dylib"             # [osx]
        - if exist %LIBRARY_BIN%\\libclang-{{ version }}.dll exit 1        # [win]
        {% endif %}

  - name: libclang
    script: install_libclang.sh  # [unix]
    script: install_libclang.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
      run_exports:
        # note that the name (e.g. libclang13) does not necessarily
        # match the version lower bound (e.g. >=14.0.0)
        - {{ pin_subpackage("libclang" + libclang_soversion, max_pin=None) }}
    requirements:
      build:
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja                      # [win]
        - make                       # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - libcxx {{ cxx_compiler_version }}                           # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                                                        # [linux or win]
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("libclang" + libclang_soversion, exact=True) }}
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}                # [osx]
        - {{ pin_subpackage("libclang" + libclang_soversion, exact=True) }}
    test:
      commands:
        - test -f "$PREFIX/lib/libclang.so"                # [linux]
        - test -f "$PREFIX/lib/libclang.dylib"             # [osx]
        - if not exist %LIBRARY_LIB%\\libclang.lib exit 1  # [win]

  - name: clang-{{ major_version }}
    script: install_clang.sh  # [unix]
    script: install_clang.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    requirements:
      build:
        - {{ compiler('cxx') }}
        - libcxx                             # [osx]
        - cmake >=3.4.3
        - ninja                              # [win]
        - make                               # [unix]
        - llvmdev =={{ version }}            # [build_platform != target_platform]
      host:
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                               # [linux or win]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, exact=True) }}  # [unix]
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}                        # [osx]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, exact=True) }}  # [unix]
      run_constrained:
        - clangdev {{ version }}
        - clangxx {{ version }}
        - clang-tools {{ version }}
        - llvm-tools {{ version }}
    test:
      commands:
        - clang-{{ major_version }} --version
        - test ! -f "$PREFIX/bin/clang"                                   # [unix]
        - test ! -f "$PREFIX/bin/clang-cl"                                # [unix]
        - test ! -f "$PREFIX/bin/clang-cpp"                               # [unix]
        - if exist %LIBRARY_BIN%\\clang.exe exit 1                        # [win]
        - if exist %LIBRARY_BIN%\\clang-cl.exe exit 1                     # [win]
        - if exist %LIBRARY_BIN%\\clang-cpp.exe exit 1                    # [win]
        - test ! -f "$PREFIX/lib/libclang-cpp.so"                         # [linux]
        - test -f "$PREFIX/lib/libclang-cpp.so.{{ minor_aware_ext }}"     # [linux]
        - test ! -f "$PREFIX/lib/libclang-cpp.dylib"                      # [osx]
        - test -f "$PREFIX/lib/libclang-cpp.{{ minor_aware_ext }}.dylib"  # [osx]
        - test -f "$PREFIX/lib/clang/{{ '.'.join(version.split('.')[:3]) }}/include/stdatomic.h"                    # [unix]
        - if not exist %LIBRARY_LIB%\\clang\\{{ '.'.join(version.split('.')[:3]) }}\\include\\stdatomic.h exit 1    # [win]

  - name: clang
    script: install_clang_symlinks.sh  # [unix]
    script: install_clang_symlinks.bat  # [win]
    requirements:
      host:
        - zlib                            # [win]
        - gcc_impl_{{ target_platform }}  # [linux]
        - {{ pin_subpackage("clang-" ~ major_version, exact=True) }}
      run:
        - gcc_impl_{{ target_platform }}  # [linux]
        - {{ pin_subpackage("clang-" ~ major_version, exact=True) }}
    test:
      commands:
        - clang --version
        - clang-cl --version
        - clang-cpp --version

  - name: clangxx
    script: install_clangxx.sh  # [unix]
    script: install_clangxx.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    requirements:
      host:
        - zlib  # [win]
        - {{ pin_subpackage("clang", exact=True) }}
      run:
        - {{ pin_subpackage("clang", exact=True) }}
    test:
      requires:
        - {{ compiler("cxx") }}
      files:
        - mytest.cxx
      commands:
        - clang++ --version
        - clang++ -v -c mytest.cxx
        - unset CONDA_BUILD_SYSROOT   # [unix]
        - set "CONDA_BUILD_SYSROOT="  # [win]
        - clang++ -v -c mytest.cxx

  - name: clang-format-{{ major_version }}
    script: install_clang_format.sh  # [unix]
    script: install_clang_format.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
      # for windows, use the clang-format package.
      skip: true  # [win]
    requirements:
      build:
        # "compiling .pyc files" fails without this
        - python >3
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("clangxx", exact=True) }}
        - {{ pin_subpackage("libclang", exact=True) }}
        - {{ pin_subpackage("libclang-cpp", exact=True) }}
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                            # [linux or win]
        - libxml2                         # [win]
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}                            # [osx]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, max_pin="x.x") }}   # [unix]
    test:
      commands:
        - clang-format-{{ major_version }} --version
        - test ! -f ${PREFIX}/bin/clang-format                   # [unix]
        - if exist %LIBRARY_BIN%\\clang-format.exe exit 1        # [win]
        - test ! -f ${PREFIX}/bin/c-index-test                   # [unix]
        - if exist %LIBRARY_BIN%\\c-index-test.exe exit 1        # [win]

  - name: clang-format
    script: install_clang_format.sh  # [unix]
    script: install_clang_format.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    requirements:
      build:
        # "compiling .pyc files" fails without this
        - python >3
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("clangxx", exact=True) }}
        - {{ pin_subpackage("libclang", exact=True) }}
        - {{ pin_subpackage("libclang-cpp", exact=True) }}
        - {{ pin_subpackage("clang-format-" + major_version, exact=True) }}      # [unix]
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                               # [linux or win]
      run:
        - {{ pin_compatible("libcxx", max_pin=None) }}                           # [osx]
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, max_pin="x.x") }}  # [unix]
        - {{ pin_subpackage("clang-format-" + major_version, exact=True) }}      # [unix]
    test:
      commands:
        - clang-format --version
        - test ! -f ${PREFIX}/bin/c-index-test                   # [unix]
        - if exist %LIBRARY_BIN%\\c-index-test.exe exit 1        # [win]

  - name: clang-tools
    script: install_clang_tools.sh  # [unix]
    script: install_clang_tools.bat  # [win]
    build:
      track_features:
        - hcc          # [variant=="hcc"]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    requirements:
      build:
        # "compiling .pyc files" fails without this
        - python >3
        - {{ compiler('cxx') }}
        - cmake >=3.4.3
        - ninja  # [win]
        - make   # [unix]
        - llvmdev =={{ version }}    # [build_platform != target_platform]
      host:
        - {{ pin_subpackage("clang", exact=True) }}
        - {{ pin_subpackage("clangxx", exact=True) }}
        - {{ pin_subpackage("libclang", exact=True) }}
        - {{ pin_subpackage("libclang-cpp", exact=True) }}
        - {{ pin_subpackage("clang-format", exact=True) }}
        - libcxx {{ cxx_compiler_version }}  # [osx]
        - llvmdev =={{ version }}
        - llvm =={{ version }}
        - zlib                               # [linux or win]
      run:
        - ucrt                                          # [win]
        - {{ pin_compatible("libcxx", max_pin=None) }}  # [osx]
        - {{ pin_subpackage("clang-format", exact=True) }}
        - {{ pin_subpackage("libclang-cpp" + minor_aware_ext, max_pin="x.x") }}   # [unix]
        - {{ pin_subpackage("libclang", max_pin="x.x") }}                         # [unix]
      run_constrained:
        - clangdev {{ version }}
    test:
      commands:
        - clang-check --version
        - clang-tidy --version

  - name: python-clang
    build:
      noarch: python
      # Building this output using this recipe is only supported on unix
      # It can still be installed on Windows as it is marked as `noarch: python`
      skip: true  # [not linux64]
      string: {{ variant }}_h{{ PKG_HASH }}_{{ build_number }}
    script: build_python_bindings.sh
    requirements:
      host:
        - python >=3.7
      run:
        - python >=3.7
        - libclang =={{ version }}
    test:
      source_files:
        - clang/bindings/python/tests
        - clang/bindings/python/examples/cindex
      imports:
        - clang
        - clang.cindex
      commands:
        - cd clang/bindings/python
        - python -m unittest discover -v

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

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