targets/toolchain_gcc.py
author lbessard
Fri, 09 Jan 2009 17:07:56 +0100
changeset 288 7ee96191c476
parent 285 e5782a52dcea
child 290 3bd617ae7a05
permissions -rwxr-xr-x
Warning on modulo by zero fixed
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
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
    def GetBinaryCode(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    23
        try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
            return open(self.exe_path, "rb").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
        except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    26
            return None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    27
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    28
    def _GetMD5FileName(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    29
        return os.path.join(self.buildpath, "lastbuildPLC.md5")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    30
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    31
    def GetBinaryCodeMD5(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    32
        if self.md5key is not None:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    33
            return self.md5key
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    34
        else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
                return open(self._GetMD5FileName(), "r").read()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
            except Exception, e:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
                return None
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    39
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    40
    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
    41
        # Get latest computed hash and deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    42
        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
    43
        # read source
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    44
        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
    45
        # compute new hash
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    46
        newhash = hashlib.md5(src).hexdigest()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    47
        # compare
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    48
        match = (oldhash == newhash)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    49
        if not match:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    50
            # file have changed
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    51
            # update direct dependencies
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    52
            deps = []
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    53
            for l in src.splitlines():
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    54
                res = includes_re.match(l)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    55
                if res is not None:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    56
                    depfn = res.groups()[0]
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    57
                    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
    58
                        #print bn + " depends on "+depfn
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    59
                        deps.append(depfn)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    60
            # store that hashand deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    61
            self.srcmd5[bn] = (newhash, deps)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    62
        # recurse through deps
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    63
        # TODO detect cicular deps.
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    64
        return reduce(operator.and_, map(self.check_and_update_hash_and_deps, deps), match)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    65
                
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    66
    def build(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    67
        # Retrieve toolchain user parameters
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    68
        toolchain_params = self.PuginsRootInstance.BeremizRoot.getTargetType().getcontent()["value"]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    69
        self.compiler = toolchain_params.getCompiler()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
        self._CFLAGS = toolchain_params.getCFLAGS()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    71
        self.linker = toolchain_params.getLinker()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
        self._LDFLAGS = toolchain_params.getLDFLAGS()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
        ######### GENERATE OBJECT FILES ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
        obns = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
        objs = []
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
        for Location, CFilesAndCFLAGS, DoCalls in self.PuginsRootInstance.LocationCFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    78
            if Location:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
                self.logger.write("Plugin : " + self.PuginsRootInstance.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
            else:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
                self.logger.write("PLC :\n")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
                
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    83
            relink = False
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
            for CFile, CFLAGS in CFilesAndCFLAGS:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
                bn = os.path.basename(CFile)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
                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
    87
                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
    88
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    89
                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
    90
                
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    91
                if match:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    92
                    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
    93
                else:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    94
                    relink = True
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    95
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    96
                    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
    97
                    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    98
                    status, result, err_result = ProcessLogger(
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
    99
                           self.logger,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   100
                           "\"%s\" -c \"%s\" -o \"%s\" %s %s"%
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   101
                               (self.compiler, CFile, objectfilename, self._CFLAGS, CFLAGS)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   102
                           ).spin()
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 status :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   105
                        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
   106
                        return False
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   107
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   108
                obns.append(obn)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   109
                objs.append(objectfilename)
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   110
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   111
        ######### GENERATE library FILE ########################################
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   112
        # Link all the object files into one binary file
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   113
        self.logger.write("Linking :\n")
285
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   114
        if relink:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   115
            objstring = []
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
            # Generate list .o files
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   118
            listobjstring = '"' + '"  "'.join(objs) + '"'
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   119
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   120
            ALLldflags = ' '.join(self.CustomLDFLAGS+self.PuginsRootInstance.LDFLAGS+[self._LDFLAGS])
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   121
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   122
            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
   123
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   124
            status, result, err_result = ProcessLogger(
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   125
                   self.logger,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   126
                   "\"%s\" %s -o \"%s\" %s"%
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   127
                       (self.linker,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   128
                        listobjstring,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   129
                        self.exe_path,
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   130
                        ALLldflags)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   131
                   ).spin()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   132
            
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   133
            if status :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   134
                return False
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   135
            else :
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   136
                # 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
   137
                data=self.GetBinaryCode()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   138
                self.md5key = hashlib.md5(data).hexdigest()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   139
    
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   140
                # 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
   141
                f = open(self._GetMD5FileName(), "w")
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   142
                f.write(self.md5key)
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   143
                f.close()
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   144
        else:
e5782a52dcea Added local C dependency dicovery and changes checking, to speed up build.
etisserant
parents: 203
diff changeset
   145
            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
   146
            
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   147
        
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   148
        return True
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   149