1#
2# Copyright (c) 2006-2023, RT-Thread Development Team
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6# Change Logs:
7# Date           Author       Notes
8# 2023-06-27     dejavudwh    the first version
9#
10
11import subprocess
12import logging
13import os
14
15CONFIG_BSP_USING_X = ["CONFIG_BSP_USING_UART", "CONFIG_BSP_USING_I2C", "CONFIG_BSP_USING_SPI", "CONFIG_BSP_USING_ADC", "CONFIG_BSP_USING_DAC"]
16
17def init_logger():
18    log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
19    date_format = '%Y-%m-%d  %H:%M:%S %a '
20    logging.basicConfig(level=logging.INFO,
21                        format=log_format,
22                        datefmt=date_format,
23                        )
24
25def diff():
26    result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE)
27    file_list = result.stdout.decode().strip().split('\n')
28    logging.info(file_list)
29    bsp_paths = set()
30    for file in file_list:
31        if "bsp/" in file:
32            logging.info("Modifed file: {}".format(file))
33            bsp_paths.add(file)
34
35    dirs = set()
36    for dir in bsp_paths:
37        dir = os.path.dirname(dir)
38        while "bsp/" in dir:
39            files = os.listdir(dir)
40            if ".config" in files and "rt-thread.elf" not in files and not dir.endswith("bsp"):
41                logging.info("Found bsp path: {}".format(dir))
42                dirs.add(dir)
43                break
44            new_dir = os.path.dirname(dir)
45            dir = new_dir
46
47    return dirs
48
49def check_config_in_line(line):
50    for config in CONFIG_BSP_USING_X:
51        if config in line and '#' in line:
52            logging.info("Found in {}".format(line))
53            return config
54
55    return ""
56
57def check_config_in_file(file_path):
58    configs = set()
59    found = False
60    try:
61        with open(file_path, 'r') as file:
62            for line in file:
63                line.strip()
64                if found:
65                    res = check_config_in_line(line)
66                    if res:
67                        configs.add(res)
68                elif "On-chip Peripheral Drivers" in line:
69                    logging.info("Found On-chip Peripheral Drivers")
70                    found = True
71    except FileNotFoundError:
72        logging.error("The .config file does not exist for this BSP, please recheck the file directory!")
73
74    return configs
75
76def modify_config(file_path, configs):
77    with open(file_path + "/rtconfig.h", 'a') as file:
78        for item in configs:
79            define1 = item.replace("CONFIG_BSP", "BSP")
80            define2 = item.replace("CONFIG_BSP", "RT")
81            file.write("#define " + define1 + "\n")
82            file.write("#define " + define2 + "\n")
83
84def recompile_bsp(dir):
85    logging.info("recomplie bsp: {}".format(dir))
86    os.system("scons -C " + dir)
87
88if __name__ == '__main__':
89    init_logger()
90    recompile_bsp_dirs = diff()
91    for dir in recompile_bsp_dirs:
92        dot_config_path = dir + "/" + ".config"
93        configs = check_config_in_file(dot_config_path)
94        logging.info("add config:")
95        logging.info(configs)
96        logging.info("Add configurations and recompile!")
97        modify_config(dir, configs)
98        recompile_bsp(dir)