author | Edouard Tisserant |
Mon, 21 May 2012 16:40:57 +0200 | |
changeset 740 | cee825fbe9b3 |
parent 728 | e0424e96e3fd |
child 1023 | 93022adef055 |
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 |
726 | 2 |
from util.ProcessLogger import ProcessLogger |
203 | 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 |
""" |
|
725 | 13 |
def __init__(self, CTRInstance): |
14 |
self.CTRInstance = CTRInstance |
|
427 | 15 |
self.buildpath = None |
725 | 16 |
self.SetBuildPath(self.CTRInstance._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 |
""" |
725 | 22 |
return [self.CTRInstance.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 |
""" |
725 | 28 |
return self.CTRInstance.LDFLAGS + \ |
29 |
[self.CTRInstance.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 |
||
677
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
40 |
def ResetBinaryCodeMD5(self): |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
41 |
self.md5key = None |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
42 |
try: |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
43 |
os.remove(self._GetMD5FileName()) |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
44 |
except Exception, e: |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
45 |
pass |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
46 |
|
203 | 47 |
def GetBinaryCodeMD5(self): |
48 |
if self.md5key is not None: |
|
49 |
return self.md5key |
|
50 |
else: |
|
51 |
try: |
|
52 |
return open(self._GetMD5FileName(), "r").read() |
|
53 |
except Exception, e: |
|
54 |
return None |
|
427 | 55 |
|
56 |
def SetBuildPath(self, buildpath): |
|
57 |
if self.buildpath != buildpath: |
|
58 |
self.buildpath = buildpath |
|
725 | 59 |
self.exe = self.CTRInstance.GetProjectName() + self.extension |
427 | 60 |
self.exe_path = os.path.join(self.buildpath, self.exe) |
61 |
self.md5key = None |
|
62 |
self.srcmd5 = {} |
|
63 |
||
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
64 |
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
|
65 |
# Get latest computed hash and deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
66 |
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
|
67 |
# read source |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
68 |
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
|
69 |
# compute new hash |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
70 |
newhash = hashlib.md5(src).hexdigest() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
71 |
# compare |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
72 |
match = (oldhash == newhash) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
73 |
if not match: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
74 |
# file have changed |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
75 |
# update direct dependencies |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
76 |
deps = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
77 |
for l in src.splitlines(): |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
78 |
res = includes_re.match(l) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
79 |
if res is not None: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
80 |
depfn = res.groups()[0] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
81 |
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
|
82 |
#print bn + " depends on "+depfn |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
83 |
deps.append(depfn) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
84 |
# store that hashand deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
85 |
self.srcmd5[bn] = (newhash, deps) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
86 |
# recurse through deps |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
87 |
# TODO detect cicular deps. |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
88 |
return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match) |
203 | 89 |
|
90 |
def build(self): |
|
91 |
# Retrieve toolchain user parameters |
|
725 | 92 |
toolchain_params = self.CTRInstance.GetTarget().getcontent()["value"] |
203 | 93 |
self.compiler = toolchain_params.getCompiler() |
94 |
self.linker = toolchain_params.getLinker() |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
95 |
|
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
96 |
Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS()) |
203 | 97 |
|
98 |
######### GENERATE OBJECT FILES ######################################## |
|
99 |
obns = [] |
|
100 |
objs = [] |
|
323
9f07f0d429df
Fix bug preventing library to re-compiled when dependant files changed.
lbessard
parents:
297
diff
changeset
|
101 |
relink = False |
725 | 102 |
for Location, CFilesAndCFLAGS, DoCalls in self.CTRInstance.LocationCFilesAndCFLAGS: |
728
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
726
diff
changeset
|
103 |
if CFilesAndCFLAGS: |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
726
diff
changeset
|
104 |
if Location : |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
726
diff
changeset
|
105 |
self.CTRInstance.logger.write(".".join(map(str,Location))+" :\n") |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
726
diff
changeset
|
106 |
else: |
e0424e96e3fd
refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
726
diff
changeset
|
107 |
self.CTRInstance.logger.write(_("PLC :\n")) |
203 | 108 |
|
109 |
for CFile, CFLAGS in CFilesAndCFLAGS: |
|
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
110 |
if CFile.endswith(".c"): |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
111 |
bn = os.path.basename(CFile) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
112 |
obn = os.path.splitext(bn)[0]+".o" |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
113 |
objectfilename = os.path.splitext(CFile)[0]+".o" |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
114 |
|
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
115 |
match = self.check_and_update_hash_and_deps(bn) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
116 |
|
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
117 |
if match: |
725 | 118 |
self.CTRInstance.logger.write(" [pass] "+bn+" -> "+obn+"\n") |
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
119 |
else: |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
120 |
relink = True |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
121 |
|
725 | 122 |
self.CTRInstance.logger.write(" [CC] "+bn+" -> "+obn+"\n") |
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
123 |
|
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
124 |
status, result, err_result = ProcessLogger( |
725 | 125 |
self.CTRInstance.logger, |
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
126 |
"\"%s\" -c \"%s\" -o \"%s\" %s %s"% |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
127 |
(self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
128 |
).spin() |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
129 |
|
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
130 |
if status : |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
131 |
self.srcmd5.pop(bn) |
725 | 132 |
self.CTRInstance.logger.write_error(_("C compilation of %s failed.\n")%bn) |
693
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
133 |
return False |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
134 |
obns.append(obn) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
135 |
objs.append(objectfilename) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
136 |
elif CFile.endswith(".o"): |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
137 |
obns.append(os.path.basename(CFile)) |
96fcadb6a7a0
Added support for object files as a result of PlugGenerate_C
Edouard Tisserant
parents:
677
diff
changeset
|
138 |
objs.append(CFile) |
203 | 139 |
|
140 |
######### GENERATE library FILE ######################################## |
|
141 |
# Link all the object files into one binary file |
|
725 | 142 |
self.CTRInstance.logger.write(_("Linking :\n")) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
143 |
if relink: |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
144 |
objstring = [] |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
145 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
146 |
# Generate list .o files |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
147 |
listobjstring = '"' + '" "'.join(objs) + '"' |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
148 |
|
290
3bd617ae7a05
Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents:
285
diff
changeset
|
149 |
ALLldflags = ' '.join(self.getBuilderLDFLAGS()) |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
150 |
|
725 | 151 |
self.CTRInstance.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
|
152 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
153 |
status, result, err_result = ProcessLogger( |
725 | 154 |
self.CTRInstance.logger, |
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
155 |
"\"%s\" %s -o \"%s\" %s"% |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
156 |
(self.linker, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
157 |
listobjstring, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
158 |
self.exe_path, |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
159 |
ALLldflags) |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
160 |
).spin() |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
161 |
|
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
162 |
if status : |
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
163 |
return False |
677
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
164 |
|
285
e5782a52dcea
Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents:
203
diff
changeset
|
165 |
else: |
725 | 166 |
self.CTRInstance.logger.write(" [pass] " + ' '.join(obns)+" -> " + self.exe + "\n") |
677
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
167 |
|
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
168 |
# Calculate md5 key and get data for the new created PLC |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
169 |
data=self.GetBinaryCode() |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
170 |
self.md5key = hashlib.md5(data).hexdigest() |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
171 |
|
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
172 |
# Store new PLC filename based on md5 key |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
173 |
f = open(self._GetMD5FileName(), "w") |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
174 |
f.write(self.md5key) |
607731b33026
Fix 'Transfer" button state according to last build result
laurent
parents:
510
diff
changeset
|
175 |
f.close() |
203 | 176 |
|
177 |
return True |
|
178 |