1import os
2import re
3import sys
4import shutil
5
6cwd_path = os.getcwd()
7sys.path.append(os.path.join(os.path.dirname(cwd_path), 'rt-thread', 'tools'))
8
9
10def bsp_update_kconfig_library(dist_dir):
11    # change RTT_ROOT in Kconfig
12    if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
13        return
14
15    with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
16        data = f.readlines()
17    with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
18        for line in data:
19            if line.find('source') != -1 and line.find('../libraries') != -1:
20                line = line.replace('../libraries', 'libraries')
21            f.write(line)
22
23
24# BSP dist function
25def dist_do_building(BSP_ROOT, dist_dir):
26    from mkdist import bsp_copy_files
27    import rtconfig
28
29    print("=> copy ht32 bsp library")
30    library_dir = os.path.join(dist_dir, 'libraries')
31    library_path = os.path.join(os.path.dirname(BSP_ROOT), 'libraries')
32    bsp_copy_files(os.path.join(library_path, rtconfig.BSP_LIBRARY_TYPE), os.path.join(library_dir, rtconfig.BSP_LIBRARY_TYPE))
33
34    print("=> copy bsp drivers")
35    bsp_copy_files(os.path.join(library_path, 'ht32_drivers'), os.path.join(library_dir, 'ht32_drivers'))
36    shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig'))
37    bsp_update_kconfig_library(dist_dir)
38
39
40def get_source(ic_model, file_path, system_path, base_path):
41    source_path = []
42    files_list = []
43    readafter = 0
44    if not os.path.isfile(file_path):
45        return
46
47    with open(file_path, 'r') as file:
48        # content = file.read()
49        for line in file:
50            if readafter == 2 and line.find('>') != -1:
51                break
52            if readafter == 2:
53                files_list.append(line.strip())
54            if line.find(ic_model) != -1:
55                readafter = 1
56            if readafter == 1 and line.find('<') != -1:
57                readafter = 2
58    for line in files_list:
59        if line.find('system') != -1:
60            source_path.append(os.path.join(system_path, line.strip()))
61        else:
62            source_path.append(os.path.join(base_path, line.strip()))
63    return source_path
64