targets/toolchain_makefile.py
author edouard
Thu, 17 Feb 2011 15:59:56 +0100
changeset 572 c965548cb6f7
parent 571 427bf9130d12
child 573 f0c5fc74018d
permissions -rwxr-xr-x
Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
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
     1
import os, re
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
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
    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
    37
        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
    38
        cflags = []
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    39
        wholesrcdata = "" 
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    40
        print self.PluginsRootInstance.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
    41
        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
    42
            # 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
    43
            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
    44
                CFileName = os.path.basename(CFile)
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
    45
                wholesrcdata += open(CFile, "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
    46
                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
    47
                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
    48
                    cflags.append(CFLAGS)
572
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    49
                        
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    50
        oldmd5 = self.md5key
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    51
        self.md5key = hashlib.md5(wholesrcdata).hexdigest()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    52
        props = self.PluginsRootInstance.GetProjectProperties()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    53
        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
    54
                                                        'projectName',
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    55
                                                        'productName']])
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    56
        self.md5key += '#' #+','.join(map(str,time.localtime()))
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    57
        # 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
    58
        f = open(self._GetMD5FileName(), "w")
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    59
        f.write(self.md5key)
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    60
        f.close()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    61
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    62
        if oldmd5 != self.md5key :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    63
            beremizcommand = {"src": ' '.join(srcfiles),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    64
                              "cflags": ' '.join(cflags),
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    65
                              "md5": '"'+self.md5key+'"'
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    66
                             }
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    67
            
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    68
            target = self.PluginsRootInstance.GetTarget().getcontent()["value"]
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    69
            command = target.getCommand().split(' ') +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    70
                      [target.getBuildPath()] +\
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    71
                      [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
    72
                      target.getRule().split(' ')
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    73
            
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    74
            # 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
    75
            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
    76
                                                       command).spin()
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    77
            if status :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    78
                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
    79
                return False
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    80
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    81
        else :
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    82
            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
    83
            return True
c965548cb6f7 Fixed MD5 computation for makefile based toolchain, helps avoiding unnecessary builds
edouard
parents: 571
diff changeset
    84