targets/toolchain_makefile.py
author Edouard Tisserant
Mon, 07 May 2012 18:47:29 +0200
changeset 717 1c23952dbde1
parent 691 bb340874f09e
child 722 a94f361fc42e
permissions -rwxr-xr-x
refactoring
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():
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    10
    def __init__(self, ConfigTreeRootInstance):
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    11
        self.ConfigTreeRootInstance = ConfigTreeRootInstance
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
    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
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    14
        self.SetBuildPath(self.ConfigTreeRootInstance._getBuildPath())
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
    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
677
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    27
    def ResetBinaryCodeMD5(self):
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    28
        self.md5key = None
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    29
        try:
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    30
            os.remove(self._GetMD5FileName())
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    31
        except Exception, e:
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    32
            pass
607731b33026 Fix 'Transfer" button state according to last build result
laurent
parents: 573
diff changeset
    33
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
    34
    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
    35
        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
    36
            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
    37
        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
    38
            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
    39
                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
    40
            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
    41
                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
    42
573
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    43
    def concat_deps(self, bn):
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    44
        # read source
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    45
        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
    46
        # update direct dependencies
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    47
        deps = []
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    48
        for l in src.splitlines():
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    49
            res = includes_re.match(l)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    50
            if res is not None:
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    51
                depfn = res.groups()[0]
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    52
                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
    53
                    #print bn + " depends on "+depfn
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    54
                    deps.append(depfn)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    55
        # recurse through deps
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    56
        # TODO detect cicular deps.
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    57
        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
    58
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
    59
    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
    60
        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
    61
        cflags = []
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    62
        wholesrcdata = "" 
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    63
        for Location, CFilesAndCFLAGS, DoCalls in self.ConfigTreeRootInstance.LocationCFilesAndCFLAGS:
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
    64
            # 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
    65
            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
    66
                CFileName = os.path.basename(CFile)
573
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    67
                wholesrcdata += self.concat_deps(CFileName)
f0c5fc74018d Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds - continued
edouard
parents: 572
diff changeset
    68
                #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
    69
                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
    70
                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
    71
                    cflags.append(CFLAGS)
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    72
                        
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    73
        oldmd5 = self.md5key
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    74
        self.md5key = hashlib.md5(wholesrcdata).hexdigest()
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    75
        props = self.ConfigTreeRootInstance.GetProjectProperties()
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    76
        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
    77
                                                        'projectName',
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    78
                                                        'productName']])
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    79
        self.md5key += '#' #+','.join(map(str,time.localtime()))
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    80
        # 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
    81
        f = open(self._GetMD5FileName(), "w")
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    82
        f.write(self.md5key)
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    83
        f.close()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    84
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    85
        if oldmd5 != self.md5key :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    86
            beremizcommand = {"src": ' '.join(srcfiles),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    87
                              "cflags": ' '.join(cflags),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    88
                              "md5": '"'+self.md5key+'"'
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
            
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    91
            target = self.ConfigTreeRootInstance.GetTarget().getcontent()["value"]
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    92
            command = target.getCommand().split(' ') +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    93
                      [target.getBuildPath()] +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    94
                      [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
    95
                      target.getRule().split(' ')
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    96
            
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    97
            # Call Makefile to build PLC code and link it with target specific code
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
    98
            status, result, err_result = ProcessLogger(self.ConfigTreeRootInstance.logger,
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    99
                                                       command).spin()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   100
            if status :
691
bb340874f09e Fix bug with "Transfer" button
smarteh-dev
parents: 677
diff changeset
   101
                self.md5key = None
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
   102
                self.ConfigTreeRootInstance.logger.write_error(_("C compilation failed.\n"))
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   103
                return False
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   104
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   105
        else :
717
1c23952dbde1 refactoring
Edouard Tisserant
parents: 691
diff changeset
   106
            self.ConfigTreeRootInstance.logger.write(_("Source didn't change, no build.\n"))
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   107
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
   108