Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 2 | # Copyright (C) 2018 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 re |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 21 | import argparse |
| 22 | import tempfile |
| 23 | import subprocess |
Matthew Clarkson | 9a5dfa5 | 2019-10-03 09:54:04 +0100 | [diff] [blame] | 24 | from compat import iteritems |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 25 | |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 26 | SOURCE_TARGET = [ |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 27 | ('protos/perfetto/trace_processor/trace_processor.proto', |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 28 | 'python/perfetto/trace_processor/trace_processor.descriptor'), |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 29 | ('protos/perfetto/metrics/metrics.proto', |
Lalit Maganti | 4c76b4d | 2022-01-11 15:37:41 +0000 | [diff] [blame] | 30 | 'python/perfetto/trace_processor/metrics.descriptor'), |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 31 | ] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 32 | |
| 33 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 34 | |
| 35 | SCRIPT_PATH = 'tools/gen_binary_descriptors' |
| 36 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 37 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 38 | def find_protoc(): |
Primiano Tucci | 561ba7e | 2019-10-10 21:23:11 +0100 | [diff] [blame] | 39 | for root, _, files in os.walk(os.path.join(ROOT_DIR, 'out')): |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 40 | if 'protoc' in files: |
| 41 | return os.path.join(root, 'protoc') |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 42 | return None |
| 43 | |
| 44 | |
Daniele Di Proietto | 9360a88 | 2023-04-28 15:38:23 +0000 | [diff] [blame] | 45 | def generate(source, target, protoc_path, check_only): |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 46 | # delete=False + manual unlink is required for Windows. Otherwise the temp |
| 47 | # file is kept locked exclusively and unaccassible until it's destroyed. |
| 48 | with tempfile.NamedTemporaryFile(delete=False) as fdescriptor: |
Lalit Maganti | 82a2c04 | 2020-07-06 13:50:33 +0100 | [diff] [blame] | 49 | subprocess.check_call([ |
| 50 | protoc_path, |
| 51 | '--include_imports', |
| 52 | '--proto_path=.', |
| 53 | '--proto_path=' + \ |
| 54 | os.path.join(ROOT_DIR, "buildtools", "protobuf", "src"), |
| 55 | '--descriptor_set_out={}'.format(fdescriptor.name), |
| 56 | source, |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 57 | ], cwd=ROOT_DIR) |
Lalit Maganti | 82a2c04 | 2020-07-06 13:50:33 +0100 | [diff] [blame] | 58 | |
| 59 | s = fdescriptor.read() |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 60 | fdescriptor.close() |
| 61 | os.remove(fdescriptor.name) |
Daniele Di Proietto | 9360a88 | 2023-04-28 15:38:23 +0000 | [diff] [blame] | 62 | |
| 63 | if check_only: |
| 64 | with open(target, 'rb') as old: |
| 65 | old_content = old.read() |
| 66 | if (s != old_content): |
| 67 | raise AssertionError('Target {} does not match', target) |
| 68 | return |
| 69 | |
Lalit Maganti | 2939c08 | 2021-03-11 17:25:44 +0000 | [diff] [blame] | 70 | with open(target, 'wb') as out: |
| 71 | out.write(s) |
| 72 | |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 73 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 74 | def main(): |
| 75 | parser = argparse.ArgumentParser() |
| 76 | parser.add_argument('--check-only', action='store_true') |
| 77 | parser.add_argument('--protoc') |
| 78 | args = parser.parse_args() |
| 79 | |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 80 | try: |
Lalit Maganti | 38365dc | 2020-08-04 13:33:17 +0100 | [diff] [blame] | 81 | for source, target in SOURCE_TARGET: |
Daniele Di Proietto | 9360a88 | 2023-04-28 15:38:23 +0000 | [diff] [blame] | 82 | protoc = args.protoc or find_protoc() |
| 83 | assert protoc, 'protoc not found specific (--protoc PROTOC_PATH)' |
| 84 | assert os.path.exists(protoc), '{} does not exist'.format(protoc) |
| 85 | if protoc is not args.protoc: |
| 86 | print('Using protoc: {}'.format(protoc)) |
| 87 | generate(source, target, protoc, args.check_only) |
Hector Dearman | 1e26914 | 2018-11-14 13:53:08 +0000 | [diff] [blame] | 88 | except AssertionError as e: |
| 89 | if not str(e): |
| 90 | raise |
| 91 | print('Error: {}'.format(e)) |
Hector Dearman | 7e07977 | 2018-11-15 16:08:12 +0000 | [diff] [blame] | 92 | return 1 |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 93 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 94 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 95 | if __name__ == '__main__': |
| 96 | exit(main()) |