1#!/usr/bin/env python
2# Copyright (C) 2017 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
16# Generates proto definitions from ftrace format files.
17# Arguments:
18#   path to ftrace_proto_gen
19#   path to input directory full of format files
20#   path to output directory for proto definitions files
21#   Either:
22#    --whitelist PATH : path to a list of events to generate
23#    --event EVENT : a single event to generate
24#
25# Example:
26# ./tools/update_protos.py
27#    out/linux/ftrace_proto_gen
28#   libftrace/test/data/android_seed_N2F62_3.10.49
29#   protos/ftrace
30#   --whitelist tools/ftrace_proto_gen/event_whitelist
31
32
33from __future__ import print_function
34import argparse
35import os
36import subprocess
37import sys
38import tempfile
39
40
41HEADER = """# Copyright (C) 2018 The Android Open Source Project
42#
43# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
45# You may obtain a copy of the License at
46#
47#      http://www.apache.org/licenses/LICENSE-2.0
48#
49# Unless required by applicable law or agreed to in writing, software
50# distributed under the License is distributed on an "AS IS" BASIS,
51# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
52# See the License for the specific language governing permissions and
53# limitations under the License.
54
55"""
56
57
58def command(*args):
59  subprocess.check_call(args, stdout=sys.stdout, stderr=sys.stderr)
60
61def main():
62  parser = argparse.ArgumentParser(description='Generate protos.')
63  parser.add_argument('ftrace_proto_gen',
64                      help='an ftrace_proto_gen binary')
65  parser.add_argument('input_dir',
66                      help='input directory')
67  parser.add_argument('output_dir',
68                      help='output directory')
69  parser.add_argument('--whitelist', dest='whitelist_path',
70                      default=None,
71                      help='path to whitelist of events')
72  parser.add_argument('--event', dest='event',
73                      default=None,
74                      help='output directory')
75  parser.add_argument('--update-build-files', dest='update_build_files',
76                      action='store_true',
77                      help='should update metadata')
78  args = parser.parse_args()
79
80  gen_path = args.ftrace_proto_gen
81  input_dir = args.input_dir
82  output_dir = args.output_dir
83  whitelist_path = args.whitelist_path
84  update_build_files = args.update_build_files
85
86  if whitelist_path is not None and not os.path.isfile(whitelist_path):
87    parser.error('Whitelist file {} does not exist.'.format(whitelist_path))
88  if not os.path.isdir(input_dir):
89    parser.error('Input directory {} does not exist.'.format(input_dir))
90  if not os.path.isdir(output_dir):
91    parser.error('Output directory {} does not exist.'.format(output_dir))
92
93  # Run ftrace_proto_gen
94  command(gen_path, whitelist_path, input_dir, output_dir)
95
96  if update_build_files:
97    names = sorted(os.listdir(output_dir))
98    names = [name for name in names if name.endswith('.proto')]
99    names = ''.join(['  "{}",\n'.format(name) for name in names])
100    body = 'ftrace_proto_names = [\n{}]'.format(names)
101    with open(os.path.join(output_dir, 'all_protos.gni'), 'wb') as f:
102      f.write(HEADER)
103      f.write(body)
104
105  return 0
106
107
108if __name__ == '__main__':
109  sys.exit(main())
110