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