{% set name = "grpc" %}
{% set version = "1.48.2" %}

{% set libext = '.dll' %}  # [win]
{% set libext = '.so' %}  # [linux]
{% set libext = '.dylib' %}  # [osx]

package:
  name: {{ name | lower }}-suite
  version: {{ version }}

source:
  url: https://github.com/{{ name }}/{{ name }}/archive/v{{ version }}.tar.gz
  sha256: bdb8e98145469d58c69ab9f2c9e0bd838c2836a99b5760bc0ebf658623768f52
  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-patch-tests-to-use-passed-CXX_STANDARD.patch
    - patches/0006-switch-to-C-17-for-grpcio.patch
    - patches/0007-fix-abseil-setup.patch

build:
  number: 3
  #skip: true  # [not win]

outputs:
  - name: {{ name }}-cpp
    script: build-cpp.sh   # [not win]
    script: build-cpp.bat  # [win]
    build:
      missing_dso_whitelist:  # [s390x]
        - '$RPATH/ld64.so.1'  # [s390x]
      script_env:
        - LIB_EXT={{libext}}
      run_exports:
        - {{ pin_subpackage('grpc-cpp', max_pin='x.x') }}
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - m2-patch  # [win]
        - patch  # [not win]
        - cmake >=3.5.1
        - pkg-config  # [unix]
        # `protoc` is also used for building
        - libprotobuf {{ libprotobuf }}
        - ninja
      host:
        - abseil-cpp 20230802.0
        - c-ares 1.19.1
        - libprotobuf {{ libprotobuf }}
        - re2 2022.04.01
        - openssl {{ openssl }}
        - zlib {{ zlib }}
      run:
        # Need to pin libprotobuf to the same version used when building,
        # otherwise we may get a different SO ABI version.
        # For example 3.6.1 is ABI-incompatible with 3.6.0...
        - {{ pin_compatible("libprotobuf", min_pin="x.x.x", max_pin="x.x.x") }}
        - zlib >=1.2.13,<1.3.0a0
        - re2
        - openssl 3.*

    # 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
    {% set core_version = "26.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"
    ] %}
    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:
        - tests/include/*
        - test_grpc.sh
        - test_grpc.bat
      commands:
        # libraries
        - ls %LIBRARY_LIB%  # [win]

        {% 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)
        # should work on windows in principle, but our openssl builds don't have a .pc file
        {% for each_lib in core_libs %}
        - pkg-config --print-errors --exact-version "{{ core_version }}" {{ each_lib }}  # [unix]
        {% endfor %}
        {% for each_lib in core_cpp_libs %}
        - pkg-config --print-errors --exact-version "{{ version }}" {{ each_lib }}       # [unix]
        {% endfor %}

        - ./test_grpc.sh   # [unix]
        - ./test_grpc.bat  # [win]

  - name: {{ name }}io
    script: build-grpcio.sh   # [not win]
    script: build-grpcio.bat  # [win]
    build:
      missing_dso_whitelist:  # [s390x]
        - '$RPATH/ld64.so.1'  # [s390x]
    requirements:
      build:
        - {{ compiler('c') }}
        - {{ compiler('cxx') }}
        - m2-patch  # [win]
        - patch  # [not win]
      host:
        - {{ pin_subpackage('grpc-cpp', exact=True) }}
        - python
        - cython 0.29
        - pip
        - pthread-stubs 0.3  # [linux]
        - setuptools
        - wheel >=0.29
        - abseil-cpp 20230802.0
        - c-ares 1.19.1
        - libprotobuf {{ libprotobuf }}
        - re2 2022.04.01
        - openssl {{ openssl }}
        - zlib {{ zlib }}
      run:
        - python
        - {{ pin_subpackage('grpc-cpp', exact=True) }}
        - six >=1.6.0
        - __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:
        - 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
    - mingwandroid
