blob: c53602ede1073d6f1c0155e34818955e46c1c21b [file] [log] [blame]
Lalit Maganti3dc8e302022-12-01 20:32:46 +00001#!/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
16import argparse
17from dataclasses import dataclass
18import os
Lalit Maganti16117cc2022-12-21 15:33:21 +000019import re
Lalit Maganti3dc8e302022-12-01 20:32:46 +000020import sys
Lalit Maganti746fc2b2023-03-24 13:21:24 +000021from typing import Dict
Lalit Maganti3dc8e302022-12-01 20:32:46 +000022from typing import List
Lalit Maganti16117cc2022-12-21 15:33:21 +000023from typing import Set
Lalit Maganti3dc8e302022-12-01 20:32:46 +000024
25# Allow importing of root-relative modules.
26ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
27sys.path.append(os.path.join(ROOT_DIR))
28
Lalit Maganti16117cc2022-12-21 15:33:21 +000029#pylint: disable=wrong-import-position
Lalit Maganti16117cc2022-12-21 15:33:21 +000030from python.generators.trace_processor_table.serialize import serialize_header
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010031from python.generators.trace_processor_table.util import find_table_deps
Lalit Maganti746fc2b2023-03-24 13:21:24 +000032from python.generators.trace_processor_table.util import ParsedTable
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010033from python.generators.trace_processor_table.util import parse_tables_from_modules
Lalit Maganti16117cc2022-12-21 15:33:21 +000034#pylint: enable=wrong-import-position
Lalit Maganti3dc8e302022-12-01 20:32:46 +000035
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010036# Suffix which replaces the .py extension for all input modules.
37OUT_HEADER_SUFFIX = '_py.h'
38
Lalit Maganti3dc8e302022-12-01 20:32:46 +000039
40@dataclass
Lalit Maganti16117cc2022-12-21 15:33:21 +000041class Header:
42 """Represents a Python module which will be converted to a header."""
Lalit Maganti746fc2b2023-03-24 13:21:24 +000043 tables: List[ParsedTable]
Lalit Maganti3dc8e302022-12-01 20:32:46 +000044
45
46def main():
47 """Main function."""
48 parser = argparse.ArgumentParser()
Lalit Maganti3dc8e302022-12-01 20:32:46 +000049 parser.add_argument('--inputs', required=True, nargs='*')
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010050 parser.add_argument('--gen-dir', required=True)
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010051 parser.add_argument('--relative-input-dir')
Lalit Maganti46794f62023-04-26 14:34:26 +010052 parser.add_argument('--import-prefix', default='')
Lalit Maganti3dc8e302022-12-01 20:32:46 +000053 args = parser.parse_args()
54
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010055 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 Magantiad706932023-04-26 13:29:27 +010061 return os.path.splitext(in_path)[0] + OUT_HEADER_SUFFIX
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010062
63 def get_out_path(in_path: str):
64 return os.path.join(args.gen_dir, get_relout_path(in_path))
65
Lalit Maganti46794f62023-04-26 14:34:26 +010066 def get_header_path(in_path: str):
67 return os.path.join(args.import_prefix, get_relout_path(in_path))
68
Lalit Maganti506bc482024-05-01 08:59:51 +010069 def get_relin_path_from_module_path(module_path: str):
Tom Renn29c61f52024-05-10 18:39:07 +000070 return module_path[module_path.rfind(os.sep + 'src') + 1:]
Lalit Maganti506bc482024-05-01 08:59:51 +010071
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010072 modules = [
Igor Kraskevich4f0bb5e2024-05-14 09:12:15 +000073 # 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 Maganti3df8a7e2023-04-25 14:18:17 +010077 for i in args.inputs
78 ]
Lalit Maganti746fc2b2023-03-24 13:21:24 +000079 headers: Dict[str, Header] = {}
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010080 for table in parse_tables_from_modules(modules):
Lalit Maganti506bc482024-05-01 08:59:51 +010081 input_path = get_relin_path_from_module_path(table.table.python_module)
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010082 header = headers.get(input_path, Header([]))
Lalit Maganti746fc2b2023-03-24 13:21:24 +000083 header.tables.append(table)
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010084 headers[input_path] = header
Lalit Maganti3dc8e302022-12-01 20:32:46 +000085
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010086 for in_path, header in headers.items():
87 out_path = get_out_path(in_path)
88 relout_path = get_relout_path(in_path)
Lalit Maganti16117cc2022-12-21 15:33:21 +000089
Lalit Maganti16117cc2022-12-21 15:33:21 +000090 # 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 Maganti746fc2b2023-03-24 13:21:24 +000093 for table in header.tables:
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010094 header_relout_deps = header_relout_deps.union([
Lalit Maganti506bc482024-05-01 08:59:51 +010095 get_header_path(get_relin_path_from_module_path(c.python_module))
Lalit Maganti3df8a7e2023-04-25 14:18:17 +010096 for c in find_table_deps(table.table)
97 ])
98 header_relout_deps.discard(relout_path)
Lalit Maganti16117cc2022-12-21 15:33:21 +000099
Lalit Maganti3df8a7e2023-04-25 14:18:17 +0100100 with open(out_path, 'w', encoding='utf8') as out:
101 ifdef_guard = re.sub(r'[^a-zA-Z0-9_-]', '_', relout_path).upper() + '_'
Lalit Maganti16117cc2022-12-21 15:33:21 +0000102 out.write(
Lalit Maganti746fc2b2023-03-24 13:21:24 +0000103 serialize_header(ifdef_guard, header.tables,
Lalit Maganti16117cc2022-12-21 15:33:21 +0000104 sorted(header_relout_deps)))
Lalit Maganti3dc8e302022-12-01 20:32:46 +0000105 out.write('\n')
106
107
108if __name__ == '__main__':
109 sys.exit(main())