blob: d25d9bd600b8ff39b29cb0ef37074d0b55d2ca04 [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
24import hashlib
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010025from compat import iteritems
Hector Dearmanb7fa5442018-11-08 18:39:32 +000026
Lalit Maganti38365dc2020-08-04 13:33:17 +010027SOURCE_TARGET = [
28 (
Lalit Maganti38365dc2020-08-04 13:33:17 +010029 'protos/perfetto/trace_processor/trace_processor.proto',
Anindita Ghosh456261f2020-09-07 11:31:24 +010030 'src/trace_processor/python/perfetto/trace_processor/trace_processor.descriptor'
Lalit Maganti38365dc2020-08-04 13:33:17 +010031 ),
32 (
33 'protos/perfetto/metrics/metrics.proto',
Anindita Ghosh456261f2020-09-07 11:31:24 +010034 'src/trace_processor/python/perfetto/trace_processor/metrics.descriptor'
Lalit Maganti38365dc2020-08-04 13:33:17 +010035 ),
36]
Hector Dearmanb7fa5442018-11-08 18:39:32 +000037
38ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
39
40SCRIPT_PATH = 'tools/gen_binary_descriptors'
41
Hector Dearman1e269142018-11-14 13:53:08 +000042
Hector Dearmanb7fa5442018-11-08 18:39:32 +000043def hash_path(path):
44 hash = hashlib.sha1()
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010045 with open(os.path.join(ROOT_DIR, path), 'rb') as f:
Hector Dearmanb7fa5442018-11-08 18:39:32 +000046 hash.update(f.read())
47 return hash.hexdigest()
48
49
Hector Dearman1e269142018-11-14 13:53:08 +000050def find_protoc():
Primiano Tucci561ba7e2019-10-10 21:23:11 +010051 for root, _, files in os.walk(os.path.join(ROOT_DIR, 'out')):
Hector Dearman1e269142018-11-14 13:53:08 +000052 if 'protoc' in files:
53 return os.path.join(root, 'protoc')
Hector Dearman1e269142018-11-14 13:53:08 +000054 return None
55
56
Lalit Maganti2939c082021-03-11 17:25:44 +000057
58def check(source, target):
59 assert os.path.exists(os.path.join(ROOT_DIR, target)), \
60 'Output file {} does not exist and so cannot be checked'.format(target)
61
62 sha1_file = target + '.sha1'
63 assert os.path.exists(sha1_file), \
64 'SHA1 file {} does not exist and so cannot be checked'.format(sha1_file)
65
66 with open(sha1_file, 'rb') as f:
Hector Dearmanb7fa5442018-11-08 18:39:32 +000067 s = f.read()
68
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010069 hashes = re.findall(r'// SHA1\((.*)\)\n// (.*)\n', s.decode())
Hector Dearmanb7fa5442018-11-08 18:39:32 +000070 assert sorted([SCRIPT_PATH, source]) == sorted([key for key, _ in hashes])
71 for path, expected_sha1 in hashes:
72 actual_sha1 = hash_path(os.path.join(ROOT_DIR, path))
73 assert actual_sha1 == expected_sha1, \
Hector Dearman1e269142018-11-14 13:53:08 +000074 'In {} hash given for {} did not match'.format(target, path)
Hector Dearmanb7fa5442018-11-08 18:39:32 +000075
76
Lalit Maganti82a2c042020-07-06 13:50:33 +010077def generate(source, target, protoc_path):
78 with tempfile.NamedTemporaryFile() as fdescriptor:
79 subprocess.check_call([
80 protoc_path,
81 '--include_imports',
82 '--proto_path=.',
83 '--proto_path=' + \
84 os.path.join(ROOT_DIR, "buildtools", "protobuf", "src"),
85 '--descriptor_set_out={}'.format(fdescriptor.name),
86 source,
87 ],
88 cwd=ROOT_DIR)
89
90 s = fdescriptor.read()
Lalit Maganti2939c082021-03-11 17:25:44 +000091 with open(target, 'wb') as out:
92 out.write(s)
93
94 sha1_path = target + '.sha1'
95 with open(sha1_path, 'wb') as c:
96 c.write("""
97// SHA1({script_path})
98// {script_hash}
99// SHA1({source_path})
100// {source_hash}
101 """.format(
102 script_path=SCRIPT_PATH,
103 script_hash=hash_path(__file__),
104 source_path=source,
105 source_hash=hash_path(os.path.join(source)),
106 ).encode())
Lalit Maganti82a2c042020-07-06 13:50:33 +0100107
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000108def main():
109 parser = argparse.ArgumentParser()
110 parser.add_argument('--check-only', action='store_true')
111 parser.add_argument('--protoc')
112 args = parser.parse_args()
113
Hector Dearman1e269142018-11-14 13:53:08 +0000114 try:
Lalit Maganti38365dc2020-08-04 13:33:17 +0100115 for source, target in SOURCE_TARGET:
Hector Dearman1e269142018-11-14 13:53:08 +0000116 if args.check_only:
Primiano Tucci834fdc72019-10-04 11:33:44 +0100117 check(source, target)
Hector Dearman1e269142018-11-14 13:53:08 +0000118 else:
119 protoc = args.protoc or find_protoc()
120 assert protoc, 'protoc not found specific (--protoc PROTOC_PATH)'
121 assert os.path.exists(protoc), '{} does not exist'.format(protoc)
122 if protoc is not args.protoc:
123 print('Using protoc: {}'.format(protoc))
124 generate(source, target, protoc)
125 except AssertionError as e:
126 if not str(e):
127 raise
128 print('Error: {}'.format(e))
Hector Dearman7e079772018-11-15 16:08:12 +0000129 return 1
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000130
Primiano Tucci834fdc72019-10-04 11:33:44 +0100131
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000132if __name__ == '__main__':
133 exit(main())