targets/toolchain_gcc.py
changeset 3893 5b2f3a915a43
parent 3755 ca814b175391
equal deleted inserted replaced
3892:1675b5533e9e 3893:5b2f3a915a43
    27 
    27 
    28 import os
    28 import os
    29 import re
    29 import re
    30 import operator
    30 import operator
    31 import hashlib
    31 import hashlib
       
    32 import subprocess
       
    33 import shlex
    32 from functools import reduce
    34 from functools import reduce
    33 from util.ProcessLogger import ProcessLogger
    35 from util.ProcessLogger import ProcessLogger
    34 
    36 
    35 
    37 
    36 includes_re = re.compile(r'\s*#include\s*["<]([^">]*)[">].*')
    38 includes_re = re.compile(r'\s*#include\s*["<]([^">]*)[">].*')
   162     def build(self):
   164     def build(self):
   163         # Retrieve compiler and linker
   165         # Retrieve compiler and linker
   164         self.compiler = self.getCompiler()
   166         self.compiler = self.getCompiler()
   165         self.linker = self.getLinker()
   167         self.linker = self.getLinker()
   166 
   168 
   167         Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
   169         Builder_CFLAGS_str = ' '.join(self.getBuilderCFLAGS())
       
   170         Builder_LDFLAGS_str = ' '.join(self.getBuilderLDFLAGS())
       
   171 
       
   172         Builder_CFLAGS = shlex.split(Builder_CFLAGS_str)
       
   173         Builder_LDFLAGS = shlex.split(Builder_LDFLAGS_str)
       
   174 
       
   175         pattern = "{SYSROOT}"
       
   176         if pattern in Builder_CFLAGS_str or pattern in Builder_LDFLAGS_str:
       
   177             try:
       
   178                 sysrootb = subprocess.check_output(["arm-unknown-linux-gnueabihf-gcc","-print-sysroot"])
       
   179             except subprocess.CalledProcessError:
       
   180                 self.CTRInstance.logger.write("GCC failed with -print-sysroot\n")
       
   181                 return False
       
   182             except FileNotFoundError:
       
   183                 self.CTRInstance.logger.write("GCC not found\n")
       
   184                 return False
       
   185 
       
   186             sysroot = sysrootb.decode().strip()
       
   187 
       
   188             replace_sysroot = lambda l:list(map(lambda s:s.replace(pattern, sysroot), l))
       
   189             Builder_CFLAGS = replace_sysroot(Builder_CFLAGS)
       
   190             Builder_LDFLAGS = replace_sysroot(Builder_LDFLAGS)
   168 
   191 
   169         # ----------------- GENERATE OBJECT FILES ------------------------
   192         # ----------------- GENERATE OBJECT FILES ------------------------
   170         obns = []
   193         obns = []
   171         objs = []
   194         objs = []
   172         relink = not os.path.exists(self.bin_path)
   195         relink = not os.path.exists(self.bin_path)
   192 
   215 
   193                         self.CTRInstance.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
   216                         self.CTRInstance.logger.write("   [CC]  "+bn+" -> "+obn+"\n")
   194 
   217 
   195                         status, _result, _err_result = ProcessLogger(
   218                         status, _result, _err_result = ProcessLogger(
   196                             self.CTRInstance.logger,
   219                             self.CTRInstance.logger,
   197                             "\"%s\" -c \"%s\" -o \"%s\" -O2 %s %s" %
   220                             [self.compiler,
   198                             (self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
   221                              "-c", CFile,
       
   222                              "-o", objectfilename,
       
   223                              "-O2"]
       
   224                             + Builder_CFLAGS
       
   225                             + shlex.split(CFLAGS)
   199                         ).spin()
   226                         ).spin()
   200 
   227 
   201                         if status:
   228                         if status:
   202                             self.srcmd5.pop(bn)
   229                             self.srcmd5.pop(bn)
   203                             self.CTRInstance.logger.write_error(_("C compilation of %s failed.\n") % bn)
   230                             self.CTRInstance.logger.write_error(_("C compilation of %s failed.\n") % bn)
   210 
   237 
   211         # ---------------- GENERATE OUTPUT FILE --------------------------
   238         # ---------------- GENERATE OUTPUT FILE --------------------------
   212         # Link all the object files into one binary file
   239         # Link all the object files into one binary file
   213         self.CTRInstance.logger.write(_("Linking :\n"))
   240         self.CTRInstance.logger.write(_("Linking :\n"))
   214         if relink:
   241         if relink:
   215             # Generate list .o files
       
   216             listobjstring = '"' + '"  "'.join(objs) + '"'
       
   217 
       
   218             ALLldflags = ' '.join(self.getBuilderLDFLAGS())
       
   219 
   242 
   220             self.CTRInstance.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.bin + "\n")
   243             self.CTRInstance.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.bin + "\n")
   221 
   244 
   222             status, _result, _err_result = ProcessLogger(
   245             status, _result, _err_result = ProcessLogger(
   223                 self.CTRInstance.logger,
   246                 self.CTRInstance.logger,
   224                 "\"%s\" %s -o \"%s\" %s" %
   247                 [self.linker] + objs
   225                 (self.linker,
   248                 + ["-o", self.bin_path]
   226                  listobjstring,
   249                 + Builder_LDFLAGS
   227                  self.bin_path,
       
   228                  ALLldflags)
       
   229             ).spin()
   250             ).spin()
   230 
   251 
   231             if status:
   252             if status:
   232                 return False
   253                 return False
   233 
   254