1import os
2import sys
3import shutil
4
5cwd_path = os.getcwd()
6sys.path.append(os.path.join(os.path.dirname(cwd_path), 'rt-thread', 'tools'))
7
8
9# BSP dist function
10def dist_do_building(BSP_ROOT, dist_dir):
11    from mkdist import bsp_copy_files
12    import rtconfig
13
14    print("=> copy ch32 bsp library")
15    library_dir = os.path.join(dist_dir, 'Libraries')
16    library_path = os.path.join(os.path.dirname(BSP_ROOT), 'Libraries')
17    bsp_copy_files(
18        os.path.join(library_path, rtconfig.BSP_LIBRARY_TYPE),
19        os.path.join(library_dir, rtconfig.BSP_LIBRARY_TYPE),
20    )
21
22    print("=> copy bsp drivers")
23    bsp_copy_files(os.path.join(library_path, 'ch32_drivers'), os.path.join(library_dir, 'ch32_drivers'))
24    shutil.copyfile(os.path.join(library_path, 'Kconfig'), os.path.join(library_dir, 'Kconfig'))
25    # change RTT_ROOT in Kconfig
26    if not os.path.isfile(os.path.join(dist_dir, 'Kconfig')):
27        return
28
29    with open(os.path.join(dist_dir, 'Kconfig'), 'r') as f:
30        data = f.readlines()
31    with open(os.path.join(dist_dir, 'Kconfig'), 'w') as f:
32        for line in data:
33            if line.find('source') != -1 and line.find('../libraries') != -1:
34                line = line.replace('../Libraries', 'Libraries')
35            f.write(line)
36