# 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")

# For use_libfuzzer.
if (!build_with_chromium) {
  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
}
assert(!monolithic_binaries || !build_with_android)

group("all") {
  testonly = true  # allow to build also test targets
  deps = [
    ":perfetto_unittests",
    "src/protozero/protoc_plugin($host_toolchain)",
  ]
  if (!build_with_chromium) {
    deps += [
      ":perfetto",
      ":perfetto_benchmarks",
      ":perfetto_integrationtests",
      ":traced",
      ":traced_probes",
      "protos/perfetto/config:merged_config",  # For syntax-checking the proto.
      "src/ipc/protoc_plugin:ipc_plugin($host_toolchain)",
      "test/configs",
      "tools:protoc_helper",
      "tools/ftrace_proto_gen:ftrace_proto_gen",
      "tools/proto_to_cpp",
    ]
    if (!build_with_android) {
      deps += [
        ":trace_processor",
        "tools/trace_to_text",
      ]
    }
    if (is_linux || is_android) {
      deps += [ "tools/skippy" ]
    }
    if (is_linux) {
      deps += [ "tools:pipestats" ]
    }
    if (use_libfuzzer) {
      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 (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",
    ]
  }
}

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

  if (!build_with_chromium) {
    deps += [
      "src/ipc:unittests",
      "src/perfetto_cmd:unittests",
      "src/profiling/memory:unittests",
      "src/traced/probes:unittests",
      "src/traced/probes/filesystem:unittests",
      "src/traced/probes/ftrace:unittests",
      "tools/ftrace_proto_gen:unittests",
      "tools/sanitizers_unittests",
    ]
  }
  if (build_standalone && !is_android) {
    deps += [ "src/trace_processor:unittests" ]
  }
}

if (!build_with_chromium) {
  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",
    ]
  }

  executable("perfetto_integrationtests") {
    testonly = true
    deps = [
      "gn:default_deps",
      "gn:gtest_main",
      "src/traced/probes/ftrace:integrationtests",
      "test:end_to_end_integrationtests",
    ]
    if (build_standalone && !is_android) {
      deps += [ "src/trace_processor:integrationtests" ]
    }
    if (build_with_android) {
      cflags = [ "-DPERFETTO_BUILD_WITH_ANDROID" ]
    }
  }

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

  target(libtraced_shared_target_type, "libtraced_shared") {
    deps = [
      "gn:default_deps",
      "src/traced/probes",
      "src/traced/service",
    ]
  }

  # 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 = [
      ":libtraced_shared",
      "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 = [
      ":libtraced_shared",
      "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",
    ]
    if (is_android) {
      deps += [ "src/base:android_task_runner" ]
    }
    if (build_with_android) {
      cflags = [ "-DPERFETTO_BUILD_WITH_ANDROID" ]
      libs = [
        "binder",
        "services",
        "utils",
      ]
    }
  }

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

    # 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",
      ]
    }
  }
}

if (build_with_chromium) {
  component("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",
    ]
  }
}

if (use_libfuzzer && !build_with_chromium) {
  group("fuzzers") {
    testonly = true
    deps = [
      "src/ipc:buffered_frame_deserializer_fuzzer",
      "src/traced/probes/ftrace:cpu_reader_fuzzer",
      "test:end_to_end_shared_memory_fuzzer",
    ]
  }
}