Move heap_profile to a separate include dir.

The previous setup caused trouble for people trying to create a prebuilt
of this library, because soong would bundle all of the Perfetto headers
with it. This is unnecessary and causes problems because now the
prebuilt can clash with other Perfetto targets.

Test: make heapprofd_client_api
Test: make heapprofd_api_noop
Test: m
Test: ninja -C out/linux_clang_release
Bug: 179915934
Change-Id: I1458e496ba2c482fb37629db7e93d96faa99603a
diff --git a/Android.bp b/Android.bp
index 948d3a9..c537e9e 100644
--- a/Android.bp
+++ b/Android.bp
@@ -167,13 +167,20 @@
   srcs: [
     "src/profiling/memory/client_api_noop.cc",
   ],
+  host_supported: true,
   export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
+    "src/profiling/memory/include",
   ],
   defaults: [
     "perfetto_defaults",
   ],
+  version_script: "src/profiling/memory/heapprofd_client_api.map.txt",
+  stubs: {
+    symbol_file: "src/profiling/memory/heapprofd_client_api.map.txt",
+    versions: [
+      "S",
+    ],
+  },
 }
 
 // GN: //src/profiling/memory:heapprofd_client
@@ -192,10 +199,6 @@
   static_libs: [
     "libasync_safe",
   ],
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   generated_headers: [
     "perfetto_src_base_version_gen_h",
   ],
@@ -239,8 +242,7 @@
     "libasync_safe",
   ],
   export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
+    "src/profiling/memory/include",
   ],
   generated_headers: [
     "perfetto_src_base_version_gen_h",
@@ -359,8 +361,7 @@
     "libunwindstack",
   ],
   export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
+    "src/profiling/memory/include",
   ],
   generated_headers: [
     "perfetto_protos_perfetto_common_cpp_gen_headers",
@@ -643,10 +644,6 @@
     "libhealthhalutils",
     "libstatslog_perfetto",
   ],
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   defaults: [
     "perfetto_defaults",
   ],
@@ -1109,10 +1106,6 @@
   whole_static_libs: [
     "perfetto_gtest_logcat_printer",
   ],
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   generated_headers: [
     "perfetto_protos_perfetto_common_cpp_gen_headers",
     "perfetto_protos_perfetto_common_zero_gen_headers",
@@ -1352,10 +1345,6 @@
     ":perfetto_src_tracing_ipc_service_service",
     ":perfetto_test_test_helper",
   ],
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   generated_headers: [
     "perfetto_protos_perfetto_common_cpp_gen_headers",
     "perfetto_protos_perfetto_common_zero_gen_headers",
@@ -1500,6 +1489,7 @@
     "external/perfetto",
     "external/perfetto/include",
     "external/perfetto/include/perfetto/base/build_configs/android_tree",
+    "external/perfetto/src/profiling/memory/include",
   ],
   product_variables: {
     debuggable: {
@@ -1525,10 +1515,6 @@
     "libgmock",
     "libgtest",
   ],
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   defaults: [
     "perfetto_defaults",
   ],
@@ -8601,10 +8587,6 @@
     "libprotobuf-cpp-lite",
   ],
   host_supported: true,
