blob: 6ad35ead5a64f480ba630ccb22f5494ddd02e851 [file] [log] [blame]
Lalit Maganti26f69bd2019-04-29 18:23:47 +01001#!/usr/bin/env python
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) 2019 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
46NAMESPACE_BEGIN = '''
47namespace perfetto {
48namespace trace_processor {
49namespace metrics {
50'''
51
52FILE_TO_SQL_STRUCT = '''
53struct FileToSql {
54 const char* filename;
55 const char* sql;
56};
57'''
58
59NAMESPACE_END = '''
60} // namespace metrics
61} // namespace trace_processor
62} // namsepace perfetto
63'''
64
65def filename_to_variable(filename):
66 return "k" + "".join([x.capitalize() for x in filename.split("_")])
67
68def main():
69 parser = argparse.ArgumentParser()
70 parser.add_argument('--cpp_out', required=True)
71 parser.add_argument('sql_files', action='append')
72 args = parser.parse_args()
73
74 # Extract the SQL output from each file.
75 escaped_sql_outputs = {}
76 for file_name in args.sql_files:
77 with open(file_name, 'r') as f:
78 basename = os.path.basename(file_name)
79
80 # Escape any quote characters.
81 escaped_sql_outputs[basename] = "".join(f.readlines())
82
83 with open(args.cpp_out, 'w+') as output:
84 output.write(REPLACEMENT_HEADER)
85 output.write(NAMESPACE_BEGIN)
86
87 # Create the C++ variable for each SQL file.
88 for name, sql in escaped_sql_outputs.items():
89 variable = filename_to_variable(os.path.splitext(name)[0])
90 output.write('\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'
91 .format(variable, sql))
92
93 output.write(FILE_TO_SQL_STRUCT)
94
95 # Create mapping of filename to variable name for each variable.
96 output.write("\nconst FileToSql kFileToSql[] = {")
97 for name in escaped_sql_outputs.keys():
98 variable = filename_to_variable(os.path.splitext(name)[0])
99 output.write('\n {{"{}", {}}},\n'.format(name, variable))
100 output.write("};\n")
101
102 output.write(NAMESPACE_END)
103
104 return 0
105
106if __name__ == '__main__':
107 sys.exit(main())