blob: 071423caa133de02239e877db1e7a7a8dbfa4392 [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 */
Lalit Maganti697cc482019-05-01 14:39:11 +010044
45 #include <string.h>
Lalit Maganti26f69bd2019-04-29 18:23:47 +010046'''
47
48NAMESPACE_BEGIN = '''
49namespace perfetto {
50namespace trace_processor {
51namespace metrics {
Lalit Maganti622676a2019-04-30 14:15:37 +010052namespace sql_metrics {
Lalit Maganti26f69bd2019-04-29 18:23:47 +010053'''
54
55FILE_TO_SQL_STRUCT = '''
56struct FileToSql {
57 const char* filename;
58 const char* sql;
59};
60'''
61
Lalit Maganti7177c7f2019-04-30 15:54:51 +010062FIND_SQL_FN = '''
63inline const char* GetBundledMetric(const char* filename) {
64 for (const auto& filename_to_sql : sql_metrics::kFileToSql) {
65 if (strcmp(filename_to_sql.filename, filename) == 0) {
66 return filename_to_sql.sql;
67 }
68 }
69 return nullptr;
70}
71'''
72
Lalit Maganti26f69bd2019-04-29 18:23:47 +010073NAMESPACE_END = '''
Lalit Maganti622676a2019-04-30 14:15:37 +010074} // namespace sql_metrics
Lalit Maganti26f69bd2019-04-29 18:23:47 +010075} // namespace metrics
76} // namespace trace_processor
77} // namsepace perfetto
78'''
79
80def filename_to_variable(filename):
81 return "k" + "".join([x.capitalize() for x in filename.split("_")])
82
83def main():
84 parser = argparse.ArgumentParser()
85 parser.add_argument('--cpp_out', required=True)
Lalit Maganti7177c7f2019-04-30 15:54:51 +010086 parser.add_argument('sql_files', nargs='*')
Lalit Maganti26f69bd2019-04-29 18:23:47 +010087 args = parser.parse_args()
88
89 # Extract the SQL output from each file.
90 escaped_sql_outputs = {}
91 for file_name in args.sql_files:
92 with open(file_name, 'r') as f:
93 basename = os.path.basename(file_name)
94
95 # Escape any quote characters.
96 escaped_sql_outputs[basename] = "".join(f.readlines())
97
98 with open(args.cpp_out, 'w+') as output:
99 output.write(REPLACEMENT_HEADER)
100 output.write(NAMESPACE_BEGIN)
101
102 # Create the C++ variable for each SQL file.
103 for name, sql in escaped_sql_outputs.items():
104 variable = filename_to_variable(os.path.splitext(name)[0])
105 output.write('\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'
106 .format(variable, sql))
107
108 output.write(FILE_TO_SQL_STRUCT)
109
110 # Create mapping of filename to variable name for each variable.
111 output.write("\nconst FileToSql kFileToSql[] = {")
112 for name in escaped_sql_outputs.keys():
113 variable = filename_to_variable(os.path.splitext(name)[0])
114 output.write('\n {{"{}", {}}},\n'.format(name, variable))
115 output.write("};\n")
116
Lalit Maganti7177c7f2019-04-30 15:54:51 +0100117 output.write(FIND_SQL_FN)
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100118 output.write(NAMESPACE_END)
119
120 return 0
121
122if __name__ == '__main__':
123 sys.exit(main())