# -----------------------------------------------------------------------------
# Repack of omniscidb-common, omniscidbe, pyomniscidbe for modin.
# modin 0.15.2 requires pyomniscidbe 5.10.1 with pyarrow==6.0.1.
# The supported versions of arrow on default are 4 and 8. A decision 
# was made to support pyomniscidbe 5.10.1 through a repack rather than
# adding full support of arrow 6 on default.
#
# What to consider for a repack:
# - Tests results should be the same on both the original and repacked package.
# - Configure dependencies of repacked packages according to host run_export 
#   and run dependencies.
# - Isolate the repacked binaries in a dedicated folder.
# ------------------------------------------------------------------------------

{% set version = "5.10.1" %}
{% set buildnum = "0" %}
{% set install_base = "opt/omnisci" %}
{% set pyver = 'py' ~ python | replace('.', '') %}
{% set arrow_ver = "6.0.1" %}

package:
  name: omniscidb-ext
  version: {{ version }}

source:
  - url: https://github.com/omnisci/omniscidb/archive/v{{ version }}.tar.gz
    folder: source
    sha256: a41099704792cf928c9bc8e7f1c6ede1a400b0c024a8d22e8c4daaff329bd451
  - url: https://anaconda.org/conda-forge/omniscidb-common/{{ version }}/download/{{ target_platform }}/omniscidb-common-{{ version }}-h1234567_{{ buildnum }}.tar.bz2
    folder: omniscidb-common
    sha256: 91c7c146381e7a8d866c23ad6d2834844922c2d5e78b5c873a0c7b5e7acbc571
  - url: https://anaconda.org/conda-forge/omniscidbe/{{ version }}/download/{{ target_platform }}/omniscidbe-{{ version }}-h1234567_{{ buildnum }}_cpu.tar.bz2
    folder: omniscidbe
    sha256: cc153d0c9fbde724457a62a661f06d00ce8946d6445e23fb2e9b3e3eccdcdca9
  - url: https://anaconda.org/conda-forge/pyomniscidbe/{{ version }}/download/{{ target_platform }}/pyomniscidbe-{{ version }}-py37h1234567_{{ buildnum }}_cpu.tar.bz2
    folder: 'pyomniscidbe_py37'
    sha256: 962b623f59c9fbc0458ed7133c760fb2d5c25204608d5c6e68a214c7f9457718
  - url: https://anaconda.org/conda-forge/pyomniscidbe/{{ version }}/download/{{ target_platform }}/pyomniscidbe-{{ version }}-py38h1234567_{{ buildnum }}_cpu.tar.bz2
    folder: 'pyomniscidbe_py38'
    sha256: 4a66ce11ee2da9d25c53f3a321dda6b3f3b247f5d4f4d534f1e29940626da63f
  - url: https://anaconda.org/conda-forge/pyomniscidbe/{{ version }}/download/{{ target_platform }}/pyomniscidbe-{{ version }}-py39h1234567_{{ buildnum }}_cpu.tar.bz2
    folder: 'pyomniscidbe_py39'
    sha256: 6ef2f624975c9776bcfde8b38e4fcce50dc33cf5033da11ba7871ad419744f69
  - url: https://anaconda.org/conda-forge/pyomniscidbe/{{ version }}/download/{{ target_platform }}/pyomniscidbe-{{ version }}-py310h1234567_{{ buildnum }}_cpu.tar.bz2
    folder: 'pyomniscidbe_py310'
    sha256: cafa947db4764a29c4679a060c4cb1b4161eb4cda8c1e0eec783bc7a85d6ead3
  # libboost built from: https://github.com/AnacondaRecipes/boost-feedstock/commit/0e783c6e3ddcb3ae857bf3d1ba49f99bebc5b3d1
  - url: https://anaconda.org/cbouss/libboost/1.74.0/download/{{ target_platform }}/libboost-1.74.0-hb88907d_0.tar.bz2
    folder: boost
    sha256: 2fededc174457aac466963ccb7858638ad91299df1addbb5fe22dea7a9a22d8e
  # arrow-cpp built from: https://github.com/AnacondaRecipes/arrow-cpp-feedstock/commit/a9e8b445fa17153813c784491e81ba3a96d52e86
  - url: https://anaconda.org/cbouss/arrow-cpp/{{ arrow_ver }}/download/linux-64/arrow-cpp-{{ arrow_ver }}-py37h60b952e_0.tar.bz2
    folder: 'arrowcpp_py37'
    sha256: 95fbeb9784f152f17b2f1340c86bc53f5a5968d63f2c4a97268e85fd3a25b3d3
  - url: https://anaconda.org/cbouss/arrow-cpp/{{ arrow_ver }}/download/linux-64/arrow-cpp-{{ arrow_ver }}-py38h60b952e_0.tar.bz2
    folder: 'arrowcpp_py38'
    sha256: caf0e8ab0d22d8c39c9c5148ab911d9c854872fe1c333f6aa0e8afed8ed3c991
  - url: https://anaconda.org/cbouss/arrow-cpp/{{ arrow_ver }}/download/linux-64/arrow-cpp-{{ arrow_ver }}-py39h60b952e_0.tar.bz2
    folder: 'arrowcpp_py39'
    sha256: 3aafd13efb31b9f58a35f44e87798e823b5c8ec7669dd35a95f0b5346553253b
  - url: https://anaconda.org/cbouss/arrow-cpp/{{ arrow_ver }}/download/linux-64/arrow-cpp-{{ arrow_ver }}-py310h3098874_0.tar.bz2
    folder: 'arrowcpp_py310'
    sha256: 1e20f1232a66cae0af57074601d0ab87a7f09da8c774f2ba31f97c080dd53fe6
  # pyarrow built from: https://github.com/AnacondaRecipes/pyarrow-feedstock/commit/64de08630f649bb9cc0eb637875a18dc3e08d7dc
  - url: https://anaconda.org/cbouss/pyarrow/{{ arrow_ver }}/download/linux-64/pyarrow-{{ arrow_ver }}-py37h992f0b0_0.tar.bz2
    folder: 'pyarrow_py37'
    sha256: 9a0bea486c461716afa493b5440e51efad2e7aafcc94a6144878c75aafebec61
  - url: https://anaconda.org/cbouss/pyarrow/{{ arrow_ver }}/download/linux-64/pyarrow-{{ arrow_ver }}-py38h992f0b0_0.tar.bz2
    folder: 'pyarrow_py38'
    sha256: 8966c4d110254be6889ab388f05307c329337b6187e0d266a946ffb4535cbf7b
  - url: https://anaconda.org/cbouss/pyarrow/{{ arrow_ver }}/download/linux-64/pyarrow-{{ arrow_ver }}-py39h992f0b0_0.tar.bz2
    folder: 'pyarrow_py39'
    sha256: a216fff5a170090a8c4f88e9c2ed16df2c444c64f03db658641cbf3c0cdce2ad
  - url: https://anaconda.org/cbouss/pyarrow/{{ arrow_ver }}/download/linux-64/pyarrow-{{ arrow_ver }}-py310h468efa6_0.tar.bz2
    folder: 'pyarrow_py310'
    sha256: 19d6522166076c49f218919bf25a9d1fe949cb21052a757db413344faf9da0e8
  - url: https://anaconda.org/conda-forge/libclang-cpp11.1/11.1.0/download/{{ target_platform }}/libclang-cpp11.1-11.1.0-default_ha53f305_1.tar.bz2
    folder: libclang-cpp
    sha256: 24427a0c7d99423199a97c4e7ddec20b1bf6f35bad3fb1d5acea0a65c52e4834

