targets/toolchain_gcc.py
author etisserant
Thu, 04 Sep 2008 16:07:14 +0200
changeset 238 02d0daed3e46
parent 203 cb9901076a21
child 285 e5782a52dcea
permissions -rwxr-xr-x
Debugger now reports BOOL as booleans
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
import os
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
from wxPopen import ProcessLogger
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
import hashlib
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
class toolchain_gcc():
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
    """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
    This abstract class contains GCC specific code.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
    It cannot be used as this and should be inherited in a target specific
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
    class such as target_linux or target_win32
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
    """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
    def __init__(self, PuginsRootInstance):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
        self.PuginsRootInstance = PuginsRootInstance
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
        self.logger = PuginsRootInstance.logger
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
        self.exe = PuginsRootInstance.GetProjectName() + self.extension
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
        self.buildpath = PuginsRootInstance._getBuildPath()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
        self.exe_path = os.path.join(self.buildpath, self.exe)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
        self.md5key = None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
    def GetBinaryCode(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
            return open(self.exe_path, "rb").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
        except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
            return None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
    def _GetMD5FileName(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    27
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    28
    def GetBinaryCodeMD5(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    29
        if self.md5key is not None:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    30
            return self.md5key
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    31
        else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    32
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    33
                return open(self._GetMD5FileName(), "r").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    34
            except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
                return None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
                
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
    def build(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    39
        # Retrieve toolchain user parameters
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
        toolchain_params = self.PuginsRootInstance.BeremizRoot.getTargetType().getcontent()["value"]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    41
        self.compiler = toolchain_params.getCompiler()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
        self._CFLAGS = toolchain_params.getCFLAGS()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    43
        self.linker = toolchain_params.getLinker()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
        self._LDFLAGS = toolchain_params.getLDFLAGS()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    45
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    46
        ######### GENERATE OBJECT FILES ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    47
        obns = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    48
        objs = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    49
        for Location, CFilesAndCFLAGS, DoCalls in self.PuginsRootInstance.LocationCFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
            if Location:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
                self.logger.write("Plugin : " + self.PuginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    52
            else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    53
                self.logger.write("PLC :\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    54
                
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    55
            for CFile, CFLAGS in CFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    56
                bn = os.path.basename(CFile)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    57
                obn = os.path.splitext(bn)[0]+".o"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    58
                obns.append(obn)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    59
                self.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    60
                objectfilename = os.path.splitext(CFile)[0]+".o"
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    61
                
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    62
                status, result, err_result = ProcessLogger(
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    63
                       self.logger,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    64
                       "\"%s\" -c \"%s\" -o \"%s\" %s %s"%
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    65
                           (self.compiler, CFile, objectfilename, self._CFLAGS, CFLAGS)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    66
                       ).spin()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    68
                if status :
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    69
                    self.logger.write_error("C compilation of "+ bn +" failed.\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
                    return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    71
                objs.append(objectfilename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
        ######### GENERATE library FILE ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
        # Link all the object files into one binary file
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
        self.logger.write("Linking :\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
        objstring = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
        # Generate list .o files
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
        listobjstring = '"' + '"  "'.join(objs) + '"'
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
        ALLldflags = ' '.join(self.CustomLDFLAGS+self.PuginsRootInstance.LDFLAGS+[self._LDFLAGS])
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
        self.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.exe + "\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
        status, result, err_result = ProcessLogger(
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
               self.logger,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
               "\"%s\" %s -o \"%s\" %s"%
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
                   (self.linker,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
                    listobjstring,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
                    self.exe_path,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
                    ALLldflags)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
               ).spin()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
        if status :
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
            return False
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    96
        else :
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    97
            # Calculate md5 key and get data for the new created PLC
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    98
            data=self.GetBinaryCode()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    99
            self.md5key = hashlib.md5(data).hexdigest()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   100
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   101
            # Store new PLC filename based on md5 key
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
            file = open(self._GetMD5FileName(), "w")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   103
            file.write(self.md5key)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   104
            file.close()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   105
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
        return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107