This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
/
build.py
executable file
·65 lines (47 loc) · 1.5 KB
/
build.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
#!/usr/bin/python2.7
import os, sys
COMPILER = "gcc"
SRC_DIR = "../src"
INCLUDE_DIR = "../src"
BIN_DIR = "../bin"
BIN_NAME = False
CFLAGS = ["-O3", "-Wall", "-Wextra", "--std=c89", "-pedantic"]
DLIBS = ["ws2_32"] if os.name == "nt" else []
DEFINES = []
def strformat(fmt, var):
for k in var:
fmt = fmt.replace("{%s}" % str(k), var[k])
return fmt
def listdir(path):
return [os.path.join(dp, f) for dp, dn, fn in os.walk(path) for f in fn]
def main():
os.chdir(sys.path[0])
if len(sys.argv) < 2:
print "usage: build.py c_file"
sys.exit()
global BIN_NAME
if not BIN_NAME:
BIN_NAME = sys.argv[1].replace(".c", ".exe" if os.name == "nt" else "")
if not os.path.exists(BIN_DIR):
os.makedirs(BIN_DIR)
cfiles = filter(lambda x:x.endswith((".c", ".C")), listdir(SRC_DIR))
cfiles.append(sys.argv[1])
cmd = strformat(
"{compiler} {flags} {include} {def} -o {outfile} {srcfiles} {libs} {argv}",
{
"compiler" : COMPILER,
"flags" : " ".join(CFLAGS),
"include" : "-I" + INCLUDE_DIR,
"def" : " ".join(map(lambda x: "-D " + x, DEFINES)),
"outfile" : BIN_DIR + "/" + BIN_NAME,
"srcfiles" : " ".join(cfiles),
"libs" : " ".join(map(lambda x: "-l" + x, DLIBS)),
"argv" : " ".join(sys.argv[2:])
})
print "compiling..."
res = os.system(cmd)
if not res:
print(BIN_DIR + "/" + BIN_NAME)
print("done" + (" with errors" if res else ""))
if __name__ == "__main__":
main()