Build example_custom_data_source.cc with gn

With this change, example_custom_data_source.cc can also be built with
gn. It will continue to work with amalgmated SDK against the perfetto.h
source.

Change-Id: Iacb38592de70d55f22913fcbb68fb81d889517a7
diff --git a/examples/sdk/BUILD.gn b/examples/sdk/BUILD.gn
index a801d29..8a49c88 100644
--- a/examples/sdk/BUILD.gn
+++ b/examples/sdk/BUILD.gn
@@ -20,10 +20,21 @@
     "trace_categories.cc",
     "trace_categories.h",
   ]
-  cflags = [ "-DPERFETTO_SDK_EXAMPLE_USE_INTERNAL_HEADERS" ]
+  defines = [ "PERFETTO_SDK_EXAMPLE_USE_INTERNAL_HEADERS" ]
   testonly = true
   deps = [
     "../..:libperfetto_client_experimental",
     "../../gn:default_deps",
   ]
 }
+
+executable("example_custom_data_source") {
+  sources = [ "example_custom_data_source.cc" ]
+  defines = [ "PERFETTO_SDK_EXAMPLE_USE_INTERNAL_HEADERS" ]
+  testonly = true
+  deps = [
+    "../..:libperfetto_client_experimental",
+    "../../gn:default_deps",
+    "../../src/base",
+  ]
+}
diff --git a/examples/sdk/example_custom_data_source.cc b/examples/sdk/example_custom_data_source.cc
index 2d475c1..c39c9f5 100644
--- a/examples/sdk/example_custom_data_source.cc
+++ b/examples/sdk/example_custom_data_source.cc
@@ -16,11 +16,25 @@
 
 // This example demonstrates a custom tracing data source.
 
+// This source file can be built in two ways:
+// 1. As part of the regular GN build, against standard includes.
+// 2. To test that the amalgmated SDK works, against the perfetto.h source.
+#if defined(PERFETTO_SDK_EXAMPLE_USE_INTERNAL_HEADERS)
+#include "perfetto/tracing.h"
+#include "perfetto/tracing/core/data_source_descriptor.h"
+#include "perfetto/tracing/core/trace_config.h"
+#include "perfetto/tracing/data_source.h"
+#include "perfetto/tracing/tracing.h"
+#include "protos/perfetto/trace/test_event.pbzero.h"
+#else
 #include <perfetto.h>
+#endif
 
 #include <fstream>
 #include <thread>
 
+namespace {
+
 // The definition of our custom data source. Instances of this class will be
 // automatically created and destroyed by Perfetto.
 class CustomDataSource : public perfetto::DataSource<CustomDataSource> {
@@ -35,9 +49,6 @@
   void OnStop(const StopArgs&) override {}
 };
 
-PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(CustomDataSource);
-PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(CustomDataSource);
-
 void InitializePerfetto() {
   perfetto::TracingInitArgs args;
   // The backends determine where trace events are recorded. For this example we
@@ -81,12 +92,21 @@
   // Note: To save memory with longer traces, you can tell Perfetto to write
   // directly into a file by passing a file descriptor into Setup() above.
   std::ofstream output;
-  output.open("example_custom_data_source.pftrace",
-              std::ios::out | std::ios::binary);
-  output.write(&trace_data[0], trace_data.size());
+  const char* filename = "example_custom_data_source.pftrace";
+  output.open(filename, std::ios::out | std::ios::binary);
+  output.write(&trace_data[0], static_cast<std::streamsize>(trace_data.size()));
   output.close();
+  PERFETTO_LOG(
+      "Trace written in %s file. To read this trace in "
+      "text form, run `./tools/traceconv text %s`",
+      filename, filename);
 }
 
+}  // namespace
+
+PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(CustomDataSource);
+PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(CustomDataSource);
+
 int main(int, const char**) {
   InitializePerfetto();
   auto tracing_session = StartTracing();