targets/toolchain_makefile.py
author laurent
Wed, 16 Dec 2009 13:26:59 +0100
changeset 523 27903000e4ea
parent 521 02cb9e5fb6f6
child 536 9b77aabf3d36
permissions -rwxr-xr-x
Bug when pyserial not available fixed
510
8038c08b9874 Getting default target when no target defined fixed
laurent
parents: 508
diff changeset
     1
import os, re
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     2
from wxPopen import ProcessLogger
510
8038c08b9874 Getting default target when no target defined fixed
laurent
parents: 508
diff changeset
     3
import hashlib
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     4
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
     5
import time
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
     6
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     7
includes_re =  re.compile('\s*#include\s*["<]([^">]*)[">].*')
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
     8
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
     9
class toolchain_makefile():
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    10
    def __init__(self, PluginsRootInstance):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    11
        self.PluginsRootInstance = PluginsRootInstance
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    12
        self.md5key = None 
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    13
        self.buildpath = None
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    14
        self.SetBuildPath(self.PluginsRootInstance._getBuildPath())
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    15
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    16
    def SetBuildPath(self, buildpath):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    17
        self.buildpath = buildpath
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    18
        self.exe_path = os.path.join(self.buildpath, "ArmPLC_rom.bin")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    19
        self.md5_path = os.path.join(self.buildpath, "ArmPLC.md5")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    20
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    21
    def GetBinaryCode(self):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    22
        try:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    23
            return open(self.exe_path, "rb").read()
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    24
        except Exception, e:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    25
            return None
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    26
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    27
    def _GetMD5FileName(self):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    28
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    29
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    30
    def GetBinaryCodeMD5(self):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    31
        if self.md5key is not None:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    32
            return self.md5key
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    33
        else:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    34
            try:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    35
                return open(self._GetMD5FileName(), "r").read()
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    36
            except Exception, e:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    37
                return None
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    38
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    39
    def build(self):
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    40
        srcfiles= []
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    41
        cflags = []
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    42
        for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS:
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    43
            wholesrcdata = "" 
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    44
            # Get CFiles list to give it to makefile
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    45
            for CFile, CFLAGS in CFilesAndCFLAGS:
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    46
                CFileName = os.path.basename(CFile)
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    47
                wholesrcdata += open(CFile, "r").read()
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    48
                srcfiles.append(CFileName)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    49
                if CFLAGS not in cflags:
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    50
                    cflags.append(CFLAGS)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    51
                    
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    52
            self.md5key = hashlib.md5(wholesrcdata).hexdigest()
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    53
            props = self.PluginsRootInstance.GetProjectProperties()
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    54
            self.md5key += '|'.join([props[key] for key in ['companyName',
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    55
                                                            'projectName',
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    56
                                                            'productName']])
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    57
            self.md5key += '|'+','.join(map(str,time.localtime()))
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    58
            # Store new PLC filename based on md5 key
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    59
            f = open(self._GetMD5FileName(), "w")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    60
            f.write(self.md5key)
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    61
            f.close()
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    62
        beremizcommand = {"src": ' '.join(srcfiles),
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    63
                          "cflags": ' '.join(cflags),
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    64
                          "md5": self.md5key
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    65
                         }
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    66
        
510
8038c08b9874 Getting default target when no target defined fixed
laurent
parents: 508
diff changeset
    67
        target = self.PluginsRootInstance.GetTarget().getcontent()["value"]
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    68
        command = target.getCommand().split(' ') +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    69
                  [target.getBuildPath()] +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    70
                  [arg % beremizcommand for arg in target.getArguments().split(' ')] +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    71
                  target.getRule().split(' ')
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    72
        
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    73
        # Call Makefile to build PLC code and link it with target specific code
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    74
        status, result, err_result = ProcessLogger(self.PluginsRootInstance.logger,
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    75
                                                   command).spin()
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    76
        if status :
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    77
            self.PluginsRootInstance.logger.write_error(_("C compilation of %s failed.\n"))
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    78
            return False
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    79
        return True