1#!/usr/bin/env python3 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 21import sys 22from codecs import open 23 24PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 25SELF_PATH = os.path.relpath(__file__, PROJECT_ROOT) 26 27CONFIG_PROTO_ROOTS = [ 28 'protos/perfetto/common/data_source_descriptor.proto', 29 'protos/perfetto/common/tracing_service_state.proto', 30 'protos/perfetto/config/trace_config.proto' 31] 32MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto' 33 34TRACE_PROTO_ROOTS = CONFIG_PROTO_ROOTS + [ 35 'protos/perfetto/trace/trace.proto', 36] 37MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto' 38 39METRICS_PROTOS_ROOTS = ['protos/perfetto/metrics/metrics.proto'] 40MERGED_METRICS_PROTO = 'protos/perfetto/metrics/perfetto_merged_metrics.proto' 41 42REPLACEMENT_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; 55''' 56 57 58def get_transitive_imports(rel_path, visited): 59 if rel_path in visited: 60 return [] 61 visited.add(rel_path) 62 with open(os.path.join(PROJECT_ROOT, rel_path), 'r', encoding='utf-8') as f: 63 content = f.read() 64 imports = re.findall(r'^import "(.*)";\n', content, flags=re.MULTILINE) 65 res = [] 66 for child in sorted(imports): 67 res += get_transitive_imports(child, visited) 68 res += [rel_path] 69 return res 70 71 72def merge_protos_content(proto_paths): 73 merged_content = REPLACEMENT_HEADER.lstrip() % SELF_PATH 74 added_files = set() 75 for proto in proto_paths: 76 if proto in added_files: 77 continue 78 added_files.add(proto) 79 80 path = os.path.join(PROJECT_ROOT, proto) 81 with open(path, 'r', encoding='utf-8') as f: 82 content = f.read() 83 84 # Remove header 85 header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content) 86 header = header.group(0) 87 content = content[len(header):] 88 89 content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE) 90 merged_content += '\n// Begin of %s\n' % proto 91 merged_content += content 92 merged_content += '\n// End of %s\n' % proto 93 94 definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {' 95 definitions = re.finditer(definitions_re, merged_content, re.MULTILINE) 96 types = set((match.group(1) for match in definitions)) 97 98 # Limitation: |types| doesn't track the nesting of messages, so a reference to 99 # a nested message (optional One.Two f = 1;) is simplified to its leafmost 100 # name (Two in this example). 101 uses_re = r'^( +)(?:repeated)?(?:optional)?\s?'\ 102 r'(?:[A-Z]\w+\.)*([A-Z]\w+)\s+[a-z]\w*\s*=\s*(\d+);' 103 uses = re.finditer(uses_re, merged_content, re.MULTILINE) 104 substitutions = [] 105 for use in uses: 106 everything = use.group(0) 107 indentation = use.group(1) 108 used_type = use.group(2) 109 field_number = use.group(3) 110 if used_type not in types: 111 replacement = '{}// removed field with id {}'.format( 112 indentation, field_number) 113 substitutions.append((everything, replacement)) 114 115 for before, after in substitutions: 116 merged_content = merged_content.replace(before, after) 117 118 return merged_content 119 120 121def merge_protos(root_paths, output_path): 122 all_protos = [] 123 for root_path in root_paths: 124 all_protos += get_transitive_imports(root_path, visited=set()) 125 merged_content = merge_protos_content(all_protos) 126 127 out_path = os.path.join(PROJECT_ROOT, output_path) 128 prev_content = None 129 if os.path.exists(out_path): 130 with open(out_path, 'r', encoding='utf-8') as fprev: 131 prev_content = fprev.read() 132 133 if prev_content == merged_content: 134 return True 135 136 if '--check-only' in sys.argv: 137 return False 138 139 print('Updating {}'.format(output_path)) 140 with open(out_path, 'w', encoding='utf-8') as fout: 141 fout.write(merged_content) 142 return True 143 144 145def main(): 146 result = merge_protos(CONFIG_PROTO_ROOTS, MERGED_CONFIG_PROTO) 147 result &= merge_protos(TRACE_PROTO_ROOTS, MERGED_TRACE_PROTO) 148 result &= merge_protos(METRICS_PROTOS_ROOTS, MERGED_METRICS_PROTO) 149 return 0 if result else 1 150 151 152if __name__ == '__main__': 153 sys.exit(main()) 154