targets/toolchain_gcc.py
changeset 717 1c23952dbde1
parent 693 96fcadb6a7a0
child 722 a94f361fc42e
equal deleted inserted replaced
716:180e4a7d945c 717:1c23952dbde1
     8     """
     8     """
     9     This abstract class contains GCC specific code.
     9     This abstract class contains GCC specific code.
    10     It cannot be used as this and should be inherited in a target specific
    10     It cannot be used as this and should be inherited in a target specific
    11     class such as target_linux or target_win32
    11     class such as target_linux or target_win32
    12     """
    12     """
    13     def __init__(self, PluginsRootInstance):
    13     def __init__(self, ConfigTreeRootInstance):
    14         self.PluginsRootInstance = PluginsRootInstance
    14         self.ConfigTreeRootInstance = ConfigTreeRootInstance
    15         self.buildpath = None
    15         self.buildpath = None
    16         self.SetBuildPath(self.PluginsRootInstance._getBuildPath())
    16         self.SetBuildPath(self.ConfigTreeRootInstance._getBuildPath())
    17     
    17     
    18     def getBuilderCFLAGS(self):
    18     def getBuilderCFLAGS(self):
    19         """
    19         """
    20         Returns list of builder specific CFLAGS
    20         Returns list of builder specific CFLAGS
    21         """
    21         """
    22         return [self.PluginsRootInstance.GetTarget().getcontent()["value"].getCFLAGS()]
    22         return [self.ConfigTreeRootInstance.GetTarget().getcontent()["value"].getCFLAGS()]
    23 
    23 
    24     def getBuilderLDFLAGS(self):
    24     def getBuilderLDFLAGS(self):
    25         """
    25         """
    26         Returns list of builder specific LDFLAGS
    26         Returns list of builder specific LDFLAGS
    27         """
    27         """
    28         return self.PluginsRootInstance.LDFLAGS + \
    28         return self.ConfigTreeRootInstance.LDFLAGS + \
    29                [self.PluginsRootInstance.GetTarget().getcontent()["value"].getLDFLAGS()]
    29                [self.ConfigTreeRootInstance.GetTarget().getcontent()["value"].getLDFLAGS()]
    30 
    30 
    31     def GetBinaryCode(self):
    31     def GetBinaryCode(self):
    32         try:
    32         try:
    33             return open(self.exe_path, "rb").read()
    33             return open(self.exe_path, "rb").read()
    34         except Exception, e:
    34         except Exception, e:
    54                 return None
    54                 return None
    55     
    55     
    56     def SetBuildPath(self, buildpath):
    56     def SetBuildPath(self, buildpath):
    57         if self.buildpath != buildpath:
    57         if self.buildpath != buildpath:
    58             self.buildpath = buildpath
    58             self.buildpath = buildpath
    59             self.exe = self.PluginsRootInstance.GetProjectName() + self.extension
    59             self.exe = self.ConfigTreeRootInstance.GetProjectName() + self.extension
    60             self.exe_path = os.path.join(self.buildpath, self.exe)
    60             self.exe_path = os.path.join(self.buildpath, self.exe)
    61             self.md5key = None
    61             self.md5key = None
    62             self.srcmd5 = {}
    62             self.srcmd5 = {}
    63     
    63     
    64     def check_and_update_hash_and_deps(self, bn):
    64     def check_and_update_hash_and_deps(self, bn):
    87         # TODO detect cicular deps.
    87         # TODO detect cicular deps.
    88         return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
    88         return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
    89                 
    89                 
    90     def build(self):
    90     def build(self):
    91         # Retrieve toolchain user parameters
    91         # Retrieve toolchain user parameters
    92         toolchain_params = self.PluginsRootInstance.GetTarget().getcontent()["value"]
    92         toolchain_params = self.ConfigTreeRootInstance.GetTarget().getcontent()["value"]
    93         self.compiler = toolchain_params.getCompiler()
    93         self.compiler = toolchain_params.getCompiler()
    94         self.linker = toolchain_params.getLinker()
    94         self.linker = toolchain_params.getLinker()
    95 
    95 
    96         Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
    96         Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
    97 
    97 
    98         ######### GENERATE OBJECT FILES ########################################
    98         ######### GENERATE OBJECT FILES ########################################
    99         obns = []
    99         obns = []
   100         objs = []
   100         objs = []
   101         relink = False
   101         relink = False
   102         for Location, CFilesAndCFLAGS, DoCalls in self.PluginsRootInstance.LocationCFilesAndCFLAGS:
   102         for Location, CFilesAndCFLAGS, DoCalls in self.ConfigTreeRootInstance.LocationCFilesAndCFLAGS:
   103             if Location:
   103             if Location:
   104                 self.PluginsRootInstance.logger.write(_("Plugin : ") + self.PluginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n")
   104                 self.ConfigTreeRootInstance.logger.write(_("ConfNode : ") + self.ConfigTreeRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n")
   105             else:
   105             else:
   106                 self.PluginsRootInstance.logger.write(_("PLC :\n"))
   106                 self.ConfigTreeRootInstance.logger.write(_("PLC :\n"))
   107                 
   107                 
   108             for CFile, CFLAGS in CFilesAndCFLAGS:
   108             for CFile, CFLAGS in CFilesAndCFLAGS:
   109                 if CFile.endswith(".c"):
   109                 if CFile.endswith(".c"):
   110                     bn = os.path.basename(CFile)
   110                     bn = os.path.basename(CFile)
   111                     obn = os.path.splitext(bn)[0]+".o"
   111                     obn = os.path.splitext(bn)[0]+".o"
   112                     objectfilename = os.path.splitext(CFile)[0]+".o"
   112                     objectfilename = os.path.splitext(CFile)[0]+".o"
   113 
   113 
   114                     match = self.check_and_update_hash_and_deps(bn)
   114                     match = self.check_and_update_hash_and_deps(bn)
   115                     
   115                     
   116                     if match:
   116                     if match:
   117                         self.PluginsRootInstance.logger.write("   [pass]  "+bn+" -> "+obn+"\n")
   117                         self.ConfigTreeRootInstance.logger.write("   [pass]  "+bn+" -> "+obn+"\n")
   118                     else:
   118                     else:
   119                         relink = True
   119                         relink = True
   120 
   120 
   121                         self.PluginsRootInstance.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
   121                         self.ConfigTreeRootInstance.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
   122                         
   122                         
   123                         status, result, err_result = ProcessLogger(
   123                         status, result, err_result = ProcessLogger(
   124                                self.PluginsRootInstance.logger,
   124                                self.ConfigTreeRootInstance.logger,
   125                                "\"%s\" -c \"%s\" -o \"%s\" %s %s"%
   125                                "\"%s\" -c \"%s\" -o \"%s\" %s %s"%
   126                                    (self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
   126                                    (self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
   127                                ).spin()
   127                                ).spin()
   128 
   128 
   129                         if status :
   129                         if status :
   130                             self.srcmd5.pop(bn)
   130                             self.srcmd5.pop(bn)
   131                             self.PluginsRootInstance.logger.write_error(_("C compilation of %s failed.\n")%bn)
   131                             self.ConfigTreeRootInstance.logger.write_error(_("C compilation of %s failed.\n")%bn)
   132                             return False
   132                             return False
   133                     obns.append(obn)
   133                     obns.append(obn)
   134                     objs.append(objectfilename)
   134                     objs.append(objectfilename)
   135                 elif CFile.endswith(".o"):
   135                 elif CFile.endswith(".o"):
   136                     obns.append(os.path.basename(CFile))
   136                     obns.append(os.path.basename(CFile))
   137                     objs.append(CFile)
   137                     objs.append(CFile)
   138 
   138 
   139         ######### GENERATE library FILE ########################################
   139         ######### GENERATE library FILE ########################################
   140         # Link all the object files into one binary file
   140         # Link all the object files into one binary file
   141         self.PluginsRootInstance.logger.write(_("Linking :\n"))
   141         self.ConfigTreeRootInstance.logger.write(_("Linking :\n"))
   142         if relink:
   142         if relink:
   143             objstring = []
   143             objstring = []
   144     
   144     
   145             # Generate list .o files
   145             # Generate list .o files
   146             listobjstring = '"' + '"  "'.join(objs) + '"'
   146             listobjstring = '"' + '"  "'.join(objs) + '"'
   147     
   147     
   148             ALLldflags = ' '.join(self.getBuilderLDFLAGS())
   148             ALLldflags = ' '.join(self.getBuilderLDFLAGS())
   149     
   149     
   150             self.PluginsRootInstance.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.exe + "\n")
   150             self.ConfigTreeRootInstance.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.exe + "\n")
   151     
   151     
   152             status, result, err_result = ProcessLogger(
   152             status, result, err_result = ProcessLogger(
   153                    self.PluginsRootInstance.logger,
   153                    self.ConfigTreeRootInstance.logger,
   154                    "\"%s\" %s -o \"%s\" %s"%
   154                    "\"%s\" %s -o \"%s\" %s"%
   155                        (self.linker,
   155                        (self.linker,
   156                         listobjstring,
   156                         listobjstring,
   157                         self.exe_path,
   157                         self.exe_path,
   158                         ALLldflags)
   158                         ALLldflags)
   160             
   160             
   161             if status :
   161             if status :
   162                 return False
   162                 return False
   163                 
   163                 
   164         else:
   164         else:
   165             self.PluginsRootInstance.logger.write("   [pass]  " + ' '.join(obns)+" -> " + self.exe + "\n")
   165             self.ConfigTreeRootInstance.logger.write("   [pass]  " + ' '.join(obns)+" -> " + self.exe + "\n")
   166         
   166         
   167         # Calculate md5 key and get data for the new created PLC
   167         # Calculate md5 key and get data for the new created PLC
   168         data=self.GetBinaryCode()
   168         data=self.GetBinaryCode()
   169         self.md5key = hashlib.md5(data).hexdigest()
   169         self.md5key = hashlib.md5(data).hexdigest()
   170 
   170