{% set version = "1.62.2" %}

# 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 23 is empirical and most recently changed with 1.54
{% set core_major = version.split(".")[1] | int - 23 %}
{% 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"] %}
{% 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.replace(".pre", "-pre") }}.tar.gz
  sha256: e5d5e0dd96fe9452fe24cc8c827381dca484c54d171fb512a198025fec81a3c8
  patches:
    - patches/0001-windows-ssl-lib-names.patch
    - patches/0002-fix-win-setup-cmds.patch
    - 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
    # mark c-ares, re2 & zlib as private, so they don't need to be found
    # at runtime just to link to grp, see grpc/grpc#30838;
    # not much point in marking baselibs or vendored libs (e.g. upb) as private
    - patches/0007-mark-linkage-of-c-ares-re2-zlib-as-private.patch
    - patches/0008-link-grpc-_unsecure-to-grpc.patch
    # see https://github.com/grpc/grpc/issues/35794
    - patches/0009-fix-missing-DLL-exports-in-vendored-upb.patch
    # fix symbol visibility regression on windows
    - patches/0010-for-main-targets-set-_DLL_EXPORTS-when-building-and-.patch

build:
  number: 0

requirements:
  build:
    - patch # [unix]
    - m2-patch # [win]

outputs:
  - name: libgrpc
    script: build-cpp.sh  # [not win]
    script: build-cpp.bat  # [win]
    build:
      run_exports:
        - {{ pin_subpackage('libgrpc', max_pin='x.x') }}
        # grpc requires C11 aligned_alloc as of 1.56;
        # ensure that all dependent packages reflect that to
        # avoid the solver getting stuck in no-man's-land.
        - __osx >={{ MACOSX_DEPLOYMENT_TARGET }}  # [osx and x86_64]
      missing_dso_whitelist:  # [s390x]
        - $RPATH/ld64.so.1    # [s390x]
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - cmake
        - ninja-base
        # During cross-compilation, we need to build the grpc_cpp_plugin for the
        # architecture of the build agent (where it will be executed afterwards),
        # thus we need the respective host dependencies also in the build env.
        - c-ares        # [build_platform != target_platform]
        - libabseil     # [build_platform != target_platform]
        - libprotobuf   # [build_platform != target_platform]
        - openssl       # [build_platform != target_platform]
        - re2           # [build_platform != target_platform]
        - zlib          # [build_platform != target_platform]
      host:
        - libabseil 20240116.2
        - c-ares 1.19.1
        - libprotobuf 4.25.3
        - re2 2022.04.01
        - openssl {{ openssl }}
        - zlib {{ zlib }}
      run:
        - __osx >={{ MACOSX_DEPLOYMENT_TARGET|default("10.9") }}  # [osx and x86_64]
      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-base
        - pkg-config
      files:
        - cmake_test/
        - 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
        - test -f $PREFIX/lib/lib{{ each_lib }}.so              # [linux]
        - test -f $PREFIX/lib/lib{{ each_lib }}.dylib           # [osx]
        - if not exist %LIBRARY_BIN%\{{ each_lib }}.dll exit 1  # [win]
        - if not exist %LIBRARY_LIB%\{{ each_lib }}.lib exit 1  # [win]

        # absence of static libs (unix)
        - test ! -f $PREFIX/lib/lib{{ each_lib }}.a             # [unix]
        {% 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)
        # a number of dependencies do not have pkgconfig files on windows. e.g. re2
        {% for each_lib in core_libs %}
        - pkg-config --print-errors --exact-version "{{ core_version }}" {{ each_lib }} # [not win]
        {% endfor %}
        {% for each_lib in core_cpp_libs %}
        - pkg-config --print-errors --exact-version "{{ version }}" {{ each_lib }} # [not win]
        {% endfor %}

        # CMake test: compile upstream example
        - ./test_grpc.sh   # [unix]
        - ./test_grpc.bat  # [win]

        # test availability of symbol needed by arrow (esp. on windows)
        - cd cmake_test
        - cmake -GNinja -DCMAKE_CXX_STANDARD=17 $CMAKE_ARGS .   # [unix]
        - cmake -GNinja -DCMAKE_CXX_STANDARD=17 %CMAKE_ARGS% .  # [win]
        - cmake --build .

  - name: grpcio
    script: build-grpcio.sh  # [not win]
    script: build-grpcio.bat  # [win]
    build:
      missing_dso_whitelist:  # [s390x]
        - $RPATH/ld64.so.1    # [s390x]
    requirements:
      build:
        - python                                 # [build_platform != target_platform]
        - cross-python_{{ target_platform }}     # [build_platform != target_platform]
        - cython <3                              # [build_platform != target_platform]
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
      host:
        - python
        - pip
        - setuptools
        - cython <3
        - {{ pin_subpackage('libgrpc', exact=True) }}
        - pthread-stubs  0.3                     # [linux]
      run:
        - python
        - {{ pin_subpackage('libgrpc', exact=True) }}
        - __osx >={{ MACOSX_DEPLOYMENT_TARGET|default("10.9") }}  # [osx and x86_64]
    test:
      source_files:
        - examples/
      files:
        - grpcio_distribtest.py
        - test_grpcio_helloworld.py
      requires:
        - pip
        - protobuf
      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
        # the following test is flaky on pypy
        {% if not (aarch64 or ppc64le) or python_impl == "cpython" %}
        # test actual RPC pattern (client & server); other tests did not catch
        # https://github.com/conda-forge/grpc-cpp-feedstock/issues/281
        - cd examples/python/helloworld
        # copy test defined in feedstock to where it needs to run
        - cp ../../../test_grpcio_helloworld.py .
        - python test_grpcio_helloworld.py
        {% endif %}

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
    - jjerphan
    - JohanMabille
  feedstock-name: grpc-cpp
