author | Edouard Tisserant |
Mon, 26 Sep 2011 00:24:04 +0200 | |
changeset 617 | 7c23fac40a2a |
parent 510 | 8038c08b9874 |
child 677 | 607731b33026 |
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 |
427 | 15 |
self.buildpath = None |
16 |
self.SetBuildPath(self.PluginsRootInstance._getBuildPath()) |
|
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
427
diff
changeset
|
17 |
|
297
8fca8b555808
Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents:
290
diff
changeset
|
18 |
def getBuilderCFLAGS(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
19 |
""" |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
20 |
Returns list of builder specific CFLAGS |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
21 |
""" |
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
427
diff
changeset
|
22 |
return [self.PluginsRootInstance.GetTarget().getcontent()["value"].getCFLAGS()] |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
23 |
|
297
8fca8b555808
Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents:
290
diff
changeset
|
24 |
def getBuilderLDFLAGS(self): |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
25 |
""" |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
26 |
Returns list of builder specific LDFLAGS |
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
27 |
""" |
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
28 |
return self.PluginsRootInstance.LDFLAGS + \ |
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
427
diff
changeset
|
29 |
[self.PluginsRootInstance.GetTarget().getcontent()["value"].getLDFLAGS()] |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
30 |
|
203 | 31 |
def GetBinaryCode(self): |
32 |
try: |
|
33 |
return open(self.exe_path, "rb").read() |
|
34 |
except Exception, e: |
|
35 |
return None |
|
36 |
||
37 |
def _GetMD5FileName(self): |
|
38 |
return os.path.join(self.buildpath, "lastbuildPLC.md5") |
|
39 |
||
40 |
def GetBinaryCodeMD5(self): |
|
41 |
if self.md5key is not None: |
|
42 |
return self.md5key |
|
43 |
else: |
|
44 |
try: |
|
45 |
return open(self._GetMD5FileName(), "r").read() |
|
46 |
except Exception, e: |
|
47 |
return None |
|
427 | 48 |
|
49 |
def SetBuildPath(self, buildpath): |
|
50 |
if self.buildpath != buildpath: |
|
51 |
self.buildpath = buildpath |
|
52 |
self.exe = self.PluginsRootInstance.GetProjectName() + self.extension |
|
53 |
self.exe_path = os.path.join(self.buildpath, self.exe) |
|
54 |
self.md5key = None |
|
55 |
self.srcmd5 = {} |
|
56 |
||
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
57 |
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
|
58 |
# Get latest computed hash and deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
59 |
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
|
60 |
# read source |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
61 |
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
|
62 |
# compute new hash |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
63 |
newhash = hashlib.md5(src).hexdigest() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
64 |
# compare |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
65 |
match = (oldhash == newhash) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
66 |
if not match: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
67 |
# file have changed |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
68 |
# update direct dependencies |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
69 |
deps = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
70 |
for l in src.splitlines(): |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
71 |
res = includes_re.match(l) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
72 |
if res is not None: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
73 |
depfn = res.groups()[0] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
74 |
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
|
75 |
#print bn + " depends on "+depfn |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
76 |
deps.append(depfn) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
77 |
# store that hashand deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
78 |
self.srcmd5[bn] = (newhash, deps) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
79 |
# recurse through deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
80 |
# TODO detect cicular deps. |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
81 |
return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match) |
203 | 82 |
|
83 |
def build(self): |
|
84 |
# Retrieve toolchain user parameters |
|
510
8038c08b9874
Getting default target when no target defined fixed
laurent
parents:
427
diff
changeset
|
85 |
toolchain_params = self.PluginsRootInstance.GetTarget().getcontent()["value"] |
203 | 86 |
self.compiler = toolchain_params.getCompiler() |
87 |
self.linker = toolchain_params.getLinker() |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
88 |
|
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
89 |
Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS()) |
203 | 90 |
|
91 |
######### GENERATE OBJECT FILES ######################################## |
|
92 |
obns = [] |
|
93 |
objs = [] |
|
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
94 |
relink = False |
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
95 |
for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS: |
203 | 96 |
if Location: |
421 | 97 |
self.PluginsRootInstance.logger.write(_("Plugin : ") + self.PluginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n") |
203 | 98 |
else: |
421 | 99 |
self.PluginsRootInstance.logger.write(_("PLC :\n")) |
203 | 100 |
|
101 |
for CFile, CFLAGS in CFilesAndCFLAGS: |
|
102 |
bn = os.path.basename(CFile) |
|
103 |
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
|
104 |
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
|
105 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
106 |
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
|
107 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
108 |
if match: |
421 | 109 |
self.PluginsRootInstance.logger.write(" [pass] "+bn+" -> "+obn+"\n") |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
110 |
else: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
111 |
relink = True |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
112 |
|
421 | 113 |
self.PluginsRootInstance.logger.write(" [CC] "+bn+" -> "+obn+"\n") |
285
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 |
status, result, err_result = ProcessLogger( |
421 | 116 |
self.PluginsRootInstance.logger, |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
117 |
"\"%s\" -c \"%s\" -o \"%s\" %s %s"% |
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
118 |
(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
|
119 |
).spin() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
120 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
121 |
if status : |
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
122 |
self.srcmd5.pop(bn) |
421 | 123 |
self.PluginsRootInstance.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
|
124 |
return False |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
125 |
|
203 | 126 |
obns.append(obn) |
127 |
objs.append(objectfilename) |
|
128 |
||
129 |
######### GENERATE library FILE ######################################## |
|
130 |
# Link all the object files into one binary file |
|
421 | 131 |
self.PluginsRootInstance.logger.write(_("Linking :\n")) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
132 |
if relink: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
133 |
objstring = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
134 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
135 |
# Generate list .o files |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
136 |
listobjstring = '"' + '" "'.join(objs) + '"' |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
137 |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
138 |
ALLldflags = ' '.join(self.getBuilderLDFLAGS()) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
139 |
|
421 | 140 |
self.PluginsRootInstance.logger.write(" [CC] " + ' '.join(obns)+" -> " + self.exe + "\n") |
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 |
status, result, err_result = ProcessLogger( |
421 | 143 |
self.PluginsRootInstance.logger, |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
144 |
"\"%s\" %s -o \"%s\" %s"% |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
145 |
(self.linker, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
146 |
listobjstring, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
147 |
self.exe_path, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
148 |
ALLldflags) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
149 |
).spin() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
150 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
151 |
if status : |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
152 |
return False |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
153 |
else : |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
154 |
# 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
|
155 |
data=self.GetBinaryCode() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
156 |
self.md5key = hashlib.md5(data).hexdigest() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
157 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
158 |
# 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
|
159 |
f = open(self._GetMD5FileName(), "w") |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
160 |
f.write(self.md5key) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
161 |
f.close() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
162 |
else: |
421 | 163 |
self.PluginsRootInstance.logger.write(" [pass] " + ' '.join(obns)+" -> " + self.exe + "\n") |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
164 |
|
203 | 165 |
|
166 |
return True |
|
167 |