xref: /aosp_15_r20/external/perfetto/tools/gen_amalgamated_sql.py (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1#!/usr/bin/env python3
2# Copyright (C) 2019 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import sys
19
20# Converts the SQL metrics for trace processor into a C++ header with the SQL
21# as a string constant to allow trace processor to exectue the metrics.
22
23REPLACEMENT_HEADER = '''/*
24 * Copyright (C) 2023 The Android Open Source Project
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License");
27 * you may not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 *      http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
37 */
38
39/*
40 *******************************************************************************
41 * AUTOGENERATED BY tools/gen_amalgamated_sql.py - DO NOT EDIT
42 *******************************************************************************
43 */
44
45 #include <string.h>
46'''
47
48NAMESPACE_BEGIN = '''
49namespace perfetto {{
50namespace trace_processor {{
51namespace {} {{
52'''
53
54NAMESPACE_END = '''
55}}  // namespace {}
56}}  // namespace trace_processor
57}}  // namespace perfetto
58'''
59
60FILE_TO_SQL_STRUCT = '''
61struct FileToSql {
62  const char* path;
63  const char* sql;
64};
65'''
66
67
68def filename_to_variable(filename: str):
69  return "k" + "".join([
70      x.capitalize()
71      for x in filename.replace(os.path.sep, '_').replace('-', '_').split("_")
72  ])
73
74
75def main():
76  parser = argparse.ArgumentParser()
77  parser.add_argument('--namespace', required=True)
78  parser.add_argument('--cpp-out', required=True)
79  parser.add_argument('--root-dir', default=None)
80  parser.add_argument('--input-list-file')
81  parser.add_argument('sql_files', nargs='*')
82  args = parser.parse_args()
83
84  if args.input_list_file and args.sql_files:
85    print("Only one of --input-list-file and list of SQL files expected")
86    return 1
87
88  sql_files = []
89  if args.input_list_file:
90    with open(args.input_list_file, 'r') as input_list_file:
91      for line in input_list_file.read().splitlines():
92        sql_files.append(line)
93  else:
94    sql_files = args.sql_files
95
96  # Unfortunately we cannot always pass this in as an arg as soong does not
97  # provide us a way to get the path to the Perfetto source directory. This
98  # fails on empty path but it's a price worth paying to have to use gross hacks
99  # in Soong.
100  root_dir = args.root_dir if args.root_dir else os.path.commonpath(sql_files)
101
102  # Extract the SQL output from each file.
103  sql_outputs = {}
104  for file_name in sql_files:
105    with open(file_name, 'r') as f:
106      relpath = os.path.relpath(file_name, root_dir)
107
108      # We've had bugs (e.g. b/264711057) when Soong's common path logic breaks
109      # and ends up with a bunch of ../ prefixing the path: disallow any ../
110      # as this should never be a valid in our C++ output.
111      assert '../' not in relpath
112      sql_outputs[relpath] = f.read()
113
114  with open(args.cpp_out, 'w+') as output:
115    output.write(REPLACEMENT_HEADER)
116    output.write(NAMESPACE_BEGIN.format(args.namespace))
117
118    # Create the C++ variable for each SQL file.
119    for path, sql in sql_outputs.items():
120      variable = filename_to_variable(os.path.splitext(path)[0])
121      output.write('\nconst char {}[] = '.format(variable))
122      # MSVC doesn't like string literals that are individually longer than 16k.
123      # However it's still fine "if" "we" "concatenate" "many" "of" "them".
124      # This code splits the sql in string literals of ~1000 chars each.
125      line_groups = ['']
126      for line in sql.split('\n'):
127        line_groups[-1] += line + '\n'
128        if len(line_groups[-1]) > 1000:
129          line_groups.append('')
130
131      for line in line_groups:
132        output.write('R"_d3l1m1t3r_({})_d3l1m1t3r_"\n'.format(line))
133      output.write(';\n')
134
135    output.write(FILE_TO_SQL_STRUCT)
136
137    # Create mapping of filename to variable name for each variable.
138    output.write("\nconst FileToSql kFileToSql[] = {")
139    for path in sql_outputs.keys():
140      variable = filename_to_variable(os.path.splitext(path)[0])
141
142      # This is for Windows which has \ as a path separator.
143      path = path.replace("\\", "/")
144      output.write('\n  {{"{}", {}}},\n'.format(path, variable))
145    output.write("};\n")
146
147    output.write(NAMESPACE_END.format(args.namespace))
148
149  return 0
150
151
152if __name__ == '__main__':
153  sys.exit(main())
154