blob: 713a5e3d3a0182dcac2ce816dd187dfc8c1b76bf [file] [log] [blame]
Hector Dearman55ef3e02018-04-11 17:28:55 +01001#!/usr/bin/env python
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
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
22
23CONFIG_PROTOS = (
Primiano Tucci82a8bfd2018-09-19 11:33:04 +010024 'protos/perfetto/common/sys_stats_counters.proto',
Hector Dearman55ef3e02018-04-11 17:28:55 +010025 'protos/perfetto/config/chrome/chrome_config.proto',
Hector Dearman55ef3e02018-04-11 17:28:55 +010026 'protos/perfetto/config/data_source_config.proto',
27 'protos/perfetto/config/ftrace/ftrace_config.proto',
Primiano Tucci82a8bfd2018-09-19 11:33:04 +010028 'protos/perfetto/config/inode_file/inode_file_config.proto',
29 'protos/perfetto/config/process_stats/process_stats_config.proto',
30 'protos/perfetto/config/sys_stats/sys_stats_config.proto',
Hector Dearman55ef3e02018-04-11 17:28:55 +010031 'protos/perfetto/config/test_config.proto',
32 'protos/perfetto/config/trace_config.proto',
Florian Mayer8e7eac42018-11-05 16:04:11 +000033 'protos/perfetto/config/profiling/heapprofd_config.proto',
Hector Dearman55ef3e02018-04-11 17:28:55 +010034)
35
36MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto'
37
38TRACE_PROTOS = (
Primiano Tucci82a8bfd2018-09-19 11:33:04 +010039 'protos/perfetto/common/sys_stats_counters.proto',
Primiano Tucci82a8bfd2018-09-19 11:33:04 +010040 'protos/perfetto/trace/clock_snapshot.proto',
Hector Dearmane92c6742018-11-22 21:42:39 +000041 'protos/perfetto/trace/filesystem/inode_file_map.proto',
Hector Dearmane0e57802018-11-21 16:09:56 +000042 'protos/perfetto/trace/ftrace/binder.proto',
43 'protos/perfetto/trace/ftrace/block.proto',
44 'protos/perfetto/trace/ftrace/clk.proto',
45 'protos/perfetto/trace/ftrace/ext4.proto',
46 'protos/perfetto/trace/ftrace/f2fs.proto',
47 'protos/perfetto/trace/ftrace/filemap.proto',
48 'protos/perfetto/trace/ftrace/ftrace.proto',
49 'protos/perfetto/trace/ftrace/ftrace_event.proto',
50 'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto',
51 'protos/perfetto/trace/ftrace/ftrace_stats.proto',
Hector Dearmane92c6742018-11-22 21:42:39 +000052 'protos/perfetto/trace/ftrace/generic.proto',
Hector Dearmane0e57802018-11-21 16:09:56 +000053 'protos/perfetto/trace/ftrace/kmem.proto',
54 'protos/perfetto/trace/ftrace/lowmemorykiller.proto',
55 'protos/perfetto/trace/ftrace/power.proto',
Hector Dearmane0e57802018-11-21 16:09:56 +000056 'protos/perfetto/trace/ftrace/sched.proto',
57 'protos/perfetto/trace/ftrace/signal.proto',
58 'protos/perfetto/trace/ftrace/task.proto',
59 'protos/perfetto/trace/ftrace/vmscan.proto',
Hector Dearmane92c6742018-11-22 21:42:39 +000060 'protos/perfetto/trace/ps/process_stats.proto',
61 'protos/perfetto/trace/ps/process_tree.proto',
62 'protos/perfetto/trace/sys_stats/sys_stats.proto',
63 'protos/perfetto/trace/trace.proto',
64 'protos/perfetto/trace/trace_packet.proto',
Hector Dearman55ef3e02018-04-11 17:28:55 +010065)
66
67MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'
68
69REPLACEMENT_HEADER = '''
70// AUTOGENERATED - DO NOT EDIT
71// ---------------------------
72// This file has been generated by
73// AOSP://external/perfetto/%s
74// merging the perfetto config protos.
75// This fused proto is intended to be copied in:
76// - Android tree, for statsd.
77// - Google internal repos.
78
79syntax = "proto2";
80
81package perfetto.protos;
82'''
83
84def merge_protos(proto_paths, output_path):
85 root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
86 merged_content = ''
87 for proto in proto_paths:
88 path = os.path.join(root_dir, proto)
89 with open(path) as f:
90 content = f.read()
91
92 # Remove header
93 header = re.match(r'\/(\*|\/)(?:.|\s)*?package.*;\n', content)
94 header = header.group(0)
95 content = content[len(header):]
96 if merged_content == '':
97 merged_content += REPLACEMENT_HEADER.lstrip() % __file__
98 content = re.sub(r'^import.*?\n', '', content, flags=re.MULTILINE)
99 content = re.sub(r'TODO\([^)]*\):', 'TODO:', content)
100 merged_content += '\n// Begin of %s\n' % proto
101 merged_content += content
102 merged_content += '\n// End of %s\n' % proto
103
104 definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {'
105 definitions = re.finditer(definitions_re, merged_content, re.MULTILINE)
106 types = set((match.group(1) for match in definitions))
107
108 uses_re = r'^( +)(?:repeated)?\s?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);'
109 uses = re.finditer(uses_re, merged_content, re.MULTILINE)
110 substitutions = []
111 for use in uses:
112 everything = use.group(0)
113 indentation = use.group(1)
114 used_type = use.group(2)
115 field_number = use.group(3)
116 if used_type not in types:
Hector Dearman55ef3e02018-04-11 17:28:55 +0100117 replacement = '{}// removed field with id {}'.format(
118 indentation, field_number)
119 substitutions.append((everything, replacement))
120
121 for before, after in substitutions:
122 merged_content = merged_content.replace(before, after)
123
124 out_path = os.path.join(root_dir, output_path)
125
126 prev_content = None
127 if os.path.exists(out_path):
128 with open(out_path, 'rb') as fprev:
129 prev_content = fprev.read()
130
131 if prev_content == merged_content:
132 return True
133
134 if '--check-only' in sys.argv:
135 return False
136
137 print('Updating {}'.format(output_path))
138 with open(out_path, 'wb') as fout:
139 fout.write(merged_content)
140 return True
141
142def main():
143 config_result = merge_protos(CONFIG_PROTOS, MERGED_CONFIG_PROTO)
144 trace_result = merge_protos(TRACE_PROTOS, MERGED_TRACE_PROTO)
145 return 0 if config_result and trace_result else 1
146
147if __name__ == '__main__':
148 sys.exit(main())