-  export_include_dirs: [
-    "include",
-    "include/perfetto/base/build_configs/android_tree",
-  ],
   generated_headers: [
     "perfetto_protos_perfetto_common_lite_gen_headers",
     "perfetto_protos_perfetto_config_android_lite_gen_headers",
diff --git a/docs/instrumentation/heapprofd-api.md b/docs/instrumentation/heapprofd-api.md
index ffcd77f..4c36029 100644
--- a/docs/instrumentation/heapprofd-api.md
+++ b/docs/instrumentation/heapprofd-api.md
@@ -53,7 +53,7 @@
 You will find the built library in
 `out/target/product/generic_arm64/system/lib64/heapprofd_standalone_client.so`.
 The header for the API can be found in
-`external/perfetto/include/perfetto/profiling/memory/heap_profile.h`.
+`external/perfetto/src/profiling/memory/include/perfetto/heap_profile.h`.
 
 WARNING: Only use the header from the checkout you used to build the library,
          as the API is not stable yet.
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 31c73b6..6793fce 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -118,7 +118,10 @@
   visibility = [ "../*" ]  # Prevent chromium targets from depending on this (breaks component).
   configs = [ ":public_config" ]
   defines = [ "PERFETTO_IMPLEMENTATION" ]
-  include_dirs = [ ".." ]
+  include_dirs = [
+    "..",
+    "../src/profiling/memory/include",
+  ]
 
   if (build_with_chromium && is_android) {
     # Included for __android_log_print
diff --git a/src/profiling/memory/client_api.cc b/src/profiling/memory/client_api.cc
index 71799e3..1143060 100644
--- a/src/profiling/memory/client_api.cc
+++ b/src/profiling/memory/client_api.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/heap_profile_internal.h"
 
 #include <inttypes.h>
diff --git a/src/profiling/memory/client_api_factory_standalone.cc b/src/profiling/memory/client_api_factory_standalone.cc
index 2ffae35..f21ce0d 100644
--- a/src/profiling/memory/client_api_factory_standalone.cc
+++ b/src/profiling/memory/client_api_factory_standalone.cc
@@ -22,7 +22,7 @@
 #include "perfetto/ext/base/utils.h"
 #include "perfetto/ext/base/watchdog.h"
 #include "perfetto/ext/tracing/ipc/default_socket.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/common/proc_utils.h"
 #include "src/profiling/memory/client.h"
 #include "src/profiling/memory/heap_profile_internal.h"
diff --git a/src/profiling/memory/client_api_noop.cc b/src/profiling/memory/client_api_noop.cc
index 0666de8..0f3732f 100644
--- a/src/profiling/memory/client_api_noop.cc
+++ b/src/profiling/memory/client_api_noop.cc
@@ -14,7 +14,8 @@
  * limitations under the License.
  */
 
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
+#include "src/profiling/memory/heap_profile_internal.h"
 
 #include <inttypes.h>
 
diff --git a/src/profiling/memory/client_api_unittest.cc b/src/profiling/memory/client_api_unittest.cc
index 3929398..ce4d1b2 100644
--- a/src/profiling/memory/client_api_unittest.cc
+++ b/src/profiling/memory/client_api_unittest.cc
@@ -16,7 +16,7 @@
 
 #include "perfetto/ext/base/unix_socket.h"
 #include "perfetto/ext/base/unix_task_runner.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/heap_profile_internal.h"
 
 #include "src/profiling/memory/client.h"
diff --git a/src/profiling/memory/heapprofd_end_to_end_test.cc b/src/profiling/memory/heapprofd_end_to_end_test.cc
index 50d87df..8b0a267 100644
--- a/src/profiling/memory/heapprofd_end_to_end_test.cc
+++ b/src/profiling/memory/heapprofd_end_to_end_test.cc
@@ -33,7 +33,7 @@
 #include "perfetto/ext/base/string_utils.h"
 #include "perfetto/ext/base/subprocess.h"
 #include "perfetto/ext/tracing/ipc/default_socket.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "perfetto/trace_processor/trace_processor.h"
 #include "protos/perfetto/trace/trace.gen.h"
 #include "protos/perfetto/trace/trace.pbzero.h"
diff --git a/src/profiling/memory/heapprofd_standalone_client_example.cc b/src/profiling/memory/heapprofd_standalone_client_example.cc
index 5032b4c..ccae01b 100644
--- a/src/profiling/memory/heapprofd_standalone_client_example.cc
+++ b/src/profiling/memory/heapprofd_standalone_client_example.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 
 #include <unistd.h>
 
diff --git a/include/perfetto/profiling/memory/heap_profile.h b/src/profiling/memory/include/perfetto/heap_profile.h
similarity index 96%
rename from include/perfetto/profiling/memory/heap_profile.h
rename to src/profiling/memory/include/perfetto/heap_profile.h
index d788e46..9d77db3 100644
--- a/include/perfetto/profiling/memory/heap_profile.h
+++ b/src/profiling/memory/include/perfetto/heap_profile.h
@@ -56,8 +56,8 @@
 // See https://perfetto.dev/docs/data-sources/native-heap-profiler for more
 // information on heapprofd in general.
 
-#ifndef INCLUDE_PERFETTO_PROFILING_MEMORY_HEAP_PROFILE_H_
-#define INCLUDE_PERFETTO_PROFILING_MEMORY_HEAP_PROFILE_H_
+#ifndef SRC_PROFILING_MEMORY_INCLUDE_PERFETTO_HEAP_PROFILE_H_
+#define SRC_PROFILING_MEMORY_INCLUDE_PERFETTO_HEAP_PROFILE_H_
 
 #include <inttypes.h>
 #include <stdlib.h>
@@ -182,4 +182,4 @@
 
 #pragma GCC diagnostic pop
 
-#endif  // INCLUDE_PERFETTO_PROFILING_MEMORY_HEAP_PROFILE_H_
+#endif  // SRC_PROFILING_MEMORY_INCLUDE_PERFETTO_HEAP_PROFILE_H_
diff --git a/src/profiling/memory/malloc_interceptor_bionic_hooks.cc b/src/profiling/memory/malloc_interceptor_bionic_hooks.cc
index 132f3a8..2e63f23 100644
--- a/src/profiling/memory/malloc_interceptor_bionic_hooks.cc
+++ b/src/profiling/memory/malloc_interceptor_bionic_hooks.cc
@@ -23,7 +23,7 @@
 
 #include "perfetto/base/logging.h"
 #include "perfetto/ext/base/utils.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/heap_profile_internal.h"
 
 #include "src/profiling/memory/wrap_allocators.h"
diff --git a/src/profiling/memory/malloc_interceptor_glibc_preload.cc b/src/profiling/memory/malloc_interceptor_glibc_preload.cc
index f911365..61e77ab 100644
--- a/src/profiling/memory/malloc_interceptor_glibc_preload.cc
+++ b/src/profiling/memory/malloc_interceptor_glibc_preload.cc
@@ -18,7 +18,7 @@
 #include <unistd.h>
 
 #include "perfetto/base/logging.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/wrap_allocators.h"
 
 namespace {
diff --git a/src/profiling/memory/wire_protocol.h b/src/profiling/memory/wire_protocol.h
index 0f3a0e2..2f0a961 100644
--- a/src/profiling/memory/wire_protocol.h
+++ b/src/profiling/memory/wire_protocol.h
@@ -29,7 +29,7 @@
 #include <unwindstack/MachineX86.h>
 #include <unwindstack/MachineX86_64.h>
 
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/shared_ring_buffer.h"
 
 // Make sure the alignment is the same on 32 and 64 bit architectures. This
diff --git a/src/profiling/memory/wrap_allocators.cc b/src/profiling/memory/wrap_allocators.cc
index 0b4debc..696337a 100644
--- a/src/profiling/memory/wrap_allocators.cc
+++ b/src/profiling/memory/wrap_allocators.cc
@@ -18,7 +18,7 @@
 #include <stdlib.h>
 
 #include "perfetto/ext/base/utils.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 #include "src/profiling/memory/wrap_allocators.h"
 
 namespace perfetto {
diff --git a/tools/fix_include_guards b/tools/fix_include_guards
index 4f08b41..84e1ad5 100755
--- a/tools/fix_include_guards
+++ b/tools/fix_include_guards
@@ -67,7 +67,8 @@
 def main():
   checkonly = '--check-only' in sys.argv
   num_files_changed = 0
-  for topdir in ('src', 'include', 'test', 'tools'):
+  for topdir in ('src', 'include', 'src/profiling/memory/include', 'test',
+                 'tools'):
     for root, dirs, files in os.walk(topdir):
       for name in files:
         if not name.endswith('.h'):
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index 0d5ba7b..3b34377 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -182,7 +182,17 @@
         ('stubs', {
           'versions': ['S'],
           'symbol_file': 'src/profiling/memory/heapprofd_client_api.map.txt',
-        })
+        }),
+        ('export_include_dirs', {'src/profiling/memory/include'}),
+    ],
+    'heapprofd_api_noop': [
+        ('version_script', 'src/profiling/memory/heapprofd_client_api.map.txt'),
+        ('stubs', {
+          'versions': ['S'],
+          'symbol_file': 'src/profiling/memory/heapprofd_client_api.map.txt',
+        }),
+        ('export_include_dirs', {'src/profiling/memory/include'}),
+        ('host_supported', True),
     ],
     'heapprofd_client': [
         ('include_dirs', {'bionic/libc'}),
@@ -191,6 +201,7 @@
     'heapprofd_standalone_client': [
         ('static_libs', {'libasync_safe'}),
         ('version_script', 'src/profiling/memory/heapprofd_client_api.map.txt'),
+        ('export_include_dirs', {'src/profiling/memory/include'}),
         ('stl', 'libc++_static'),
     ],
     'perfetto_unittests': [
@@ -220,6 +231,7 @@
         'com.android.art',
         'com.android.art.debug'}),
       ('shared_libs', {'liblog'}),
+      ('export_include_dirs', {'include', buildflags_dir}),
     ],
     'perfetto_trace_protos': [
       ('apex_available', {
@@ -227,6 +239,9 @@
         'com.android.art',
         'com.android.art.debug'}),
     ],
