-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectHandler.py
508 lines (481 loc) · 19.9 KB
/
ProjectHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import datetime
import json
import os
from pathlib import Path
import copy
def create_vscode_folder(path : str, project : dict):
if not os.path.exists(os.path.join(path, ".vscode")):
os.makedirs(os.path.join(path, ".vscode"))
print("Created .vscode folder")
create_settings(os.path.join(path, ".vscode"), project)
create_c_cpp_properties(os.path.join(path, ".vscode"), project)
create_tasks(os.path.join(path, ".vscode"), project)
create_launch(os.path.join(path, ".vscode"), project)
def create_launch(path : str, project : dict):
json_template = {
"version": "0.2.0",
"configurations": [
{
"name": "x86_x64 UnitTest",
"cwd": "${workspaceFolder}",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/.obj/unittest_x86_x64/main.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": True
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": True
}
],
"args": [
"--colour",
"-s"
],
"preLaunchTask": "x86_x64 Build UnitTest"
}
]
}
json_template["configurations"] += project["custom-launch-configurations"]
with open(os.path.join(path, "launch.json"), "w") as file:
json.dump(json_template, file, indent=4)
print("Created tasks.json")
def create_tasks(path : str, project : dict):
json_template = {
"version": "2.0.0",
"tasks": [
{
"label": "x86_x64 Build UnitTest",
"type": "shell",
"command": "make",
"args": [
"-j4",
"unittest_x86_x64_build",
"UNIT=${input:UNIT}"
],
"group": {
"kind": "build",
"isDefault": True
},
"problemMatcher":[
"$gcc"
]
},
{
"label": "x86_x64 Run UnitTest",
"command":".\\.obj\\unittest_x86_x64\\main.exe",
"args": [
"--colour",
"-a",
"-s"
],
"type": "shell",
"group": {
"kind": "test",
"isDefault": True
},
"dependsOn": "x86_x64 Build UnitTest",
"problemMatcher":[
"$gcc",
{
"pattern":[
{
"regexp": "^([\\S]+):(\\d+): ([\\S]+): ([\\S ]+)$",
"file": 1,
"location": 2,
"severity": 3,
"message": 4
}
]
}
]
},
{
"label": "Target Build UnitTest",
"type": "shell",
"command": "make",
"args": [
"-j4",
"unittest_platform_build",
"UNIT=${input:UNIT}"
],
"group": {
"kind": "build",
"isDefault": True
},
"problemMatcher":[
"$gcc"
]
},
{
"label": "Target Flash UnitTest",
"type": "shell",
"command": "make",
"args": [
"-j4",
"unittest_platform_flash",
"UNIT=${input:UNIT}"
],
"problemMatcher":[
"$gcc"
]
},
{
"label": "Target Run UnitTest",
"type": "shell",
"command": "make",
"args": [
"-j4",
"unittest_platform_run",
"UNIT=${input:UNIT}"
],
"group": {
"kind": "test",
"isDefault": True
},
"problemMatcher":[
"$gcc",
{
"pattern":[
{
"regexp": "^([\\S]+):(\\d+): ([\\S]+): ([\\S ]+)$",
"file": 1,
"location": 2,
"severity": 3,
"message": 4
}
]
}
]
},
{
"label": "Software build",
"type": "shell",
"command": "make",
"args": [
"-j4",
"all"
],
"group": {
"kind": "build",
"isDefault": True
},
"problemMatcher":[
"$gcc"
]
},
{
"label": "Software flash",
"type": "shell",
"command": "make",
"args": [
"-j4",
"flash-all"
],
"problemMatcher":[
"$gcc"
]
},
{
"label": "Clean",
"type": "shell",
"command": "make",
"args": [
"-j4",
"clean"
],
"problemMatcher":[
"$gcc"
]
},
{
"label": "Unit stub generation",
"type": "shell",
"command": "make",
"args": [
"-j4",
"stubgen",
"UNIT=${input:UNIT}"
]
},
{
"label": "Generate Coverage",
"type": "shell",
"command": "make",
"args": [
"-j4",
"coverage",
"UNIT=${input:UNIT}"
],
"problemMatcher":[
"$gcc"
]
},
{
"label": "Generate Coverage HTML",
"type": "shell",
"command": "make",
"args": [
"-j4",
"coverage-html",
"UNIT=${input:UNIT}"
],
"problemMatcher":[
"$gcc"
]
},
{
"label": "Toggle coverage",
"command": "${command:gcov-viewer.toggle}"
},
{
"label": "Load coverage",
"command": "${command:gcov-viewer.reloadGcdaFiles}"
},
{
"label": "Check dependencies",
"type": "shell",
"command": "make",
"args": [
"check"
]
}
],
"inputs": [
{
"id": "UNIT",
"type": "command",
"command": "cpptools.activeConfigCustomVariable",
"args": "UNIT"
}
]
}
with open(os.path.join(path, "tasks.json"), "w") as file:
json.dump(json_template, file, indent=4)
print("Created tasks.json")
def create_settings(path : str, project : dict):
# Create settings.json
json_template = {
"taskExplorer.pathToMake" : "make",
"gcovViewer.highlightMissedLines" : True,
"gcovViewer.buildDirectories" : [
"${workspaceFolder}/.obj"
]
}
with open(os.path.join(path, "settings.json"), "w") as file:
json.dump(json_template, file, indent=4)
print("Created settings.json")
def create_c_cpp_properties(path : str, project : dict):
# Create a c_cpp_properties.json file
config_template = {
"name": "",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"UNITTEST=1"
],
"customConfigurationVariables": {
"UNIT":""
},
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "gnu99",
"cppStandard": "c++98",
"intelliSenseMode": "windows-gcc-x64"
}
json_template = {
"configurations": [
],
"version": 4
}
include_paths = ["${workspaceFolder}/UnitTestRunner/include"]
for component in project["components"]:
include_paths += {"${workspaceFolder}/" + component["name"] + "/Unit/include"}
for component in project["components"]:
config = copy.deepcopy(config_template)
config["name"] = component["name"] + " UnitTest"
config["includePath"] += include_paths
config["includePath"] += project["platform-include-paths"]
config["includePath"] += {"${workspaceFolder}/" + component["name"] + "/UnitTest/include"}
config["includePath"] += {"${workspaceFolder}/" + component["name"] + "/UnitTest/include/stubs"}
config["defines"] += project["platform-defines"]
config["customConfigurationVariables"]["UNIT"] = component["name"]
json_template["configurations"].append(config)
project["platform-template"]["name"] = project["name"]
project["platform-template"]["includePath"] += include_paths
project["platform-template"]["includePath"] += project["platform-include-paths"]
project["platform-template"]["defines"] += project["platform-defines"]
json_template["configurations"].append(project["platform-template"])
with open(os.path.join(path, "c_cpp_properties.json"), "w") as file:
json.dump(json_template, file, indent=4)
print("Created c_cpp_properties.json")
def create_db_mk(path : str, project : dict):
# Create a database.mk file
file = open(os.path.join(path, "database.mk"), "w")
file.write("UNITLIST = \n")
for component in project["components"]:
file.write("UNITLIST += " + component["name"] + "\n")
def create_tree(path : str, project : dict):
# Create a tree of directories and files
for component in project["components"]:
# create directory if it doesn't exist
if not os.path.exists(os.path.join(path, component["name"])):
os.makedirs(os.path.join(path, component["name"]))
if not os.path.exists(os.path.join(path, component["name"],"Unit")):
os.makedirs(os.path.join(path, component["name"], "Unit"))
if not os.path.exists(os.path.join(path, component["name"],"Unit","src")):
os.makedirs(os.path.join(path, component["name"], "Unit", "src"))
if not os.path.exists(os.path.join(path, component["name"],"Unit","include")):
os.makedirs(os.path.join(path, component["name"], "Unit", "include"))
if not os.path.exists(os.path.join(path, component["name"],"Unit")):
os.makedirs(os.path.join(path, component["name"], "UnitTest"))
if not os.path.exists(os.path.join(path, component["name"],"UnitTest","src")):
os.makedirs(os.path.join(path, component["name"], "UnitTest", "src"))
if not os.path.exists(os.path.join(path, component["name"],"UnitTest","include")):
os.makedirs(os.path.join(path, component["name"], "UnitTest", "include"))
if not os.path.exists(os.path.join(path, component["name"],"UnitTest","src","stubs")):
os.makedirs(os.path.join(path, component["name"], "UnitTest", "src","stubs"))
if not os.path.exists(os.path.join(path, component["name"],"UnitTest","include","stubs")):
os.makedirs(os.path.join(path, component["name"], "UnitTest", "include","stubs"))
print("Created directory tree for " + component["name"])
else:
print( "Folder for "+component["name"] + " already exists, skipping...")
# Create files for each component
create_files(path, project)
def create_unit_source(path : str, name : str):
# Create a source file for the unit test
if not Path(os.path.join(path, name + ".c")).is_file():
file = open(os.path.join(path, name + ".c"), "w")
file.write("/**\n * @file " + name + ".c\n * @brief TODO\n * @date " + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")+ "\n*/")
file.write("\n")
file.write("#include \"" + name + ".h\"")
file.write("\n")
file.write("/* Includes ------------------------------------------------------------------*/\n\n\n")
file.write("/* Private includes ----------------------------------------------------------*/\n\n\n")
file.write("/* Private typedef -----------------------------------------------------------*/\n\n\n")
file.write("/* Private define ------------------------------------------------------------*/\n\n\n")
file.write("/* Private macro -------------------------------------------------------------*/\n\n\n")
file.write("/* Private variables ---------------------------------------------------------*/\n\n\n")
file.close()
print("Created source file for " + name)
else:
print( name +"/Unit/src/" + name + ".c already exists, skipping...")
def create_unit_header(path : str, name : str):
# Create a header file for the unit test
if not Path(os.path.join(path, name + ".h")).is_file():
file = open(os.path.join(path, name + ".h"), "w")
file.write("/**\n * @file " + name + ".h\n * @brief TODO\n * @date " + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "\n*/")
file.write("\n")
file.write("#ifndef " + name + "_H\n#define " + name + "_H\n")
file.write("\n")
file.write("/* Includes ------------------------------------------------------------------*/\n\n\n")
file.write("/* Type definitions ----------------------------------------------------------*/\n\n\n")
file.write("/* Defines -------------------------------------------------------------------*/\n\n\n")
file.write("/* Global variables ----------------------------------------------------------*/\n\n\n")
file.write("/* Function declarations -----------------------------------------------------*/\n\n\n")
file.write("\n\n\n")
file.write("#endif /* " + name + "_H */")
file.close()
print("Created unit header file for " + name)
else:
print(name + ".h already exists, skipping...")
def create_unittest_header(path : str, name : str):
# Create a header file for the unit test
if not Path(os.path.join(name,"UnitTest","include","TestSuites.h")).is_file():
file = open((os.path.join(name,"UnitTest","include","TestSuites.h")), "w")
file.write("/**\n * @file TestSuites.h\n * @brief TODO\n * @date " + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "\n*/")
file.write("\n")
file.write("#ifndef TESTSUITES_H\n#define TESTSUITES_H\n")
file.write("\n")
file.write("/* Includes ------------------------------------------------------------------*/\n\n\n")
file.write("/* Type definitions ----------------------------------------------------------*/\n\n\n")
file.write("/* Defines -------------------------------------------------------------------*/\n\n\n")
file.write("/* Global variables ----------------------------------------------------------*/\n\n\n")
file.write("/* Function declarations -----------------------------------------------------*/\n\n\n")
file.write("\n\n\n")
file.write("#endif /* TESTSUITES_H */")
file.close()
print("Created unittest header file for " + name)
else:
print(name + "/UnitTest/src/TestSuites.h already exists, skipping...")
def create_unittest_source(path : str, name : str, testsuites : dict):
# Create a source file for the unit test
if not Path(os.path.join(name,"UnitTest","src","TestSuites.c")).is_file():
file = open((os.path.join(name,"UnitTest","src","TestSuites.c")), "w")
file.write("/**\n * @file TestSuites.c\n * @brief TODO\n * @date " + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")+ "\n*/")
file.write("\n")
file.write("#include \"TestSuites.h\"")
file.write("\n")
file.write("/* Private includes ----------------------------------------------------------*/\n\n\n")
file.write("/* Private typedef -----------------------------------------------------------*/\n\n\n")
file.write("/* Private define ------------------------------------------------------------*/\n\n\n")
file.write("/* Private macro -------------------------------------------------------------*/\n\n\n")
file.write("/* Private variables ---------------------------------------------------------*/\n\n\n")
for testSuite in testsuites:
file.write("extern TestSuite " + testSuite["name"] + ";\n")
file.write("\n")
file.write("TestSuite* testSuites[] = {\n")
for testSuite in testsuites:
file.write("\t&" + testSuite["name"] + ",\n")
file.write("\tTEST_SUITE_END\n")
file.write("};\n")
file.close()
print("Created source file for " + name)
else:
print(name + "/UnitTest/src/TestSuites.c already exists, skipping...")
for testSuite in testsuites:
if not Path(os.path.join(name,"UnitTest","src",testSuite["name"],".c")).is_file():
file = open(os.path.join(name,"UnitTest","src",testSuite["name"]+".c"), "w")
file.write("/**\n * @file "+ testSuite["name"] +".c\n * @brief TODO\n * @date " + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")+ "\n*/")
file.write("\n\n")
file.write("/* Private includes ----------------------------------------------------------*/\n\n")
file.write("#include \"TestSuites.h\"\n")
file.write("#include \"stub.h\"")
file.write("\n\n")
file.write("/* Private typedef -----------------------------------------------------------*/\n\n\n")
file.write("/* Private define ------------------------------------------------------------*/\n\n\n")
file.write("/* Private macro -------------------------------------------------------------*/\n\n\n")
file.write("/* Private variables ---------------------------------------------------------*/\n\n\n")
for testCase in testSuite["testcases"]:
file.write("/**\n * @brief \n * \n */\n")
file.write("void " + testCase["name"] + "()\n")
file.write("{\n")
file.write("}\n")
file.write("\n")
file.write("TestSuite " + testSuite["name"] + " = {\n")
file.write("\t.name = \"" + testSuite["name"] + "\",\n")
file.write("\t.TestCases = \n\t{\n")
for testCase in testSuite["testcases"]:
file.write("\t\tTEST_CASE_ENTRY(" + testCase["name"] + "),\n")
file.write("\t\tTEST_CASE_ENTRY(TEST_CASE_END),\n")
file.write("\t}\n};")
else:
print(name + "/UnitTest/src/"+ testSuite["name"] +".c already exists, skipping...")
def create_files(path : str, project : dict):
# Create files for each component
for component in project["components"]:
create_unit_source(os.path.join(path, component["name"], "Unit", "src"), component["name"])
create_unit_header(os.path.join(path, component["name"], "Unit", "include"), component["name"])
create_unittest_source(os.path.join(path, component["name"], "UnitTest", "include"), component["name"], component["testsuites"])
def main():
# Get CWD and create path to JSON file
cwd = os.getcwd()
# Get the path to the JSON file
json_path = os.path.join(cwd, "project.json")
# Open the JSON file
project = json.load(open(json_path))
# Create the directory tree and C files
create_tree(cwd, project)
# Create .vscode folder
create_vscode_folder(cwd, project)
# Create database makefile
create_db_mk(cwd,project)
if __name__ == '__main__':
main()