-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile.py
157 lines (138 loc) · 5.25 KB
/
makefile.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
import os
import sys
from collections import deque
#I wrote makefile for cfiles but i couldn't run the make command properly. I don't know why.
#I also create the makefile
#for cfiles and their roots
cfiles = []
#for cfiles
cfiles1 = []
#for hfiles
hfiles = []
#for hfiles
hfiles1 = []
#for include statements in the .c files
includes = []
#for error checking
counterror = 0
#for addresses of the .h files in makefile
address = []
#the string which will be written to makefile
write = ""
#taking the argument
qlist = deque([sys.argv[1]])
#taking the current directory
currentdir = qlist.popleft()
for root, dirs, files in os.walk(currentdir):
for file in files:
if file.endswith(".c"):
#appending to an array cfiles and roots
cfiles.append(os.path.join(root, file))
# appending to an array cfiles
cfiles1.append(os.path.join(file))
#reading the cfiles line by line
with open(cfiles[len(cfiles) - 1]) as f:
content = f.read().splitlines()
for x in range(0, len(content)):
#searching for include statements
if content[x].startswith("#include"):
#splitting by spaces
lhs, rhs = content[x].split(' ')
state = rhs[1:-1]
if not state == "stdio.h":
# appending to include statements to includes array
includes.append(state)
if file.endswith(".h"):
# appending to an array cfiles
hfiles.append(os.path.join(file))
# appending to an array cfiles
hfiles1.append(file)
#splitting by '\\'
arr = root.split("\\")
# appending the root directory of hfiles to address
address.append(arr[len(arr) - 1])
#checking whether there is already that address in the array or not
if len(address) > 1:
if not address[len(address)-1] == address[len(address)-2]:
write += arr[len(arr) - 1] + "_ = " + root + "\n"
else:
write += arr[len(arr) - 1] + "_ = " + root + "\n"
#If there is an error, it prints the error message
for x in range(len(includes)):
if not hfiles.__contains__(includes[x]):
print("Error: The file", includes[x], "is not found.")
counterror += 1
break
#If there is a warning, it prints the warning message
for x in range(len(hfiles)):
if not includes.__contains__(hfiles[x]):
print("Warning: The file", hfiles[x], "is not used.")
#the exe() function creates a string which will be written in the makefile.
# This string is the .exe part
def exe():
#cf is a string for exe part of the makefile
cf = os.path.basename(currentdir) + ":\n\t"
#adding cfiles.o's
for x in range(0, len(cfiles)):
cf += cfiles1[x][0:-2]
cf += ".o"
cf += " "
cf += "\n\t"
cf += "gcc "
#
for x in range(0, len(cfiles)):
cf += cfiles1[x][0:-2]
cf += ".o"
cf += " "
cf = cf + "-o " + os.path.basename(currentdir) + " lm\n\t" + "echo \"done\""
return cf
#the obj() function creates a string which will be written in the makefile.
# This string is the .o parts
def obj():
cfiles1 = []
cfiles = []
can = ""
for root, dirs, files in os.walk(currentdir):
for file in files:
if file.endswith(".c"):
cfiles.append(os.path.join(root, file))
# cfiles1.append(os.path.join(file))
# print(os.path.join(root, file)[2:])
if not os.path.join(root, file).__contains__("\\"):
can += os.path.join(file)
can = can[0:-2] + ".o:\n\t"
can += os.path.join(file) + " "
else:
can += os.path.join(file)
can = can[0:-2] + ".o:\n\t" + os.path.join(root, file) + " "
with open(cfiles[len(cfiles) - 1]) as f:
content = f.read().splitlines()
inc = ""
for x in range(0, len(content)):
if content[x].startswith("#include"):
lhs, rhs = content[x].split(' ')
state = rhs[1:-1]
if not state == "stdio.h":
for x in range(0, len(hfiles1)):
if hfiles1[x].__contains__(state):
can += "$(" + address[x] + "_)/" + hfiles1[x] + " "
if not inc.__contains__(address[x]):
inc += "$(" + address[x] + "_) "
can = can + "\n\t" + "gcc -c -I " + inc + os.path.join(root, file)
can += "\n"
return can
#if there is no error, writes makefile
if counterror == 0:
write = write + exe() + "\n" + obj()
write = write + "clean: -rm - f *.o\n\t-rm - f " + os.path.basename(currentdir)
#opens a file in current directory and writes makefile
f1 = open(currentdir + '/makefile', 'w+')
f1.write(write)
f1.close()
#is there is an error, it says 'error'
else:
f1 = open(currentdir + '/makefile', 'w+')
f1.write("error")
f1.close()
print()
print("done")