aboutsummaryrefslogtreecommitdiff
path: root/tools/gen_merged_protos
blob: 5a0a97a5ec4348baa4667e26452c0e23781631f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import re
import sys

COMMON_PROTOS = (
  'protos/perfetto/common/android_log_constants.proto',
  'protos/perfetto/common/sys_stats_counters.proto',
)

CONFIG_PROTOS = (
  'protos/perfetto/config/android/android_log_config.proto',
  'protos/perfetto/config/chrome/chrome_config.proto',
  'protos/perfetto/config/data_source_config.proto',
  'protos/perfetto/config/ftrace/ftrace_config.proto',
  'protos/perfetto/config/inode_file/inode_file_config.proto',
  'protos/perfetto/config/power/android_power_config.proto',
  'protos/perfetto/config/process_stats/process_stats_config.proto',
  'protos/perfetto/config/sys_stats/sys_stats_config.proto',
  'protos/perfetto/config/test_config.proto',
  'protos/perfetto/config/trace_config.proto',
  'protos/perfetto/config/profiling/heapprofd_config.proto',
  'protos/perfetto/config/android/packages_list_config.proto',
)

MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto'

TRACE_PROTOS = (
  'protos/perfetto/common/trace_stats.proto', # only referenced by trace protos
  'protos/perfetto/trace/android/android_log.proto',
  'protos/perfetto/trace/android/packages_list.proto',
  'protos/perfetto/trace/clock_snapshot.proto',
  'protos/perfetto/trace/filesystem/inode_file_map.proto',
  'protos/perfetto/trace/ftrace/binder.proto',
  'protos/perfetto/trace/ftrace/block.proto',
  'protos/perfetto/trace/ftrace/clk.proto',
  'protos/perfetto/trace/ftrace/ext4.proto',
  'protos/perfetto/trace/ftrace/f2fs.proto',
  'protos/perfetto/trace/ftrace/filemap.proto',
  'protos/perfetto/trace/ftrace/ftrace.proto',
  'protos/perfetto/trace/ftrace/ftrace_event.proto',
  'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto',
  'protos/perfetto/trace/ftrace/ftrace_stats.proto',
  'protos/perfetto/trace/ftrace/generic.proto',
  'protos/perfetto/trace/ftrace/kmem.proto',
  'protos/perfetto/trace/ftrace/lowmemorykiller.proto',
  'protos/perfetto/trace/ftrace/mm_event.proto',
  'protos/perfetto/trace/ftrace/power.proto',
  'protos/perfetto/trace/ftrace/raw_syscalls.proto',
  'protos/perfetto/trace/ftrace/sched.proto',
  'protos/perfetto/trace/ftrace/signal.proto',
  'protos/perfetto/trace/ftrace/systrace.proto',
  'protos/perfetto/trace/ftrace/task.proto',
  'protos/perfetto/trace/ftrace/vmscan.proto',
  'protos/perfetto/trace/interned_data/interned_data.proto',
  'protos/perfetto/trace/power/battery_counters.proto',
  'protos/perfetto/trace/power/power_rails.proto',
  'protos/perfetto/trace/profiling/profile_packet.proto',
  'protos/perfetto/trace/ps/process_stats.proto',
  'protos/perfetto/trace/ps/process_tree.proto',
  'protos/perfetto/trace/sys_stats/sys_stats.proto',
  'protos/perfetto/trace/system_info.proto',
  'protos/perfetto/trace/trace.proto',
  'protos/perfetto/trace/trace_packet.proto',
  'protos/perfetto/trace/track_event/debug_annotation.proto',
  'protos/perfetto/trace/track_event/process_descriptor.proto',
  'protos/perfetto/trace/track_event/task_execution.proto',
  'protos/perfetto/trace/track_event/thread_descriptor.proto',
  'protos/perfetto/trace/track_event/track_event.proto',
  'protos/perfetto/trace/trigger.proto',
)

MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'

REPLACEMENT_HEADER = '''
// AUTOGENERATED - DO NOT EDIT
// ---------------------------
// This file has been generated by
// AOSP://external/perfetto/%s
// merging the perfetto config protos.
// This fused proto is intended to be copied in:
//  - Android tree, for statsd.
//  - Google internal repos.

syntax = "proto2";

package perfetto.protos;
'''

def merge_protos(proto_paths, output_path):
  root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  merged_content = ''
  for proto in proto_paths:
    path = os.path.join(root_dir, proto)
    with open(path) as f:
      content = f.read()

    # Remove header
    header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content)
    header = header.group(0)
    content = content[len(header):]
    if merged_content == '':
      merged_content += REPLACEMENT_HEADER.lstrip() % __file__
    content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE)
    merged_content += '\n// Begin of %s\n' % proto
    merged_content += content
    merged_content += '\n// End of %s\n' % proto

  definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {'
  definitions = re.finditer(definitions_re, merged_content, re.MULTILINE)
  types = set((match.group(1) for match in definitions))

  uses_re = r'^( +)(?:repeated)?(?:optional)?\s'\
            r'?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);'
  uses = re.finditer(uses_re, merged_content, re.MULTILINE)
  substitutions = []
  for use in uses:
    everything = use.group(0)
    indentation = use.group(1)
    used_type = use.group(2)
    field_number = use.group(3)
    if used_type not in types:
      replacement = '{}// removed field with id {}'.format(
          indentation, field_number)
      substitutions.append((everything, replacement))

  for before, after in substitutions:
    merged_content = merged_content.replace(before, after)

  out_path = os.path.join(root_dir, output_path)

  prev_content = None
  if os.path.exists(out_path):
    with open(out_path, 'rb') as fprev:
      prev_content = fprev.read()

  if prev_content == merged_content:
    return True

  if '--check-only' in sys.argv:
    return False

  print('Updating {}'.format(output_path))
  with open(out_path, 'wb') as fout:
    fout.write(merged_content)
  return True

def main():
  config_result = merge_protos(COMMON_PROTOS + CONFIG_PROTOS,
                               MERGED_CONFIG_PROTO)
  trace_result = merge_protos(COMMON_PROTOS + TRACE_PROTOS + CONFIG_PROTOS,
                              MERGED_TRACE_PROTO)
  return 0 if config_result and trace_result else 1

if __name__ == '__main__':
  sys.exit(main())