targets/toolchain_makefile.py
author edouard
Fri, 18 Feb 2011 12:29:48 +0100
changeset 576 7fcdc0d3d8d9
parent 573 f0c5fc74018d
child 677 607731b33026
permissions -rwxr-xr-x
Some typo fixes to make debug related methods in LPCAppOject stop throwing exceptions, less agressive error message when unplugging LPC
573
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
     1
import os, re, operator
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     2
from wxPopen import ProcessLogger
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     3
import hashlib
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     4
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     5
import time
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     6
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     7
includes_re =  re.compile('\s*#include\s*["<]([^">]*)[">].*')
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     8
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
     9
class toolchain_makefile():
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    10
    def __init__(self, PluginsRootInstance):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    11
        self.PluginsRootInstance = PluginsRootInstance
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    12
        self.md5key = None 
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    13
        self.buildpath = None
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    14
        self.SetBuildPath(self.PluginsRootInstance._getBuildPath())
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    15
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    16
    def SetBuildPath(self, buildpath):
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    17
        if self.buildpath != buildpath:
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    18
            self.buildpath = buildpath
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    19
            self.md5key = None
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    20
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    21
    def GetBinaryCode(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    22
        return None
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    23
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    24
    def _GetMD5FileName(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    25
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    26
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    27
    def GetBinaryCodeMD5(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    28
        if self.md5key is not None:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    29
            return self.md5key
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    30
        else:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    31
            try:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    32
                return open(self._GetMD5FileName(), "r").read()
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    33
            except IOError, e:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    34
                return None
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    35
573
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    36
    def concat_deps(self, bn):
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    37
        # read source
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    38
        src = open(os.path.join(self.buildpath, bn),"r").read()
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    39
        # update direct dependencies
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    40
        deps = []
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    41
        for l in src.splitlines():
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    42
            res = includes_re.match(l)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    43
            if res is not None:
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    44
                depfn = res.groups()[0]
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    45
                if os.path.exists(os.path.join(self.buildpath, depfn)):
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    46
                    #print bn + " depends on "+depfn
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    47
                    deps.append(depfn)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    48
        # recurse through deps
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    49
        # TODO detect cicular deps.
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    50
        return reduce(operator.concat, map(self.concat_deps, deps), src)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    51
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    52
    def build(self):
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    53
        srcfiles= []
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    54
        cflags = []
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    55
        wholesrcdata = "" 
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    56
        for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    57
            # Get CFiles list to give it to makefile
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    58
            for CFile, CFLAGS in CFilesAndCFLAGS:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    59
                CFileName = os.path.basename(CFile)
573
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    60
                wholesrcdata += self.concat_deps(CFileName)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    61
                #wholesrcdata += open(CFile, "r").read()
571
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    62
                srcfiles.append(CFileName)
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    63
                if CFLAGS not in cflags:
427bf9130d12 Debug switch (file in CWD). LPC : better MD5 handling, Run button in boot mode, handling data feedback in boot protocol
edouard
parents: 546
diff changeset
    64
                    cflags.append(CFLAGS)
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    65
                        
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    66
        oldmd5 = self.md5key
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    67
        self.md5key = hashlib.md5(wholesrcdata).hexdigest()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    68
        props = self.PluginsRootInstance.GetProjectProperties()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    69
        self.md5key += '#'.join([props[key] for key in ['companyName',
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    70
                                                        'projectName',
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    71
                                                        'productName']])
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    72
        self.md5key += '#' #+','.join(map(str,time.localtime()))
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    73
        # Store new PLC filename based on md5 key
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    74
        f = open(self._GetMD5FileName(), "w")
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    75
        f.write(self.md5key)
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    76
        f.close()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    77
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    78
        if oldmd5 != self.md5key :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    79
            beremizcommand = {"src": ' '.join(srcfiles),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    80
                              "cflags": ' '.join(cflags),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    81
                              "md5": '"'+self.md5key+'"'
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    82
                             }
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    83
            
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    84
            target = self.PluginsRootInstance.GetTarget().getcontent()["value"]
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    85
            command = target.getCommand().split(' ') +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    86
                      [target.getBuildPath()] +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    87
                      [arg % beremizcommand for arg in target.getArguments().split(' ')] +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    88
                      target.getRule().split(' ')
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    89
            
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    90
            # Call Makefile to build PLC code and link it with target specific code
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    91
            status, result, err_result = ProcessLogger(self.PluginsRootInstance.logger,
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    92
                                                       command).spin()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    93
            if status :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    94
                self.PluginsRootInstance.logger.write_error(_("C compilation of %s failed.\n"))
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    95
                return False
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    96
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    97
        else :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    98
            self.PluginsRootInstance.logger.write(_("Source didn't change, no build.\n"))
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    99
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   100