• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2022 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from dataclasses import replace
16import os
17import shutil
18import sys
19import subprocess
20
21
22def _run_cmd(cmd):
23    print(cmd)
24    res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
25                           stderr=subprocess.PIPE)
26    sout, serr = res.communicate()
27    return  sout.rstrip().decode('utf-8'), serr, res.returncode
28
29
30def _make_dir(file_path):
31    is_exist = os.path.exists(file_path)
32    print (file_path)
33    if not is_exist:
34        os.makedirs(file_path)
35    print (file_path)
36
37
38def _read_file(file_path):
39    file_handler = open(file_path, 'r+', encoding='utf-8')
40    content = file_handler.read()
41    file_handler.close
42
43    return content
44
45def _write_file(file_path, content):
46    file_handler = open(file_path, 'w', encoding='utf-8')
47    file_handler.write(content)
48    file_handler.close
49
50
51def _write_internal_methods(new_init_file, internal_method_name, init_file_list):
52    new_init_file.write("void init_" + internal_method_name + "(void);\n")
53    new_init_file.write("void init_" + internal_method_name + "(void)\n{\n")
54    for init_file_name in init_file_list:
55        new_init_file.write(init_file_name)
56    new_init_file.write("}")
57
58
59def _need_rebuild(src_file, dest_file, src_md5_file):
60    if os.path.exists(src_file) and os.path.exists(dest_file) and os.path.exists(src_md5_file):
61        this_md5, err, returncode = _run_cmd("md5sum " + src_file + " | awk '{print $1}'")
62        last_md5, err, returncode = _run_cmd("cat " + src_md5_file)
63        if this_md5 == last_md5:
64            return 0
65        else:
66            return 1
67    else:
68        print("src_file, dest_file or src_md5_file doesn't exist. Generate new md5 file.")
69        this_md5, err, returncode = _run_cmd("md5sum " + src_file + " | awk '{print $1}' >" + src_md5_file)
70        return 1
71
72
73def main():
74    # sys.argv[1]: filter pattern
75    # sys.argv[2]: exclude file list
76    # sys.argv[3]: output file
77    # sys.argv[4]: internal method name
78    ori_path = os.path.dirname(os.path.abspath(__file__))
79    out_path = os.path.dirname(os.path.dirname(os.path.dirname(ori_path)))
80    out_path += "/out"
81    gen_path = out_path + "/gen"
82    md5_path = out_path + "/gen/md5"
83    _make_dir(gen_path)
84    _make_dir(md5_path)
85    new_init_file_name = gen_path + "/" + sys.argv[3]
86    if os.path.exists(new_init_file_name):
87        os.remove(new_init_file_name)
88    os.mknod(new_init_file_name)
89    new_init_file = open(new_init_file_name, 'a', encoding='utf-8')
90
91    init_file_list = []
92    for filter_file in os.listdir(ori_path):
93        file_name, extension = os.path.splitext(filter_file)
94        if (sys.argv[1] in filter_file) & (extension == ".c") & (filter_file not in sys.argv[2]):
95            print(filter_file)
96            src_path = os.path.join(ori_path, filter_file)
97            src_md5_file = os.path.join(md5_path, filter_file + ".md5")
98            dst_path = os.path.join(gen_path, filter_file)
99            need_rebuild = _need_rebuild(src_path, dst_path, src_md5_file)
100            if need_rebuild:
101                shutil.copy(src_path, dst_path)
102            content = _read_file(dst_path)
103            if "_init(void)" in content:
104                replace_init_text = filter_file.rstrip("\.c") + "_init(void)"
105                new_content = content.replace("_init(void)", replace_init_text)
106                if need_rebuild:
107                    _write_file(dst_path, new_content)
108
109                new_init_file.write("extern " + "void  " + replace_init_text + ";\n")
110                init_file_list.append(filter_file.rstrip("\.c") + "_init" + "();\n")
111
112    internal_method_name = sys.argv[4]
113    _write_internal_methods(new_init_file, internal_method_name, init_file_list)
114    new_init_file.close
115
116
117if __name__ == '__main__':
118    sys.exit(main())
119