blob: 18ce3bdfb97ea7fdf7a0c4ffe25680e38d39ccd8 [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 = (
22 'protos/perfetto/config/data_source_config.proto',
Hector Dearmana89cc572018-02-23 12:02:58 +000023 'protos/perfetto/config/ftrace/ftrace_config.proto',
Primiano Tuccic5010802018-01-19 17:13:21 +000024 'protos/perfetto/config/trace_config.proto',
25)
26
Primiano Tucci08882a42018-01-19 22:09:01 +000027MERGED_OUT_PROTO = 'protos/perfetto/config/perfetto_config.proto'
Primiano Tuccic5010802018-01-19 17:13:21 +000028
Primiano Tucci08882a42018-01-19 22:09:01 +000029REPLACEMENT_HEADER = '''
30// AUTOGENERATED - DO NOT EDIT
31// ---------------------------
32// This file has been generated by
33// AOSP://external/perfetto/%s
34// merging the perfetto config protos.
35// This fused proto is intended to be copied in:
36// - Android tree, for statsd.
37// - Google internal repos.
Primiano Tuccic5010802018-01-19 17:13:21 +000038
Primiano Tucci08882a42018-01-19 22:09:01 +000039syntax = "proto2";
40
41package perfetto.protos;
Primiano Tuccic5010802018-01-19 17:13:21 +000042'''
43
44def main():
45 root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
46 merged_content = ''
47 for proto in PROTOS:
48 path = os.path.join(root_dir, proto)
49 with open(path) as f:
50 content = f.read()
Primiano Tucci08882a42018-01-19 22:09:01 +000051
52 # Remove header
53 header = re.match(r'\/\*(?:.|\s)*?package.*;\n', content)
54 header = header.group(0)
55 content = content[len(header):]
56 if merged_content == '':
57 merged_content += REPLACEMENT_HEADER.lstrip() % __file__
58 content = re.sub(r'\nimport.*?\n', '', content)
59 content = re.sub(r'TODO\([^)]*\):', 'TODO:', content)
60 merged_content += '\n// Begin of %s\n' % proto
61 merged_content += content
62 merged_content += '\n// End of %s\n' % proto
Primiano Tuccic5010802018-01-19 17:13:21 +000063
64 out_path = os.path.join(root_dir, MERGED_OUT_PROTO)
65
66 prev_content = None
67 if os.path.exists(out_path):
68 with open(out_path, 'rb') as fprev:
69 prev_content = fprev.read()
70
71 if prev_content == merged_content:
72 return 0
73
74 if '--check-only' in sys.argv:
75 return 1
76
77 print 'Updating %s' % MERGED_OUT_PROTO
78 with open(out_path, 'wb') as fout:
79 fout.write(merged_content)
80 return 0
81
82if __name__ == '__main__':
83 sys.exit(main())