-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_makefile
executable file
·151 lines (127 loc) · 4.4 KB
/
run_makefile
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
#!/usr/bin/env python3
# This program was developed using python version 3.4.3
# Copyright (c) 2015 Joshua Gutow under the MIT License
import subprocess
import argparse
import sys
import os
# Global flag to determine if the program should be verbose or not
global VERBOSE
VERBOSE = False
# Prints the message if verbose mode is on
def verbose(msg):
if VERBOSE:
print(msg)
# Runs make clean
def clean():
try:
tmp = subprocess.check_output(["make", "clean"])
tmp = tmp.decode("utf-8")
verbose(tmp[0:-1])
except subprocess.CalledProcessError:
pass
# Compiles the build using make
def build():
try:
tmp = subprocess.check_output(["make"])
verbose(tmp.decode("utf-8")[0:-1])
except subprocess.CalledProcessError:
if not args.force:
sys.exit(1) # Error in the compilation of the project
# Runs the build
def execute():
params = ["java", "-cp", "build/", "main"]
verbose(" ".join(params) + "\n----Output Below----")
try:
subprocess.call(params)
except subprocess.CalledProcessError:
sys.exit(1) # Error when running the program
# Gets all the files in a given dir.
def lsfiles(directory):
if directory[-1] != "/":
directory = directory + "/"
tmp = os.listdir(directory)
ret = []
for path in tmp:
if os.path.isdir(directory + path):
ret = ret + lsfiles(directory+ path)
else:
ret.append(directory + path)
return ret
# Checks if there is a newer source file than the existing class files
def incremental(force_compile):
src = lsfiles("src/")
bld = lsfiles("build/")
if force_compile:
verbose("Rebuilding the project b/c of -f flag.")
build()
elif len(bld) == 0:
verbose("Rebuilding the project b/c no files build directory.")
build()
else:
verbose("Checking if project needs to be recompiled.")
rebuild = False
src = path_clean(src)
bld = path_clean(bld)
for path in bld:
if path not in src:
rebuild = True
break
elif is_updated(path):
rebuild = True
break
if rebuild:
verbose("Rebuilding the project b/c a change was found.")
build()
else:
verbose("Not rebuilding the project.")
# Given a filename, this checks if the src file is newer than the .class file
def is_updated(filename):
srcpath = "src/"
bldpath = "build/"
src = os.stat(srcpath + filename + ".java").st_mtime_ns
bld = os.stat(bldpath + filename + ".class").st_mtime_ns
return src > bld
# Cleans up the path lists so that we are only looking at what we care about
def path_clean(path_list):
ret = []
for path in path_list:
ret.append(path[path.find("/") + 1:path.find(".")])
return ret
# Main method of the program
def main():
# Sets up the flags that can be passed to the build system
parser = argparse.ArgumentParser(
description="builds and then runs the project as described by the makefile")
parser.add_argument("-r", "--rebuild", action="store_true",
help="runs make clean before building the project")
parser.add_argument("--run", action="store_true",
help="attempts to run the project without compiling it")
parser.add_argument("-v", "--verbose", action="store_true",
help="prints what the program is doing")
parser.add_argument("-c", "--compile", action="store_true",
help="compiles the project without running it")
parser.add_argument("-f", "--force", action="store_true",
help="forces the program to recompile the entire project")
args = parser.parse_args()
if not os.path.isfile("makefile") and not os.path.isfile("Makefile"):
print("There must be a vaild makefile in the directory.")
sys.exit(1)
if args.verbose == True: # If -v was passed, VERBOSE = True
global VERBOSE
VERBOSE = True
rebuild = args.rebuild # If -r was passed, clean = True
build = args.compile # If -c was passed, comp = True
force = args.force # If -f was passed, force = True
run = args.run # If --run was passed, run = True
if rebuild:
clean()
if build:
incremental(force)
elif run:
execute()
else:
incremental(force)
execute()
# Runs the program
main()