{% set version = "2021.11.0" %}

{% set vmajor = version.split('.')[0]|int %}
{% set vminor = version.split('.')[1]|int %}
{% set vpatch = version.split('.')[2]|int %}
{% set vbinary = vmajor - 2009 %}

{% set vtag = "v%d.%d.%d" % (vmajor, vminor, vpatch) %}

# Official patch version for the first release of 2021 is 1,
# but internal patch version is 0, handle it for interface version.
{% set vinterface = "%d" % (vbinary * 1000 + vminor * 10 + (vpatch if vmajor != 2021 or vminor != 1 else 0)) %}

# Libraries on Windows are expected to be installed into LIBRARY_PREFIX.
# tbb4py files should be installed into PREFIX.
# See https://docs.conda.io/projects/conda-build/en/latest/user-guide/environment-variables.html.
# On non-Windows CMAKE_ARGS already contains CMAKE_INSTALL_PREFIX.
# CMAKE_PREFIX_PATH is needed to discover libhwloc with PkgConfig.
{% if win %}
    {% set cmake_args = "%CMAKE_ARGS% -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%LIBRARY_PREFIX%" %}
    {% set cmake_tbb4py_args = "%CMAKE_ARGS% -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%PREFIX%" %}
{% else %}
    {% set cmake_args = "$CMAKE_ARGS" %}
    {% set cmake_tbb4py_args = "$CMAKE_ARGS" %}
{% endif %}

# Use specific test list agreed with TBB team to cover core functionality on high level.
# Execution of all tests is too heavy task for packaging CI.
# test_malloc_compliance is temporary excluded from the list since it causes failures that likely appear due to test implementation or env misconfiguration.
# oneTBB issue: https://github.com/oneapi-src/oneTBB/issues/700
# Agreed with oneTBB developers that it is suitable approach since the product is fully tested on oneTBB side.
{% set tests = "test_parallel_for test_tbb_header conformance_version test_flow_graph test_arena_constraints" %}
{% set ctest_regex = "^(%s)$" % (tests.replace(' ', '|')) %}

{% set tbb4py_build_dir = "build_$PKG_HASH" %}  # [not win]
{% set tbb4py_build_dir = "build_%PKG_HASH%" %}  # [win]

package:
  name: tbb
  version: {{ version }}

source:
  fn: {{ vtag }}.tar.gz
  url: https://github.com/oneapi-src/oneTBB/archive/{{ vtag }}.tar.gz
  sha256: 782ce0cab62df9ea125cdea253a50534862b563f1d85d4cda7ad4e77550ac363

build:
  number: 0

requirements:
  build:
    - python *
    - {{ compiler('cxx') }}
    - ninja       # [win]
    - make        # [not win]
    - cmake
    - pkg-config
  host:
    - libhwloc >=2.5

