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 = '''/* |
Lalit Maganti | 95f25b1 | 2023-06-22 18:11:05 +0100 | [diff] [blame] | 24 | * Copyright (C) 2023 The Android Open Source Project |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 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 | ******************************************************************************* |
Lalit Maganti | 95f25b1 | 2023-06-22 18:11:05 +0100 | [diff] [blame] | 41 | * AUTOGENERATED BY tools/gen_amalgamated_sql.py - DO NOT EDIT |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 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 | |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 48 | NAMESPACE_BEGIN = ''' |
| 49 | namespace perfetto {{ |
| 50 | namespace trace_processor {{ |
| 51 | namespace {} {{ |
Anna Mayzner | cc18bfd | 2022-11-03 14:05:19 +0000 | [diff] [blame] | 52 | ''' |
| 53 | |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 54 | NAMESPACE_END = ''' |
| 55 | }} // namespace {} |
| 56 | }} // namespace trace_processor |
| 57 | }} // namespace perfetto |
Anna Mayzner | cc18bfd | 2022-11-03 14:05:19 +0000 | [diff] [blame] | 58 | ''' |
| 59 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 60 | FILE_TO_SQL_STRUCT = ''' |
| 61 | struct FileToSql { |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 62 | const char* path; |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 63 | const char* sql; |
| 64 | }; |
| 65 | ''' |
| 66 | |
Lalit Maganti | 684a699 | 2023-03-07 18:23:52 +0000 | [diff] [blame] | 67 | |
| 68 | def filename_to_variable(filename: str): |
| 69 | return "k" + "".join( |
| 70 | [x.capitalize() for x in filename.replace(os.path.sep, '_').split("_")]) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 71 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 72 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 73 | def main(): |
| 74 | parser = argparse.ArgumentParser() |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 75 | parser.add_argument('--namespace', required=True) |
| 76 | parser.add_argument('--cpp-out', required=True) |
| 77 | parser.add_argument('--input-list-file') |
Lalit Maganti | 7177c7f | 2019-04-30 15:54:51 +0100 | [diff] [blame] | 78 | parser.add_argument('sql_files', nargs='*') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 79 | args = parser.parse_args() |
| 80 | |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 81 | if args.input_list_file and args.sql_files: |
| 82 | print("Only one of --input-list-file and list of SQL files expected") |
| 83 | return 1 |
| 84 | |
| 85 | sql_files = [] |
| 86 | if args.input_list_file: |
| 87 | with open(args.input_list_file, 'r') as input_list_file: |
| 88 | for line in input_list_file.read().splitlines(): |
| 89 | sql_files.append(line) |
| 90 | else: |
| 91 | sql_files = args.sql_files |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 92 | |
Lalit Maganti | 9380b0a | 2023-01-12 10:51:13 +0000 | [diff] [blame] | 93 | # Unfortunately we cannot pass this in as an arg as soong does not provide |
| 94 | # us a way to get the path to the Perfetto source directory. This fails on |
| 95 | # empty path but it's a price worth paying to have to use gross hacks in |
| 96 | # Soong. |
| 97 | root_dir = os.path.commonpath(sql_files) |
| 98 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 99 | # Extract the SQL output from each file. |
Lalit Maganti | 5f3a018 | 2019-05-07 16:40:36 +0100 | [diff] [blame] | 100 | sql_outputs = {} |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 101 | for file_name in sql_files: |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 102 | with open(file_name, 'r') as f: |
Lalit Maganti | 9380b0a | 2023-01-12 10:51:13 +0000 | [diff] [blame] | 103 | relpath = os.path.relpath(file_name, root_dir) |
| 104 | |
| 105 | # We've had bugs (e.g. b/264711057) when Soong's common path logic breaks |
| 106 | # and ends up with a bunch of ../ prefixing the path: disallow any ../ |
| 107 | # as this should never be a valid in our C++ output. |
| 108 | assert '../' not in relpath |
Lalit Maganti | 95f25b1 | 2023-06-22 18:11:05 +0100 | [diff] [blame] | 109 | sql_outputs[relpath] = f.read() |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 110 | |
| 111 | with open(args.cpp_out, 'w+') as output: |
| 112 | output.write(REPLACEMENT_HEADER) |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 113 | output.write(NAMESPACE_BEGIN.format(args.namespace)) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 114 | |
| 115 | # Create the C++ variable for each SQL file. |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 116 | for path, sql in sql_outputs.items(): |
Lalit Maganti | 684a699 | 2023-03-07 18:23:52 +0000 | [diff] [blame] | 117 | variable = filename_to_variable(os.path.splitext(path)[0]) |
Primiano Tucci | 6320170 | 2020-12-04 17:38:12 +0100 | [diff] [blame] | 118 | output.write('\nconst char {}[] = '.format(variable)) |
| 119 | # MSVC doesn't like string literals that are individually longer than 16k. |
| 120 | # However it's still fine "if" "we" "concatenate" "many" "of" "them". |
| 121 | # This code splits the sql in string literals of ~1000 chars each. |
| 122 | line_groups = [''] |
| 123 | for line in sql.split('\n'): |
| 124 | line_groups[-1] += line + '\n' |
| 125 | if len(line_groups[-1]) > 1000: |
| 126 | line_groups.append('') |
| 127 | |
| 128 | for line in line_groups: |
| 129 | output.write('R"_d3l1m1t3r_({})_d3l1m1t3r_"\n'.format(line)) |
| 130 | output.write(';\n') |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 131 | |
| 132 | output.write(FILE_TO_SQL_STRUCT) |
| 133 | |
| 134 | # Create mapping of filename to variable name for each variable. |
| 135 | output.write("\nconst FileToSql kFileToSql[] = {") |
Lalit Maganti | 72f1b1a | 2019-05-24 13:38:37 +0100 | [diff] [blame] | 136 | for path in sql_outputs.keys(): |
Lalit Maganti | 684a699 | 2023-03-07 18:23:52 +0000 | [diff] [blame] | 137 | variable = filename_to_variable(os.path.splitext(path)[0]) |
Lalit Maganti | eb06d51 | 2019-09-20 13:17:25 +0100 | [diff] [blame] | 138 | |
| 139 | # This is for Windows which has \ as a path separator. |
Lalit Maganti | caaf0ce | 2020-06-08 12:14:09 +0100 | [diff] [blame] | 140 | path = path.replace("\\", "/") |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 141 | output.write('\n {{"{}", {}}},\n'.format(path, variable)) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 142 | output.write("};\n") |
| 143 | |
Lalit Maganti | 358a7e4 | 2022-11-09 15:16:21 +0000 | [diff] [blame] | 144 | output.write(NAMESPACE_END.format(args.namespace)) |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 145 | |
| 146 | return 0 |
| 147 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 148 | |
Lalit Maganti | 26f69bd | 2019-04-29 18:23:47 +0100 | [diff] [blame] | 149 | if __name__ == '__main__': |
| 150 | sys.exit(main()) |