# Copyright 2020 The Pigweed 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
#
#     https://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.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/target_types.gni")
import("$dir_pw_sync/backend.gni")
import("$dir_pw_thread/backend.gni")
import("$dir_pw_unit_test/test.gni")
import("$pw_external_nanopb/nanopb.gni")

config("public") {
  include_dirs = [ "public" ]
  visibility = [ ":*" ]
}

pw_source_set("server_api") {
  public_configs = [ ":public" ]
  public = [
    "public/pw_rpc/nanopb/internal/method.h",
    "public/pw_rpc/nanopb/internal/method_union.h",
    "public/pw_rpc/nanopb/server_reader_writer.h",
  ]
  sources = [
    "method.cc",
    "server_reader_writer.cc",
  ]
  public_deps = [
    ":common",
    "$dir_pw_rpc/raw:server_api",
    "..:config",
    "..:server",
    dir_pw_bytes,
    dir_pw_span,
  ]
  deps = [
    "..:log_config",
    dir_pw_log,
  ]
  allow_circular_includes_from = [ ":common" ]
}

pw_source_set("client_api") {
  public_configs = [ ":public" ]
  public_deps = [
    ":common",
    "..:client",
    dir_pw_function,
  ]
  public = [ "public/pw_rpc/nanopb/client_reader_writer.h" ]
}

pw_source_set("common") {
  public_deps = [
    "..:common",
    dir_pw_bytes,
  ]
  public_configs = [ ":public" ]
  deps = [
    "..:client",
    "..:log_config",
    dir_pw_log,
  ]
  public = [ "public/pw_rpc/nanopb/internal/common.h" ]
  sources = [ "common.cc" ]

  if (dir_pw_third_party_nanopb != "") {
    public_deps += [ "$pw_external_nanopb" ]
  }
}

pw_source_set("test_method_context") {
  public_configs = [ ":public" ]
  public = [
    "public/pw_rpc/nanopb/fake_channel_output.h",
    "public/pw_rpc/nanopb/test_method_context.h",
  ]
  public_deps = [
    ":server_api",
    "..:test_utils",
    dir_pw_assert,
    dir_pw_containers,
  ]
}

pw_source_set("client_testing") {
  public = [ "public/pw_rpc/nanopb/client_testing.h" ]
  public_deps = [
    ":test_method_context",
    "..:client",
    "../raw:client_testing",
  ]
}

pw_source_set("client_server_testing") {
  public = [ "public/pw_rpc/nanopb/client_server_testing.h" ]
  public_deps = [
    ":test_method_context",
    "..:client_server_testing",
  ]
}

pw_source_set("client_server_testing_threaded") {
  public = [ "public/pw_rpc/nanopb/client_server_testing_threaded.h" ]
  public_deps = [
    ":test_method_context",
    "..:client_server_testing_threaded",
  ]
}

pw_source_set("internal_test_utils") {
  public = [ "pw_rpc_nanopb_private/internal_test_utils.h" ]
  public_deps = [ dir_pw_span ]
  if (dir_pw_third_party_nanopb != "") {
    public_deps += [ "$pw_external_nanopb" ]
  }
}

pw_source_set("echo_service") {
  public_configs = [ ":public" ]
  public_deps = [ "..:protos.nanopb_rpc" ]
  sources = [ "public/pw_rpc/echo_service_nanopb.h" ]
}

pw_source_set("client_integration_test") {
  testonly = pw_unit_test_TESTONLY
  public_configs = [ ":public" ]
  public_deps = [
    "$dir_pw_sync:binary_semaphore",
    "..:integration_testing",
    "..:protos.nanopb_rpc",
    dir_pw_assert,
    dir_pw_unit_test,
  ]
  sources = [ "client_integration_test.cc" ]
}

pw_test_group("tests") {
  tests = [
    ":callback_test",
    ":client_call_test",
    ":client_reader_writer_test",
    ":client_server_context_test",
    ":client_server_context_threaded_test",
    ":codegen_test",
    ":echo_service_test",
    ":fake_channel_output_test",
    ":method_lookup_test",
    ":method_test",
    ":method_info_test",
    ":method_union_test",
    ":server_callback_test",
    ":server_reader_writer_test",
    ":serde_test",
    ":stub_generation_test",
    ":synchronous_call_test",
  ]
}

