|
1 | 1 | #!/usr/bin/python3 |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import sys |
4 | 5 | import xml.dom.minidom as XML |
| 6 | +import glob |
5 | 7 |
|
6 | | -# Read base configuration |
7 | | -base = "" |
8 | | -with open("iar_template.ipcf") as f: |
9 | | - base = f.read() |
| 8 | +def Main(): |
| 9 | + # Read base configuration |
| 10 | + base = "" |
| 11 | + with open("iar_template.ipcf") as f: |
| 12 | + base = f.read() |
10 | 13 |
|
11 | | -# Enumerate all device/host examples |
12 | | -dir_1 = os.listdir("../examples") |
13 | | -for dir_2 in dir_1: |
14 | | - if os.path.isdir("../examples/{}".format(dir_2)): |
15 | | - print(dir_2) |
16 | | - examples = os.listdir("../examples/{}".format(dir_2)) |
17 | | - for example in examples: |
18 | | - if os.path.isdir("../examples/{}/{}".format(dir_2, example)): |
19 | | - print("../examples/{}/{}".format(dir_2, example)) |
20 | | - conf = XML.parseString(base) |
21 | | - files = conf.getElementsByTagName("files")[0] |
22 | | - inc = conf.getElementsByTagName("includePath")[0] |
23 | | - # Add bsp inc |
24 | | - path = conf.createElement('path') |
25 | | - path_txt = conf.createTextNode("$TUSB_DIR$/hw") |
26 | | - path.appendChild(path_txt) |
27 | | - inc.appendChild(path) |
28 | | - # Add board.c/.h |
29 | | - grp = conf.createElement('group') |
30 | | - grp.setAttribute("name", "bsp") |
31 | | - path = conf.createElement('path') |
32 | | - path_txt = conf.createTextNode("$TUSB_DIR$/hw/bsp/board.c") |
33 | | - path.appendChild(path_txt) |
34 | | - grp.appendChild(path) |
35 | | - files.appendChild(grp) |
36 | | - # Add example's .c/.h |
37 | | - grp = conf.createElement('group') |
38 | | - grp.setAttribute("name", "example") |
39 | | - for file in os.listdir("../examples/{}/{}/src".format(dir_2, example)): |
40 | | - if file.endswith(".c") or file.endswith(".h"): |
41 | | - path = conf.createElement('path') |
42 | | - path.setAttribute("copyTo", "$PROJ_DIR$/{}".format(file)) |
43 | | - path_txt = conf.createTextNode("$TUSB_DIR$/examples/{0}/{1}/src/{2}".format(dir_2, example, file)) |
44 | | - path.appendChild(path_txt) |
45 | | - grp.appendChild(path) |
46 | | - files.appendChild(grp) |
47 | | - cfg_str = conf.toprettyxml() |
48 | | - cfg_str = '\n'.join([s for s in cfg_str.splitlines() if s.strip()]) |
49 | | - #print(cfg_str) |
50 | | - with open("../examples/{0}/{1}/iar_{1}.ipcf".format(dir_2, example), 'w') as f: |
51 | | - f.write(cfg_str) |
| 14 | + # Enumerate all device/host examples |
| 15 | + dir_1 = os.listdir("../examples") |
| 16 | + for dir_2 in dir_1: |
| 17 | + if os.path.isdir("../examples/{}".format(dir_2)): |
| 18 | + print(dir_2) |
| 19 | + examples = os.listdir("../examples/{}".format(dir_2)) |
| 20 | + for example in examples: |
| 21 | + if os.path.isdir("../examples/{}/{}".format(dir_2, example)): |
| 22 | + print("../examples/{}/{}".format(dir_2, example)) |
| 23 | + conf = XML.parseString(base) |
| 24 | + files = conf.getElementsByTagName("files")[0] |
| 25 | + inc = conf.getElementsByTagName("includePath")[0] |
| 26 | + # Add bsp inc |
| 27 | + path = conf.createElement('path') |
| 28 | + path_txt = conf.createTextNode("$TUSB_DIR$/hw") |
| 29 | + path.appendChild(path_txt) |
| 30 | + inc.appendChild(path) |
| 31 | + # Add board.c/.h |
| 32 | + grp = conf.createElement('group') |
| 33 | + grp.setAttribute("name", "bsp") |
| 34 | + path = conf.createElement('path') |
| 35 | + path_txt = conf.createTextNode("$TUSB_DIR$/hw/bsp/board.c") |
| 36 | + path.appendChild(path_txt) |
| 37 | + grp.appendChild(path) |
| 38 | + files.appendChild(grp) |
| 39 | + # Add example's .c/.h |
| 40 | + grp = conf.createElement('group') |
| 41 | + grp.setAttribute("name", "example") |
| 42 | + for file in os.listdir("../examples/{}/{}/src".format(dir_2, example)): |
| 43 | + if file.endswith(".c") or file.endswith(".h"): |
| 44 | + path = conf.createElement('path') |
| 45 | + path.setAttribute("copyTo", "$PROJ_DIR$/{}".format(file)) |
| 46 | + path_txt = conf.createTextNode("$TUSB_DIR$/examples/{0}/{1}/src/{2}".format(dir_2, example, file)) |
| 47 | + path.appendChild(path_txt) |
| 48 | + grp.appendChild(path) |
| 49 | + files.appendChild(grp) |
| 50 | + cfg_str = conf.toprettyxml() |
| 51 | + cfg_str = '\n'.join([s for s in cfg_str.splitlines() if s.strip()]) |
| 52 | + #print(cfg_str) |
| 53 | + with open("../examples/{0}/{1}/iar_{1}.ipcf".format(dir_2, example), 'w') as f: |
| 54 | + f.write(cfg_str) |
| 55 | + |
| 56 | +def ListPath(path, blacklist=[]): |
| 57 | + # Get all .c files |
| 58 | + files = glob.glob(f'../{path}/**/*.c', recursive=True) |
| 59 | + # Filter |
| 60 | + files = [x for x in files if all(y not in x for y in blacklist)] |
| 61 | + # Get common dir list |
| 62 | + dirs = [] |
| 63 | + for file in files: |
| 64 | + dir = os.path.dirname(file) |
| 65 | + if dir not in dirs: |
| 66 | + dirs.append(dir) |
| 67 | + # Print .c grouped by dir |
| 68 | + for dir in dirs: |
| 69 | + print('<group name="' + dir.replace('../', '').replace('\\','/') + '">') |
| 70 | + for file in files: |
| 71 | + if os.path.dirname(file) == dir: |
| 72 | + print(' <path>$TUSB_DIR$/' + file.replace('../','').replace('\\','/')+'</path>') |
| 73 | + print('</group>') |
| 74 | + |
| 75 | +def List(): |
| 76 | + ListPath('src', [ 'template.c', 'dcd_synopsys.c', 'dcd_esp32sx.c' ]) |
| 77 | + ListPath('lib/SEGGER_RTT') |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + if (len(sys.argv) > 1): |
| 81 | + if (sys.argv[1] == 'l'): |
| 82 | + List() |
| 83 | + else: |
| 84 | + Main() |
0 commit comments