build:
  number: {{ buildnum|int }}
  binary_relocation: false
  detect_binary_files_with_prefix: false
  skip: True  # [not (x86 and linux)]
  runpath_whitelist:
    - $ORIGIN

outputs:
  - name: omniscidb-common
    version: {{ version }}
    script: repack.sh   # [unix]
    build:
      #string: h{{PKG_HASH}}_{{number}}_{{build_ext}}
      skip: True  # [not (x86 and linux)]
      run_exports:
        - {{ pin_subpackage('omniscidb-common',  max_pin='x.x.x') }}
    requirements:
      build:
        # c compiler is specified here to get run constraint pins correct, presumably...
        - {{ compiler('c') }}
    test:
      commands:
        # Test installation
        # doc
        - test -f ${PREFIX}/share/doc/omnisci/LICENSE.md
        # data
        - test -d ${PREFIX}/{{ install_base }}/ThirdParty/gdal-data
        # thrift
        - test -f ${PREFIX}/{{ install_base }}/completion_hints.thrift
        - test -f ${PREFIX}/{{ install_base }}/omnisci.thrift
        - test -f ${PREFIX}/{{ install_base }}/common.thrift
        - test -f ${PREFIX}/{{ install_base }}/QueryEngine/serialized_result_set.thrift
        - test -f ${PREFIX}/{{ install_base }}/QueryEngine/extension_functions.thrift
        # includes
        - test -f ${PREFIX}/include/omnisci/Shared/funcannotations.h
        - test -f ${PREFIX}/include/omnisci/Shared/InlineNullValues.h
        - test -f ${PREFIX}/include/omnisci/Logger/Logger.h
        # QE
        - test -f ${PREFIX}/include/omnisci/QueryEngine/OmniSciTypes.h
        - test -f ${PREFIX}/{{ install_base }}/QueryEngine/RuntimeFunctions.bc
        - test -f ${PREFIX}/{{ install_base }}/QueryEngine/RuntimeFunctions.bc
        - test -f ${PREFIX}/{{ install_base }}/QueryEngine/ExtensionFunctions.ast
        # jar
        - test -f ${PREFIX}/{{ install_base }}/bin/omnisci-utility-{{ version }}.jar
        - test -f ${PREFIX}/{{ install_base }}/bin/omnisci-jdbc-{{ version }}.jar
        - test -f ${PREFIX}/{{ install_base }}/bin/calcite-1.0-SNAPSHOT-jar-with-dependencies.jar
        # Unspecified
        - test -f ${PREFIX}/{{ install_base }}/bin/startomnisci
        - test -f ${PREFIX}/{{ install_base }}/bin/omnisci_insert_sample_data
        #
        - test -f ${PREFIX}/{{ install_base }}/bin/generate_cert
    about:
      home: https://www.heavy.ai/
      license: Apache-2.0
      license_family: APACHE
      license_file: omniscidb-common/share/doc/omnisci/LICENSE.md
      summary: The OmniSci / HeavyDB database common files.
      description: |
        OmniSciDB / HeavyDB is an in-memory, column store, SQL relational database
        that was designed from the ground up to run on GPUs.
      doc_url: https://docs.heavy.ai/
      dev_url: https://github.com/heavyai/heavydb

  - name: omniscidbe
    version: {{ version }}
    script: repack.sh   # [unix]
    build:
      #string: h{{PKG_HASH}}_{{number}}_{{build_ext}}
      skip: True  # [not (x86 and linux)]
      run_exports:
        - {{ pin_subpackage('omniscidbe',  max_pin='x.x.x') }}
      ignore_run_exports:
        - omniscidb-common
        - ncurses

        - gflags
        - gxx_linux-64
        - python

        # ------------------------------------------------------------------------
        # repack of arrow-cpp requires providing the same dependencies
        # the following section is a copy-paste from arrow-cpp's recipe
        # ------------------------------------------------------------------------
        # openssl, glog and zlib are expected by the build system but not
        # used/needed in all the configurations that we build.
        - glog
        - openssl  # [not win]
        - zlib
        - abseil-cpp  # [win]
        # ------------------------------------------------------------------------
    requirements:
      build:
        # c compiler is specified here to get run constraint pins correct, presumably..
        - {{ compiler('c') }}
        - patchelf
      host:
        #- arrow-cpp >=6.0.1,<6.0.2.0a0       # --> repack of arrow-cpp
        #- arrow-cpp-proc 3.* cpu             # --> repack of arrow-cpp
        - blosc >=1.21.0,<2.0a0
        #- boost-cpp >=1.74.0,<1.74.1.0a0     # --> repack of boost-cpp
        - bzip2
        #- double-conversion >=3.2.0,<3.3.0a0 # --> 3.1.5 in default is compatible
        - double-conversion >=3.1.5,<3.3.0a0
        - gxx_linux-64
        - libarchive >=3.5.2,<3.6.0a0
        #- libclang-cpp >=11.1.0,<11.2.0a0    # --> repack of libclang-cpp
        - libgcc-ng >=9.4.0
        - libgdal >=3.4.1,<3.5.0a0
        - libllvm11 >=11.1.0,<11.2.0a0
        - libpng >=1.6.37,<1.7.0a0
        - libstdcxx-ng >=9.4.0
        - libthrift >=0.15.0,<0.15.1.0a0
        - ncurses
        - omniscidb-common >=5.10.1,<5.10.2.0a0
        - openjdk 8.*
        - tbb >=2021.5.0
        - xz
        - zlib
        - snappy
        - xerces-c

        # ------------------------------------------------------------------------
        # repack of libboost requires providing the same dependencies
        # the following section is a copy-paste from boost's recipe
        # ------------------------------------------------------------------------
        - icu  # [unix]
        - libiconv  # [osx]
        - bzip2  # [unix]
        - xz >=5.2.5
        - zstd >=1.4.5
        - zlib >=1.2.11

        # ------------------------------------------------------------------------
        # repack of arrow-cpp requires providing the same dependencies
        # the following section is a copy-paste from arrow-cpp's recipe
        # ------------------------------------------------------------------------
        - abseil-cpp  # [not osx]
        - aws-sdk-cpp
        - boost-cpp
        - brotli
        - bzip2
        # Only required and used on Windows.
        - c-ares  # [win]
        # The binary using this presumably isn't built on Windows.
        - gflags  # [not win]
        - glog
        - grpc-cpp
        - lz4-c
        - numpy
        - openssl
        - orc  # [unix]
        - libprotobuf
        - rapidjson
        - re2
        - snappy
        - thrift-cpp
        - uriparser
        - utf8proc
        - zlib
        - zstd
        - python 
        # ------------------------------------------------------------------------
      run:
        - {{ pin_subpackage('omniscidb-common', exact=True) }}

        #- arrow-cpp >=6.0.1,<6.0.2.0a0       # --> repack of arrow-cpp
        #- arrow-cpp-proc 3.* cpu             # --> repack of arrow-cpp
        - blosc >=1.21.0,<2.0a0
        #- boost-cpp >=1.74.0,<1.74.1.0a0     # --> repack of boost-cpp
        - bzip2
        #- double-conversion >=3.2.0,<3.3.0a0 # --> 3.1.5 in default is compatible
        - double-conversion >=3.1.5,<3.3.0a0
        - gxx_linux-64
        - libarchive >=3.5.2,<3.6.0a0
        #- libclang-cpp >=11.1.0,<11.2.0a0    # --> repack of libclang-cpp
        - libgcc-ng >=9.4.0
        - libgdal >=3.4.1,<3.5.0a0
        - libllvm11 >=11.1.0,<11.2.0a0
        - libpng >=1.6.37,<1.7.0a0
        - libstdcxx-ng >=9.4.0
        - libthrift >=0.15.0,<0.15.1.0a0
        - ncurses
        - openjdk 8.*
        - tbb >=2021.5.0
        - xz
        - zlib
        - zstd

        # ------------------------------------------------------------------------
        # repack of arrow-cpp requires providing the same dependencies
        # the following section is a copy-paste from arrow-cpp's recipe
        # ------------------------------------------------------------------------
        # gflags does have a run_exports, but for some reason conda-build doesn't
        # pick it up.
        - {{ pin_compatible('gflags', max_pin='x.x') }}  # [not win]
        # brotli doesn't have run_exports defined.
        - brotli  # [osx]
        # Set this to some reasonable lower limit.
        - {{ pin_compatible('numpy', lower_bound='1.16') }}
        - utf8proc
        # It's not clear to me, why conda-build detects them, but doesn't create
        # an automatic dependency.
        - zlib  # [win]
        - python
        # ------------------------------------------------------------------------

      # run_constrained:
      #   - arrow-cpp-proc 3.* cpu

    test:
      commands:
        # Test installation
        - test -f ${PREFIX}/lib/libDBEngine.so
        # Verify the activation scripts are in-place.
        {% for state in ["activate", "deactivate"] %}
        - test -f "${PREFIX}/etc/conda/{{ state }}.d/{{ PKG_NAME }}_{{ state }}.sh"
        {% endfor %}
        - |
          source ${PREFIX}/etc/conda/activate.d/{{ PKG_NAME }}_activate.sh
          test -n "${OMNISCI_ROOT_PATH+x}"
          ldd ${PREFIX}/lib/libDBEngine.so
    about:
      home: https://www.heavy.ai/
      license: Apache-2.0
      license_family: APACHE
      license_file: omniscidb-common/share/doc/omnisci/LICENSE.md
      summary: The OmniSci / HeavyDB database
      description: |
        OmniSciDB / HeavyDB is an in-memory, column store, SQL relational database
        that was designed from the ground up to run on GPUs.
      doc_url: https://docs.heavy.ai/
      dev_url: https://github.com/heavyai/heavydb

  - name: pyomniscidbe
    script: repack.sh   # [unix]
    build:
      #string: h{{PKG_HASH}}_{{number}}_{{build_ext}}
      skip: True  # [not (x86 and linux)]
      run_exports:
        - {{ pin_subpackage('pyomniscidbe',  max_pin='x.x.x') }}
      ignore_run_exports:
        # all deps below are from arrow-cpp.
        # somehow pyomniscidbe doesn't seem to need them...
        - python
        - abseil-cpp
        - orc
        - zstd
        - libthrift
        - brotli
        - libprotobuf
        - glog
        - grpc-cpp
        - lz4-c
        - openssl
        - xz
        - bzip2
        - zlib
        - aws-sdk-cpp
        - gflags
        - re2
        - snappy
        - icu
    requirements:
      build:
        # c compiler is specified here to get run constraint pins correct, presumably..
        - {{ compiler('c') }}
      host:
        # ------------------------------------------------------------------------
        # repack of pyomniscidbe requires providing the host dependencies
        # which trigger a run_export.
        # ------------------------------------------------------------------------
        - {{ pin_subpackage('omniscidbe', exact=True) }}
        - libgcc-ng >=9.4.0
        - libstdcxx-ng >=9.4.0
        - libthrift >=0.15.0,<0.15.1.0a0
        # ------------------------------------------------------------------------

        # ------------------------------------------------------------------------
        # repack of libboost requires providing the same dependencies
        # the following section is a copy-paste from boost's recipe
        # ------------------------------------------------------------------------
        - icu  # [unix]
        - libiconv  # [osx]
        - bzip2  # [unix]
        - xz >=5.2.5
        - zstd >=1.4.5
        - zlib >=1.2.11

        # ------------------------------------------------------------------------
        # repack of arrow-cpp requires providing the same dependencies
        # the following section is a copy-paste from arrow-cpp's recipe
        # ------------------------------------------------------------------------
        - abseil-cpp  # [not osx]
        - aws-sdk-cpp
        - boost-cpp
        - brotli
        - bzip2
        # Only required and used on Windows.
        - c-ares  # [win]
        # The binary using this presumably isn't built on Windows.
        - gflags  # [not win]
        - glog
        - grpc-cpp
        - lz4-c
        - numpy
        - openssl
        - orc  # [unix]
        - libprotobuf
        - rapidjson
        - re2
        - snappy
        - thrift-cpp
        - uriparser
        - utf8proc
        - zlib
        - zstd
        - python 
        # ------------------------------------------------------------------------
      run:
        # ------------------------------------------------------------------------
        # repack of pyomniscidbe requires providing the same dependencies
        # the following section is a copy-paste from pyomniscidbe's recipe
        # ------------------------------------------------------------------------
        #- arrow-cpp-proc {{ arrow_proc_version }} {{ build_ext }}  # --> repack of arrow-cpp
        #- pyarrow ={{ arrow_version }}=*{{ build_ext }}            # --> repack of pyarrow
        - python
        - tbb4py
        - {{ pin_subpackage('omniscidbe', exact=True) }}
        - libstdcxx-ng >=9.4.0
        # ------------------------------------------------------------------------

        # ------------------------------------------------------------------------
        # repack of arrow-cpp requires providing the same dependencies
        # the following section is a copy-paste from arrow-cpp's recipe
        # ------------------------------------------------------------------------
        # gflags does have a run_exports, but for some reason conda-build doesn't
        # pick it up.
        - {{ pin_compatible('gflags', max_pin='x.x') }}  # [not win]
        # brotli doesn't have run_exports defined.
        - brotli  # [osx]
        # Set this to some reasonable lower limit.
        - {{ pin_compatible('numpy', lower_bound='1.16') }}
        - utf8proc
        # It's not clear to me, why conda-build detects them, but doesn't create
        # an automatic dependency.
        - zlib  # [win]
        - python
        # ------------------------------------------------------------------------
      run_constrained:
        # ------------------------------------------------------------------------
        # repack of pyarrow 6.0.1 requires preventing installation with other 
        # versions of pyarrow.
        # ------------------------------------------------------------------------
        - pyarrow =={{ arrow_ver }}
    test:
      requires:
        - pytest
        - numpy
        - pandas
      imports:
        - omniscidbe
      source_files:
        - source/Embedded/test/
        - source/Tests/
      commands:
        - python source/Embedded/test/test_fsi.py
        - python source/Embedded/test/test_readcsv.py
        # Commenting test_exceptions as it crashes.
        # The behavior is the same with the original pyomniscidbe package on conda-forge.
        #- pytest -sv source/Embedded/test/test_exceptions.py
    about:
      home: https://www.heavy.ai/
      license: Apache-2.0
      license_family: APACHE
      license_file: omniscidb-common/share/doc/omnisci/LICENSE.md
      summary: The OmniSci / HeavyDB database
      description: |
        OmniSciDB / HeavyDB is an in-memory, column store, SQL relational database
        that was designed from the ground up to run on GPUs.
      doc_url: https://docs.heavy.ai/
      dev_url: https://github.com/heavyai/heavydb