# This file is part of Beremiz, a Integrated Development Environment for
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
# See COPYING file for copyrights details.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from util.ProcessLogger import ProcessLogger
includes_re = re.compile('\s*#include\s*["<]([^">]*)[">].*')
This abstract class contains GCC specific code.
It cannot be used as this and should be inherited in a target specific
class such as target_linux or target_win32
def __init__(self, CTRInstance):
self.CTRInstance = CTRInstance
self.SetBuildPath(self.CTRInstance._getBuildPath())
def getBuilderCFLAGS(self):
Returns list of builder specific CFLAGS
return [self.CTRInstance.GetTarget().getcontent().getCFLAGS()]
def getBuilderLDFLAGS(self):
Returns list of builder specific LDFLAGS
return self.CTRInstance.LDFLAGS + \
[self.CTRInstance.GetTarget().getcontent().getLDFLAGS()]
return open(self.exe_path, "rb").read()
def _GetMD5FileName(self):
return os.path.join(self.buildpath, "lastbuildPLC.md5")
def ResetBinaryCodeMD5(self):
os.remove(self._GetMD5FileName())
def GetBinaryCodeMD5(self):
if self.md5key is not None:
return open(self._GetMD5FileName(), "r").read()
def SetBuildPath(self, buildpath):
if self.buildpath != buildpath:
self.buildpath = buildpath
self.exe = self.CTRInstance.GetProjectName() + self.extension
self.exe_path = os.path.join(self.buildpath, self.exe)
def check_and_update_hash_and_deps(self, bn):
# Get latest computed hash and deps
oldhash, deps = self.srcmd5.get(bn,(None,[]))
src = open(os.path.join(self.buildpath, bn)).read()
newhash = hashlib.md5(src).hexdigest()
match = (oldhash == newhash)
# update direct dependencies
for l in src.splitlines():
res = includes_re.match(l)
if os.path.exists(os.path.join(self.buildpath, depfn)):
#print bn + " depends on "+depfn
# store that hashand deps
self.srcmd5[bn] = (newhash, deps)
# TODO detect cicular deps.
return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
# Retrieve toolchain user parameters
toolchain_params = self.CTRInstance.GetTarget().getcontent()
self.compiler = toolchain_params.getCompiler()
self.linker = toolchain_params.getLinker()
Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
######### GENERATE OBJECT FILES ########################################
relink = self.GetBinaryCode() is None
for Location, CFilesAndCFLAGS, DoCalls in self.CTRInstance.LocationCFilesAndCFLAGS:
self.CTRInstance.logger.write(".".join(map(str,Location))+" :\n")
self.CTRInstance.logger.write(_("PLC :\n"))
for CFile, CFLAGS in CFilesAndCFLAGS:
bn = os.path.basename(CFile)
obn = os.path.splitext(bn)[0]+".o"
objectfilename = os.path.splitext(CFile)[0]+".o"
match = self.check_and_update_hash_and_deps(bn)
self.CTRInstance.logger.write(" [pass] "+bn+" -> "+obn+"\n")
self.CTRInstance.logger.write(" [CC] "+bn+" -> "+obn+"\n")
status, result, err_result = ProcessLogger(
"\"%s\" -c \"%s\" -o \"%s\" %s %s"%
(self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
self.CTRInstance.logger.write_error(_("C compilation of %s failed.\n")%bn)
objs.append(objectfilename)
elif CFile.endswith(".o"):
obns.append(os.path.basename(CFile))
######### GENERATE library FILE ########################################
# Link all the object files into one binary file
self.CTRInstance.logger.write(_("Linking :\n"))
listobjstring = '"' + '" "'.join(objs) + '"'
ALLldflags = ' '.join(self.getBuilderLDFLAGS())
self.CTRInstance.logger.write(" [CC] " + ' '.join(obns)+" -> " + self.exe + "\n")
status, result, err_result = ProcessLogger(
"\"%s\" %s -o \"%s\" %s"%
self.CTRInstance.logger.write(" [pass] " + ' '.join(obns)+" -> " + self.exe + "\n")
# Calculate md5 key and get data for the new created PLC
data=self.GetBinaryCode()
self.md5key = hashlib.md5(data).hexdigest()
# Store new PLC filename based on md5 key
f = open(self._GetMD5FileName(), "w")