tp: fix metrics and stdlib on Android builds

Bug: 264711057
Change-Id: Ib66087e4aaaf294038539015785f0ea599824e5a
diff --git a/Android.bp b/Android.bp
index 8a8de0b..fd057f2 100644
--- a/Android.bp
+++ b/Android.bp
@@ -9758,7 +9758,7 @@
         "src/trace_processor/metrics/sql/trace_stats.sql",
         "src/trace_processor/metrics/sql/webview/webview_power_usage.sql",
     ],
-    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=sql_metrics --root-dir=src/trace_processor/metrics/sql --cpp-out=$(out) $(in)",
+    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=sql_metrics --cpp-out=$(out) $(in)",
     out: [
         "src/trace_processor/metrics/sql/amalgamated_sql_metrics.h",
     ],
@@ -9904,7 +9904,7 @@
         "src/trace_processor/stdlib/common/timestamps.sql",
         "src/trace_processor/stdlib/experimental/android_broadcast.sql",
     ],
-    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=stdlib --root-dir=src/trace_processor/stdlib --cpp-out=$(out) $(in)",
+    cmd: "$(location tools/gen_amalgamated_sql.py) --namespace=stdlib --cpp-out=$(out) $(in)",
     out: [
         "src/trace_processor/stdlib/amalgamated_stdlib.h",
     ],
diff --git a/BUILD b/BUILD
index 5834959..3a63151 100644
--- a/BUILD
+++ b/BUILD
@@ -1695,7 +1695,6 @@
     outs = [
         "src/trace_processor/metrics/sql/amalgamated_sql_metrics.h",
     ],
-    root_dir = "src/trace_processor/metrics/sql",
     namespace = "sql_metrics",
 )
 
@@ -1895,7 +1894,6 @@
     outs = [
         "src/trace_processor/stdlib/amalgamated_stdlib.h",
     ],
-    root_dir = "src/trace_processor/stdlib",
     namespace = "stdlib",
 )
 
diff --git a/bazel/rules.bzl b/bazel/rules.bzl
index eb07d42..7becc96 100644
--- a/bazel/rules.bzl
+++ b/bazel/rules.bzl
@@ -280,8 +280,7 @@
         **kwargs
     )
 
-def perfetto_cc_amalgamated_sql(name, deps, outs, root_dir, namespace,
-                                **kwargs):
+def perfetto_cc_amalgamated_sql(name, deps, outs, namespace, **kwargs):
     if PERFETTO_CONFIG.root[:2] != "//":
         fail("Expected PERFETTO_CONFIG.root to start with //")
 
@@ -289,8 +288,6 @@
         "$(location gen_amalgamated_sql_py)",
         "--namespace",
         namespace,
-        "--root-dir",
-        PERFETTO_CONFIG.root[2:] + "/" + root_dir,
         "--cpp-out=$@",
         "$(SRCS)",
     ]
