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