outputs:
  - name: tbb
    about:
      summary: TBB Libraries
      license_file:
        - LICENSE.txt
        - third-party-programs.txt
    build:
      script:
        - set CMAKE_GENERATOR=Ninja      # [win]
        # unset option that ninja complains about
        - set CMAKE_GENERATOR_TOOLSET=   # [win]
        - set CMAKE_GENERATOR_PLATFORM=  # [win]
        - cmake {{ cmake_args }} -DTBB_TEST=OFF -S . -B build
        - cmake --build build --parallel
        - cmake -DCOMPONENT=runtime -P build/cmake_install.cmake
    requirements:
      host:
        - libhwloc >=2.5
    test:
      requires:
        # any python version is ok for sake of testing of shared libraries
        - python *
      commands:
        {% set libname = "libtbb.so." ~ vbinary ~ "." ~ vminor %}          # [linux]
        {% set libname = "libtbb." ~ vbinary ~ "." ~ vminor ~ ".dylib" %}  # [osx]
        {% set libname = "tbb" ~ vbinary ~ ".dll" %}                       # [win]
        {% set win_extra = "os.add_dll_directory(os.environ['LIBRARY_BIN']); " if win else "" %}
        - python -c "import ctypes, os; {{ win_extra }} assert {{ vinterface }} == ctypes.cdll[r'{{ libname }}']['TBB_runtime_interface_version']()"

  - name: tbb-devel
    about:
      summary: TBB Development files
      license_file:
        - LICENSE.txt
        - third-party-programs.txt
    build:
      script:
        - set CMAKE_GENERATOR=Ninja      # [win]
        - set CMAKE_GENERATOR_TOOLSET=   # [win]
        - set CMAKE_GENERATOR_PLATFORM=  # [win]
        - cmake {{ cmake_args }} -DTBB_TEST=OFF -S . -B build
        - cmake --build build --parallel
        - cmake -DCOMPONENT=devel -P build/cmake_install.cmake
      run_exports:
        - tbb >={{ version }}
      ignore_run_exports:
        - libhwloc
    requirements:
      build:
        - python *
        - {{ compiler('cxx') }}
        - ninja  # [win]
        - make   # [not win]
        - cmake >=3.13
        - pkg-config
      host:
        - libhwloc
      run:
        - {{ pin_subpackage('tbb', exact=True) }}        # development package is for specific version of tbb
    test:
      requires:
        - {{ compiler('cxx') }}
        - ninja  # [win]
        - make   # [not win]
        - cmake
        - pkg-config
        - libhwloc
        # When cross-compiling we need to get uname from coreutils so that
        # CMAKE_SYSTEM_PROCESSOR is set to the correct value
        - coreutils  # [build_platform != target_platform]
      source_files:
        - cmake
        - test
        - CMakeLists.txt
        - include  # some tests depend on files from include
        - src      # some tests depend on files from src
      commands:
        - set CMAKE_GENERATOR=Ninja      # [win]
        - set CMAKE_GENERATOR_TOOLSET=   # [win]
        - set CMAKE_GENERATOR_PLATFORM=  # [win]
        # Setting TBB_DIR=TRUE is needed to use libraries from the tested package,
        # but do not build them from sources; real path to TBBConfig files is not required,
        # because CMake will successfully find installed tested package.
        - cmake {{ cmake_args }} -DTBB_TEST=ON -DTBB_DIR=TRUE -S . -B test_build
        - cmake --build test_build --target {{ tests }} --parallel  # build tests
        - cd test_build
        - ctest -R "{{ ctest_regex }}" --output-on-failure

  - name: tbb4py
    about:
      summary: TBB module for Python
      license_file:
        - LICENSE.txt
        - third-party-programs.txt
    build:
      skip: true  # [py>=312]
      script:
        - set CMAKE_GENERATOR=Ninja      # [win]
        - set CMAKE_GENERATOR_TOOLSET=   # [win]
        - set CMAKE_GENERATOR_PLATFORM=  # [win]
        - cmake {{ cmake_tbb4py_args }} -DTBB4PY_BUILD=ON -DTBB_TEST=OFF -S . -B {{ tbb4py_build_dir }}
        - cmake --build {{ tbb4py_build_dir }} --target python_build --parallel
        - cmake -DCOMPONENT=tbb4py -P {{ tbb4py_build_dir }}/cmake_install.cmake
    requirements:
      build:
        - python                                 # [build_platform != target_platform]
        - cross-python_{{ target_platform }}     # [build_platform != target_platform]
        - {{ compiler('cxx') }}
        - ninja  # [win]
        - make   # [not win]
        - cmake
        - swig
      host:
        - python
        - {{ pin_subpackage('tbb-devel', exact=True) }}
      run:
        - tbb >={{ version }}                            # while python module works with any compatible tbb...
        - python
    test:
      requires:
        - python
        - {{ pin_subpackage('tbb', exact=True) }}        # we want to test with this specific tbb package
      imports:
        - tbb
        - TBB
      commands:
        - python -m TBB -h
        - python -m tbb -h
        - python -m tbb test

about:
  home: https://github.com/oneapi-src/oneTBB
  license: Apache-2.0
  license_file:
    - LICENSE.txt
    - third-party-programs.txt
  summary: High level abstract threading library
  dev_url: https://github.com/oneapi-src/oneTBB
  doc_url: https://software.intel.com/en-us/oneapi-tbb-documentation

extra:
  recipe-maintainers:
    - anton-malakhov
    - jschueller
    - AlexVeprev
    - isaevil
    - ilya-lavrenov
