Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (C) 2020 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 | from __future__ import absolute_import |
| 17 | from __future__ import division |
| 18 | from __future__ import print_function |
| 19 | import os |
| 20 | import sys |
| 21 | import argparse |
| 22 | import tempfile |
| 23 | import subprocess |
| 24 | import textwrap |
| 25 | |
| 26 | |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 27 | def write_cpp_header(gendir, target, namespace, descriptor_bytes): |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 28 | _, target_name = os.path.split(target) |
| 29 | |
| 30 | proto_name = target_name[:-len('.descriptor.h')].title().replace("_", "") |
| 31 | try: |
| 32 | ord(descriptor_bytes[0]) |
| 33 | ordinal = ord |
| 34 | except TypeError: |
| 35 | ordinal = lambda x: x |
| 36 | binary = '{' + ', '.join( |
| 37 | '{0:#04x}'.format(ordinal(c)) for c in descriptor_bytes) + '}' |
| 38 | binary = textwrap.fill( |
| 39 | binary, width=80, initial_indent=' ', subsequent_indent=' ') |
| 40 | |
| 41 | relative_target = os.path.relpath(target, gendir) |
Lalit Maganti | 07ae00d | 2020-09-14 13:20:46 +0100 | [diff] [blame] | 42 | include_guard = relative_target.replace('\\', '_').replace('/', '_').replace( |
| 43 | '.', '_').upper() + '_' |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 44 | |
| 45 | with open(target, 'wb') as f: |
| 46 | f.write("""/* |
| 47 | * Copyright (C) 2020 The Android Open Source Project |
| 48 | * |
| 49 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 50 | * you may not use this file except in compliance with the License. |
| 51 | * You may obtain a copy of the License at |
| 52 | * |
| 53 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 54 | * |
| 55 | * Unless required by applicable law or agreed to in writing, software |
| 56 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 57 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 58 | * See the License for the specific language governing permissions and |
| 59 | * limitations under the License. |
| 60 | */ |
| 61 | |
| 62 | #ifndef {include_guard} |
| 63 | #define {include_guard} |
| 64 | |
| 65 | #include <stddef.h> |
| 66 | #include <stdint.h> |
Lalit Maganti | 379d6d5 | 2020-09-11 16:15:42 +0100 | [diff] [blame] | 67 | #include <array> |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 68 | |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 69 | namespace {namespace} {{ |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 70 | |
Lalit Maganti | 952aa9d | 2024-08-09 10:39:34 +0100 | [diff] [blame] | 71 | inline constexpr std::array<uint8_t, {size}> k{proto_name}Descriptor{{ |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 72 | {binary}}}; |
| 73 | |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 74 | }} // namespace {namespace} |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 75 | |
| 76 | #endif // {include_guard} |
| 77 | """.format( |
| 78 | proto_name=proto_name, |
| 79 | size=len(descriptor_bytes), |
| 80 | binary=binary, |
| 81 | include_guard=include_guard, |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 82 | namespace=namespace, |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 83 | ).encode()) |
| 84 | |
| 85 | |
| 86 | def main(): |
| 87 | parser = argparse.ArgumentParser() |
| 88 | parser.add_argument('--cpp_out', required=True) |
| 89 | parser.add_argument('--gen_dir', default='') |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 90 | parser.add_argument('--namespace', default='perfetto') |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 91 | parser.add_argument('descriptor') |
| 92 | args = parser.parse_args() |
| 93 | |
| 94 | with open(args.descriptor, 'rb') as fdescriptor: |
| 95 | s = fdescriptor.read() |
Daniele Di Proietto | 62dcb72 | 2024-08-13 10:44:13 +0000 | [diff] [blame] | 96 | write_cpp_header(args.gen_dir, args.cpp_out, args.namespace, s) |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 97 | |
| 98 | return 0 |
| 99 | |
| 100 | |
| 101 | if __name__ == '__main__': |
| 102 | sys.exit(main()) |