-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
154 lines (118 loc) · 5.02 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
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
""" A basic python library for building a C/C++ project """
from os import path
from sys import argv
import os, shutil, sys
import click
sys.path.append(os.path.join(".", "build"))
from globalsettings import *
from includer import lastTimeMod
# from objer import *
print("Bulding project in directory", sourcedir)
srcs = []
objs = []
def getSrcFiles(testcase: str):
global srcs
srcsTmp = []
# Get all directories with src files
for (root,dirs,files) in os.walk(sourcedir):
# print(root)
# print(dirs)
# print(files)
# print("-" * (max(len(str(root)), len(str(dirs)), len(str(files)))))
if root[0:len(testdir)] == testdir:
# print("PASSING", root)
continue
for file in files:
if checkExts(file):
srcsTmp.append([root,files])
break
#Filter for source files only
for root, files in srcsTmp:
filesTmp = []
# print(root,"\t", files)
for file in files:
if checkExts(file):
filesTmp.append(file)
srcs.append([root, filesTmp])
if testcase == None:
return
# Change srcs for testcase
srcs.remove(['.', ['main.cpp']])
# Match testcase to a test file name
for (root,dirs,files) in os.walk(testdir):
print(root)
print(dirs)
print(files)
print("-" * (max(len(str(root)), len(str(dirs)), len(str(files)))))
for file in files:
if checkExts(file) and testcase in file:
srcs.append([root, [file]])
def compileFile(root, file):
absName = path.join(root,file)
objName = path.join(objdir, path.splitext(file)[0] + ".o")
# print("Compiling", absName, "to", objName)
objs.append(objName)
#Check if OBJ exists
precomped = path.exists(objName)
if precomped:
...
#Check if file was modified before obj
compModTime = path.getmtime(objName)
fileModTime = lastTimeMod(absName)
# import time
# modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(compModTime))
# print("Obj Last Modified Time : ", modificationTime )
# modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(fileModTime))
# print("File Last Modified Time : ", modificationTime )
if compModTime > fileModTime:
print("Obj already found for", absName)
return
#Compile file
cmd = " ".join([cxx, pflags, "-c", absName, flags, lflags, "-o", objName])
print("Compiling", absName, "to", objName, "with", cmd)
os.system(cmd)
def compile(forceRecompile: bool):
if not path.exists(objdir):
os.mkdir(objdir)
if forceRecompile:
cleanObjs()
for root, files in srcs:
for file in files:
compileFile(root, file)
def cleanObjs():
print("Removing objs")
for filename in os.listdir(objdir):
file_path = path.join(objdir, filename)
try:
if path.isfile(file_path) or path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def link():
print("Linking obj files")
objsStr = " ".join(objs)
cmd = " ".join([cxx, pflags, objsStr, flags, lflags, "-o", out])
print("Linking with", cmd)
os.system(cmd)
def checkExts(file):
return any(file.lower().endswith(ext) for ext in sourceTypes)
@click.command()
@click.option('-f', '--force-recompilation', 'forceRecompile', is_flag=True, default=False, help='Forcefully remove all cached object files and recompile everything')
@click.option('-r', '--autorun', 'runAfterDone', is_flag=True, default=True, help='Automatically run after finished compiling.')
@click.option('-o', '--output', 'output', default=out, help='Executable to output to.')
@click.option('-t', '--test', 'testcase', default=None, help='Case to test')
def main(forceRecompile, runAfterDone, output, testcase):
out = output
if forceRecompile:
print("Forcing recompilation")
getSrcFiles(testcase)
print("srcs:", srcs)
compile(forceRecompile)
link()
if runAfterDone:
print("\nRunning\n")
os.system(path.join(".", out))
if __name__ == "__main__":
main()