{% set name = "grpc-cpp" %}
{% set version = "1.43.0" %}

# It is not currently possible to compile grpc-cpp on Windows in shared mode:
# _h_env\Library\include\google/protobuf/io/coded_stream.h(1250):
#   warning C4251: 'google::protobuf::io::CodedOutputStream::default_serialization_deterministic_': struct 'std::atomic<bool>'
#   needs to have dll-interface to be used by clients of class 'google::protobuf::io::CodedOutputStream'
# LINK : fatal error LNK1181: cannot open input file 'gpr.lib'
{% set static_build = 'yes' %}  # [win]
{% set static_build = 'no' %}  # [not win]

{% if static_build == 'yes' %}
{% set libext = '.lib' %}  # [win]
{% set libext = '.a' %}  # [not win]
{% else %}
{% set libext = '.dll' %}  # [win]
{% set libext = '.so' %}  # [linux]
{% set libext = '.dylib' %}  # [osx]
{% endif %}

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

source:
  url: https://github.com/grpc/grpc/archive/v{{ version }}.tar.gz
  sha256: 9647220c699cea4dafa92ec0917c25c7812be51a18143af047e20f3fb05adddc
  patches:
    - force-protoc-executable.patch
  # You can use module version of re2 if you add this and chage build.sh:
  # - git_url: https://github.com/google/re2
  #   git_ref: aecba11114cf1fac5497aeb844b6966106de3eb6
  #   folder: third_party/re2

build:
  number: 0
  # Anaconda's s390x does not contain depdendencies for abseil-cpp or re2, this package is skipped for s390x at this time.
  # Latest protobuf recipe skips win32, so this package does as well.
  skip: True  # [win32 or s390x]
{% if static_build == 'yes' %}
  ignore_run_exports:
    - abseil-cpp
    - c-ares
    - re2
    - openssl
    - zlib
{% else %}
  run_exports:
    - {{ pin_subpackage('grpc-cpp', max_pin='x.x') }}
{% endif %}
  script_env:
    - STATIC_BUILD={{ static_build }}

requirements:
  build:
    - {{ compiler('c') }}
    - {{ compiler('cxx') }}
    - m2-patch  # [win]
    - patch  # [not win]
    - cmake
    - pkg-config  # [unix]
    # `protoc` is also used for building
    - libprotobuf >=3.19
    - ninja
    # We need all host deps also in build for cross-compiling
    - abseil-cpp  # [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:
    - abseil-cpp
    - c-ares
    - libprotobuf >=3.19
    - re2
    - openssl
    - 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
    - re2

test:
  files:
    - tests/include/*
  commands:
    - grpc_cpp_plugin < /dev/null  # [not win]
    - grpc_cpp_plugin < NUL  # [win]

    - test -f $PREFIX/lib/libgrpc{{ libext }}  # [not win]
    - test -f $PREFIX/lib/libgrpc_unsecure{{ libext }}  # [not win]
    - test -f $PREFIX/lib/libgrpc++{{ libext }}  # [not win]
    - test -f $PREFIX/lib/libgrpc++_unsecure{{ libext }}  # [not win]

    - if not exist %PREFIX%\\Library\\lib\\address_sorting{{ libext }} exit 1  # [win]
    - if not exist %PREFIX%\\Library\\lib\\gpr{{ libext }} exit 1  # [win]
    - if not exist %PREFIX%\\Library\\lib\\grpc{{ libext }} exit 1  # [win]
    - if not exist %PREFIX%\\Library\\lib\\grpc++{{ libext }} exit 1  # [win]

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
