1010
1111
1212def check_makefile_clean_dir (project_path : str , binary_name : str , root_path : str ):
13+ """
14+ This function will clean the project's directory and test if the binary
15+ and the .o were removed.
16+
17+ :param project_path: The project path where what needs to be tested is.
18+ :param binary_name: The name of the binary the makefile will compile
19+ :param root_path: The absolute path leading to the script's main file.
20+
21+ :return: Returns the number of errors encountered during that function.
22+ """
1323 error_count = 0
1424 with open (root_path + "/.mymakefile" , 'w+' ) as file :
1525 file .write ("Cleaning Directory\n " )
1626 print ("Cleaning Directory" )
1727 file .write ("*------------------------------------------------------*\n " )
1828 file .write ("" )
29+ # Run make fclean in the project's directory
1930 result = subprocess .run ('make ' + '-C ' + project_path + ' fclean' ,
2031 shell = True , stdout = subprocess .PIPE ,
2132 stderr = subprocess .STDOUT ).stdout .decode ('utf-8' )
2233 file .write (result + '\n ' )
2334 print (result )
35+ # If the binary or the object file still exists after the fclean,
36+ # print an error and increment the error counter.
2437 if os .path .exists (project_path + '/' + binary_name ):
2538 file .write ("-> Error when processing rule `fclean':"
2639 " It should have removed {}\n " .format (binary_name ))
@@ -37,6 +50,15 @@ def check_makefile_clean_dir(project_path: str, binary_name: str, root_path: str
3750
3851
3952def check_makefile_all (project_path : str , binary_name : str , root_path : str ):
53+ """
54+ This function will check the rule `all` for the makefile of the given project.
55+
56+ :param project_path: The project path where what needs to be tested is.
57+ :param binary_name: The name of the binary the makefile will compile
58+ :param root_path: The absolute path leading to the script's main file.
59+
60+ :return: Returns the number of errors encountered during that function.
61+ """
4062 error_count = 0
4163 makefile_path = project_path + '/Makefile'
4264 match = False
@@ -45,6 +67,8 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
4567 file .write ("*------------------------------------------------------*\n " )
4668 file .write ("Checking rule: `all'\n " )
4769 print ("Checking rule: `all'" )
70+ # Searches every line of the makefile for the string `all`
71+ # followed by spaces and/or tabs and then a `:`
4872 for line in makefile :
4973 if re .match ('^all[\t ]*:' , line ):
5074 match = True
@@ -53,6 +77,9 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
5377 print ("-> Error: rule `all' not found in the Makefile." )
5478 error_count += 1
5579 return error_count
80+ # Searches every line of the makefile for the string `all` followed
81+ # by tabs and/or spaces and then `:` then tabs and spaces again and
82+ # then for the string `$(NAME)`
5683 for line in makefile :
5784 if re .match ('^all[\t ]*:[\t ]*\$\(NAME\)' , line ):
5885 match = True
@@ -61,11 +88,13 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
6188 print ("-> Error: rule `all' should call the rule `$(NAME)'" )
6289 error_count += 1
6390 return error_count
91+ # Runs make all in the project's directory
6492 result = subprocess .run ('make ' + '-C ' + project_path + ' all' ,
6593 shell = True , stdout = subprocess .PIPE ,
6694 stderr = subprocess .STDOUT ).stdout .decode ('utf-8' )
6795 file .write (result + '\n ' )
6896 print (result )
97+ # Checks if binary and object file were created.
6998 if not os .path .exists (project_path + '/' + binary_name ):
7099 file .write ("-> Error when processing rule `all':"
71100 " It should have created {}\n " .format (binary_name ))
@@ -83,6 +112,15 @@ def check_makefile_all(project_path: str, binary_name: str, root_path: str):
83112
84113
85114def check_makefile_clean (project_path : str , binary_name : str , root_path : str ):
115+ """
116+ This function will check the rule `clean` for the makefile of the given project.
117+
118+ :param project_path: The project path where what needs to be tested is.
119+ :param binary_name: The name of the binary the makefile will compile
120+ :param root_path: The absolute path leading to the script's main file.
121+
122+ :return: Returns the number of errors encountered during that function.
123+ """
86124 error_count = 0
87125 makefile_path = project_path + '/Makefile'
88126 match = False
@@ -91,6 +129,8 @@ def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
91129 file .write ("*------------------------------------------------------*\n " )
92130 file .write ("Checking rule: `clean'\n " )
93131 print ("Checking rule: `clean'" )
132+ # Searches every line of the makefile for the string `clean`
133+ # followed by `:`
94134 for line in makefile :
95135 if re .match ('^clean[\t ]*:' , line ):
96136 match = True
@@ -99,18 +139,23 @@ def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
99139 print ("-> Error: rule `clean' not found in the Makefile." )
100140 error_count += 1
101141 return error_count
142+ # If previous rule failed, print an error message, increment error
143+ # counter and return number of errors.
102144 if not os .path .exists (project_path + '/' + binary_name ):
103145 file .write ("-> Error: Cannot test rule `clean' because rule"
104146 " `all' failed\n " )
105147 print ("-> Error: Cannot test rule `clean' because rule"
106148 " `all' failed" )
107149 error_count += 1
108150 return error_count
151+ # Run `make clean` in the project's directory
109152 result = subprocess .run ('make ' + '-C ' + project_path + ' clean' ,
110153 shell = True , stdout = subprocess .PIPE ,
111154 stderr = subprocess .STDOUT ).stdout .decode ('utf-8' )
112155 file .write (result + '\n ' )
113156 print (result )
157+ # If the binary name is not here and/or the object files are here,
158+ # the rule failed.
114159 if not os .path .exists (project_path + '/' + binary_name ):
115160 file .write ("-> Error: Failing Rule: It should not have "
116161 "cleaned the binary named {}.\n " .format (binary_name ))
@@ -125,6 +170,15 @@ def check_makefile_clean(project_path: str, binary_name: str, root_path: str):
125170
126171
127172def check_makefile_re (project_path : str , binary_name : str , root_path : str ):
173+ """
174+ This function will check the rule `re` for the makefile of the given project.
175+
176+ :param project_path: The project path where what needs to be tested is.
177+ :param binary_name: The name of the binary the makefile will compile
178+ :param root_path: The absolute path leading to the script's main file.
179+
180+ :return: Returns the number of errors encountered during that function.
181+ """
128182 error_count = 0
129183 makefile_path = project_path + '/Makefile'
130184 match = False
@@ -181,6 +235,15 @@ def check_makefile_re(project_path: str, binary_name: str, root_path: str):
181235
182236
183237def check_makefile_fclean (project_path : str , binary_name : str , root_path : str ):
238+ """
239+ This function will check the rule `fclean` for the makefile of the given project.
240+
241+ :param project_path: The project path where what needs to be tested is.
242+ :param binary_name: The name of the binary the makefile will compile
243+ :param root_path: The absolute path leading to the script's main file.
244+
245+ :return: Returns the number of errors encountered during that function.
246+ """
184247 error_count = 0
185248 makefile_path = project_path + '/Makefile'
186249 match = False
@@ -220,6 +283,15 @@ def check_makefile_fclean(project_path: str, binary_name: str, root_path: str):
220283
221284
222285def check_makefile_name (project_path : str , binary_name : str , root_path : str ):
286+ """
287+ This function will check the rule `$(NAME)` for the makefile of the given project.
288+
289+ :param project_path: The project path where what needs to be tested is.
290+ :param binary_name: The name of the binary the makefile will compile
291+ :param root_path: The absolute path leading to the script's main file.
292+
293+ :return: Returns the number of errors encountered during that function.
294+ """
223295 error_count = 0
224296 makefile_path = project_path + '/Makefile'
225297 match = False
@@ -258,6 +330,15 @@ def check_makefile_name(project_path: str, binary_name: str, root_path: str):
258330
259331
260332def check_makefile_phony (project_path : str , binary_name : str , root_path : str ):
333+ """
334+ This function will check the rule `.PHONY` for the makefile of the given project.
335+
336+ :param project_path: The project path where what needs to be tested is.
337+ :param binary_name: The name of the binary the makefile will compile
338+ :param root_path: The absolute path leading to the script's main file.
339+
340+ :return: Returns the number of errors encountered during that function.
341+ """
261342 error_count = 0
262343 makefile_path = project_path + '/Makefile'
263344 match = False
@@ -299,14 +380,15 @@ def check_makefile_phony(project_path: str, binary_name: str, root_path: str):
299380 return error_count
300381
301382
302- def check (project_path : str , binary_name : str , root_path : str ):
383+ def check (project_path : str , root_path : str ):
303384 """
304- This function is the one you call to test the makefile given with the project .
385+ This function will run all the makefile checks .
305386
306387 :param project_path: The path of the project you want to test.
307388 :param binary_name: The binary that the makefile compiles
389+ :param root_path: The absolute path leading to the script's main file.
308390
309- :return This functions returns 1 If an error occured, 0 otherwise.
391+ :return This functions returns the number of errors found on the makefile
310392 """
311393 print ("*---------------------------------------------------------------*" )
312394 print ("*----------------------------Makefile---------------------------*" )
@@ -315,6 +397,9 @@ def check(project_path: str, binary_name: str, root_path: str):
315397 if not os .path .exists (makefile_path ):
316398 print ("--> Error: Makefile not found." )
317399 return "--> Error: Makefile not found."
400+ with open (makefile_path , 'r' ) as file :
401+ data = file .read ()
402+ binary_name = re .findall ("NAME[\s]*=[\s]*(.*)" , data )[0 ]
318403 error_count = 0
319404 error_count += check_makefile_clean_dir (project_path , binary_name , root_path )
320405 error_count += check_makefile_all (project_path , binary_name , root_path )
0 commit comments