{% set version = "1.51.1" %}

# core package & vendored libs use different version scheme than CPP libs, see
# https://github.com/grpc/grpc/blob/v1.48.1/CMakeLists.txt#L28-L32
# the difference of 22 is empirical and has held since grpc 1.40
{% set core_major = version.split(".")[1] | int - 22 %}
{% set core_version = core_major | string ~ ".0.0" %}

{% set core_libs = ["gpr", "grpc", "grpc_unsecure"] %}
{% set core_cpp_libs = ["grpc++", "grpc++_unsecure"] %}
{% set vendored_libs = ["address_sorting", "upb"] %}
{% set binaries_plugin_langs = [
    "cpp", "csharp", "node", "objective_c", "php", "python", "ruby"
] %}

package:
  name: grpc-split
  version: {{ version }}

source:
  url: https://github.com/grpc/grpc/archive/v{{ version }}.tar.gz
  sha256: b55696fb249669744de3e71acc54a9382bea0dce7cd5ba379b356b12b82d4229
  patches:
    - patches/0001-windows-ssl-lib-names.patch         # [win]
    - patches/0002-fix-win-setup-cmds.patch            # [win]
    - patches/0003-Link-against-grpc.patch
    - patches/0004-force-protoc-executable.patch
    - patches/0005-switch-to-C-17-for-grpcio.patch
    - patches/0006-fix-abseil-setup.patch

build:
  number: 1

outputs:
  - name: libgrpc
    script: build-cpp.sh  # [not win]
    script: build-cpp.bat  # [win]
    build:
      run_exports:
        - {{ pin_subpackage('libgrpc', max_pin='x.x') }}
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - cmake >=3.5.1
        # `protoc` is also used for building
        - libprotobuf
        - ninja
        # We need all host deps also in build for cross-compiling
        - libabseil   # [build_platform != target_platform]
        - c-ares      # [build_platform != target_platform]
        - re2         # [build_platform != target_platform]
        - openssl     # [build_platform != target_platform]
        - zlib        # [build_platform != target_platform]
      host:
        - libabseil
        - c-ares
        - libprotobuf
        - re2
        - openssl
        - zlib
      run:
        - ucrt  # [win]
        - openssl
        - zlib
      run_constrained:
        # make sure we don't co-install with old version of previous package name
        - grpc-cpp ={{ version }}
    test:
      requires:
        # protoc needs compiler; cmake also needs one to run package detection, see
        # https://discourse.cmake.org/t/questions-about-find-package-cli-msvc/6194
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - cmake
        - ninja
        - pkg-config
      files:
        - hello.proto
        - test_grpc.sh
        - test_grpc.bat
      source_files:
        - examples/
      commands:
        # libraries
        {% for each_lib in core_libs + core_cpp_libs + vendored_libs %}
        # presence of shared libs (unix)
        - test -f $PREFIX/lib/lib{{ each_lib }}.so              # [linux]
        - test -f $PREFIX/lib/lib{{ each_lib }}.dylib           # [osx]

        # absence of static libs (unix)
        - test ! -f $PREFIX/lib/lib{{ each_lib }}.a             # [unix]

        # static libs on windows
        - if not exist %LIBRARY_LIB%\{{ each_lib }}.lib exit 1  # [win]
        {% endfor %}

        # binaries
        {% for each_lang in binaries_plugin_langs %}
        - test -f $PREFIX/bin/grpc_{{ each_lang }}_plugin                    # [unix]
        - if not exist %LIBRARY_BIN%\grpc_{{ each_lang }}_plugin.exe exit 1  # [win]
        {% endfor %}

        # pkg-config (no metadata for vendored libs)
        {% for each_lib in core_libs %}
        - pkg-config --print-errors --exact-version "{{ core_version }}" {{ each_lib }}
        {% endfor %}
        {% for each_lib in core_cpp_libs %}
        - pkg-config --print-errors --exact-version "{{ version }}" {{ each_lib }}
        {% endfor %}

        # final CMake test to compile examples
        # partially disabled on unix until libprotobuf-feedstock#127 & 128 are merged
        - ./test_grpc.sh   # [unix]
        - ./test_grpc.bat  # [win]

  # for compatibility with previous behavior of "grpc-cpp"
  - name: grpc-cpp
    build:
      run_exports:
        - {{ pin_subpackage('libgrpc', max_pin='x.x') }}
    requirements:
      host:
        - openssl
        - {{ pin_subpackage('libgrpc', exact=True) }}
      run:
        - {{ pin_subpackage('libgrpc', exact=True) }}
    test:
      commands:
        # tested in other outputs
        - echo "hello"

  - name: grpcio
    script: build-grpcio.sh  # [not win]
    script: build-grpcio.bat  # [win]
    requirements:
      build:
        - python                                 # [build_platform != target_platform]
        - cross-python_{{ target_platform }}     # [build_platform != target_platform]
        - cython                                 # [build_platform != target_platform]
        - libabseil                              # [build_platform != target_platform]
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
      host:
        - python
        - pip
        - setuptools
        - cython
        - six >=1.6.0
        - libabseil
        - {{ pin_subpackage('libgrpc', exact=True) }}
        - c-ares
        - libprotobuf
        - re2
        - openssl
        - pthread-stubs                               # [linux]
      run:
        - python
        - setuptools
        - six >=1.6.0
        - {{ pin_subpackage('libgrpc', exact=True) }}
        - __osx >={{ MACOSX_DEPLOYMENT_TARGET|default("10.9") }}  # [osx and x86_64]
    test:
      files:
        - grpcio_distribtest.py
      requires:
        - pip
      imports:
        - grpc
        - grpc._cython
        - grpc._cython._cygrpc
        - grpc.beta
        - grpc.framework
        - grpc.framework.common
        - grpc.framework.foundation
        - grpc.framework.interfaces
        - grpc.framework.interfaces.base
        - grpc.framework.interfaces.face
      commands:
        - python -m pip check
        - python grpcio_distribtest.py

about:
  home: https://grpc.io/
  license: Apache-2.0
  license_family: APACHE
  license_file: LICENSE
  summary: gRPC - A high-performance, open-source universal RPC framework
  description: |
    gRPC is a modern, open source, high-performance remote procedure call
    (RPC) framework that can run anywhere. gRPC enables client and server
    applications to communicate transparently, and simplifies the building
    of connected systems.

    This package contains the gRPC C++ libraries and header files, as well
    as the code generation plugins.
  doc_url: https://grpc.io/docs/
  dev_url: https://github.com/grpc/grpc

extra:
  recipe-maintainers:
    - pitrou
    - xhochy
    - wesm
    - h-vetinari
    - mariusvniekerk
  feedstock-name: grpc-cpp
