Add a way to use libraries from the Android tree

This patch adds a gn variable build_with_android which makes it possible
to use Android internal (i.e., non-NDK) libraries when Perfetto is being
built as a part of the Android tree.

Change-Id: I935582fa969ab230b9ab0ac7d392dfeace802b0e
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index 48f69ca..79416d4 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -26,6 +26,7 @@
 # libraries are also mapped to their Android equivalents -- see |builtin_deps|.
 
 import argparse
+import errno
 import json
 import os
 import re
@@ -35,11 +36,12 @@
 
 # Default targets to translate to the blueprint file.
 default_targets = [
+    '//:libtraced_shared',
+    '//:perfetto',
     '//:perfetto_tests',
     '//:perfetto',
     '//:traced',
     '//:traced_probes',
-    '//:libtraced_shared',
     '//src/tracing:consumer_cmd',
 ]
 
@@ -48,7 +50,7 @@
 target_initrc = {}  # TODO(primiano): populate in upcoming CLs.
 
 # Arguments for the GN output directory.
-gn_args = 'target_os="android" target_cpu="arm" is_debug=false'
+gn_args = 'target_os="android" target_cpu="arm" is_debug=false build_with_android=true'
 
 # All module names are prefixed with this string to avoid collisions.
 module_prefix = 'perfetto_'
@@ -57,6 +59,7 @@
 library_whitelist = [
     'android',
     'log',
+    'utils',
 ]
 
 # Name of the module which settings such as compiler flags for all other
@@ -66,6 +69,9 @@
 # Location of the project in the Android source tree.
 tree_path = 'external/perfetto'
 
+# Compiler flags which are passed through to the blueprint.
+cflag_whitelist = r'^-DPERFETTO.*$'
+
 
 def enable_gmock(module):
     module.static_libs.append('libgmock')
@@ -437,6 +443,9 @@
         # Don't try to inject library/source dependencies into genrules because
         # they are not compiled in the traditional sense.
         if module.type != 'genrule':
+            for flag in target.get('cflags', []):
+                if re.match(cflag_whitelist, flag):
+                  module.cflags.append(flag)
             module.defaults = [defaults_module]
             apply_module_dependency(blueprint, desc, module, target_name)
             for dep in resolve_dependencies(desc, target_name):