• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() for x in filename.replace(os.path.sep, '_').split("_")])
71
72
73def main():
74  parser = argparse.ArgumentParser()
75  parser.add_argument('--namespace', required=True)
76  parser.add_argument('--cpp-out', required=True)
77  parser.add_argument('--root-dir', default=None)
78  parser.add_argument('--input-list-file')
79  parser.add_argument('sql_files', nargs='*')
80  args = parser.parse_args()
81
82  if args.input_list_file and args.sql_files:
83    print("Only one of --input-list-file and list of SQL files expected")
84    return 1
85
86  sql_files = []
87  if args.input_list_file:
88    with open(args.input_list_file, 'r') as input_list_file:
89      for line in input_list_file.read().splitlines():
90        sql_files.append(line)
91  else:
92    sql_files = args.sql_files
93
94  # Unfortunately we cannot always pass this in as an arg as soong does not
95  # provide us a way to get the path to the Perfetto source directory. This
96  # fails on empty path but it's a price worth paying to have to use gross hacks
97  # in Soong.
98  root_dir = args.root_dir if args.root_dir else os.path.commonpath(sql_files)
99
100  # Extract the SQL output from each file.
101  sql_outputs = {}
102  for file_name in sql_files:
103    with open(file_name, 'r') as f:
104      relpath = os.path.relpath(file_name, root_dir)
105
106      # We've had bugs (e.g. b/264711057) when Soong's common path logic breaks
107      # and ends up with a bunch of ../ prefixing the path: disallow any ../
108      # as this should never be a valid in our C++ output.
109      assert '../' not in relpath
110      sql_outputs[relpath] = f.read()
111
112  with open(args.cpp_out, 'w+') as output:
113    output.write(REPLACEMENT_HEADER)
114    output.write(NAMESPACE_BEGIN.format(args.namespace))
115
116    # Create the C++ variable for each SQL file.
117    for path, sql in sql_outputs.items():
118      variable = filename_to_variable(os.path.splitext(path)[0])
119      output.write('\nconst char {}[] = '.format(variable))
120      # MSVC doesn't like string literals that are individually longer than 16k.
121      # However it's still fine "if" "we" "concatenate" "many" "of" "them".
122      # This code splits the sql in string literals of ~1000 chars each.
123      line_groups = ['']
124      for line in sql.split('\n'):
125        line_groups[-1] += line + '\n'
126        if len(line_groups[-1]) > 1000:
127          line_groups.append('')
128
129      for line in line_groups:
130        output.write('R"_d3l1m1t3r_({})_d3l1m1t3r_"\n'.format(line))
131      output.write(';\n')
132
133    output.write(FILE_TO_SQL_STRUCT)
134
135    # Create mapping of filename to variable name for each variable.
136    output.write("\nconst FileToSql kFileToSql[] = {")
137    for path in sql_outputs.keys():
138      variable = filename_to_variable(os.path.splitext(path)[0])
139
140      # This is for Windows which has \ as a path separator.
141      path = path.replace("\\", "/")
142      output.write('\n  {{"{}", {}}},\n'.format(path, variable))
143    output.write("};\n")
144
145    output.write(NAMESPACE_END.format(args.namespace))
146
147  return 0
148
149
150if __name__ == '__main__':
151  sys.exit(main())
152