Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 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 | |
| 33 | from __future__ import print_function |
| 34 | import argparse |
| 35 | import os |
| 36 | import subprocess |
| 37 | import sys |
| 38 | import tempfile |
| 39 | |
| 40 | |
Hector Dearman | 7f37dc8 | 2018-01-25 12:25:40 +0000 | [diff] [blame] | 41 | HEADER = """# 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 | |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 58 | def command(*args): |
| 59 | subprocess.check_call(args, stdout=sys.stdout, stderr=sys.stderr) |
| 60 | |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 61 | def 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') |
Hector Dearman | 7f37dc8 | 2018-01-25 12:25:40 +0000 | [diff] [blame] | 75 | parser.add_argument('--update-build-files', dest='update_build_files', |
| 76 | action='store_true', |
| 77 | help='should update metadata') |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 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 |
Hector Dearman | 7f37dc8 | 2018-01-25 12:25:40 +0000 | [diff] [blame] | 84 | update_build_files = args.update_build_files |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 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)) |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 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 | |
Isabelle Taylor | 6498fcf | 2018-02-06 11:35:05 +0000 | [diff] [blame] | 93 | # Run ftrace_proto_gen |
| 94 | command(gen_path, whitelist_path, input_dir, output_dir) |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 95 | |
Hector Dearman | 7f37dc8 | 2018-01-25 12:25:40 +0000 | [diff] [blame] | 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 | |
Hector Dearman | 46c66db | 2017-10-26 11:26:28 +0100 | [diff] [blame] | 107 | |
| 108 | if __name__ == '__main__': |
| 109 | sys.exit(main()) |