Subject: [PATCH 3/5] Add libstdc++-nano for arm-none-eabi

From: Rutherther <[email-masked]>
Date: Sun, 08 Sep 2024 07:49:06 +0000

The arm-none-eabi nano toolchain had same libstdc++ as the non-nano variant.
The libstdc++ should be compiled with -fno-exceptions to make a nano
toolchain.

Additionally, since the "_nano.a" variants were not present, it was impossible
to compile C++ programs with libstdc++ using --specs=nano.specs,
since libstdc++_nano.a was not found.
---
 gnu/packages/embedded.scm | 76 ++++++++++++++++++++++++++++++++-------
 1 file changed, 64 insertions(+), 12 deletions(-)

diff --git a/gnu/packages/embedded.scm b/gnu/packages/embedded.scm
index 59ffd9a661..1ea794724c 100644
--- a/gnu/packages/embedded.scm
+++ b/gnu/packages/embedded.scm
@@ -486,7 +486,14 @@ (define make-libstdc++-arm-none-eabi
         (arguments
          (substitute-keyword-arguments (package-arguments libstdc++)
            ((#:configure-flags _)
-            ``("--target=arm-none-eabi"
+            ``("CFLAGS=-ffunction-sections -fdata-sections"
+               "CXXFLAGS=-ffunction-sections -fdata-sections"
+               ; This is more of a hack. This option doesn't really seem
+               ; to change what subdir is used eventually, but without it there is
+               ; error: Link tests are not allowed after GCC_NO_EXECUTABLES with
+               ; The 12.3 toolchain
+               "--with-target-subdir=\".\""
+               "--target=arm-none-eabi"
                "--host=arm-none-eabi"
                "--disable-libstdcxx-pch"
                "--enable-multilib"
@@ -506,21 +513,65 @@ (define make-libstdc++-arm-none-eabi
            ("xgcc" ,xgcc)
            ,@(package-native-inputs libstdc++)))))))
 
+(define make-libstdc++-nano-arm-none-eabi
+  (mlambda (xgcc newlib-nano)
+    (let ((base (make-libstdc++-arm-none-eabi xgcc newlib-nano)))
+      (package
+        (inherit base)
+        (name "libstdc++-nano-arm-none-eabi")
+        (arguments (substitute-keyword-arguments (package-arguments base)
+                     ((#:configure-flags flags)
+                      #~(cons* "CFLAGS=-ffunction-sections -fdata-sections -fno-exceptions"
+                               "CXXFLAGS=-ffunction-sections -fdata-sections -fno-exceptions"
+                               (filter
+                                  (lambda (flag)
+                                    (not (or (string-prefix? "CFLAGS=" flag)
+                                             (string-prefix? "CXXFLAGS=" flag))))
+                                  #$flags)))
+                     ((#:phases phases)
+                      #~(modify-phases #$phases
+                          (add-after 'install 'hardlink-libstdc++
+                            ;; XXX: Most arm toolchains offer both *.a and *_nano.a as
+                            ;; newlib and newlib-nano respectively.  The headers are
+                            ;; usually arm-none-eabi/include/newlib.h for newlib and
+                            ;; arm-none-eabi/include/newlib-nano/newlib.h for newlib-nano.
+                            ;; We have two different toolchain packages for each which
+                            ;; works but is a little strange.
+                            (lambda* (#:key outputs #:allow-other-keys)
+                              (let ((out (assoc-ref outputs "out")))
+                                ;; The nano.specs file says that newlib-nano files should
+                                ;; end in "_nano.a" instead of just ".a".  Note that this
+                                ;; applies to all the multilib folders too.
+                                (for-each
+                                 (lambda (file)
+                                   (link file
+                                         (string-append
+                                          ;; Strip ".a" off the end
+                                          (substring file 0 (- (string-length file) 2))
+                                          ;; Add "_nano.a" onto the end
+                                          "_nano.a")))
+                                 (find-files
+                                  out "^(libstdc\\+\\+.a|libsupc\\+\\+.a)$")))))))))))))
+
 (define make-arm-none-eabi-toolchain
   (mlambda (xgcc newlib)
     "Produce a cross-compiler toolchain package with the compiler XGCC and the
 C library variant NEWLIB."
-    (let ((newlib-with-xgcc
-           (package
-             (inherit newlib)
-             (native-inputs
-              (alist-replace "xgcc" (list xgcc)
-                             (package-native-inputs newlib))))))
+    (let* ((nano? (string=? (package-name newlib)
+                            "newlib-nano"))
+           (newlib-with-xgcc
+            (package
+              (inherit newlib)
+              (native-inputs
+               (alist-replace "xgcc" (list xgcc)
+                              (package-native-inputs newlib)))))
+           (libstdc++
+            (if nano?
+                (make-libstdc++-nano-arm-none-eabi xgcc newlib-with-xgcc)
+                (make-libstdc++-arm-none-eabi xgcc newlib-with-xgcc))))
       (package
         (name (string-append "arm-none-eabi"
-                             (if (string=? (package-name newlib-with-xgcc)
-                                           "newlib-nano")
-                                 "-nano" "")
+                             (if nano? "-nano" "")
                              "-toolchain"))
         (version (package-version xgcc))
         (source #f)
@@ -537,9 +588,10 @@ (define make-arm-none-eabi-toolchain
                              directories))))))
         (propagated-inputs
          `(("binutils" ,(cross-binutils "arm-none-eabi"))
-           ("libstdc++" ,(make-libstdc++-arm-none-eabi xgcc newlib-with-xgcc))
+           ("libstdc++" ,libstdc++)
            ("gcc" ,xgcc)
-           ("newlib" ,newlib-with-xgcc)))
+           ("newlib" ,newlib-with-xgcc)
+           ))
         (synopsis "Complete GCC tool chain for ARM bare metal development")
         (description "This package provides a complete GCC tool chain for ARM
 bare metal development.  This includes the GCC arm-none-eabi cross compiler
-- 
2.45.2