Subject: [bug#65860] [PATCH 1/4] gnu: avr: Delay all cross compilation packages.

From: Maxim Cournoyer <[email-masked]>
Date: Mon, 11 Sep 2023 00:25:57 -0400

Partially addresses <https://issues.guix.gnu.org/65716>.

* gnu/packages/avr.scm: Add commentary comment.
(avr-gcc): Turn into this...
(make-avr-gcc): ... procedure.
(avr-libc): Likewise, into...
(make-avr-gcc): ... this.  Adjust native-inputs accordingly.
(avr-toolchain): Likewise, into...
(make-avr-toolchain): ... this.
* gnu/packages/avr-xyz.scm (simavr) [propagated-inputs]: replace avr-toolchain
with a call to the 'make-avr-toolchain' procedure.
---

 gnu/packages/avr-xyz.scm |  2 +-
 gnu/packages/avr.scm     | 66 +++++++++++++++++++++++++---------------
 2 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/gnu/packages/avr-xyz.scm b/gnu/packages/avr-xyz.scm
index a05157ede7..e8844b8d43 100644
--- a/gnu/packages/avr-xyz.scm
+++ b/gnu/packages/avr-xyz.scm
@@ -71,7 +71,7 @@ (define-public simavr
                            (string-append "PREFIX=" #$output)
                            (string-append "DESTDIR=" #$output))))
     (propagated-inputs
-     (list avr-toolchain))
+     (list (make-avr-toolchain)))
     (native-inputs
      (list autoconf
            which
diff --git a/gnu/packages/avr.scm b/gnu/packages/avr.scm
index 9c623a9626..abca60eeac 100644
--- a/gnu/packages/avr.scm
+++ b/gnu/packages/avr.scm
@@ -38,15 +38,27 @@ (define-module (gnu packages avr)
   #:use-module (gnu packages flashing-tools)
   #:use-module (gnu packages gcc)
   #:use-module (gnu packages llvm)
-  #:use-module (gnu packages vim))
+  #:use-module (gnu packages vim)
+  #:export (make-avr-toolchain))
 
-(define-public avr-binutils
+;;; Commentary:
+;;;
+;;; This module defines a procedure that can be used to create a complete
+;;; avr-toolchain package.  The procedure must not be used at the top level,
+;;; to avoid cyclic module dependencies caused by the (gnu packages
+;;; cross-base) module referring to top level bindings from (gnu packages
+;;; gcc).
+;;;
+;;; It also contains packages for working with or targeting the AVR system.
+;;;
+
+(define (make-avr-binutils)
   (package
     (inherit (cross-binutils "avr"))
     (name "avr-binutils")))
 
-(define avr-gcc
-  (let ((xgcc (cross-gcc "avr" #:xbinutils avr-binutils)))
+(define (make-avr-gcc)
+  (let ((xgcc (cross-gcc "avr" #:xbinutils (make-avr-binutils))))
     (package
       (inherit xgcc)
       (name "avr-gcc")
@@ -99,7 +111,7 @@ (define avr-gcc
        `(("gcc" ,gcc)
          ,@(package-native-inputs xgcc))))))
 
-(define avr-libc
+(define (make-avr-libc)
   (package
     (name "avr-libc")
     (version "2.0.0")
@@ -114,8 +126,8 @@ (define avr-libc
     (arguments
      '(#:out-of-source? #t
        #:configure-flags '("--host=avr")))
-    (native-inputs `(("avr-binutils" ,avr-binutils)
-                     ("avr-gcc" ,avr-gcc)))
+    (native-inputs `(("avr-binutils" ,(make-avr-binutils))
+                     ("avr-gcc" ,(make-avr-gcc))))
     (home-page "https://www.nongnu.org/avr-libc/")
     (synopsis "The AVR C Library")
     (description
@@ -124,27 +136,31 @@ (define avr-libc
     (license
      (license:non-copyleft "http://www.nongnu.org/avr-libc/LICENSE.txt"))))
 
-(define-public avr-toolchain
-  ;; avr-libc checks the compiler version and passes "--enable-device-lib" for avr-gcc > 5.1.0.
-  ;; It wouldn't install the library for atmega32u4 etc if we didn't use the corret avr-gcc.
-  (package
-    (name "avr-toolchain")
-    (version (package-version avr-gcc))
-    (source #f)
-    (build-system trivial-build-system)
-    (arguments '(#:builder (begin (mkdir %output) #t)))
-    (propagated-inputs
-     `(("avrdude" ,avrdude)
-       ("binutils" ,avr-binutils)
-       ("gcc" ,avr-gcc)
-       ("libc" ,avr-libc)))
-    (synopsis "Complete GCC tool chain for AVR microcontroller development")
-    (description "This package provides a complete GCC tool chain for AVR
+(define (make-avr-toolchain)
+  (let ((avr-binutils (make-avr-binutils))
+        (avr-libc (make-avr-libc))
+        (avr-gcc (make-avr-gcc)))
+    ;; avr-libc checks the compiler version and passes "--enable-device-lib"
+    ;; for avr-gcc > 5.1.0.  It wouldn't install the library for atmega32u4
+    ;; etc if we didn't use the corret avr-gcc.
+    (package
+      (name "avr-toolchain")
+      (version (package-version avr-gcc))
+      (source #f)
+      (build-system trivial-build-system)
+      (arguments '(#:builder (begin (mkdir %output) #t)))
+      (propagated-inputs
+       `(("avrdude" ,avrdude)
+         ("binutils" ,avr-binutils)
+         ("gcc" ,avr-gcc)
+         ("libc" ,avr-libc)))
+      (synopsis "Complete GCC tool chain for AVR microcontroller development")
+      (description "This package provides a complete GCC tool chain for AVR
 microcontroller development.  This includes the GCC AVR cross compiler and
 avrdude for firmware flashing.  The supported programming languages are C and
 C++.")
-    (home-page (package-home-page avr-libc))
-    (license (package-license avr-gcc))))
+      (home-page (package-home-page avr-libc))
+      (license (package-license avr-gcc)))))
 
 (define-public microscheme
   (package
-- 
2.41.0