blob: 319d0d34ca35dbe328864839c720157dc4902b49 [file] [log] [blame]
Primiano Tucci1d409982019-09-19 10:15:18 +01001#!/usr/bin/env python
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 = '''/*
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 {
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010057 const char* path;
Lalit Maganti26f69bd2019-04-29 18:23:47 +010058 const char* sql;
59};
60'''
61
62NAMESPACE_END = '''
Lalit Maganti622676a2019-04-30 14:15:37 +010063} // namespace sql_metrics
Lalit Maganti26f69bd2019-04-29 18:23:47 +010064} // namespace metrics
65} // namespace trace_processor
66} // namsepace perfetto
67'''
68
69def filename_to_variable(filename):
70 return "k" + "".join([x.capitalize() for x in filename.split("_")])
71
72def main():
73 parser = argparse.ArgumentParser()
74 parser.add_argument('--cpp_out', required=True)
Lalit Maganti7177c7f2019-04-30 15:54:51 +010075 parser.add_argument('sql_files', nargs='*')
Lalit Maganti26f69bd2019-04-29 18:23:47 +010076 args = parser.parse_args()
77
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010078 root_path = os.path.commonprefix(
79 [os.path.abspath(x) for x in args.sql_files])
80
Lalit Maganti26f69bd2019-04-29 18:23:47 +010081 # Extract the SQL output from each file.
Lalit Maganti5f3a0182019-05-07 16:40:36 +010082 sql_outputs = {}
Lalit Maganti26f69bd2019-04-29 18:23:47 +010083 for file_name in args.sql_files:
84 with open(file_name, 'r') as f:
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010085 relpath = os.path.relpath(file_name, root_path)
86 sql_outputs[relpath] = "".join(
Lalit Maganti5f3a0182019-05-07 16:40:36 +010087 x for x in f.readlines() if not x.startswith('--'))
Lalit Maganti26f69bd2019-04-29 18:23:47 +010088
89 with open(args.cpp_out, 'w+') as output:
90 output.write(REPLACEMENT_HEADER)
91 output.write(NAMESPACE_BEGIN)
92
93 # Create the C++ variable for each SQL file.
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010094 for path, sql in sql_outputs.items():
95 name = os.path.basename(path)
Lalit Maganti26f69bd2019-04-29 18:23:47 +010096 variable = filename_to_variable(os.path.splitext(name)[0])
97 output.write('\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'
98 .format(variable, sql))
99
100 output.write(FILE_TO_SQL_STRUCT)
101
102 # Create mapping of filename to variable name for each variable.
103 output.write("\nconst FileToSql kFileToSql[] = {")
Lalit Maganti72f1b1a2019-05-24 13:38:37 +0100104 for path in sql_outputs.keys():
105 name = os.path.basename(path)
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100106 variable = filename_to_variable(os.path.splitext(name)[0])
Primiano Tucci1d409982019-09-19 10:15:18 +0100107 output.write('\n {{"{}", {}}},\n'.format(path, variable))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100108 output.write("};\n")
109
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100110 output.write(NAMESPACE_END)
111
112 return 0
113
114if __name__ == '__main__':
115 sys.exit(main())