blob: 07c57e16b09893d240d7f95bdcf2707082abff1d [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
Primiano Tucci834fdc72019-10-04 11:33:44 +010069
Lalit Maganti26f69bd2019-04-29 18:23:47 +010070def filename_to_variable(filename):
71 return "k" + "".join([x.capitalize() for x in filename.split("_")])
72
Primiano Tucci834fdc72019-10-04 11:33:44 +010073
Lalit Maganti26f69bd2019-04-29 18:23:47 +010074def main():
75 parser = argparse.ArgumentParser()
76 parser.add_argument('--cpp_out', required=True)
Lalit Maganti7177c7f2019-04-30 15:54:51 +010077 parser.add_argument('sql_files', nargs='*')
Lalit Maganti26f69bd2019-04-29 18:23:47 +010078 args = parser.parse_args()
79
Primiano Tucci834fdc72019-10-04 11:33:44 +010080 root_path = os.path.commonprefix([os.path.abspath(x) for x in args.sql_files])
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010081
Lalit Maganti26f69bd2019-04-29 18:23:47 +010082 # Extract the SQL output from each file.
Lalit Maganti5f3a0182019-05-07 16:40:36 +010083 sql_outputs = {}
Lalit Maganti26f69bd2019-04-29 18:23:47 +010084 for file_name in args.sql_files:
85 with open(file_name, 'r') as f:
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010086 relpath = os.path.relpath(file_name, root_path)
87 sql_outputs[relpath] = "".join(
Primiano Tucci834fdc72019-10-04 11:33:44 +010088 x for x in f.readlines() if not x.startswith('--'))
Lalit Maganti26f69bd2019-04-29 18:23:47 +010089
90 with open(args.cpp_out, 'w+') as output:
91 output.write(REPLACEMENT_HEADER)
92 output.write(NAMESPACE_BEGIN)
93
94 # Create the C++ variable for each SQL file.
Lalit Maganti72f1b1a2019-05-24 13:38:37 +010095 for path, sql in sql_outputs.items():
96 name = os.path.basename(path)
Lalit Maganti26f69bd2019-04-29 18:23:47 +010097 variable = filename_to_variable(os.path.splitext(name)[0])
Primiano Tucci834fdc72019-10-04 11:33:44 +010098 output.write(
99 '\nconst char {}[] = R"gendelimiter(\n{})gendelimiter";\n'.format(
100 variable, sql))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100101
102 output.write(FILE_TO_SQL_STRUCT)
103
104 # Create mapping of filename to variable name for each variable.
105 output.write("\nconst FileToSql kFileToSql[] = {")
Lalit Maganti72f1b1a2019-05-24 13:38:37 +0100106 for path in sql_outputs.keys():
107 name = os.path.basename(path)
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100108 variable = filename_to_variable(os.path.splitext(name)[0])
Lalit Magantieb06d512019-09-20 13:17:25 +0100109
110 # This is for Windows which has \ as a path separator.
111 path = path.replace("\\", "\\\\")
Primiano Tucci1d409982019-09-19 10:15:18 +0100112 output.write('\n {{"{}", {}}},\n'.format(path, variable))
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100113 output.write("};\n")
114
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100115 output.write(NAMESPACE_END)
116
117 return 0
118
Primiano Tucci834fdc72019-10-04 11:33:44 +0100119
Lalit Maganti26f69bd2019-04-29 18:23:47 +0100120if __name__ == '__main__':
121 sys.exit(main())