tp: Create stdlib stub

Bug:255535171
Change-Id: I7a44ff6e683eefbefc3764a5c76a2ca944a0d241
diff --git a/tools/gen_amalgamated_sql_metrics.py b/tools/gen_amalgamated_sql.py
similarity index 81%
rename from tools/gen_amalgamated_sql_metrics.py
rename to tools/gen_amalgamated_sql.py
index 16f6aac..099bc92 100755
--- a/tools/gen_amalgamated_sql_metrics.py
+++ b/tools/gen_amalgamated_sql.py
@@ -21,7 +21,7 @@
 # as a string constant to allow trace processor to exectue the metrics.
 
 REPLACEMENT_HEADER = '''/*
- * Copyright (C) 2019 The Android Open Source Project
+ * Copyright (C) 2022 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.
@@ -45,13 +45,32 @@
  #include <string.h>
 '''
 
-NAMESPACE_BEGIN = '''
+STDLIB_NAMESPACE_BEGIN = '''
+namespace perfetto {
+namespace trace_processor {
+namespace stdlib {
+'''
+
+STDLIB_NAMESPACE_END = '''
+}  // namespace stdlib
+}  // namespace trace_processor
+}  // namespace perfetto
+'''
+
+METRICS_NAMESPACE_BEGIN = '''
 namespace perfetto {
 namespace trace_processor {
 namespace metrics {
 namespace sql_metrics {
 '''
 
+METRICS_NAMESPACE_END = '''
+}  // namespace sql_metrics
+}  // namespace metrics
+}  // namespace trace_processor
+}  // namespace perfetto
+'''
+
 FILE_TO_SQL_STRUCT = '''
 struct FileToSql {
   const char* path;
@@ -59,12 +78,8 @@
 };
 '''
 
-NAMESPACE_END = '''
-}  // namespace sql_metrics
-}  // namespace metrics
-}  // namespace trace_processor
-}  // namsepace perfetto
-'''
+ROOT = '''
+const char kRootPath[] = '''
 
 
 def filename_to_variable(filename):
@@ -73,11 +88,14 @@
 
 def main():
   parser = argparse.ArgumentParser()
+  parser.add_argument('--type', required=True)
   parser.add_argument('--cpp_out', required=True)
   parser.add_argument('sql_files', nargs='*')
   args = parser.parse_args()
 
   root_path = os.path.commonprefix([os.path.abspath(x) for x in args.sql_files])
+  if '.sql' in root_path:
+    root_path = root_path.rsplit('/', 1)[0]
 
   # Extract the SQL output from each file.
   sql_outputs = {}
@@ -89,7 +107,13 @@
 
   with open(args.cpp_out, 'w+') as output:
     output.write(REPLACEMENT_HEADER)
-    output.write(NAMESPACE_BEGIN)
+    output.write(
+        METRICS_NAMESPACE_BEGIN) if args.type == 'METRICS' else output.write(
+            STDLIB_NAMESPACE_BEGIN)
+
+    if args.type == "LIB":
+      output.write(ROOT + f'''"{root_path.rsplit("stdlib/")[-1]}";
+      ''')
 
     # Create the C++ variable for each SQL file.
     for path, sql in sql_outputs.items():
@@ -122,7 +146,9 @@
       output.write('\n  {{"{}", {}}},\n'.format(path, variable))
     output.write("};\n")
 
-    output.write(NAMESPACE_END)
+    output.write(
+        METRICS_NAMESPACE_END) if args.type == 'METRICS' else output.write(
+            STDLIB_NAMESPACE_END)
 
   return 0
 
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index 2469958..0fc587c 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -771,10 +771,11 @@
   bp_module_name = label_to_module_name(target.name)
   module = Module('genrule', bp_module_name, target.name)
   module.tool_files = [
-      'tools/gen_amalgamated_sql_metrics.py',
+      'tools/gen_amalgamated_sql.py',
   ]
   module.cmd = ' '.join([
-      '$(location tools/gen_amalgamated_sql_metrics.py)',
+      '$(location tools/gen_amalgamated_sql.py)',
+      '--type=METRICS',
       '--cpp_out=$(out)',
       '$(in)',
   ])
@@ -784,6 +785,23 @@
   blueprint.add_module(module)
   return module
 
+def create_amalgamated_stdlib_module(blueprint, target):
+  bp_module_name = label_to_module_name(target.name)
+  module = Module('genrule', bp_module_name, target.name)
+  module.tool_files = [
+      'tools/gen_amalgamated_sql.py',
+  ]
+  module.cmd = ' '.join([
+      '$(location tools/gen_amalgamated_sql.py)',
+      '--type=LIB',
+      '--cpp_out=$(out)',
+      '$(in)',
+  ])
+  module.genrule_headers.add(module.name)
+  module.out.update(target.outputs)
+  module.srcs.update(gn_utils.label_to_path(src) for src in target.inputs)
+  blueprint.add_module(module)
+  return module
 
 def create_cc_proto_descriptor_module(blueprint, target):
   bp_module_name = label_to_module_name(target.name)
@@ -891,6 +909,8 @@
   elif target.type == 'action':
     if 'gen_amalgamated_sql_metrics' in target.name:
       module = create_amalgamated_sql_metrics_module(blueprint, target)
+    elif 'gen_amalgamated_stdlib' in target.name:
+      module = create_amalgamated_stdlib_module(blueprint, target)
     elif re.match('.*gen_cc_.*_descriptor$', name_without_toolchain):
       module = create_cc_proto_descriptor_module(blueprint, target)
     elif target.type == 'action' and \
diff --git a/tools/gen_bazel b/tools/gen_bazel
index 1c64257..e64307a 100755
--- a/tools/gen_bazel
+++ b/tools/gen_bazel
@@ -114,6 +114,9 @@
     '//src/trace_processor/metrics/sql:gen_amalgamated_sql_metrics': [[
         ':cc_amalgamated_sql_metrics'
     ]],
+    '//src/trace_processor/stdlib:gen_amalgamated_stdlib': [[
+        ':cc_amalgamated_stdlib'
+    ]],
     gn_utils.GEN_VERSION_TARGET: ['PERFETTO_CONFIG.deps.version_header'],
 }
 
@@ -122,8 +125,18 @@
   label = BazelLabel(get_bazel_label_name(target.name), 'perfetto_genrule')
   label.srcs += [re.sub('^//', '', x) for x in sorted(target.inputs)]
   label.outs += target.outputs
-  label.cmd = r'$(location gen_amalgamated_sql_metrics_py) --cpp_out=$@ $(SRCS)'
-  label.exec_tools += [':gen_amalgamated_sql_metrics_py']
+  label.cmd = (r'$(location gen_amalgamated_sql_py) ' 
+  r'--type=METRICS --cpp_out=$@ $(SRCS)')
+  label.exec_tools += [':gen_amalgamated_sql_py']
+  return [label]
+
+def gen_amalgamated_stdlib(target):
+  label = BazelLabel(get_bazel_label_name(target.name), 'perfetto_genrule')
+  label.srcs += [re.sub('^//', '', x) for x in sorted(target.inputs)]
+  label.outs += target.outputs
+  label.cmd = (r'$(location gen_amalgamated_sql_py) '
+  r'--type=LIB --cpp_out=$@ $(SRCS)')
+  label.exec_tools += [':gen_amalgamated_sql_py']
   return [label]
 
 
@@ -152,6 +165,8 @@
         gen_version_header,
     '//src/trace_processor/metrics/sql:gen_amalgamated_sql_metrics':
         gen_amalgamated_sql_metrics,
+    '//src/trace_processor/stdlib:gen_amalgamated_stdlib':
+        gen_amalgamated_stdlib,
 }
 
 # ------------------------------------------------------------------------------