blob: a7fc12d6905a9a6ae77c48af75fab11a0783aae8 [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Hector Dearman55ef3e02018-04-11 17:28:55 +01002# 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 Dearman55ef3e02018-04-11 17:28:55 +010021import sys
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010022from codecs import open
Hector Dearman55ef3e02018-04-11 17:28:55 +010023
Primiano Tuccic98edc42020-04-08 00:42:11 +010024PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
Primiano Tuccieac5d712021-05-18 20:45:05 +010025SELF_PATH = os.path.relpath(__file__, PROJECT_ROOT).replace('\\', '/')
Primiano Tuccic98edc42020-04-08 00:42:11 +010026
27CONFIG_PROTO_ROOTS = [
Matthew Clarkson63028d62019-10-10 15:48:23 +010028 'protos/perfetto/common/data_source_descriptor.proto',
Primiano Tuccibd25ec12020-04-17 10:22:28 +010029 'protos/perfetto/common/tracing_service_state.proto',
Primiano Tuccic98edc42020-04-08 00:42:11 +010030 'protos/perfetto/config/trace_config.proto'
31]
Hector Dearman55ef3e02018-04-11 17:28:55 +010032MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto'
33
Andrew Shulaev46b12a92020-07-09 00:45:37 +010034TRACE_PROTO_ROOTS = CONFIG_PROTO_ROOTS + [
Primiano Tuccieac5d712021-05-18 20:45:05 +010035 'protos/perfetto/trace/trace.proto',
Andrew Shulaev46b12a92020-07-09 00:45:37 +010036]
Hector Dearman55ef3e02018-04-11 17:28:55 +010037MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'
38
Primiano Tuccic98edc42020-04-08 00:42:11 +010039METRICS_PROTOS_ROOTS = ['protos/perfetto/metrics/metrics.proto']
Lalit Magantiaa035b22019-12-20 16:13:09 +000040MERGED_METRICS_PROTO = 'protos/perfetto/metrics/perfetto_merged_metrics.proto'
41
Hector Dearman55ef3e02018-04-11 17:28:55 +010042REPLACEMENT_HEADER = '''
43// AUTOGENERATED - DO NOT EDIT
44// ---------------------------
45// This file has been generated by
46// AOSP://external/perfetto/%s
47// merging the perfetto config protos.
48// This fused proto is intended to be copied in:
49// - Android tree, for statsd.
50// - Google internal repos.
51
52syntax = "proto2";
53
54package perfetto.protos;
Allenb90c4e42021-11-04 18:49:20 -070055
56option go_package = "github.com/google/perfetto/perfetto_proto";
Hector Dearman55ef3e02018-04-11 17:28:55 +010057'''
58
Matthew Clarkson63028d62019-10-10 15:48:23 +010059
Primiano Tuccic98edc42020-04-08 00:42:11 +010060def get_transitive_imports(rel_path, visited):
61 if rel_path in visited:
62 return []
63 visited.add(rel_path)
64 with open(os.path.join(PROJECT_ROOT, rel_path), 'r', encoding='utf-8') as f:
65 content = f.read()
66 imports = re.findall(r'^import "(.*)";\n', content, flags=re.MULTILINE)
67 res = []
68 for child in sorted(imports):
69 res += get_transitive_imports(child, visited)
70 res += [rel_path]
71 return res
72
73
74def merge_protos_content(proto_paths):
Chris Hamilton4aa09f82021-04-06 15:55:23 -040075 merged_content = REPLACEMENT_HEADER.lstrip() % SELF_PATH
Primiano Tuccic98edc42020-04-08 00:42:11 +010076 added_files = set()
Hector Dearman55ef3e02018-04-11 17:28:55 +010077 for proto in proto_paths:
Lalit Magantiaa035b22019-12-20 16:13:09 +000078 if proto in added_files:
79 continue
80 added_files.add(proto)
81
Chris Hamilton4aa09f82021-04-06 15:55:23 -040082 path = os.path.join(PROJECT_ROOT, proto)
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +010083 with open(path, 'r', encoding='utf-8') as f:
Hector Dearman55ef3e02018-04-11 17:28:55 +010084 content = f.read()
85
86 # Remove header
Florian Mayer12f0f8e2019-04-02 12:31:13 +010087 header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content)
Hector Dearman55ef3e02018-04-11 17:28:55 +010088 header = header.group(0)
89 content = content[len(header):]
Lalit Magantiaa035b22019-12-20 16:13:09 +000090
Isabelle Taylor80ec5f72019-02-27 11:27:41 +000091 content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE)
Hector Dearman55ef3e02018-04-11 17:28:55 +010092 merged_content += '\n// Begin of %s\n' % proto
93 merged_content += content
94 merged_content += '\n// End of %s\n' % proto
95
96 definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {'
97 definitions = re.finditer(definitions_re, merged_content, re.MULTILINE)
98 types = set((match.group(1) for match in definitions))
99
Ryan Savitski4756ef42020-02-12 15:14:06 +0000100 # Limitation: |types| doesn't track the nesting of messages, so a reference to
101 # a nested message (optional One.Two f = 1;) is simplified to its leafmost
102 # name (Two in this example).
103 uses_re = r'^( +)(?:repeated)?(?:optional)?\s?'\
104 r'(?:[A-Z]\w+\.)*([A-Z]\w+)\s+[a-z]\w*\s*=\s*(\d+);'
Hector Dearman55ef3e02018-04-11 17:28:55 +0100105 uses = re.finditer(uses_re, merged_content, re.MULTILINE)
106 substitutions = []
107 for use in uses:
108 everything = use.group(0)
109 indentation = use.group(1)
110 used_type = use.group(2)
111 field_number = use.group(3)
112 if used_type not in types:
Hector Dearman55ef3e02018-04-11 17:28:55 +0100113 replacement = '{}// removed field with id {}'.format(
114 indentation, field_number)
115 substitutions.append((everything, replacement))
116
117 for before, after in substitutions:
118 merged_content = merged_content.replace(before, after)
119
Lalit Magantiaa035b22019-12-20 16:13:09 +0000120 return merged_content
121
122
Primiano Tuccic98edc42020-04-08 00:42:11 +0100123def merge_protos(root_paths, output_path):
Primiano Tuccic98edc42020-04-08 00:42:11 +0100124 all_protos = []
125 for root_path in root_paths:
126 all_protos += get_transitive_imports(root_path, visited=set())
127 merged_content = merge_protos_content(all_protos)
Hector Dearman55ef3e02018-04-11 17:28:55 +0100128
Chris Hamilton4aa09f82021-04-06 15:55:23 -0400129 out_path = os.path.join(PROJECT_ROOT, output_path)
Hector Dearman55ef3e02018-04-11 17:28:55 +0100130 prev_content = None
131 if os.path.exists(out_path):
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100132 with open(out_path, 'r', encoding='utf-8') as fprev:
Hector Dearman55ef3e02018-04-11 17:28:55 +0100133 prev_content = fprev.read()
134
135 if prev_content == merged_content:
136 return True
137
138 if '--check-only' in sys.argv:
139 return False
140
141 print('Updating {}'.format(output_path))
Matthew Clarkson9a5dfa52019-10-03 09:54:04 +0100142 with open(out_path, 'w', encoding='utf-8') as fout:
Hector Dearman55ef3e02018-04-11 17:28:55 +0100143 fout.write(merged_content)
144 return True
145
Matthew Clarkson63028d62019-10-10 15:48:23 +0100146
Hector Dearman55ef3e02018-04-11 17:28:55 +0100147def main():
Primiano Tuccic98edc42020-04-08 00:42:11 +0100148 result = merge_protos(CONFIG_PROTO_ROOTS, MERGED_CONFIG_PROTO)
149 result &= merge_protos(TRACE_PROTO_ROOTS, MERGED_TRACE_PROTO)
150 result &= merge_protos(METRICS_PROTOS_ROOTS, MERGED_METRICS_PROTO)
Eric Seckler0c7921b2020-01-10 09:46:13 +0000151 return 0 if result else 1
Hector Dearman55ef3e02018-04-11 17:28:55 +0100152
Matthew Clarkson63028d62019-10-10 15:48:23 +0100153
Hector Dearman55ef3e02018-04-11 17:28:55 +0100154if __name__ == '__main__':
155 sys.exit(main())