Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 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 | */ |
Lalit Maganti | 697cc48 | 2019-05-01 14:39:11 +0100 | [diff] [blame] | 44 | |
| 45 | #include <string.h> |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 46 | ''' |
| 47 | |
| 48 | NAMESPACE_BEGIN = ''' |
| 49 | namespace perfetto { |
| 50 | namespace trace_processor { |
| 51 | namespace metrics { |
Lalit Maganti | 622676a | 2019-04-30 14:15:37 +0100 | [diff] [blame] | 52 | namespace sql_metrics { |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 53 | ''' |
| 54 | |
| 55 | FILE_TO_SQL_STRUCT = ''' |
| 56 | struct FileToSql { |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 57 | const char* path; |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 58 | const char* sql; |
| 59 | }; |
| 60 | ''' |
| 61 | |
| 62 | NAMESPACE_END = ''' |
Lalit Maganti | 622676a | 2019-04-30 14:15:37 +0100 | [diff] [blame] | 63 | } // namespace sql_metrics |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 64 | } // namespace metrics |
| 65 | } // namespace trace_processor |
| 66 | } // namsepace perfetto |
| 67 | ''' |
| 68 | |
| 69 | def filename_to_variable(filename): |
| 70 | return "k" + "".join([x.capitalize() for x in filename.split("_")]) |
| 71 | |
| 72 | def main(): |
| 73 | parser = argparse.ArgumentParser() |
| 74 | parser.add_argument('--cpp_out', required=True) |
Lalit Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame] | 75 | parser.add_argument('sql_files', nargs='*') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 76 | args = parser.parse_args() |
| 77 | |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 78 | root_path = os.path.commonprefix( |
| 79 | [os.path.abspath(x) for x in args.sql_files]) |
| 80 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 81 | # Extract the SQL output from each file. |
Lalit Maganti | 5f3a018 | 2019-05-07 16:40:36 +0100 | [diff] [blame] | 82 | sql_outputs = {} |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 83 | for file_name in args.sql_files: |
| 84 | with open(file_name, 'r') as f: |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 85 | relpath = os.path.relpath(file_name, root_path) |
| 86 | sql_outputs[relpath] = "".join( |
Lalit Maganti | 5f3a018 | 2019-05-07 16:40:36 +0100 | [diff] [blame] | 87 | x for x in f.readlines() if not x.startswith('--')) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 88 | |
| 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 Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 94 | for path, sql in sql_outputs.items(): |
| 95 | name = os.path.basename(path) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 96 | 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 Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 104 | for path in sql_outputs.keys(): |
| 105 | name = os.path.basename(path) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 106 | variable = filename_to_variable(os.path.splitext(name)[0]) |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame^] | 107 | output.write('\n {{"{}", {}}},\n'.format(path, variable)) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 108 | output.write("};\n") |
| 109 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 110 | output.write(NAMESPACE_END) |
| 111 | |
| 112 | return 0 |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | sys.exit(main()) |