targets/toolchain_gcc.py
author etisserant
Fri, 16 Jan 2009 16:50:54 +0100
changeset 297 8fca8b555808
parent 290 3bd617ae7a05
child 323 9f07f0d429df
permissions -rwxr-xr-x
Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
     1
import os, re, operator
203
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
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
     5
includes_re =  re.compile('\s*#include\s*["<]([^">]*)[">].*')
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
     6
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
class toolchain_gcc():
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
    """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
    This abstract class contains GCC specific code.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
    It cannot be used as this and should be inherited in a target specific
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
    class such as target_linux or target_win32
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
    """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
    def __init__(self, PuginsRootInstance):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
        self.PuginsRootInstance = PuginsRootInstance
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
        self.logger = PuginsRootInstance.logger
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
        self.exe = PuginsRootInstance.GetProjectName() + self.extension
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
        self.buildpath = PuginsRootInstance._getBuildPath()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
        self.exe_path = os.path.join(self.buildpath, self.exe)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
        self.md5key = None
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    20
        self.srcmd5 = {}
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
297
8fca8b555808 Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents: 290
diff changeset
    22
    def getBuilderCFLAGS(self):
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    23
        """
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    24
        Returns list of builder specific CFLAGS
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    25
        """
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    26
        return [self.PuginsRootInstance.BeremizRoot.getTargetType().getcontent()["value"].getCFLAGS()]
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    27
297
8fca8b555808 Fixed typo in (LD/C)FLAGS hendling in toolchain_gcc.py
etisserant
parents: 290
diff changeset
    28
    def getBuilderLDFLAGS(self):
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    29
        """
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    30
        Returns list of builder specific LDFLAGS
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    31
        """
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    32
        return self.PuginsRootInstance.LDFLAGS + \
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    33
               [self.PuginsRootInstance.BeremizRoot.getTargetType().getcontent()["value"].getLDFLAGS()]
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    34
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
    def GetBinaryCode(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
            return open(self.exe_path, "rb").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
        except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    39
            return None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    41
    def _GetMD5FileName(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    43
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
    def GetBinaryCodeMD5(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    45
        if self.md5key is not None:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    46
            return self.md5key
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    47
        else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    48
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    49
                return open(self._GetMD5FileName(), "r").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
            except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
                return None
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    52
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    53
    def check_and_update_hash_and_deps(self, bn):
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    54
        # Get latest computed hash and deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    55
        oldhash, deps = self.srcmd5.get(bn,(None,[]))
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    56
        # read source
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    57
        src = open(os.path.join(self.buildpath, bn)).read()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    58
        # compute new hash
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    59
        newhash = hashlib.md5(src).hexdigest()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    60
        # compare
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    61
        match = (oldhash == newhash)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    62
        if not match:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    63
            # file have changed
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    64
            # update direct dependencies
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    65
            deps = []
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    66
            for l in src.splitlines():
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    67
                res = includes_re.match(l)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    68
                if res is not None:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    69
                    depfn = res.groups()[0]
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    70
                    if os.path.exists(os.path.join(self.buildpath, depfn)):
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    71
                        #print bn + " depends on "+depfn
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    72
                        deps.append(depfn)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    73
            # store that hashand deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    74
            self.srcmd5[bn] = (newhash, deps)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    75
        # recurse through deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    76
        # TODO detect cicular deps.
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    77
        return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
                
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
    def build(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
        # Retrieve toolchain user parameters
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
        toolchain_params = self.PuginsRootInstance.BeremizRoot.getTargetType().getcontent()["value"]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
        self.compiler = toolchain_params.getCompiler()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
        self.linker = toolchain_params.getLinker()
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    84
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
    85
        Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    87
        ######### GENERATE OBJECT FILES ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    88
        obns = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
        objs = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
        for Location, CFilesAndCFLAGS, DoCalls in self.PuginsRootInstance.LocationCFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
            if Location:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
                self.logger.write("Plugin : " + self.PuginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
            else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
                self.logger.write("PLC :\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
                
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    96
            relink = False
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    97
            for CFile, CFLAGS in CFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    98
                bn = os.path.basename(CFile)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    99
                obn = os.path.splitext(bn)[0]+".o"
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   100
                objectfilename = os.path.splitext(CFile)[0]+".o"
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   101
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   102
                match = self.check_and_update_hash_and_deps(bn)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   103
                
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   104
                if match:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   105
                    self.logger.write("   [pass]  "+bn+" -> "+obn+"\n")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   106
                else:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   107
                    relink = True
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   108
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   109
                    self.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   110
                    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   111
                    status, result, err_result = ProcessLogger(
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   112
                           self.logger,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   113
                           "\"%s\" -c \"%s\" -o \"%s\" %s %s"%
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
   114
                               (self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   115
                           ).spin()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   116
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   117
                    if status :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   118
                        self.logger.write_error("C compilation of "+ bn +" failed.\n")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   119
                        return False
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   120
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   121
                obns.append(obn)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   122
                objs.append(objectfilename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   123
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   124
        ######### GENERATE library FILE ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   125
        # Link all the object files into one binary file
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   126
        self.logger.write("Linking :\n")
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   127
        if relink:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   128
            objstring = []
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   129
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   130
            # Generate list .o files
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   131
            listobjstring = '"' + '"  "'.join(objs) + '"'
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   132
    
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 285
diff changeset
   133
            ALLldflags = ' '.join(self.getBuilderLDFLAGS())
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   134
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   135
            self.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.exe + "\n")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   136
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   137
            status, result, err_result = ProcessLogger(
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   138
                   self.logger,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   139
                   "\"%s\" %s -o \"%s\" %s"%
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   140
                       (self.linker,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   141
                        listobjstring,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   142
                        self.exe_path,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   143
                        ALLldflags)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   144
                   ).spin()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   145
            
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   146
            if status :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   147
                return False
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   148
            else :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   149
                # Calculate md5 key and get data for the new created PLC
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   150
                data=self.GetBinaryCode()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   151
                self.md5key = hashlib.md5(data).hexdigest()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   152
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   153
                # Store new PLC filename based on md5 key
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   154
                f = open(self._GetMD5FileName(), "w")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   155
                f.write(self.md5key)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   156
                f.close()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   157
        else:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   158
            self.logger.write("   [pass]  " + ' '.join(obns)+" -> " + self.exe + "\n")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   159
            
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   160
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   161
        return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   162