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).replace('\\', '/') 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 56option go_package = "github.com/google/perfetto/perfetto_proto"; 57''' 58 59 60def 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): 75 merged_content = REPLACEMENT_HEADER.lstrip() % SELF_PATH 76 added_files = set() 77 for proto in proto_paths: 78 if proto in added_files: 79 continue 80 added_files.add(proto) 81 82 path = os.path.join(PROJECT_ROOT, proto) 83 with open(path, 'r', encoding='utf-8') as f: 84 content = f.read() 85 86 # Remove header 87 header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content) 88 header = header.group(0) 89 content = content[len(header):] 90 91 content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE) 92 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 100 # 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+);' 105 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: 113 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 120 return merged_content 121 122 123def merge_protos(root_paths, output_path): 124 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) 128 129 out_path = os.path.join(PROJECT_ROOT, output_path) 130 prev_content = None 131 if os.path.exists(out_path): 132 with open(out_path, 'r', encoding='utf-8') as fprev: 133 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)) 142 with open(out_path, 'w', encoding='utf-8') as fout: 143 fout.write(merged_content) 144 return True 145 146 147def main(): 148 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) 151 return 0 if result else 1 152 153 154if __name__ == '__main__': 155 sys.exit(main()) 156