# Copyright (C) 2017 The Android Open Source Project
#
# 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.

import("gn/perfetto.gni")
import("gn/test.gni")

if (perfetto_build_standalone || perfetto_build_with_android) {
  import("//gn/standalone/android.gni")
} else {
  import("//build/config/android/config.gni")
}

# For use_libfuzzer.
if (perfetto_build_standalone || perfetto_build_with_android) {
  import("//gn/standalone/sanitizers/vars.gni")
} else {
  import("//build/config/sanitizers/sanitizers.gni")
}

declare_args() {
  # Only for local development. When true the binaries (perfetto, traced, ...)
  # are monolithic and don't use a common shared library. This is mainly to
  # avoid LD_LIBRARY_PATH dances when testing locally.
  monolithic_binaries = false

  # libunwindstack requires API level 26 or newer.
  should_build_heapprofd =
      (perfetto_build_standalone || perfetto_build_with_android) && is_clang &&
      (is_linux || is_android) &&
      (!is_android || android_api_level >= 26 || perfetto_build_with_android)
}
assert(!monolithic_binaries || !perfetto_build_with_android)

group("all") {
  testonly = true  # allow to build also test targets
  deps = [
    ":perfetto_unittests",
    "src/protozero/protoc_plugin($host_toolchain)",
  ]
  if (perfetto_build_standalone || perfetto_build_with_android) {
    deps += [
      ":perfetto",
      ":perfetto_integrationtests",
      ":traced",
      ":traced_probes",
      ":trigger_perfetto",
      "protos/perfetto/config:merged_config",  # For syntax-checking the proto.
      "protos/perfetto/trace:merged_trace",  # For syntax-checking the proto.
      "src/ipc/protoc_plugin:ipc_plugin($host_toolchain)",
      "tools:protoc_helper",
    ]
    if (perfetto_build_standalone) {
      deps += [
        ":perfetto_benchmarks",
        ":trace_processor",
        "src/profiling/memory:ring_buffer",
        "src/tracing:consumer_api_test",
        "test/configs",
        "tools/ftrace_proto_gen:ftrace_proto_gen",
        "tools/proto_to_cpp",
        "tools/trace_to_text",
        "tools/trace_to_text:trace_to_text_lite_host($host_toolchain)",
      ]
      if (is_linux || is_android) {
        deps += [ "tools/skippy" ]
        deps += [ "tools/busy_threads" ]
        deps += [ "tools/dump_ftrace_stats" ]
      }
      if (is_fuzzer) {
        deps += [ ":fuzzers" ]
      }
    }
  }
}

# TODO(primiano): temporary workaround to:
# 1) Prevent that the UI gets build automatically when doing ninja -C out/xx .
# 2) Avoid breaking the chrome build, that right now depends on "all".
group("default") {
  testonly = true  # allow to build also test targets
  deps = [
    ":all",
  ]
}

if (perfetto_build_standalone) {
  group("ui") {
    deps = [
      "ui",
    ]
  }

  # The trace processor shell executable. An interactive shell that allows to
  # make queries on the trace using the terminal.
  group("trace_processor") {
    deps = [
      "src/trace_processor:trace_processor_shell",
    ]
  }
}

test("perfetto_unittests") {
  deps = [
    "gn:default_deps",
    "gn:gtest_main",
    "src/base:unittests",
    "src/protozero:unittests",
    "src/tracing:unittests",
  ]

  if (perfetto_build_standalone || perfetto_build_with_android) {
    deps += [
      "src/ipc:unittests",
      "src/perfetto_cmd:unittests",
      "src/profiling/memory:ring_buffer_unittests",
      "src/traced/probes:unittests",
      "src/traced/probes/filesystem:unittests",
      "src/traced/probes/ftrace:unittests",
      "src/traced/service:unittests",
      "tools/ftrace_proto_gen:unittests",
      "tools/sanitizers_unittests",
    ]
  }
  if (should_build_heapprofd) {
    # Restrict to clang, as libunwindstack and its dependencies is never
    # built using GCC in the Android tree.
    deps += [ "src/profiling/memory:unittests" ]
  }
  if (perfetto_build_standalone && !is_android) {
    deps += [
      "src/trace_processor:unittests",
    ]
  }
}

