%YAML 1.2
--- |
  # This file has been automatically generated from a template file.
  # Please make modifications to `templates/gRPC-C++.podspec.template`
  # instead. This file can be regenerated from the template by running
  # `tools/buildgen/generate_projects.sh`.

  # gRPC C++ CocoaPods podspec
  #
  # Copyright 2017 gRPC authors.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  <%
  lib_maps = {lib.name: lib for lib in libs}
  filegroup_maps = {filegroup.name: filegroup for filegroup in filegroups}

  def ruby_multiline_list(files, indent):
    return (',\n' + indent*' ').join('\'%s\'' % f for f in files)

  def is_absl_lib(target_name):
    return target_name.startswith("absl/")

  def get_absl_spec_name(label):
    # e.g. absl/apple:banana -> abseil/apple/banana
    return "abseil/" + label[5:].replace(":", "/")

  def lib_and_transitive_deps(lib):
    return list(sorted(set({lib} | set(lib_maps[lib].transitive_deps))))

  def non_abseil_lib_and_transitive_deps(lib):
    return [l for l in lib_and_transitive_deps(lib) if not is_absl_lib(l)]

  def list_abseil_specs(lib):
    # This returns a list of abseil specs which the given lib and
    # its non-abseil transitive dependencies depend on.
    # As a result, internal abseil libraries are excluded from the result.
    absl_specs = set()
    for lib_name in lib_and_transitive_deps(lib):
      if is_absl_lib(lib_name): continue
      for dep in lib_maps[lib_name].deps:
        if is_absl_lib(dep):
          absl_specs.add(get_absl_spec_name(dep))
    return list(sorted(absl_specs))

  def list_lib_files(lib, fields):
    files = set()
    for lib_name in non_abseil_lib_and_transitive_deps(lib):
      lib = lib_maps[lib_name]
      for field in fields:
        files.update(lib.get(field, []))
    return list(sorted(files))

  def filter_grpcpp(files):
    return sorted([file for file in files if not file.startswith("include/grpc++")])

  # ObjectiveC doesn't use c-ares so we don't need address_sorting files at all
  address_sorting_unwanted_files = list_lib_files("address_sorting", ("public_headers", "headers", "src"))

  # ObjectiveC needs to obtain re2 explicitly unlike other languages; TODO @donnadionne make ObjC more consistent with others
  grpc_private_files = list(sorted((set(list_lib_files("grpc", ("headers", "src"))) - set(address_sorting_unwanted_files)) | set(list_lib_files("re2", ("headers", "src")))))
  grpc_public_headers = list(sorted((set(list_lib_files("grpc", ("public_headers",))) - set(address_sorting_unwanted_files)) | set(list_lib_files("re2", ("public_headers",)))))
  grpc_private_headers = list(sorted((set(list_lib_files("grpc", ("headers",))) - set(address_sorting_unwanted_files)) | set(list_lib_files("re2", ("headers",)))))

  # TODO(jtattermusch): build.yaml no longer has filegroups, so the files here are just hand-listed
  # This template shouldn't be touching the filegroups anyway, so this is only a bit more fragile.
  grpcpp_proto_files = ['include/grpcpp/impl/codegen/config_protobuf.h',
                        'include/grpcpp/impl/codegen/proto_buffer_reader.h',
                        'include/grpcpp/impl/codegen/proto_buffer_writer.h',
                        'include/grpcpp/impl/codegen/proto_utils.h']

  grpcpp_private_files = filter_grpcpp(
    set(list_lib_files("grpc++", ("headers", "src")))
    - set(grpc_private_files)
    # We exclude proto related files in this particular podspec so that we can provide a protobuf-independent lib
    - set(grpcpp_proto_files)
    - set(address_sorting_unwanted_files)
  )
  grpcpp_private_headers = filter_grpcpp(
    set(list_lib_files("grpc++", ("headers",)))
    - set(grpc_private_headers)
    # We exclude proto related files in this particular podspec so that we can provide a protobuf-independent lib
    - set(grpcpp_proto_files)
    - set(address_sorting_unwanted_files)
  )
  grpcpp_public_headers = filter_grpcpp(
    set(list_lib_files("grpc++", ("public_headers",)))
    - set(grpc_public_headers)
    # We exclude proto related files in this particular podspec so that we can provide a protobuf-independent lib
    - set(grpcpp_proto_files)
    - set(address_sorting_unwanted_files)
  )
  grpcpp_abseil_specs = list_abseil_specs("grpc++")
  %>
  Pod::Spec.new do |s|
    s.name     = 'gRPC-C++'
    # TODO (mxyan): use version that match gRPC version when pod is stabilized
    version = '${settings.version}'
    s.version  = version
    s.summary  = 'gRPC C++ library'
    s.homepage = 'https://grpc.io'
    s.license  = 'Apache License, Version 2.0'
    s.authors  = { 'The gRPC contributors' => 'grpc-packages@google.com' }

    s.source = {
      :git => 'https://github.com/grpc/grpc.git',
      :tag => "v#{version}",
    }

    s.ios.deployment_target = '9.0'
    s.osx.deployment_target = '10.10'
    s.tvos.deployment_target = '10.0'

    s.requires_arc = false

    name = 'grpcpp'
    # Use `grpcpp` as framework name so that `#include <grpcpp/xxx.h>` works when built as
    # framework.
    s.module_name = name

    # Add include prefix `grpcpp` so that `#include <grpcpp/xxx.h>` works when built as static
    # library.
    s.header_dir = name

    s.pod_target_xcconfig = {
      'HEADER_SEARCH_PATHS' => '"$(inherited)" "$(PODS_TARGET_SRCROOT)/include"',
      'USER_HEADER_SEARCH_PATHS' => '"$(PODS_TARGET_SRCROOT)"',
      'GCC_PREPROCESSOR_DEFINITIONS' => '"$(inherited)" "COCOAPODS=1"',
      'CLANG_WARN_STRICT_PROTOTYPES' => 'NO',
      'CLANG_WARN_DOCUMENTATION_COMMENTS' => 'NO',

      # If we don't set these two settings, `include/grpc/support/time.h` and
      # `src/core/lib/support/string.h` shadow the system `<time.h>` and `<string.h>`, breaking the
      # build.
      'USE_HEADERMAP' => 'NO',
      'ALWAYS_SEARCH_USER_PATHS' => 'NO',
    }

    s.libraries = 'c++'
    s.compiler_flags = '-Wno-comma -Wno-unreachable-code -Wno-shorten-64-to-32'

    s.default_subspecs = 'Interface', 'Implementation'

    # Certificates, to be able to establish TLS connections:
    s.resource_bundles = { 'gRPCCertificates-Cpp' => ['etc/roots.pem'] }

    s.header_mappings_dir = 'include/grpcpp'

    s.subspec 'Interface' do |ss|
      ss.header_mappings_dir = 'include/grpcpp'

      ss.source_files = ${ruby_multiline_list(grpcpp_public_headers, 22)}
    end

    s.subspec 'Implementation' do |ss|
      ss.header_mappings_dir = '.'
      ss.dependency "#{s.name}/Interface", version
      ss.dependency 'gRPC-Core', version
      abseil_version = '1.20210324.0'
      % for abseil_spec in grpcpp_abseil_specs:
      ss.dependency '${abseil_spec}', abseil_version
      % endfor

      ss.source_files = ${ruby_multiline_list(sorted(grpcpp_private_files + grpc_private_headers), 22)}

      ss.private_header_files = ${ruby_multiline_list(sorted(grpcpp_private_headers + grpc_private_headers), 30)}
    end

    s.subspec 'Protobuf' do |ss|
      ss.header_mappings_dir = 'include/grpcpp'
      ss.dependency "#{s.name}/Interface", version

      ss.source_files = ${ruby_multiline_list(grpcpp_proto_files, 22)}
    end

    s.subspec 'Cronet-Interface' do |ss|
      ss.header_mappings_dir = 'include/grpcpp'
      ss.public_header_files = "include/grpcpp/security/cronet_credentials.h",
                               "include/grpcpp/security/cronet_credentials_impl.h"
      ss.source_files = "include/grpcpp/security/cronet_credentials.h",
                        "include/grpcpp/security/cronet_credentials_impl.h"
    end

    s.subspec 'Cronet-Implementation' do |ss|
      ss.header_mappings_dir = '.'
      ss.dependency "#{s.name}/Cronet-Interface", version
      ss.dependency "#{s.name}/Implementation", version

      ss.dependency 'gRPC-Core/Cronet-Implementation', version

      ss.source_files = "src/cpp/client/cronet_credentials.cc"
    end

    s.prepare_command = <<-END_OF_COMMAND
      find src/core -type f \\( -path '*.h' -or -path '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include <openssl/(.*)>;#if COCOAPODS==1\\\n  #include <openssl_grpc/\\1>\\\n#else\\\n  #include <openssl/\\1>\\\n#endif;g'
      find third_party/upb/ -type f \\( -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "third_party/(.*)";#if COCOAPODS==1\\\n  #include  "third_party/upb/third_party/\\1"\\\n#else\\\n  #include  "third_party/\\1"\\\n#endif;g'
      find src/core/ src/cpp/ third_party/upb/ -type f \\( -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "upb/(.*)";#if COCOAPODS==1\\\n  #include  "third_party/upb/upb/\\1"\\\n#else\\\n  #include  "upb/\\1"\\\n#endif;g'
      find src/core/ src/cpp/ third_party/upb/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
      find src/core/ src/cpp/ third_party/upb/ -type f \\( -name '*.h' -or -name '*.c' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "(.*).upb.h";#if COCOAPODS==1\\\n  #include  "src/core/ext/upb-generated/\\1.upb.h"\\\n#else\\\n  #include  "\\1.upb.h"\\\n#endif;g'
      find src/core/ src/cpp/ third_party/upb/ -type f \\( -name '*.h' -or -name '*.c' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "(.*).upbdefs.h";#if COCOAPODS==1\\\n  #include  "src/core/ext/upbdefs-generated/\\1.upbdefs.h"\\\n#else\\\n  #include  "\\1.upbdefs.h"\\\n#endif;g'
      find src/core/ src/cpp/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
      find third_party/re2/re2/ third_party/re2/util/ -type f \\( -name '*.h' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "re2/(.*)";#if COCOAPODS==1\\\n  #include  "third_party/re2/re2/\\1"\\\n#else\\\n  #include  "re2/\\1"\\\n#endif;g;s;#include "util/(.*)";#if COCOAPODS==1\\\n  #include  "third_party/re2/util/\\1"\\\n#else\\\n  #include  "util/\\1"\\\n#endif;g'
      find src/core/ -type f \\( -name '*.h' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "re2/(.*)";#if COCOAPODS==1\\\n  #include  "third_party/re2/re2/\\1"\\\n#else\\\n  #include  "re2/\\1"\\\n#endif;g'
      find src/core/ third_party/re2/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
      find src/core/ -type f \\( -name '*.h' -or -name '*.cc' \\) -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;#include "xxhash.h";#if COCOAPODS==1\\\n #include "third_party/xxhash/xxhash.h"\\\n#else\\\n #include "xxhash.h"\\\n#endif;g'
      find third_party/xxhash  -type f -name xxhash.h -print0 | xargs -0 -L1 sed -E -i'.grpc_back' 's;@param([^,]*),;@param\\1 ,;g'
      find src/core/ third_party/xxhash/ -type f -name '*.grpc_back' -print0 | xargs -0 rm
    END_OF_COMMAND
  end