Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 1 | #!/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 | |
| 16 | import argparse |
| 17 | import os |
| 18 | import 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 | |
| 23 | REPLACEMENT_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 | |
| 46 | NAMESPACE_BEGIN = ''' |
| 47 | namespace perfetto { |
| 48 | namespace trace_processor { |
| 49 | namespace metrics { |
Lalit Maganti | 622676a | 2019-04-30 14:15:37 +0100 | [diff] [blame] | 50 | namespace sql_metrics { |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 51 | ''' |
| 52 | |
| 53 | FILE_TO_SQL_STRUCT = ''' |
| 54 | struct FileToSql { |
| 55 | const char* filename; |
| 56 | const char* sql; |
| 57 | }; |
| 58 | ''' |
| 59 | |
Lalit Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame^] | 60 | FIND_SQL_FN = ''' |
| 61 | inline 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 Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 71 | NAMESPACE_END = ''' |
Lalit Maganti | 622676a | 2019-04-30 14:15:37 +0100 | [diff] [blame] | 72 | } // namespace sql_metrics |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 73 | } // namespace metrics |
| 74 | } // namespace trace_processor |
| 75 | } // namsepace perfetto |
| 76 | ''' |
| 77 | |
| 78 | def filename_to_variable(filename): |
| 79 | return "k" + "".join([x.capitalize() for x in filename.split("_")]) |
| 80 | |
| 81 | def main(): |
| 82 | parser = argparse.ArgumentParser() |
| 83 | parser.add_argument('--cpp_out', required=True) |
Lalit Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame^] | 84 | parser.add_argument('sql_files', nargs='*') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 85 | 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 Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame^] | 115 | output.write(FIND_SQL_FN) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 116 | output.write(NAMESPACE_END) |
| 117 | |
| 118 | return 0 |
| 119 | |
| 120 | if __name__ == '__main__': |
| 121 | sys.exit(main()) |