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