LPCtarget/toolchain_makefile.py
author Laurent Bessard
Fri, 27 Sep 2013 14:01:03 +0200
changeset 55 0be064739434
parent 53 d92a0c3040a4
permissions -rwxr-xr-x
Fixed bug when starting LPCManager
1
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     1
import os, re, operator
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     2
from util.ProcessLogger import ProcessLogger
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     3
import hashlib
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     4
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     5
import time
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     6
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     7
includes_re =  re.compile('\s*#include\s*["<]([^">]*)[">].*')
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     8
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
     9
class toolchain_makefile():
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    10
    def __init__(self, CTRInstance):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    11
        self.CTRInstance = CTRInstance
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    12
        self.md5key = None 
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    13
        self.buildpath = None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    14
        self.SetBuildPath(self.CTRInstance._getBuildPath())
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    15
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    16
    def SetBuildPath(self, buildpath):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    17
        if self.buildpath != buildpath:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    18
            self.buildpath = buildpath
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    19
            self.md5key = None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    20
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    21
    def GetBinaryCode(self):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    22
        return None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    23
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    24
    def _GetMD5FileName(self):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    25
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    26
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    27
    def ResetBinaryCodeMD5(self):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    28
        self.md5key = None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    29
        try:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    30
            os.remove(self._GetMD5FileName())
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    31
        except Exception, e:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    32
            pass
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    33
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    34
    def GetBinaryCodeMD5(self):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    35
        if self.md5key is not None:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    36
            return self.md5key
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    37
        else:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    38
            try:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    39
                return open(self._GetMD5FileName(), "r").read()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    40
            except IOError, e:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    41
                return None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    42
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    43
    def concat_deps(self, bn):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    44
        # read source
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    45
        src = open(os.path.join(self.buildpath, bn),"r").read()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    46
        # update direct dependencies
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    47
        deps = []
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    48
        for l in src.splitlines():
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    49
            res = includes_re.match(l)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    50
            if res is not None:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    51
                depfn = res.groups()[0]
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    52
                if os.path.exists(os.path.join(self.buildpath, depfn)):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    53
                    #print bn + " depends on "+depfn
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    54
                    deps.append(depfn)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    55
        # recurse through deps
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    56
        # TODO detect cicular deps.
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    57
        return reduce(operator.concat, map(self.concat_deps, deps), src)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    58
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    59
    def build(self):
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    60
        srcfiles= []
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    61
        cflags = []
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    62
        wholesrcdata = "" 
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    63
        for Location, CFilesAndCFLAGS, DoCalls in self.CTRInstance.LocationCFilesAndCFLAGS:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    64
            # Get CFiles list to give it to makefile
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    65
            for CFile, CFLAGS in CFilesAndCFLAGS:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    66
                CFileName = os.path.basename(CFile)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    67
                wholesrcdata += self.concat_deps(CFileName)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    68
                #wholesrcdata += open(CFile, "r").read()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    69
                srcfiles.append(CFileName)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    70
                if CFLAGS not in cflags:
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    71
                    cflags.append(CFLAGS)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    72
                        
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    73
        oldmd5 = self.md5key
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    74
        self.md5key = hashlib.md5(wholesrcdata).hexdigest()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    75
        props = self.CTRInstance.GetProjectProperties()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    76
        self.md5key += '#'.join([props[key] for key in ['companyName',
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    77
                                                        'projectName',
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    78
                                                        'productName']])
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    79
        self.md5key += '#' #+','.join(map(str,time.localtime()))
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    80
        # Store new PLC filename based on md5 key
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    81
        f = open(self._GetMD5FileName(), "w")
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    82
        f.write(self.md5key)
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    83
        f.close()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    84
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    85
        if oldmd5 != self.md5key :
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    86
            beremizcommand = {"src": ' '.join(srcfiles),
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    87
                              "cflags": ' '.join(cflags),
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    88
                              "md5": '"'+self.md5key+'"'
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    89
                             }
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    90
            
53
d92a0c3040a4 Fixed LPCManager according to newer version of Beremiz
Laurent Bessard
parents: 1
diff changeset
    91
            target = self.CTRInstance.GetTarget().getcontent()
1
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    92
            command = target.getCommand().split(' ') +\
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    93
                      [target.getBuildPath()] +\
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    94
                      [arg % beremizcommand for arg in target.getArguments().split(' ')] +\
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    95
                      target.getRule().split(' ')
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    96
            
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    97
            # Call Makefile to build PLC code and link it with target specific code
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    98
            status, result, err_result = ProcessLogger(self.CTRInstance.logger,
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
    99
                                                       command).spin()
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   100
            if status :
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   101
                self.md5key = None
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   102
                self.CTRInstance.logger.write_error(_("C compilation failed.\n"))
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   103
                return False
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   104
            return True
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   105
        else :
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   106
            self.CTRInstance.logger.write(_("Source didn't change, no build.\n"))
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   107
            return True
4f6d393cb36e Moved makefile target into LPCManager, from sam beremiz revision
Edouard Tisserant
parents:
diff changeset
   108