diff --git a/gn/perfetto_sql.gni b/gn/perfetto_sql.gni
index 9c9a5b2..d50864d 100644
--- a/gn/perfetto_sql.gni
+++ b/gn/perfetto_sql.gni
@@ -60,8 +60,6 @@
     args = [
       "--namespace",
       invoker.namespace,
-      "--root-dir",
-      rebase_path(".", root_build_dir),
       "--cpp-out",
       rebase_path(generated_file, root_build_dir),
       "--input-list-file",
@@ -85,8 +83,6 @@
       generated_file = "${target_gen_dir}/stdlib_docs.json"
 
       args = [
-        "--root-dir",
-        rebase_path(".", root_build_dir),
         "--json-out",
         rebase_path(generated_file, root_build_dir),
         "--input-list-file",
diff --git a/tools/gen_amalgamated_sql.py b/tools/gen_amalgamated_sql.py
index 285093d..1ac0ab0 100755
--- a/tools/gen_amalgamated_sql.py
+++ b/tools/gen_amalgamated_sql.py
@@ -73,7 +73,6 @@
   parser.add_argument('--namespace', required=True)
   parser.add_argument('--cpp-out', required=True)
   parser.add_argument('--input-list-file')
-  parser.add_argument('--root-dir', required=True)
   parser.add_argument('sql_files', nargs='*')
   args = parser.parse_args()
 
@@ -89,11 +88,23 @@
   else:
     sql_files = args.sql_files
 
+  # Unfortunately we cannot pass this in as an arg as soong does not provide
+  # us a way to get the path to the Perfetto source directory. This fails on
+  # empty path but it's a price worth paying to have to use gross hacks in
+  # Soong.
+  root_dir = os.path.commonpath(sql_files)
+
   # Extract the SQL output from each file.
   sql_outputs = {}
   for file_name in sql_files:
     with open(file_name, 'r') as f:
-      relpath = os.path.relpath(file_name, args.root_dir)
+      relpath = os.path.relpath(file_name, root_dir)
+
+      # We've had bugs (e.g. b/264711057) when Soong's common path logic breaks
+      # and ends up with a bunch of ../ prefixing the path: disallow any ../
+      # as this should never be a valid in our C++ output.
+      assert '../' not in relpath
+
       sql_outputs[relpath] = "".join(
           x.lstrip() for x in f.readlines() if not x.lstrip().startswith('--'))
 
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index 1dfd390..bb83471 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -826,7 +826,6 @@
         return target.args[i + 1]
 
   namespace = find_arg('namespace')
-  root_dir = gn_utils.label_to_path(find_arg('root-dir'))
 
   module = Module('genrule', bp_module_name, target.name)
   module.tool_files = [
@@ -835,7 +834,6 @@
   module.cmd = ' '.join([
       '$(location tools/gen_amalgamated_sql.py)',
       f'--namespace={namespace}',
-      f'--root-dir={root_dir}',
       '--cpp-out=$(out)',
       '$(in)',
   ])
diff --git a/tools/gen_bazel b/tools/gen_bazel
index 050e893..9c5b0c7 100755
--- a/tools/gen_bazel
+++ b/tools/gen_bazel
@@ -547,7 +547,6 @@
 
   label.comment = target.name
   label.namespace = find_arg('namespace')
-  label.root_dir = gn_utils.label_to_path(find_arg('root-dir'))
 
   label.deps += sorted(':' + get_bazel_label_name(x.name)
                        for x in target.transitive_deps)
diff --git a/tools/gen_stdlib_docs_json.py b/tools/gen_stdlib_docs_json.py
index e6953d0..c89b7a7 100755
--- a/tools/gen_stdlib_docs_json.py
+++ b/tools/gen_stdlib_docs_json.py
@@ -30,7 +30,6 @@
   parser = argparse.ArgumentParser()
   parser.add_argument('--json-out', required=True)
   parser.add_argument('--input-list-file')
-  parser.add_argument('--root-dir', required=True)
   parser.add_argument('sql_files', nargs='*')
   args = parser.parse_args()
 
@@ -46,11 +45,23 @@
   else:
     sql_files = args.sql_files
 
+  # Unfortunately we cannot pass this in as an arg as soong does not provide
+  # us a way to get the path to the Perfetto source directory. This fails on
+  # empty path but it's a price worth paying to have to use gross hacks in
+  # Soong.
+  root_dir = os.path.commonpath(sql_files)
+
   # Extract the SQL output from each file.
   sql_outputs: Dict[str, str] = {}
   for file_name in sql_files:
     with open(file_name, 'r') as f:
-      relpath = os.path.relpath(file_name, args.root_dir)
+      relpath = os.path.relpath(file_name, root_dir)
+
+      # We've had bugs (e.g. b/264711057) when Soong's common path logic breaks
+      # and ends up with a bunch of ../ prefixing the path: disallow any ../
+      # as this should never be a valid in our C++ output.
+      assert '../' not in relpath
+
       sql_outputs[relpath] = f.read()
 
   modules = defaultdict(list)