Lalit Maganti | c449f77 | 2020-06-03 14:20:10 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 69 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 70 | def filename_to_variable(filename): |
| 71 | return "k" + "".join([x.capitalize() for x in filename.split("_")]) |
| 72 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 73 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 74 | def main(): |
| 75 | parser = argparse.ArgumentParser() |
| 76 | parser.add_argument('--cpp_out', required=True) |
Lalit Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame] | 77 | parser.add_argument('sql_files', nargs='*') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 78 | args = parser.parse_args() |
| 79 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 80 | root_path = os.path.commonprefix([os.path.abspath(x) for x in args.sql_files]) |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 81 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 82 | # Extract the SQL output from each file. |
Lalit Maganti | 5f3a018 | 2019-05-07 16:40:36 +0100 | [diff] [blame] | 83 | sql_outputs = {} |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 84 | for file_name in args.sql_files: |
| 85 | with open(file_name, 'r') as f: |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 86 | relpath = os.path.relpath(file_name, root_path) |
| 87 | sql_outputs[relpath] = "".join( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 88 | x for x in f.readlines() if not x.startswith('--')) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 89 | |
| 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 Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 95 | for path, sql in sql_outputs.items(): |
| 96 | name = os.path.basename(path) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 97 | variable = filename_to_variable(os.path.splitext(name)[0]) |
Primiano Tucci | 6320170 | 2020-12-04 17:38:12 +0100 | [diff] [blame] | 98 | output.write('\nconst char {}[] = '.format(variable)) |
| 99 | # MSVC doesn't like string literals that are individually longer than 16k. |
| 100 | # However it's still fine "if" "we" "concatenate" "many" "of" "them". |
| 101 | # This code splits the sql in string literals of ~1000 chars each. |
| 102 | line_groups = [''] |
| 103 | for line in sql.split('\n'): |
| 104 | line_groups[-1] += line + '\n' |
| 105 | if len(line_groups[-1]) > 1000: |
| 106 | line_groups.append('') |
| 107 | |
| 108 | for line in line_groups: |
| 109 | output.write('R"_d3l1m1t3r_({})_d3l1m1t3r_"\n'.format(line)) |
| 110 | output.write(';\n') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 111 | |
| 112 | output.write(FILE_TO_SQL_STRUCT) |
| 113 | |
| 114 | # Create mapping of filename to variable name for each variable. |
| 115 | output.write("\nconst FileToSql kFileToSql[] = {") |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 116 | for path in sql_outputs.keys(): |
| 117 | name = os.path.basename(path) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 118 | variable = filename_to_variable(os.path.splitext(name)[0]) |
Lalit Maganti | eb06d51 | 2019-09-20 13:17:25 +0100 | [diff] [blame] | 119 | |
| 120 | # This is for Windows which has \ as a path separator. |
Lalit Maganti | caaf0ce | 2020-06-08 12:14:09 +0100 | [diff] [blame] | 121 | path = path.replace("\\", "/") |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 122 | output.write('\n {{"{}", {}}},\n'.format(path, variable)) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 123 | output.write("};\n") |
| 124 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 125 | output.write(NAMESPACE_END) |
| 126 | |
| 127 | return 0 |
| 128 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 129 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 130 | if __name__ == '__main__': |
| 131 | sys.exit(main()) |