+    'libperfetto': [
+      ('export_include_dirs', {'include', buildflags_dir}),
+    ],
 }
 
 
@@ -799,7 +814,6 @@
   if bp_module_name in blueprint.modules:
     return blueprint.modules[bp_module_name]
   target = gn.get_target(gn_target_name)
-  export_include_dirs = {'include', buildflags_dir}
 
   if target.type == 'executable':
     if target.toolchain == gn_utils.HOST_TOOLCHAIN:
@@ -811,10 +825,8 @@
     module = Module(module_type, bp_module_name, gn_target_name)
   elif target.type == 'static_library':
     module = Module('cc_library_static', bp_module_name, gn_target_name)
-    module.export_include_dirs = export_include_dirs
   elif target.type == 'shared_library':
     module = Module('cc_library_shared', bp_module_name, gn_target_name)
-    module.export_include_dirs = export_include_dirs
   elif target.type == 'source_set':
     module = Module('filegroup', bp_module_name, gn_target_name)
   elif target.type == 'group':
@@ -882,7 +894,8 @@
     elif isinstance(add_val, dict) and isinstance(curr, Target):
       curr.__dict__.update(add_val)
     else:
-      raise Error('Unimplemented type of additional_args: %r' % key)
+      raise Error('Unimplemented type %r of additional_args: %r' %
+                  (type(add_val), key))
 
   # dep_name is an unmangled GN target name (e.g. //foo:bar(toolchain)).
   for dep_name in target.deps | target.source_set_deps | target.proto_deps:
@@ -949,7 +962,8 @@
   # become relative to the Android.bp that *uses* cc_defaults (not the one
   # that defines it).s
   defaults.include_dirs = {
-      tree_path, tree_path + '/include', tree_path + '/' + buildflags_dir
+      tree_path, tree_path + '/include', tree_path + '/' + buildflags_dir,
+      tree_path + '/src/profiling/memory/include'
   }
   defaults.cflags = [
       '-Wno-error=return-type',
diff --git a/tools/multithreaded_alloc.cc b/tools/multithreaded_alloc.cc
index ae01aaf..6125777 100644
--- a/tools/multithreaded_alloc.cc
+++ b/tools/multithreaded_alloc.cc
@@ -31,7 +31,7 @@
 #include "perfetto/ext/base/getopt.h"
 #include "perfetto/ext/base/optional.h"
 #include "perfetto/ext/base/string_utils.h"
-#include "perfetto/profiling/memory/heap_profile.h"
+#include "perfetto/heap_profile.h"
 
 namespace {