blob: 75745f8eb5f7f46a50874b6e8d20b163db77c82a [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 {
Lalit Maganti622676a2019-04-30 14:15:37 +010050namespace sql_metrics {
Lalit Maganti26f69bd2019-04-29 18:23:47 +010051'''
52
53FILE_TO_SQL_STRUCT = '''
54struct FileToSql {
55 const char* filename;
56 const char* sql;
57};
58'''
59
Lalit Maganti7177c7f2019-04-30 15:54:51 +010060FIND_SQL_FN = '''
61inline const char* GetBundledMetric(const char* filename) {
62 for (const auto& filename_to_sql : sql_metrics::kFileToSql) {
63 if (strcmp(filename_to_sql.filename, filename) == 0) {
64 return filename_to_sql.sql;
65 }
66 }
67 return nullptr;
68}
69'''
70
Lalit Maganti26f69bd2019-04-29 18:23:47 +010071NAMESPACE_END = '''
Lalit Maganti622676a2019-04-30 14:15:37 +010072} // namespace sql_metrics
Lalit Maganti26f69bd2019-04-29 18:23:47 +010073} // namespace metrics
74} // namespace trace_processor
75} // namsepace perfetto
76'''
77
78def filename_to_variable(filename):
79 return "k" + "".join([x.capitalize() for x in filename.split("_")])
80
81def main():
82 parser = argparse.ArgumentParser()
83 parser.add_argument('--cpp_out', required=True)
Lalit Maganti7177c7f2019-04-30 15:54:51 +010084 parser.add_argument('sql_files', nargs='*')
Lalit Maganti26f69bd2019-04-29 18:23:47 +010085 args = parser.parse_args()
86
87 # Extract the SQL output from each file.
88 escaped_sql_outputs = {}
89 for file_name in args.sql_files:
90 with open(file_name, 'r') as f:
91 basename = os.path.basename(file_name)
92
93 # Escape any quote characters.
94 escaped_sql_outputs[basename] = "".join(f.readlines())
95
96 with open(args.cpp_out, 'w+') as output:
97 output.write(REPLACEMENT_HEADER)
98 output.write(NAMESPACE_BEGIN)
99
100 # Create the C++ variable for each SQL file.
101 for name, sql in escaped_sql_outputs.items():
102 variable = filename_to_variable(os.path.splitext(name)[0])
103 output.write('\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'
104 .format(variable, sql))
105
106 output.write(FILE_TO_SQL_STRUCT)
107
108 # Create mapping of filename to variable name for each variable.
109 output.write("\nconst FileToSql kFileToSql[] = {")
110 for name in escaped_sql_outputs.keys():
111 variable = filename_to_variable(os.path.splitext(name)[0])
112 output.write('\n {{"{}", {}}},\n'.format(name, variable))
113 output.write("};\n")
114
Lalit Maganti7177c7f2019-04-30 15:54:51 +0100115 output.write(FIND_SQL_FN)
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100116 output.write(NAMESPACE_END)
117
118 return 0
119
120if __name__ == '__main__':
121 sys.exit(main())