author | greg |
Thu, 15 Oct 2009 17:39:26 +0200 | |
changeset 418 | 01f6bfc01251 |
parent 411 | 8261c8f1e365 |
child 421 | c9ec111ad275 |
permissions | -rwxr-xr-x |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
1 |
import os, re, operator |
203 | 2 |
from wxPopen import ProcessLogger |
3 |
import hashlib |
|
4 |
||
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
5 |
includes_re = re.compile('\s*#include\s*["<]([^">]*)[">].*') |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
6 |
|
203 | 7 |
class toolchain_gcc(): |
8 |
""" |
|
9 |
This abstract class contains GCC specific code. |
|
10 |
It cannot be used as this and should be inherited in a target specific |
|
11 |
class such as target_linux or target_win32 |
|
12 |
""" |
|
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
13 |
def __init__(self, PluginsRootInstance): |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
14 |
self.PluginsRootInstance = PluginsRootInstance |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
15 |
self.logger = PluginsRootInstance.logger |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
16 |
self.exe = PluginsRootInstance.GetProjectName() + self.extension |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
17 |
self.buildpath = PluginsRootInstance._getBuildPath() |
203 | 18 |
self.exe_path = os.path.join(self.buildpath, self.exe) |
19 |
self.md5key = None |
|
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
20 |
self.srcmd5 = {} |
203 | 21 |
|
411
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
22 |
def getTarget(self): |
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
23 |
target = self.PluginsRootInstance.BeremizRoot.getTargetType() |
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
24 |
if target.getcontent() is None: |
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
25 |
target = self.PluginsRootInstance.GetDefaultTarget() |
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
26 |
return target |
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
27 |
|
297
8fca8b555808
Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents:
290
diff
changeset
|
28 |
def getBuilderCFLAGS(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
29 |
""" |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
30 |
Returns list of builder specific CFLAGS |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
31 |
""" |
411
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
32 |
return [self.getTarget().getcontent()["value"].getCFLAGS()] |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
33 |
|
297
8fca8b555808
Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents:
290
diff
changeset
|
34 |
def getBuilderLDFLAGS(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
35 |
""" |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
36 |
Returns list of builder specific LDFLAGS |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
37 |
""" |
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
38 |
return self.PluginsRootInstance.LDFLAGS + \ |
411
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
39 |
[self.getTarget().getcontent()["value"].getLDFLAGS()] |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
40 |
|
203 | 41 |
def GetBinaryCode(self): |
42 |
try: |
|
43 |
return open(self.exe_path, "rb").read() |
|
44 |
except Exception, e: |
|
45 |
return None |
|
46 |
||
47 |
def _GetMD5FileName(self): |
|
48 |
return os.path.join(self.buildpath, "lastbuildPLC.md5") |
|
49 |
||
50 |
def GetBinaryCodeMD5(self): |
|
51 |
if self.md5key is not None: |
|
52 |
return self.md5key |
|
53 |
else: |
|
54 |
try: |
|
55 |
return open(self._GetMD5FileName(), "r").read() |
|
56 |
except Exception, e: |
|
57 |
return None |
|
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
58 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
59 |
def check_and_update_hash_and_deps(self, bn): |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
60 |
# Get latest computed hash and deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
61 |
oldhash, deps = self.srcmd5.get(bn,(None,[])) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
62 |
# read source |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
63 |
src = open(os.path.join(self.buildpath, bn)).read() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
64 |
# compute new hash |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
65 |
newhash = hashlib.md5(src).hexdigest() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
66 |
# compare |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
67 |
match = (oldhash == newhash) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
68 |
if not match: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
69 |
# file have changed |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
70 |
# update direct dependencies |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
71 |
deps = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
72 |
for l in src.splitlines(): |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
73 |
res = includes_re.match(l) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
74 |
if res is not None: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
75 |
depfn = res.groups()[0] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
76 |
if os.path.exists(os.path.join(self.buildpath, depfn)): |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
77 |
#print bn + " depends on "+depfn |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
78 |
deps.append(depfn) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
79 |
# store that hashand deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
80 |
self.srcmd5[bn] = (newhash, deps) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
81 |
# recurse through deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
82 |
# TODO detect cicular deps. |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
83 |
return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match) |
203 | 84 |
|
85 |
def build(self): |
|
86 |
# Retrieve toolchain user parameters |
|
411
8261c8f1e365
Bug on Debug trying to start (and stop) before PLC started fixed.
laurent
parents:
361
diff
changeset
|
87 |
toolchain_params = self.getTarget().getcontent()["value"] |
203 | 88 |
self.compiler = toolchain_params.getCompiler() |
89 |
self.linker = toolchain_params.getLinker() |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
90 |
|
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
91 |
Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS()) |
203 | 92 |
|
93 |
######### GENERATE OBJECT FILES ######################################## |
|
94 |
obns = [] |
|
95 |
objs = [] |
|
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
96 |
relink = False |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
97 |
for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS: |
203 | 98 |
if Location: |
361 | 99 |
self.logger.write(_("Plugin : ") + self.PluginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n") |
203 | 100 |
else: |
361 | 101 |
self.logger.write(_("PLC :\n")) |
203 | 102 |
|
103 |
for CFile, CFLAGS in CFilesAndCFLAGS: |
|
104 |
bn = os.path.basename(CFile) |
|
105 |
obn = os.path.splitext(bn)[0]+".o" |
|
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
106 |
objectfilename = os.path.splitext(CFile)[0]+".o" |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
107 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
108 |
match = self.check_and_update_hash_and_deps(bn) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
109 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
110 |
if match: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
111 |
self.logger.write(" [pass] "+bn+" -> "+obn+"\n") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
112 |
else: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
113 |
relink = True |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
114 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
115 |
self.logger.write(" [CC] "+bn+" -> "+obn+"\n") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
116 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
117 |
status, result, err_result = ProcessLogger( |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
118 |
self.logger, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
119 |
"\"%s\" -c \"%s\" -o \"%s\" %s %s"% |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
120 |
(self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
121 |
).spin() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
122 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
123 |
if status : |
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
124 |
self.srcmd5.pop(bn) |
361 | 125 |
self.logger.write_error(_("C compilation of %s failed.\n")%bn) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
126 |
return False |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
127 |
|
203 | 128 |
obns.append(obn) |
129 |
objs.append(objectfilename) |
|
130 |
||
131 |
######### GENERATE library FILE ######################################## |
|
132 |
# Link all the object files into one binary file |
|
361 | 133 |
self.logger.write(_("Linking :\n")) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
134 |
if relink: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
135 |
objstring = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
136 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
137 |
# Generate list .o files |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
138 |
listobjstring = '"' + '" "'.join(objs) + '"' |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
139 |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
140 |
ALLldflags = ' '.join(self.getBuilderLDFLAGS()) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
141 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
142 |
self.logger.write(" [CC] " + ' '.join(obns)+" -> " + self.exe + "\n") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
143 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
144 |
status, result, err_result = ProcessLogger( |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
145 |
self.logger, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
146 |
"\"%s\" %s -o \"%s\" %s"% |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
147 |
(self.linker, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
148 |
listobjstring, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
149 |
self.exe_path, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
150 |
ALLldflags) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
151 |
).spin() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
152 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
153 |
if status : |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
154 |
return False |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
155 |
else : |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
156 |
# Calculate md5 key and get data for the new created PLC |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
157 |
data=self.GetBinaryCode() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
158 |
self.md5key = hashlib.md5(data).hexdigest() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
159 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
160 |
# Store new PLC filename based on md5 key |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
161 |
f = open(self._GetMD5FileName(), "w") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
162 |
f.write(self.md5key) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
163 |
f.close() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
164 |
else: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
165 |
self.logger.write(" [pass] " + ' '.join(obns)+" -> " + self.exe + "\n") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
166 |
|
203 | 167 |
|
168 |
return True |
|
169 |