if (perfetto_build_standalone || perfetto_build_with_android) {
  test("perfetto_integrationtests") {
    deps = [
      "gn:default_deps",
      "gn:gtest_main",
      "src/traced/probes/ftrace:integrationtests",
      "test:end_to_end_integrationtests",
    ]
    if (perfetto_build_standalone && !is_android) {
      deps += [ "src/trace_processor:integrationtests" ]
    }

    # TODO(fmayer): Fix in process daemons.
    if (should_build_heapprofd && is_android && !start_daemons_for_testing) {
      deps += [ "src/profiling/memory:end_to_end_tests" ]
    }
  }

  if (monolithic_binaries) {
    libperfetto_target_type = "source_set"
  } else {
    libperfetto_target_type = "shared_library"
  }

  target(libperfetto_target_type, "libperfetto") {
    deps = [
      "gn:default_deps",
      "src/traced/probes",
      "src/traced/service",
      "src/tracing:api",
    ]
  }

  # The unprivileged trace daemon that listens for Producer and Consumer
  # connections, handles the coordination of the tracing sessions and owns the
  # log buffers.
  executable("traced") {
    deps = [
      ":libperfetto",
      "gn:default_deps",
      "include/perfetto/traced",
    ]
    sources = [
      "src/traced/service/main.cc",
    ]
  }

  # The unprivileged daemon that is allowed to access tracefs (for ftrace).
  # Registers as a Producer on the traced daemon.
  executable("traced_probes") {
    deps = [
      ":libperfetto",
      "gn:default_deps",
      "include/perfetto/traced",
    ]
    sources = [
      "src/traced/probes/main.cc",
    ]
  }

  # The command line client for Perfetto. Allows to configure / start / stop
  # tracing, acting as a Consumer.
  executable("perfetto") {
    deps = [
      "gn:default_deps",
      "src/perfetto_cmd",
    ]
    sources = [
      "src/perfetto_cmd/main.cc",
    ]
  }

  # Tool to finalize long running traces.
  # This connects to traced as a producer and sends the triggers passed on the
  # commandline. This is a subset of what the perfetto binary can do but we
  # need a separate binary for programs that cannot (for good reason) use the
  # additional functionality (for example starting traces via consumer socket)
  # due to selinux rules.
  executable("trigger_perfetto") {
    deps = [
      "gn:default_deps",
      "src/perfetto_cmd:trigger_perfetto_cmd",
    ]
    sources = [
      "src/perfetto_cmd/trigger_perfetto_main.cc",
    ]
  }

  if (perfetto_build_with_android) {
    executable("trace_to_text") {
      testonly = true
      deps = [
        "gn:default_deps",
        "tools/trace_to_text:full",
      ]
    }

    # This target exports perfetto trace protos in the Android build system,
    # allowing both host and device targets to implement custom parsers based on
    # our protos.
    static_library("perfetto_trace_protos") {
      deps = [
        "protos/perfetto/trace:lite",
      ]
    }

    shared_library("libperfetto_android_internal") {
      deps = [
        "gn:default_deps",
        "src/android_internal",
      ]
    }
  }  # if (perfetto_build_with_android)
}  # if (perfetto_build_standalone || perfetto_build_with_android)

if (perfetto_build_with_embedder) {
  if (build_with_chromium) {
    libperfetto_target_type = "component"
  } else {
    libperfetto_target_type = "source_set"
  }
  target(libperfetto_target_type, "libperfetto") {
    public_configs = [ "gn:public_config" ]
    deps = [
      "src/tracing",
    ]
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [ "//build/config/compiler:no_chromium_code" ]
    public_deps = [
      "include/perfetto/tracing/core",
      "protos/perfetto/trace:zero",
      "protos/perfetto/trace/chrome:zero",
      "protos/perfetto/trace/interned_data:zero",
      "protos/perfetto/trace/track_event:zero",
    ]
  }
}

if (perfetto_build_standalone) {
  executable("perfetto_benchmarks") {
    testonly = true
    deps = [
      "gn:default_deps",
      "src/traced/probes/ftrace:benchmarks",
      "src/tracing:tracing_benchmarks",
      "test:benchmark_main",
      "test:end_to_end_benchmarks",
    ]
  }

  group("fuzzers") {
    testonly = true
    deps = [
      "src/ipc:buffered_frame_deserializer_fuzzer",
      "src/profiling/memory:shared_ring_buffer_fuzzer",
      "src/profiling/memory:shared_ring_buffer_write_fuzzer",
      "src/profiling/memory:unwinding_fuzzer",
      "src/protozero:protozero_decoder_fuzzer",
      "src/trace_processor:trace_processor_fuzzer",
      "src/traced/probes/ftrace:cpu_reader_fuzzer",
      "test:end_to_end_shared_memory_fuzzer",
    ]
  }
}

# WARNING: this builds correctly only when using the generated Android.bp.
#
# This library gets loaded into (and executes in) arbitrary android processes.
# Logging must be non-allocating. This is achieved by defining
# PERFETTO_ANDROID_ASYNC_SAFE_LOG, which needs to be set for all perfetto code
# being compiled for this library. When generating Android.bp, the |cflags|
# entry on this target is sufficient (as all sources are flattened into a
# single bp target). However this is not correctly reflected in the gn
# structure (which is a tree of targets) as the dependencies would not pick
# up the flag (and thus use the wrong logging macro).
#
# This is deemed acceptable as, at the time of writing, there is no interest in
# building this library standalone.
if (perfetto_build_with_android) {
  # TODO(fmayer): Investigate shared library for common pieces.
  shared_library("heapprofd_client") {
    configs -= [ "//gn/standalone:android_liblog" ]
    cflags = [ "-DPERFETTO_ANDROID_ASYNC_SAFE_LOG" ]
    deps = [
      "src/profiling/memory:malloc_hooks",
    ]
  }
}

if (should_build_heapprofd) {
  executable("heapprofd") {
    deps = [
      "gn:default_deps",
      "protos/perfetto/trace:zero",
      "src/base",
      "src/base:unix_socket",
      "src/profiling/memory:daemon",
      "src/profiling/memory:wire_protocol",
      "src/tracing:ipc",
    ]
    sources = [
      "src/profiling/memory/main.cc",
    ]
  }
}