Support a build flag to optimize binary size

Introduce build flag `enable_perfetto_system_consumer` which allows
excluding only the consumer portion of the SDK's system backend..
When this flag is set to false, the binary size is reduced by ~300 KB.

Chrome on Android isn't allowed to start system tracing sessions, so
there's no point including consumer IPC code in the Chrome binary.
As a result, we don't need consumer_ipc in system backend. Hence, in
those cases, we could save the binary size by not including the
consumer_ipc.

Change-Id: Iccc737c66e9d97f80b2d75f9e6933fec32c807ff
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 8a7ca72..bc8b9b9 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -89,6 +89,7 @@
     "PERFETTO_STDERR_CRASH_DUMP=$enable_perfetto_stderr_crash_dump",
     "PERFETTO_X64_CPU_OPT=$enable_perfetto_x64_cpu_opt",
     "PERFETTO_LLVM_DEMANGLE=$enable_perfetto_llvm_demangle",
+    "PERFETTO_SYSTEM_CONSUMER=$enable_perfetto_system_consumer",
   ]
 
   rel_out_path = rebase_path(gen_header_path, "$root_build_dir")
diff --git a/gn/perfetto.gni b/gn/perfetto.gni
index f59deff..3ee5659 100644
--- a/gn/perfetto.gni
+++ b/gn/perfetto.gni
@@ -245,6 +245,14 @@
 }
 
 declare_args() {
+  # When false, it disables system backend consumer support in the Perfetto SDK.
+  # Saves ~300KB binary size.
+  if (!defined(enable_perfetto_system_consumer)) {
+    enable_perfetto_system_consumer = enable_perfetto_ipc
+  }
+}
+
+declare_args() {
   perfetto_enable_git_rev_version_header =
       enable_perfetto_version_gen && perfetto_build_standalone &&
       !is_perfetto_build_generator
diff --git a/gn/perfetto_integrationtests.gni b/gn/perfetto_integrationtests.gni
index 9657a3d..2001d34 100644
--- a/gn/perfetto_integrationtests.gni
+++ b/gn/perfetto_integrationtests.gni
@@ -19,7 +19,7 @@
   "src/tracing/test:client_api_integrationtests",
 ]
 
-if (enable_perfetto_ipc) {
+if (enable_perfetto_ipc && enable_perfetto_system_consumer) {
   perfetto_integrationtests_targets +=
       [ "src/tracing/test:tracing_integration_test" ]
 }
diff --git a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
index 85f8ad2..c36d04b 100644
--- a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
@@ -45,6 +45,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_STDERR_CRASH_DUMP() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_X64_CPU_OPT() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_LLVM_DEMANGLE() (0)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_SYSTEM_CONSUMER() (1)
 
 // clang-format on
 #endif  // GEN_BUILD_CONFIG_PERFETTO_BUILD_FLAGS_H_
diff --git a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
index e0958d4..54fe273 100644
--- a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
@@ -45,6 +45,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_STDERR_CRASH_DUMP() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_X64_CPU_OPT() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_LLVM_DEMANGLE() (1)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_SYSTEM_CONSUMER() (1)
 
 // clang-format on
 #endif  // GEN_BUILD_CONFIG_PERFETTO_BUILD_FLAGS_H_
diff --git a/src/tracing/BUILD.gn b/src/tracing/BUILD.gn
index e149dd1..b0ce40a 100644
--- a/src/tracing/BUILD.gn
+++ b/src/tracing/BUILD.gn
@@ -181,10 +181,12 @@
       "../../include/perfetto/tracing/core",
       "../base",
       "ipc:common",
-      "ipc/consumer",
       "ipc/producer",
       "ipc/service",
     ]
+    if (enable_perfetto_system_consumer) {
+      deps += [ "ipc/consumer" ]
+    }
     sources = [ "internal/system_tracing_backend.cc" ]
   }
 } else {
diff --git a/src/tracing/internal/system_tracing_backend.cc b/src/tracing/internal/system_tracing_backend.cc
index d63f270..8aacd8d 100644
--- a/src/tracing/internal/system_tracing_backend.cc
+++ b/src/tracing/internal/system_tracing_backend.cc
@@ -19,10 +19,13 @@
 #include "perfetto/base/logging.h"
 #include "perfetto/base/task_runner.h"
 #include "perfetto/ext/tracing/core/tracing_service.h"
-#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
 #include "perfetto/ext/tracing/ipc/default_socket.h"
 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
 
+#if PERFETTO_BUILDFLAG(PERFETTO_SYSTEM_CONSUMER)
+#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
+#endif
+
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 #include "src/tracing/ipc/shared_memory_windows.h"
 #else
@@ -73,10 +76,16 @@
 
 std::unique_ptr<ConsumerEndpoint> SystemTracingBackend::ConnectConsumer(
     const ConnectConsumerArgs& args) {
+#if PERFETTO_BUILDFLAG(PERFETTO_SYSTEM_CONSUMER)
   auto endpoint = ConsumerIPCClient::Connect(GetConsumerSocket(), args.consumer,
                                              args.task_runner);
   PERFETTO_CHECK(endpoint);
   return endpoint;
+#else
+  base::ignore_result(args);
+  PERFETTO_FATAL("System backend consumer support disabled");
+  return nullptr;
+#endif
 }
 
 }  // namespace internal
diff --git a/src/tracing/test/BUILD.gn b/src/tracing/test/BUILD.gn
index 3ab9980..ba0fd7e 100644
--- a/src/tracing/test/BUILD.gn
+++ b/src/tracing/test/BUILD.gn
@@ -59,7 +59,7 @@
   }
 }
 
-if (enable_perfetto_ipc) {
+if (enable_perfetto_ipc && enable_perfetto_system_consumer) {
   perfetto_unittest_source_set("tracing_integration_test") {
     testonly = true
     deps = [