1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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.
15import os
16import subprocess
17import sys
18import argparse
19import logging
20from pathlib import Path
21
22
23class Common:
24    this_file = os.path.basename(__file__)
25    logging.basicConfig(
26        format="%(asctime)s %(levelname)s %(message)s", level=logging.INFO
27    )
28    logger = logging.getLogger(this_file)
29
30
31def main():
32    parser = argparse.ArgumentParser(description="FTrace C++ code generator.")
33    parser.add_argument(
34        "-v", dest="version", required=True, type=str, help="device kernel version"
35    )
36
37    args = parser.parse_args(sys.argv[1:])
38    version = args.version
39
40    version_path = "device_kernel_version/{}".format(version)
41    events_file = "{}/events".format(version_path)
42    if not Path(events_file).is_dir():
43        Common.logger.error(
44            "device kernel events directory does not exist(%s)" % (events_file)
45        )
46        exit(1)
47
48    # generate proto file
49    proto_path = "{}/../../../../protos/types/plugins/ftrace_data/{}/".format(
50        os.getcwd(), version
51    )
52    proto_cmd = "python ftrace_proto_generator.py -a allowlist.txt -e {} -o {}".format(
53        events_file, proto_path
54    )
55    proto_sub = subprocess.Popen(proto_cmd, shell=True)
56    proto_sub.wait()
57    if proto_sub.returncode != 0:
58        Common.logger.error("Execution python failed! cmd: %s" % (proto_cmd))
59        exit(2)
60
61    # generate parsers cpp
62    cpp_cmd = "python {}/ftrace_cpp_generator.py -a allowlist.txt -e {}".format(
63        version_path, events_file
64    )
65    parsers_cmd = "{} -p {}/event_parsers/".format(cpp_cmd, version_path)
66    parsers_sub = subprocess.Popen(parsers_cmd, shell=True)
67    parsers_sub.wait()
68    if parsers_sub.returncode != 0:
69        Common.logger.error("Execution python failed! cmd: %s" % (parsers_cmd))
70        exit(3)
71
72    # generate formatters cpp
73    formatters_cmd = "{} -f {}/event_formatters/".format(cpp_cmd, version_path)
74    formatters_sub = subprocess.Popen(formatters_cmd, shell=True)
75    formatters_sub.wait()
76    if formatters_sub.returncode != 0:
77        Common.logger.error("Execution python failed! cmd: {}".format(parsers_cmd))
78        exit(4)
79
80
81if __name__ == "__main__":
82    main()
83    print("refresh device kernel code sucess!")
84