3 from __future__
import print_function
10 import multiprocessing
38 return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
44 if not os.path.isdir(path):
49 try: shutil.rmtree(directory)
51 if e.errno != errno.ENOENT:
56 for root, dirs, files
in os.walk(
fullPath(directory), topdown=
False):
58 if fnmatch.fnmatch(f, pattern):
59 os.remove(os.path.join(root, f))
62 if e.errno != errno.ENOTEMPTY:
78 for subdir
in subdirs:
83 for root, dirs, files
in os.walk(base):
84 if root == base:
continue 85 subdirs.add(os.path.relpath(root, base))
88 ra4_draw = os.path.dirname(
fullPath(__file__))
89 srcdir =
fullPath(os.path.join(ra4_draw, dirs.src))
90 incdir =
fullPath(os.path.join(ra4_draw, dirs.inc))
91 objdir =
fullPath(os.path.join(ra4_draw, dirs.obj))
92 makedir =
fullPath(os.path.join(ra4_draw, dirs.make))
93 exedir =
fullPath(os.path.join(ra4_draw, dirs.exe))
108 ra4_draw = os.path.dirname(
fullPath(__file__))
109 with open(os.path.join(ra4_draw,
".subdirs.mk"),
"w")
as f:
110 for subdir
in subdirs:
111 make = os.path.join(
"$(MAKEDIR)", os.path.join(subdir,
"%.d"))
112 cpp = os.path.join(
"$(SRCDIR)", os.path.join(subdir,
"%.cpp"))
113 cxx = os.path.join(
"$(SRCDIR)", os.path.join(subdir,
"%.cxx"))
114 obj = os.path.join(
"$(OBJDIR)", os.path.join(subdir,
"%.o"))
115 exe = os.path.join(
"$(EXEDIR)", os.path.join(subdir,
"%.exe"))
117 f.write(
"".join((make,
": ",cpp,
"\n")))
118 f.write(
"\t$(GET_DEPS)\n\n")
120 f.write(
"".join((make,
": ",cxx,
"\n")))
121 f.write(
"\t$(GET_DEPS)\n\n")
123 f.write(
"".join((obj,
": ",cpp,
"\n")))
124 f.write(
"\t$(COMPILE)\n\n")
126 f.write(
"".join((obj,
": ",cxx,
"\n")))
127 f.write(
"\t$(COMPILE)\n\n")
129 f.write(
"".join((exe,
": ",obj,
" $(LIBFILE)\n")))
130 f.write(
"\t$(LINK)\n\n")
139 command = [
"make",
"-j",str(multiprocessing.cpu_count()),
"-k",
"-r",
"-R",
140 "SRCDIR="+dirs.src,
"INCDIR="+dirs.inc,
141 "MAKEDIR="+dirs.make,
"OBJDIR="+dirs.obj,
"EXEDIR="+dirs.exe]
143 command.append(
"--silent")
145 command.append(
"--debug")
146 p = subprocess.Popen(command, stderr=subprocess.PIPE)
147 err_msg = p.communicate()[1]
148 if p.returncode == 0:
149 print(
"\n\n"+Term.GREEN+Term.BOLD
150 +
"Compilation succeeded!" 153 print(
"\n\n"+Term.RED+Term.BOLD
154 +
"################ ERRORS AND WARNINGS ################" 155 +Term.END+Term.END+
"\n", file=sys.stderr)
156 print(err_msg.decode(
"utf-8"), file=sys.stderr)
157 print(
"\n\n"+Term.RED+Term.BOLD
158 +
"Compilation failed." 159 +Term.END+
"\n", file=sys.stderr)
160 sys.exit(p.returncode)
164 build(dirs, verbosity)
165 elif mode ==
"clean":
167 elif mode ==
"set_dirs":
169 elif mode==
"print_vars":
170 subprocess.check_call([
"make",
"test",
"-r",
"-R",
"--silent",
171 "SRCDIR="+dirs.src,
"INCDIR="+dirs.inc,
172 "MAKEDIR="+dirs.make,
"OBJDIR="+dirs.obj,
"EXEDIR="+dirs.exe,
"print_vars"])
174 raise Exception(
"Unrecognized option: "+mode)
176 if __name__ ==
"__main__":
177 parser = argparse.ArgumentParser(description =
"Compiles ra4_draw code",
178 formatter_class = argparse.ArgumentDefaultsHelpFormatter)
179 parser.add_argument(
"mode", nargs=
"?", default=
"build", choices=[
"build",
"clean",
"set_dirs",
"print_vars"],
180 help =
"Selects which action to perform")
181 parser.add_argument(
"-v",
"--verbosity", type=int, default=1, choices=[0,1,2],
182 help =
"Set verbosity. Lower = less printing.")
183 parser.add_argument(
"--src_dir", default =
"src",
184 help =
"Directory containing .cpp and .cxx files")
185 parser.add_argument(
"--inc_dir", default =
"inc",
186 help =
"Directory containing .hpp files")
187 parser.add_argument(
"--make_dir", default =
"bin",
188 help =
"Directory in which to store .d files")
189 parser.add_argument(
"--obj_dir", default =
"bin",
190 help =
"Directory in which to place .o files")
191 parser.add_argument(
"--exe_dir", default =
"run",
192 help =
"Directory in which to store .exe files")
193 args = parser.parse_args()
195 compile(args.mode, args.verbosity,
196 DirStructure(args.src_dir, args.inc_dir, args.make_dir, args.obj_dir, args.exe_dir))
def build(dirs, verbosity)
def compile(mode, verbosity, dirs)
def ensureSubdirs(base, subdirs)
def tryRemove(directory, pattern)
def writeMakefile(subdirs)
def __init__(self, src, inc, make, obj, exe)
def getSubdirs(base, subdirs)