Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (C) 2022 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 | from dataclasses import dataclass |
| 18 | import os |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 19 | import re |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 20 | import sys |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 21 | from typing import Dict |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 22 | from typing import List |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 23 | from typing import Set |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 24 | |
| 25 | # Allow importing of root-relative modules. |
| 26 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 27 | sys.path.append(os.path.join(ROOT_DIR)) |
| 28 | |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 29 | #pylint: disable=wrong-import-position |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 30 | from python.generators.trace_processor_table.serialize import serialize_header |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 31 | from python.generators.trace_processor_table.util import find_table_deps |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 32 | from python.generators.trace_processor_table.util import ParsedTable |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 33 | from python.generators.trace_processor_table.util import parse_tables_from_modules |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 34 | #pylint: enable=wrong-import-position |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 35 | |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 36 | # Suffix which replaces the .py extension for all input modules. |
| 37 | OUT_HEADER_SUFFIX = '_py.h' |
| 38 | |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 39 | |
| 40 | @dataclass |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 41 | class Header: |
| 42 | """Represents a Python module which will be converted to a header.""" |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 43 | tables: List[ParsedTable] |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def main(): |
| 47 | """Main function.""" |
| 48 | parser = argparse.ArgumentParser() |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 49 | parser.add_argument('--inputs', required=True, nargs='*') |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 50 | parser.add_argument('--gen-dir', required=True) |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 51 | parser.add_argument('--relative-input-dir') |
Lalit Maganti | 46794f6 | 2023-04-26 14:34:26 +0100 | [diff] [blame] | 52 | parser.add_argument('--import-prefix', default='') |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 53 | args = parser.parse_args() |
| 54 | |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 55 | def get_relin_path(in_path: str): |
| 56 | if not args.relative_input_dir: |
| 57 | return in_path |
| 58 | return os.path.relpath(in_path, args.relative_input_dir) |
| 59 | |
| 60 | def get_relout_path(in_path: str): |
Lalit Maganti | ad70693 | 2023-04-26 13:29:27 +0100 | [diff] [blame] | 61 | return os.path.splitext(in_path)[0] + OUT_HEADER_SUFFIX |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 62 | |
| 63 | def get_out_path(in_path: str): |
| 64 | return os.path.join(args.gen_dir, get_relout_path(in_path)) |
| 65 | |
Lalit Maganti | 46794f6 | 2023-04-26 14:34:26 +0100 | [diff] [blame] | 66 | def get_header_path(in_path: str): |
| 67 | return os.path.join(args.import_prefix, get_relout_path(in_path)) |
| 68 | |
Lalit Maganti | 506bc48 | 2024-05-01 08:59:51 +0100 | [diff] [blame] | 69 | def get_relin_path_from_module_path(module_path: str): |
Tom Renn | 29c61f5 | 2024-05-10 18:39:07 +0000 | [diff] [blame] | 70 | return module_path[module_path.rfind(os.sep + 'src') + 1:] |
Lalit Maganti | 506bc48 | 2024-05-01 08:59:51 +0100 | [diff] [blame] | 71 | |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 72 | modules = [ |
Igor Kraskevich | 4f0bb5e | 2024-05-14 09:12:15 +0000 | [diff] [blame] | 73 | # On Windows the path can contain '/' or os.sep, depending on how this |
| 74 | # script is executed. So we need to replace both. |
| 75 | os.path.splitext( |
| 76 | get_relin_path(i).replace('/', '.').replace(os.sep, '.'))[0] |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 77 | for i in args.inputs |
| 78 | ] |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 79 | headers: Dict[str, Header] = {} |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 80 | for table in parse_tables_from_modules(modules): |
Lalit Maganti | 506bc48 | 2024-05-01 08:59:51 +0100 | [diff] [blame] | 81 | input_path = get_relin_path_from_module_path(table.table.python_module) |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 82 | header = headers.get(input_path, Header([])) |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 83 | header.tables.append(table) |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 84 | headers[input_path] = header |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 85 | |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 86 | for in_path, header in headers.items(): |
| 87 | out_path = get_out_path(in_path) |
| 88 | relout_path = get_relout_path(in_path) |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 89 | |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 90 | # Find all headers depended on by this table. These will be #include-ed when |
| 91 | # generating the header file below so ensure we remove ourself. |
| 92 | header_relout_deps: Set[str] = set() |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 93 | for table in header.tables: |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 94 | header_relout_deps = header_relout_deps.union([ |
Lalit Maganti | 506bc48 | 2024-05-01 08:59:51 +0100 | [diff] [blame] | 95 | get_header_path(get_relin_path_from_module_path(c.python_module)) |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 96 | for c in find_table_deps(table.table) |
| 97 | ]) |
| 98 | header_relout_deps.discard(relout_path) |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 99 | |
Lalit Maganti | 3df8a7e | 2023-04-25 14:18:17 +0100 | [diff] [blame] | 100 | with open(out_path, 'w', encoding='utf8') as out: |
| 101 | ifdef_guard = re.sub(r'[^a-zA-Z0-9_-]', '_', relout_path).upper() + '_' |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 102 | out.write( |
Lalit Maganti | 746fc2b | 2023-03-24 13:21:24 +0000 | [diff] [blame] | 103 | serialize_header(ifdef_guard, header.tables, |
Lalit Maganti | 16117cc | 2022-12-21 15:33:21 +0000 | [diff] [blame] | 104 | sorted(header_relout_deps))) |
Lalit Maganti | 3dc8e30 | 2022-12-01 20:32:46 +0000 | [diff] [blame] | 105 | out.write('\n') |
| 106 | |
| 107 | |
| 108 | if __name__ == '__main__': |
| 109 | sys.exit(main()) |