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