blob: 6149100a9834c8b140350fb1918e627d070b0793 [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
60NAMESPACE_END = '''
Lalit Maganti622676a2019-04-30 14:15:37 +010061} // namespace sql_metrics
Lalit Maganti26f69bd2019-04-29 18:23:47 +010062} // namespace metrics
63} // namespace trace_processor
64} // namsepace perfetto
65'''
66
67def filename_to_variable(filename):
68 return "k" + "".join([x.capitalize() for x in filename.split("_")])
69
70def main():
71 parser = argparse.ArgumentParser()
72 parser.add_argument('--cpp_out', required=True)
73 parser.add_argument('sql_files', action='append')
74 args = parser.parse_args()
75
76 # Extract the SQL output from each file.
77 escaped_sql_outputs = {}
78 for file_name in args.sql_files:
79 with open(file_name, 'r') as f:
80 basename = os.path.basename(file_name)
81
82 # Escape any quote characters.
83 escaped_sql_outputs[basename] = "".join(f.readlines())
84
85 with open(args.cpp_out, 'w+') as output:
86 output.write(REPLACEMENT_HEADER)
87 output.write(NAMESPACE_BEGIN)
88
89 # Create the C++ variable for each SQL file.
90 for name, sql in escaped_sql_outputs.items():
91 variable = filename_to_variable(os.path.splitext(name)[0])
92 output.write('\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'
93 .format(variable, sql))
94
95 output.write(FILE_TO_SQL_STRUCT)
96
97 # Create mapping of filename to variable name for each variable.
98 output.write("\nconst FileToSql kFileToSql[] = {")
99 for name in escaped_sql_outputs.keys():
100 variable = filename_to_variable(os.path.splitext(name)[0])
101 output.write('\n {{"{}", {}}},\n'.format(name, variable))
102 output.write("};\n")
103
104 output.write(NAMESPACE_END)
105
106 return 0
107
108if __name__ == '__main__':
109 sys.exit(main())