-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
executable file
·196 lines (167 loc) · 6.52 KB
/
compile.py
File metadata and controls
executable file
·196 lines (167 loc) · 6.52 KB
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
#! /usr/bin/env python3
from __future__ import print_function
import argparse
import shutil
import errno
import os
import subprocess
import multiprocessing
import sys
import fnmatch
class DirStructure:
def __init__(self, src, inc, make, obj, exe):
self.src = src
self.inc = inc
self.make = make
self.obj = obj
self.exe = exe
class Term(object):
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
def fullPath(path):
return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
def ensureDir(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
def tryRemove(directory, pattern):
if pattern == None:
try: shutil.rmtree(directory)
except OSError as e:
if e.errno != errno.ENOENT:
raise
finally:
return
for root, dirs, files in os.walk(fullPath(directory), topdown=False):
for f in files:
if fnmatch.fnmatch(f, pattern):
os.remove(os.path.join(root, f))
try: os.rmdir(root)
except OSError as e:
if e.errno != errno.ENOTEMPTY:
raise
def clean(dirs):
tryRemove(".", "*~")
tryRemove(".", "*#")
tryRemove(dirs.exe, "*.exe")
tryRemove(dirs.make, "*.d")
tryRemove(dirs.obj, "*.o")
tryRemove(dirs.obj, "*.a")
tryRemove(dirs.inc, "baby*.hpp")
tryRemove(dirs.src, "baby*.cpp")
tryRemove(".", ".subdirs.mk")
pass
def ensureSubdirs(base, subdirs):
for subdir in subdirs:
ensureDir(os.path.join(base, subdir))
def getSubdirs(base, subdirs):
base = fullPath(base)
for root, dirs, files in os.walk(base):
if root == base: continue
subdirs.add(os.path.relpath(root, base))
def genSubdirs(dirs):
plot_scripts = os.path.dirname(fullPath(__file__))
srcdir = fullPath(os.path.join(plot_scripts, dirs.src))
incdir = fullPath(os.path.join(plot_scripts, dirs.inc))
objdir = fullPath(os.path.join(plot_scripts, dirs.obj))
makedir = fullPath(os.path.join(plot_scripts, dirs.make))
exedir = fullPath(os.path.join(plot_scripts, dirs.exe))
subdirs = set()
getSubdirs(srcdir, subdirs)
getSubdirs(incdir, subdirs)
ensureSubdirs(srcdir, subdirs)
ensureSubdirs(incdir, subdirs)
ensureSubdirs(objdir, subdirs)
ensureSubdirs(makedir, subdirs)
ensureSubdirs(exedir, subdirs)
return subdirs
def writeMakefile(subdirs):
plot_scripts = os.path.dirname(fullPath(__file__))
with open(os.path.join(plot_scripts, ".subdirs.mk"), "w") as f:
for subdir in subdirs:
make = os.path.join("$(MAKEDIR)", os.path.join(subdir, "%.d"))
cpp = os.path.join("$(SRCDIR)", os.path.join(subdir, "%.cpp"))
cxx = os.path.join("$(SRCDIR)", os.path.join(subdir, "%.cxx"))
obj = os.path.join("$(OBJDIR)", os.path.join(subdir, "%.o"))
exe = os.path.join("$(EXEDIR)", os.path.join(subdir, "%.exe"))
f.write("".join((make,": ",cpp,"\n")))
f.write("\t$(GET_DEPS)\n\n")
f.write("".join((make,": ",cxx,"\n")))
f.write("\t$(GET_DEPS)\n\n")
f.write("".join((obj,": ",cpp,"\n")))
f.write("\t$(COMPILE)\n\n")
f.write("".join((obj,": ",cxx,"\n")))
f.write("\t$(COMPILE)\n\n")
f.write("".join((exe,": ",obj," $(LIBFILE)\n")))
f.write("\t$(LINK)\n\n")
def genSubdirMake(dirs):
subdirs = genSubdirs(dirs)
writeMakefile(subdirs)
def build(dirs, verbosity):
genSubdirs(dirs)
command = ["make","-j",str(multiprocessing.cpu_count()),"-k","-r","-R",
"SRCDIR="+dirs.src, "INCDIR="+dirs.inc,
"MAKEDIR="+dirs.make, "OBJDIR="+dirs.obj, "EXEDIR="+dirs.exe]
if verbosity < 1:
command.append("--silent")
elif verbosity > 1:
command.append("--debug")
p = subprocess.Popen(command, stderr=subprocess.PIPE)
err_msg = p.communicate()[1]
if p.returncode == 0:
print("\n\n"+Term.GREEN+Term.BOLD
+"Compilation succeeded!"
+Term.END+"\n")
else:
print("\n\n"+Term.RED+Term.BOLD
+"################ ERRORS AND WARNINGS ################"
+Term.END+Term.END+"\n", file=sys.stderr)
print(err_msg.decode("utf-8"), file=sys.stderr)
print("\n\n"+Term.RED+Term.BOLD
+"Compilation failed."
+Term.END+"\n", file=sys.stderr)
sys.exit(p.returncode)
def compile(mode, verbosity, dirs):
if mode == "build":
build(dirs, verbosity)
elif mode == "clean":
clean(dirs)
elif mode == "set_dirs":
genSubdirMake(dirs)
elif mode== "print_vars":
subprocess.check_call(["make","test","-r","-R","--silent",
"SRCDIR="+dirs.src, "INCDIR="+dirs.inc,
"MAKEDIR="+dirs.make, "OBJDIR="+dirs.obj, "EXEDIR="+dirs.exe, "print_vars"])
else:
raise Exception("Unrecognized option: "+mode)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = "Compiles plot_scripts code",
formatter_class = argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("mode", nargs="?", default="build", choices=["build","clean","set_dirs","print_vars"],
help = "Selects which action to perform")
parser.add_argument("-v","--verbosity", type=int, default=1, choices=[0,1,2],
help = "Set verbosity. Lower = less printing.")
parser.add_argument("--src_dir", default = "src",
help = "Directory containing .cpp and .cxx files")
parser.add_argument("--inc_dir", default = "inc",
help = "Directory containing .hpp files")
parser.add_argument("--make_dir", default = "bin",
help = "Directory in which to store .d files")
parser.add_argument("--obj_dir", default = "bin",
help = "Directory in which to place .o files")
parser.add_argument("--exe_dir", default = "run",
help = "Directory in which to store .exe files")
args = parser.parse_args()
compile(args.mode, args.verbosity,
DirStructure(args.src_dir, args.inc_dir, args.make_dir, args.obj_dir, args.exe_dir))