targets/toolchain_makefile.py
author Lolitech
Fri, 04 Jun 2010 14:59:11 +0200
changeset 556 1ad4e7f1e89e
parent 546 093a20ea5ffc
child 571 427bf9130d12
permissions -rwxr-xr-x
LPC Composer don't send comma at the end of serial port name.
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
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    19
    def GetBinaryCode(self):
538
ad05f27222cd Moved LPC specific code from toolchain_makefile into LPC target, added FROM_BEREMIZ variable to tell LPC's makefile it is invoked from here.
edouard
parents: 536
diff changeset
    20
        return None
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    21
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    22
    def _GetMD5FileName(self):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    23
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    24
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    25
    def GetBinaryCodeMD5(self):
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    26
        if self.md5key is not None:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    27
            return self.md5key
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    28
        else:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    29
            try:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    30
                return open(self._GetMD5FileName(), "r").read()
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    31
            except Exception, e:
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    32
                return None
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    33
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    34
    def build(self):
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    35
        srcfiles= []
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    36
        cflags = []
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    37
        for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS:
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    38
            wholesrcdata = "" 
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    39
            # Get CFiles list to give it to makefile
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    40
            for CFile, CFLAGS in CFilesAndCFLAGS:
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    41
                CFileName = os.path.basename(CFile)
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    42
                wholesrcdata += open(CFile, "r").read()
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    43
                srcfiles.append(CFileName)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    44
                if CFLAGS not in cflags:
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    45
                    cflags.append(CFLAGS)
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    46
                    
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    47
            self.md5key = hashlib.md5(wholesrcdata).hexdigest()
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    48
            props = self.PluginsRootInstance.GetProjectProperties()
536
9b77aabf3d36 Cosmetix fixes for LPC tests/build
Lolitech
parents: 521
diff changeset
    49
            self.md5key += '#'.join([props[key] for key in ['companyName',
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    50
                                                            'projectName',
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 510
diff changeset
    51
                                                            'productName']])
536
9b77aabf3d36 Cosmetix fixes for LPC tests/build
Lolitech
parents: 521
diff changeset
    52
            self.md5key += '#'+','.join(map(str,time.localtime()))
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    53
            # Store new PLC filename based on md5 key
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    54
            f = open(self._GetMD5FileName(), "w")
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    55
            f.write(self.md5key)
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    56
            f.close()
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    57
        beremizcommand = {"src": ' '.join(srcfiles),
508
73ecb803d8af Revamped LPC connector and toolchain
edouard
parents: 425
diff changeset
    58
                          "cflags": ' '.join(cflags),
546
093a20ea5ffc Safer passing of PLC ID to make command line.
Lolitech
parents: 538
diff changeset
    59
                          "md5": '"'+self.md5key+'"'
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    60
                         }
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    61
        
510
8038c08b9874 Getting default target when no target defined fixed
laurent
parents: 508
diff changeset
    62
        target = self.PluginsRootInstance.GetTarget().getcontent()["value"]
425
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    63
        command = target.getCommand().split(' ') +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    64
                  [target.getBuildPath()] +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    65
                  [arg % beremizcommand for arg in target.getArguments().split(' ')] +\
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    66
                  target.getRule().split(' ')
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    67
        
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    68
        # Call Makefile to build PLC code and link it with target specific code
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    69
        status, result, err_result = ProcessLogger(self.PluginsRootInstance.logger,
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    70
                                                   command).spin()
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    71
        if status :
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    72
            self.PluginsRootInstance.logger.write_error(_("C compilation of %s failed.\n"))
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    73
            return False
f390e9fdd2cf Add new target (Makefile)
greg
parents:
diff changeset
    74
        return True