blob: a3621f7d92426268b697c1800add18e4c2f8e002 [file] [log] [blame]
Lalit Magantic449f772020-06-03 14:20:10 +01001#!/usr/bin/env python3
Lalit Maganti26f69bd2019-04-29 18:23:47 +01002# 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 = '''/*
Lalit Maganti95f25b12023-06-22 18:11:05 +010024 * Copyright (C) 2023 The Android Open Source Project
Lalit Maganti26f69bd2019-04-29 18:23:47 +010025 *
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 *******************************************************************************
Lalit Maganti95f25b12023-06-22 18:11:05 +010041 * AUTOGENERATED BY tools/gen_amalgamated_sql.py - DO NOT EDIT
Lalit Maganti26f69bd2019-04-29 18:23:47 +010042 *******************************************************************************
43 */
Lalit Maganti697cc482019-05-01 14:39:11 +010044
45 #include <string.h>
Lalit Maganti26f69bd2019-04-29 18:23:47 +010046'''
47
Lalit Maganti358a7e42022-11-09 15:16:21 +000048NAMESPACE_BEGIN = '''
49namespace perfetto {{
50namespace trace_processor {{
51namespace {} {{
Anna Mayznercc18bfd2022-11-03 14:05:19 +000052'''
53
Lalit Maganti358a7e42022-11-09 15:16:21 +000054NAMESPACE_END = '''
55}} // namespace {}
56}} // namespace trace_processor
57}} // namespace perfetto
Anna Mayznercc18bfd2022-11-03 14:05:19 +000058'''
59
Lalit Maganti26f69bd2019-04-29 18:23:47 +010060FILE_TO_SQL_STRUCT = '''
61struct FileToSql {
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010062 const char* path;
Lalit Maganti26f69bd2019-04-29 18:23:47 +010063 const char* sql;
64};
65'''
66
Lalit Maganti684a6992023-03-07 18:23:52 +000067
68def filename_to_variable(filename: str):
69 return "k" + "".join(
70 [x.capitalize() for x in filename.replace(os.path.sep, '_').split("_")])
Lalit Maganti26f69bd2019-04-29 18:23:47 +010071
Primiano Tucci834fdc72019-10-04 11:33:44 +010072
Lalit Maganti26f69bd2019-04-29 18:23:47 +010073def main():
74 parser = argparse.ArgumentParser()
Lalit Maganti358a7e42022-11-09 15:16:21 +000075 parser.add_argument('--namespace', required=True)
76 parser.add_argument('--cpp-out', required=True)
77 parser.add_argument('--input-list-file')
Lalit Maganti7177c7f2019-04-30 15:54:51 +010078 parser.add_argument('sql_files', nargs='*')
Lalit Maganti26f69bd2019-04-29 18:23:47 +010079 args = parser.parse_args()
80
Lalit Maganti358a7e42022-11-09 15:16:21 +000081 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
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010092
Lalit Maganti9380b0a2023-01-12 10:51:13 +000093 # 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
Lalit Maganti26f69bd2019-04-29 18:23:47 +010099 # Extract the SQL output from each file.
Lalit Maganti5f3a0182019-05-07 16:40:36 +0100100 sql_outputs = {}
Lalit Maganti358a7e42022-11-09 15:16:21 +0000101 for file_name in sql_files:
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100102 with open(file_name, 'r') as f:
Lalit Maganti9380b0a2023-01-12 10:51:13 +0000103 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
Lalit Maganti95f25b12023-06-22 18:11:05 +0100109 sql_outputs[relpath] = f.read()
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100110
111 with open(args.cpp_out, 'w+') as output:
112 output.write(REPLACEMENT_HEADER)
Lalit Maganti358a7e42022-11-09 15:16:21 +0000113 output.write(NAMESPACE_BEGIN.format(args.namespace))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100114
115 # Create the C++ variable for each SQL file.
Lalit Maganti72f1b1a2019-05-24 13:38:37 +0100116 for path, sql in sql_outputs.items():
Lalit Maganti684a6992023-03-07 18:23:52 +0000117 variable = filename_to_variable(os.path.splitext(path)[0])
Primiano Tucci63201702020-12-04 17:38:12 +0100118 output.write('\nconst char {}[] = '.format(variable))
119 # MSVC doesn't like string literals that are individually longer than 16k.
120 # However it's still fine "if" "we" "concatenate" "many" "of" "them".
121 # This code splits the sql in string literals of ~1000 chars each.
122 line_groups = ['']
123 for line in sql.split('\n'):
124 line_groups[-1] += line + '\n'
125 if len(line_groups[-1]) > 1000:
126 line_groups.append('')
127
128 for line in line_groups:
129 output.write('R"_d3l1m1t3r_({})_d3l1m1t3r_"\n'.format(line))
130 output.write(';\n')
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100131
132 output.write(FILE_TO_SQL_STRUCT)
133
134 # Create mapping of filename to variable name for each variable.
135 output.write("\nconst FileToSql kFileToSql[] = {")
Lalit Maganti72f1b1a2019-05-24 13:38:37 +0100136 for path in sql_outputs.keys():
Lalit Maganti684a6992023-03-07 18:23:52 +0000137 variable = filename_to_variable(os.path.splitext(path)[0])
Lalit Magantieb06d512019-09-20 13:17:25 +0100138
139 # This is for Windows which has \ as a path separator.
Lalit Maganticaaf0ce2020-06-08 12:14:09 +0100140 path = path.replace("\\", "/")
Primiano Tucci1d409982019-09-19 10:15:18 +0100141 output.write('\n {{"{}", {}}},\n'.format(path, variable))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100142 output.write("};\n")
143
Lalit Maganti358a7e42022-11-09 15:16:21 +0000144 output.write(NAMESPACE_END.format(args.namespace))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100145
146 return 0
147
Primiano Tucci834fdc72019-10-04 11:33:44 +0100148
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100149if __name__ == '__main__':
150 sys.exit(main())