blob: 135fd024115df64d5650ec607dffb3a231bf880f [file] [log] [blame]
Primiano Tuccic5010802018-01-19 17:13:21 +00001#!/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
16import os
17import re
18import subprocess
19import sys
20
21PROTOS = (
Oystein Eftevaagab3b1b22018-03-08 16:27:06 -080022 'protos/perfetto/config/chrome/chrome_config.proto',
Primiano Tuccidae35652018-03-29 18:32:02 +010023 'protos/perfetto/config/inode_file/inode_file_config.proto',
Hector Dearman1b9c58a2018-03-29 18:45:06 +010024 'protos/perfetto/config/process_stats/process_stats_config.proto',
Primiano Tuccic5010802018-01-19 17:13:21 +000025 'protos/perfetto/config/data_source_config.proto',
Hector Dearmana89cc572018-02-23 12:02:58 +000026 'protos/perfetto/config/ftrace/ftrace_config.proto',
Lalit Maganti3f5705c2018-03-09 12:09:44 +000027 'protos/perfetto/config/test_config.proto',
Primiano Tuccic5010802018-01-19 17:13:21 +000028 'protos/perfetto/config/trace_config.proto',
29)
30
Primiano Tucci08882a42018-01-19 22:09:01 +000031MERGED_OUT_PROTO = 'protos/perfetto/config/perfetto_config.proto'
Primiano Tuccic5010802018-01-19 17:13:21 +000032
Primiano Tucci08882a42018-01-19 22:09:01 +000033REPLACEMENT_HEADER = '''
34// AUTOGENERATED - DO NOT EDIT
35// ---------------------------
36// This file has been generated by
37// AOSP://external/perfetto/%s
38// merging the perfetto config protos.
39// This fused proto is intended to be copied in:
40// - Android tree, for statsd.
41// - Google internal repos.
Primiano Tuccic5010802018-01-19 17:13:21 +000042
Primiano Tucci08882a42018-01-19 22:09:01 +000043syntax = "proto2";
44
45package perfetto.protos;
Primiano Tuccic5010802018-01-19 17:13:21 +000046'''
47
48def main():
49 root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
50 merged_content = ''
51 for proto in PROTOS:
52 path = os.path.join(root_dir, proto)
53 with open(path) as f:
54 content = f.read()
Primiano Tucci08882a42018-01-19 22:09:01 +000055
56 # Remove header
57 header = re.match(r'\/\*(?:.|\s)*?package.*;\n', content)
58 header = header.group(0)
59 content = content[len(header):]
60 if merged_content == '':
61 merged_content += REPLACEMENT_HEADER.lstrip() % __file__
Oystein Eftevaagab3b1b22018-03-08 16:27:06 -080062 content = re.sub(r'^import.*?\n', '', content, flags=re.MULTILINE)
Primiano Tucci08882a42018-01-19 22:09:01 +000063 content = re.sub(r'TODO\([^)]*\):', 'TODO:', content)
64 merged_content += '\n// Begin of %s\n' % proto
65 merged_content += content
66 merged_content += '\n// End of %s\n' % proto
Primiano Tuccic5010802018-01-19 17:13:21 +000067
68 out_path = os.path.join(root_dir, MERGED_OUT_PROTO)
69
70 prev_content = None
71 if os.path.exists(out_path):
72 with open(out_path, 'rb') as fprev:
73 prev_content = fprev.read()
74
75 if prev_content == merged_content:
76 return 0
77
78 if '--check-only' in sys.argv:
79 return 1
80
81 print 'Updating %s' % MERGED_OUT_PROTO
82 with open(out_path, 'wb') as fout:
83 fout.write(merged_content)
84 return 0
85
86if __name__ == '__main__':
87 sys.exit(main())