-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbuilder.py
executable file
·164 lines (116 loc) · 3.45 KB
/
cbuilder.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
#!/usr/bin/env python3
import os, sys, fnmatch
from subprocess import Popen
from datetime import datetime
folderIgnore={}
IgnoreNames={"ccc.c"}
ignorePatternFront = ["."]
ignorePatternEnd = []
onlyTakePatternEnd = [".c"]
CC="clang"
CFLAGS=["-std=c99", "-ffunction-sections", "-fdata-sections", "-Os", "-O2"]
LDFLAGS=["-lm"]
OUTEXE="build/ccc"
gTestCommands = ["-I2","h=12,90,88", "-s"]
gSourceCode = "src"
#-----G-END-----#
TREE_SYNTX = {'E':'├', 'C':'│', 'S':'─', 'N':'└'}
def run(commands):
Popen(commands).wait()
def log(msg):
print(msg, end='')
def checkIfNameFound(name, nArray):
for i in nArray:
if fnmatch.fnmatch(name, "*"+i):
return 1
return 0
def compile(cfiles):
cf = cfiles.split(" ")
run_command = [ CC ] + CFLAGS + LDFLAGS + cf
run_command.append('-o')
run_command.append(OUTEXE)
run([ "rm","-rf","build" ])
run(["mkdir", "build"])
run(run_command)
def getallFiles(dirName):
log("Getting C files...")
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(dirName):
dirnames[:] = [d for d in dirnames if d not in folderIgnore]
listOfFiles += [os.path.join(dirpath, file) for file in filenames]
log("DONE\n\n")
return listOfFiles
def filterListintoFile(ls):
log("Filtering Files...")
outstr = ""
prev = ""
for elem in ls:
if elem[-1] == "c":
if elem[-2] == ".":
if checkIfNameFound(elem, IgnoreNames) != 1:
outstr += elem + " "
log("\n\t\tGot: " + elem)
log("\nCOMPLETED\n\n")
return outstr
def do_clean():
log("Cleaning...\r")
run(["rm", "-rf", "build"])
log("COMPLETED\n")
def checkforfileExistance(source):
try:
fp = open(source, "rb")
fp.close()
return True
except:
return False
def main(source, clean, testCommands):
if clean:
do_clean()
return
log("Running At ... " + source + "\n")
# Get the list of all files in directory tree at given path
outstr = filterListintoFile(getallFiles(source))
log("Compiling...")
compile(outstr)
if checkforfileExistance(OUTEXE):
log("SUCCESS\n\n")
else:
log("FAILED\n\n")
log("Executable: " + OUTEXE + "\n\n")
log("Testing...\n")
try:
run([ "./"+OUTEXE] + testCommands)
except:
log("FAILED\n")
return
log("\nSUCCESS\n")
def argp(arg):
global gTestCommands, gSourceCode
cleanEnabled = 0
testEnabled = 1
count = 1
for i in arg:
if i[0] == '-':
if i[1:] == "-clean" or i[1:] == 'c':
cleanEnabled= 1
elif i[1:] == "-source" or i[1:] == 's':
gSourceCode = arg[count+1]
elif i[1:] == "-testdisable" or i[1:] == 't':
testEnabled = 0
elif i[1:] == "-testCommands" or i[1:] == 'm':
gTestCommands = arg[count+1::]
main(gSourceCode, cleanEnabled, gTestCommands)
return
else:
pass
count += 1
main(gSourceCode, cleanEnabled, gTestCommands)
return
if __name__ == '__main__':
now = datetime.now()
timestamp = datetime.timestamp(now)
log("Starting...\n\n")
argp(sys.argv)
log("\nEND\n")
timest1 = datetime.timestamp(datetime.now())
print("Time Elasped...{:.3} total".format(timest1 - timestamp) )