pw_test("callback_test") {
  enable_if = dir_pw_third_party_nanopb != "" &&
              pw_thread_THREAD_BACKEND == "$dir_pw_thread_stl:thread"
  deps = [
    ":client_testing",
    "$dir_pw_sync:binary_semaphore",
    "$dir_pw_thread:non_portable_test_thread_options",
    "$dir_pw_thread:sleep",
    "$dir_pw_thread:yield",
    "$dir_pw_thread_stl:non_portable_test_thread_options",
    "..:client",
    "..:server",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "callback_test.cc" ]
}

pw_test("client_call_test") {
  deps = [
    ":client_api",
    ":internal_test_utils",
    "..:test_protos.nanopb",
    "..:test_utils",
  ]
  sources = [ "client_call_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""

  # TODO: https://pwbug.dev/325509758 - Doesn't work on the Pico yet; hangs
  # indefinitely.
  if (pw_build_EXECUTABLE_TARGET_TYPE == "pico_executable") {
    enable_if = false
  }
}

pw_test("client_reader_writer_test") {
  deps = [
    ":client_api",
    ":client_testing",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "client_reader_writer_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("client_server_context_test") {
  deps = [
    ":client_api",
    ":client_server_testing",
    "$dir_pw_sync:mutex",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "client_server_context_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != "" && pw_sync_MUTEX_BACKEND != ""
}

_stl_threading_and_nanopb_enabled =
    pw_thread_THREAD_BACKEND == "$dir_pw_thread_stl:thread" &&
    pw_sync_BINARY_SEMAPHORE_BACKEND != "" && pw_sync_MUTEX_BACKEND != "" &&
    dir_pw_third_party_nanopb != ""

pw_test("client_server_context_threaded_test") {
  deps = [
    ":client_api",
    ":client_server_testing_threaded",
    "$dir_pw_sync:binary_semaphore",
    "$dir_pw_sync:mutex",
    "$dir_pw_thread:non_portable_test_thread_options",
    "$dir_pw_thread_stl:non_portable_test_thread_options",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "client_server_context_threaded_test.cc" ]
  enable_if = _stl_threading_and_nanopb_enabled
}

pw_test("codegen_test") {
  deps = [
    ":client_api",
    ":internal_test_utils",
    ":server_api",
    ":test_method_context",
    "..:test_protos.nanopb_rpc",
    "..:test_utils",
  ]
  sources = [ "codegen_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("fake_channel_output_test") {
  deps = [
    ":server_api",
    ":test_method_context",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "fake_channel_output_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("method_test") {
  deps = [
    ":internal_test_utils",
    ":server_api",
    "$dir_pw_containers",
    "..:test_protos.nanopb",
    "..:test_utils",
  ]
  sources = [ "method_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("method_info_test") {
  deps = [
    "..:common",
    "..:test_protos.nanopb_rpc",
    "..:test_utils",
  ]
  sources = [ "method_info_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("method_lookup_test") {
  deps = [
    ":server_api",
    ":test_method_context",
    "..:test_protos.nanopb_rpc",
    "..:test_utils",
    "../raw:test_method_context",
  ]
  sources = [ "method_lookup_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("method_union_test") {
  deps = [
    ":internal_test_utils",
    ":server_api",
    "..:test_protos.nanopb",
    "..:test_utils",
  ]
  sources = [ "method_union_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("echo_service_test") {
  deps = [
    ":echo_service",
    ":server_api",
    ":test_method_context",
  ]
  sources = [ "echo_service_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("serde_test") {
  deps = [
    ":server_api",
    "..:test_protos.nanopb",
  ]
  sources = [ "serde_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("server_callback_test") {
  deps = [
    ":server_api",
    ":test_method_context",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "server_callback_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("server_reader_writer_test") {
  deps = [
    ":server_api",
    ":test_method_context",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "server_reader_writer_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("stub_generation_test") {
  deps = [ "..:test_protos.nanopb_rpc" ]
  sources = [ "stub_generation_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != ""
}

pw_test("synchronous_call_test") {
  deps = [
    ":test_method_context",
    "$dir_pw_work_queue:pw_work_queue",
    "$dir_pw_work_queue:stl_test_thread",
    "$dir_pw_work_queue:test_thread",
    "..:synchronous_client_api",
    "..:test_protos.nanopb_rpc",
  ]
  sources = [ "synchronous_call_test.cc" ]
  enable_if = dir_pw_third_party_nanopb != "" &&
              pw_sync_TIMED_THREAD_NOTIFICATION_BACKEND != ""

  # TODO: https://pwbug.dev/325509758 - Doesn't work on the Pico yet; hangs
  # indefinitely.
  if (pw_build_EXECUTABLE_TARGET_TYPE == "pico_executable") {
    enable_if = false
  }
}
