GCC toolchain: Add {SYSROOT} substitution with support for whitespaces
authorEdouard Tisserant <edouard@beremiz.fr>
Tue, 20 Feb 2024 11:42:02 +0100
changeset 3893 5b2f3a915a43
parent 3892 1675b5533e9e
child 3897 fc3621302cfe
GCC toolchain: Add {SYSROOT} substitution with support for whitespaces

Any instance of {SYROOT} string in config's CFLAGS and LDFLAGS is replaced by sysroot path obtained from "gcc -print-sysroot"
targets/toolchain_gcc.py
--- a/targets/toolchain_gcc.py	Tue Feb 20 11:37:54 2024 +0100
+++ b/targets/toolchain_gcc.py	Tue Feb 20 11:42:02 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: