1import os
2import sys
3
4def usage():
5    print('%s all     -- build all bsp' % os.path.basename(sys.argv[0]))
6    print('%s clean   -- clean all bsp' % os.path.basename(sys.argv[0]))
7    print('%s update  -- update all prject files' % os.path.basename(sys.argv[0]))
8
9BSP_ROOT = os.path.join("..", "..", "bsp")
10
11if len(sys.argv) != 2:
12    usage()
13    sys.exit(0)
14
15def update_project_file(project_dir):
16    if os.path.isfile(os.path.join(project_dir, 'template.Uv2')):
17        print('prepare MDK3 project file on ' + project_dir)
18        command = ' --target=mdk -s'
19        os.system('scons --directory=' + project_dir + command + ' > 1.txt')
20
21    if os.path.isfile(os.path.join(project_dir, 'template.uvproj')):
22        print('prepare MDK4 project file on ' + project_dir)
23        command = ' --target=mdk4 -s'
24        os.system('scons --directory=' + project_dir + command + ' > 1.txt')
25
26    if os.path.isfile(os.path.join(project_dir, 'template.uvprojx')):
27        print('prepare MDK5 project file on ' + project_dir)
28        command = ' --target=mdk5 -s'
29        os.system('scons --directory=' + project_dir + command + ' > 1.txt')
30
31    if os.path.isfile(os.path.join(project_dir, 'template.ewp')):
32        print('prepare IAR project file on ' + project_dir)
33        command = ' --target=iar -s'
34        os.system('scons --directory=' + project_dir + command + ' > 1.txt')
35
36def update_all_project_files(root_path):
37    # current path is dir
38    if os.path.isdir(root_path):
39        projects = os.listdir(root_path)
40        # is a project path?
41        if "SConstruct" in projects:
42            try:
43                # update rtconfig.h and .config
44                if "Kconfig" in projects:
45                    if "win32" in sys.platform:
46                        retval = os.getcwd()
47                        os.chdir(root_path)
48                        os.system("menuconfig --silent")
49                        os.chdir(retval)
50                    else:
51                        os.system('scons --pyconfig-silent -C {0}'.format(root_path))
52                update_project_file(root_path)
53            except Exception as e:
54                print("error message: {}".format(e))
55                sys.exit(-1)
56        else:
57            for i in projects:
58                new_root_path = os.path.join(root_path, i)
59                update_all_project_files(new_root_path)
60
61# get command options
62command = ''
63if sys.argv[1] == 'all':
64    command = ' '
65elif sys.argv[1] == 'clean':
66    command = ' -c'
67elif sys.argv[1] == 'update':
68    print('begin to update all the bsp projects')
69
70    from stm32_update import stm32_update
71    stm32_update(os.path.join(BSP_ROOT, 'stm32'))
72
73    update_all_project_files(BSP_ROOT)
74
75    print('finished!')
76    sys.exit(0)
77else:
78    usage()
79    sys.exit(0)
80
81projects = os.listdir(BSP_ROOT)
82for item in projects:
83    project_dir = os.path.join(BSP_ROOT, item)
84    if os.path.isfile(os.path.join(project_dir, 'SConstruct')):
85        if os.system('scons --directory=' + project_dir + command) != 0:
86            print('build failed!!')
87            break
88