blob: e09a5c4deba6240b1da6ab3999dbcb93a51e9829 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Hector Dearmanb7fa5442018-11-08 18:39:32 +00002# 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
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19import os
20import re
Hector Dearmanb7fa5442018-11-08 18:39:32 +000021import argparse
22import tempfile
23import subprocess
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010024from compat import iteritems
Hector Dearmanb7fa5442018-11-08 18:39:32 +000025
Lalit Maganti38365dc2020-08-04 13:33:17 +010026SOURCE_TARGET = [
Primiano Tuccieac5d712021-05-18 20:45:05 +010027 ('protos/perfetto/trace_processor/trace_processor.proto',
Hector Dearmanb1989b02022-07-05 20:11:07 +010028 'python/perfetto/trace_processor/trace_processor.descriptor'),
Primiano Tuccieac5d712021-05-18 20:45:05 +010029 ('protos/perfetto/metrics/metrics.proto',
Lalit Maganti4c76b4d2022-01-11 15:37:41 +000030 'python/perfetto/trace_processor/metrics.descriptor'),
Lalit Maganti38365dc2020-08-04 13:33:17 +010031]
Hector Dearmanb7fa5442018-11-08 18:39:32 +000032
33ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
34
35SCRIPT_PATH = 'tools/gen_binary_descriptors'
36
Hector Dearman1e269142018-11-14 13:53:08 +000037
Hector Dearman1e269142018-11-14 13:53:08 +000038def find_protoc():
Primiano Tucci561ba7e2019-10-10 21:23:11 +010039 for root, _, files in os.walk(os.path.join(ROOT_DIR, 'out')):
Hector Dearman1e269142018-11-14 13:53:08 +000040 if 'protoc' in files:
41 return os.path.join(root, 'protoc')
Hector Dearman1e269142018-11-14 13:53:08 +000042 return None
43
44
Daniele Di Proietto9360a882023-04-28 15:38:23 +000045def generate(source, target, protoc_path, check_only):
Primiano Tuccieac5d712021-05-18 20:45:05 +010046 # 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 Maganti82a2c042020-07-06 13:50:33 +010049 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 Tuccieac5d712021-05-18 20:45:05 +010057 ], cwd=ROOT_DIR)
Lalit Maganti82a2c042020-07-06 13:50:33 +010058
59 s = fdescriptor.read()
Primiano Tuccieac5d712021-05-18 20:45:05 +010060 fdescriptor.close()
61 os.remove(fdescriptor.name)
Daniele Di Proietto9360a882023-04-28 15:38:23 +000062
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 Maganti2939c082021-03-11 17:25:44 +000070 with open(target, 'wb') as out:
71 out.write(s)
72
Primiano Tuccieac5d712021-05-18 20:45:05 +010073
Hector Dearmanb7fa5442018-11-08 18:39:32 +000074def 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 Dearman1e269142018-11-14 13:53:08 +000080 try:
Lalit Maganti38365dc2020-08-04 13:33:17 +010081 for source, target in SOURCE_TARGET:
Daniele Di Proietto9360a882023-04-28 15:38:23 +000082 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 Dearman1e269142018-11-14 13:53:08 +000088 except AssertionError as e:
89 if not str(e):
90 raise
91 print('Error: {}'.format(e))
Hector Dearman7e079772018-11-15 16:08:12 +000092 return 1
Hector Dearmanb7fa5442018-11-08 18:39:32 +000093
Primiano Tucci834fdc72019-10-04 11:33:44 +010094
Hector Dearmanb7fa5442018-11-08 18:39:32 +000095if __name__ == '__main__':
96 exit(main())