targets/toolchain_gcc.py
changeset 3897 fc3621302cfe
parent 3893 5b2f3a915a43
child 3982 69d161fc2e08
--- a/targets/toolchain_gcc.py	Thu Feb 15 17:03:06 2024 +0100
+++ b/targets/toolchain_gcc.py	Tue Feb 20 14:53:33 2024 +0100
@@ -29,6 +29,8 @@
 import re
 import operator
 import hashlib
+import subprocess
+import shlex
 from functools import reduce
 from util.ProcessLogger import ProcessLogger
 
@@ -164,7 +166,28 @@
         self.compiler = self.getCompiler()
         self.linker = self.getLinker()
 
-        Builder_CFLAGS = ' '.join(self.getBuilderCFLAGS())
+        Builder_CFLAGS_str = ' '.join(self.getBuilderCFLAGS())
+        Builder_LDFLAGS_str = ' '.join(self.getBuilderLDFLAGS())
+
+        Builder_CFLAGS = shlex.split(Builder_CFLAGS_str)
+        Builder_LDFLAGS = shlex.split(Builder_LDFLAGS_str)
+
+        pattern = "{SYSROOT}"
+        if pattern in Builder_CFLAGS_str or pattern in Builder_LDFLAGS_str:
+            try:
+                sysrootb = subprocess.check_output(["arm-unknown-linux-gnueabihf-gcc","-print-sysroot"])
+            except subprocess.CalledProcessError:
+                self.CTRInstance.logger.write("GCC failed with -print-sysroot\n")
+                return False
+            except FileNotFoundError:
+                self.CTRInstance.logger.write("GCC not found\n")
+                return False
+
+            sysroot = sysrootb.decode().strip()
+
+            replace_sysroot = lambda l:list(map(lambda s:s.replace(pattern, sysroot), l))
+            Builder_CFLAGS = replace_sysroot(Builder_CFLAGS)
+            Builder_LDFLAGS = replace_sysroot(Builder_LDFLAGS)
 
         # ----------------- GENERATE OBJECT FILES ------------------------
         obns = []
@@ -194,8 +217,12 @@
 
                         status, _result, _err_result = ProcessLogger(
                             self.CTRInstance.logger,
-                            "\"%s\" -c \"%s\" -o \"%s\" -O2 %s %s" %
-                            (self.compiler, CFile, objectfilename, Builder_CFLAGS, CFLAGS)
+                            [self.compiler,
+                             "-c", CFile,
+                             "-o", objectfilename,
+                             "-O2"]
+                            + Builder_CFLAGS
+                            + shlex.split(CFLAGS)
                         ).spin()
 
                         if status:
@@ -212,20 +239,14 @@
         # Link all the object files into one binary file
         self.CTRInstance.logger.write(_("Linking :\n"))
         if relink:
-            # Generate list .o files
-            listobjstring = '"' + '"  "'.join(objs) + '"'
-
-            ALLldflags = ' '.join(self.getBuilderLDFLAGS())
 
             self.CTRInstance.logger.write("   [CC]  " + ' '.join(obns)+" -> " + self.bin + "\n")
 
             status, _result, _err_result = ProcessLogger(
                 self.CTRInstance.logger,
-                "\"%s\" %s -o \"%s\" %s" %
-                (self.linker,
-                 listobjstring,
-                 self.bin_path,
-                 ALLldflags)
+                [self.linker] + objs
+                + ["-o", self.bin_path]
+                + Builder_LDFLAGS
             ).spin()
 
             if status: