-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·276 lines (211 loc) · 7.9 KB
/
run
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
#!/usr/bin/env python3
"""
Filename: run
Created on: November 5, 2023
Author: Lucas Araújo <[email protected]>
CMake Build and Clean Script
This script provides functions to build and clean CMake projects. It includes
functions for building the project, running the main program, executing unit tests,
and cleaning CMake artifacts.
Usage:
./run [OPTIONS]
Options:
-b, --build Build the CMake project
-d, --debug Build the CMake project in Debug mode
-e, --exec Run the main program
-t, --test Run the unit tests
-c, --clean Clean CMake artifacts
-v, --valgrind Run Valgrind tests to check for memory leaks
-h, --help Show this help message and exit
-h, --help Show this help message and exit
-x, --exec_args Arguments for cmake_exec
"""
import os
import subprocess
import argparse
import shutil
BUILD_DIR = "build"
BIN_DIR = "bin"
RELEASE_DIR = "Release"
DEBUG_DIR = "Debug"
PROGRAM_NAME = "sudoku_solver"
UNIT_TEST_NAME = "unit_test"
VALGRIND_MEMLEAK = "valgrind --leak-check=full"
VALGRIND_MASSIF = "valgrind --tool=massif"
def run_valgrind_tests(program_args=None):
"""
@brief
Run Valgrind tests on the program and unit tests.
This function uses Valgrind to check for memory leaks in the program and unit tests.
"""
cmake_build(debug=True)
valgrind_memleak_program = [
f"{VALGRIND_MEMLEAK} {BIN_DIR}/{DEBUG_DIR}/{PROGRAM_NAME}"
]
valgrind_memleak_tests = [
f"{VALGRIND_MEMLEAK} {BIN_DIR}/{DEBUG_DIR}/{UNIT_TEST_NAME}"
]
valgrind_massif_program = [
f"{VALGRIND_MASSIF} {BIN_DIR}/{DEBUG_DIR}/{PROGRAM_NAME}"
]
if program_args is not None:
# Convert program_args to string
program_args_str = " ".join(map(str, program_args))
# Append program arguments to Valgrind commands
valgrind_memleak_program[0] += " " + program_args_str
valgrind_massif_program[0] += " " + program_args_str
print("Running Valgrind memleak on program:")
subprocess.run(valgrind_memleak_program, shell=True, check=True)
print("Running Valgrind memleka on unit tests:")
subprocess.run(valgrind_memleak_tests, shell=True, check=True)
print("Running Valgrind massif on program:")
subprocess.run(valgrind_massif_program, shell=True, check=True)
def cmake_build(debug=False):
"""
@brief Build a CMake project in the specified build directory.
This function creates the build directory if it doesn't exist, changes to that
directory, runs CMake configuration, and then builds the project.
"""
check_submodules()
if not os.path.exists(BUILD_DIR):
os.makedirs(BUILD_DIR)
build_type = "Debug" if debug else "Release"
build_dir = os.path.join(BUILD_DIR, build_type)
if not os.path.exists(build_dir):
os.makedirs(build_dir)
current_dir = os.getcwd()
os.chdir(build_dir)
cmake_command = f"cmake ../.. -DCMAKE_BUILD_TYPE={build_type}"
subprocess.run(cmake_command, shell=True, check=True)
build_command = "cmake --build ."
subprocess.run(build_command, shell=True, check=True)
os.chdir(current_dir)
def cmake_exec(program_args=None):
"""
@brief
Build and execute the main program generated by CMake.
This function triggers the CMake build process and then runs the main program
located in the binary directory.
"""
cmake_build()
exec_command = [f"{BIN_DIR}/{RELEASE_DIR}/{PROGRAM_NAME}"]
if program_args is not None:
exec_command += program_args
subprocess.run(exec_command, check=True)
def cmake_unit_tests():
"""
@brief
Build and execute the unit tests generated by CMake.
This function triggers the CMake build process and then runs the unit tests
located in the binary directory.
"""
cmake_build()
exec_command = f"{BIN_DIR}/{RELEASE_DIR}/{UNIT_TEST_NAME}"
subprocess.run(exec_command, shell=True, check=True)
def clean_cmake_artifacts(project_path=None):
"""
@brief
Cleans the artifacts generated by CMake in a project, including build and binary
folders.
@param project_path:
The path to the project where CMake artifacts should be cleaned. If not
provided, the current working directory will be used.
"""
if project_path is None:
project_path = os.getcwd()
out_dir = os.path.join(project_path, BUILD_DIR)
bin_dir = os.path.join(project_path, BIN_DIR)
if not os.path.exists(out_dir) and not os.path.exists(bin_dir):
print("No CMake artifacts found to clean")
return
# Clean build dir
if os.path.exists(out_dir):
for item in os.listdir(out_dir):
item_path = os.path.join(out_dir, item)
if item != ".gitkeep":
if os.path.isdir(item_path):
shutil.rmtree(item_path)
else:
os.remove(item_path)
# Clean bin dir
if os.path.exists(bin_dir):
for item in os.listdir(bin_dir):
item_path = os.path.join(bin_dir, item)
if item != ".gitkeep":
if os.path.isdir(item_path):
shutil.rmtree(item_path)
else:
os.remove(item_path)
print(f"Cleaning CMake artifacts for project in: {project_path}")
print(f"Removing build files: {out_dir}")
print(f"Removing bin files: {bin_dir}")
def check_submodules():
"""
@brief
Check if the project has submodules and update them if necessary.
This function checks if the project has submodules and updates them if necessary.
"""
if os.path.exists(".gitmodules"):
# Check if the submodules are empty
submodules_dir = "submodules"
if os.path.isdir(submodules_dir):
for d in os.listdir(submodules_dir):
submodule_path = os.path.join(submodules_dir, d)
if os.path.isdir(submodule_path):
if not os.listdir(submodule_path):
print(
f"The submodule directory {submodule_path} is empty. Updating..."
)
subprocess.run(
[
"git",
"submodule",
"update",
"--init",
submodule_path,
],
check=True,
)
def main():
"""
@brief
main function
"""
parser = argparse.ArgumentParser(description="CMake Build, Execute, or Clean")
parser.add_argument("-b", "--build", action="store_true", help="Run CMake build")
parser.add_argument(
"-d", "--debug", action="store_true", help="Build in Debug mode"
)
parser.add_argument("-e", "--exec", action="store_true", help="Run CMake execute")
parser.add_argument(
"-c", "--clean", action="store_true", help="Clean CMake artifacts"
)
parser.add_argument(
"-t", "--test", action="store_true", help="Run CMake unit tests"
)
parser.add_argument(
"-v",
"--valgrind",
action="store_true",
help="Run Valgrind tests for memory leaks and memory usage",
)
parser.add_argument(
"-x", "--exec_args", nargs=argparse.REMAINDER, help="Arguments for cmake_exec"
)
args = parser.parse_args()
if not any(vars(args).values()): # No arguments passed by user
parser.print_help()
elif args.build:
cmake_build()
elif args.debug:
cmake_build(debug=True)
elif args.exec:
cmake_exec(args.exec_args)
elif args.clean:
clean_cmake_artifacts()
elif args.test:
cmake_unit_tests()
elif args.valgrind:
run_valgrind_tests(args.exec_args)
if __name__ == "__main__":
main()