Subject: Re: bug#50201: [PATCH core-updates-frozen 0/52] Support cross-compilation in glib-or-gtk-build-system and fix cross-compilation errors

From: Maxime Devos <[email-masked]>
Date: Fri, 03 Sep 2021 14:35:57 +0200

Mathieu Othacehe schreef op ma 30-08-2021 om 14:58 [+0200]:
> Hey,
> 
> > +                         (string-append dir "/_giscanner.cpython-39-"
> > +                                        (normalise-system system) ".so"))
> 
> Won't this be "cpython-310-" or so when we will update the Python
> package definition?

Indeed.  I attached a patch independent of python's version number,
to be squashed in the v2 patch series.

Greetings,
Maxime
From 653b54de7b24db588a6d063beab82ae2218153e5 Mon Sep 17 00:00:00 2001
From: Maxime Devos <[email-masked]>
Date: Fri, 3 Sep 2021 13:25:10 +0200
Subject: [PATCH] SQUASH! gobject-introspection! C extension correction!

* gnu/packages/glib.scm
  (python-extension-suffix, correct-library-name-phase): New procedures.
  (gobject-introspection)[arguments]<#:phases>{rename-library}: New phase.
---
 gnu/packages/glib.scm | 54 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 13 deletions(-)

diff --git a/gnu/packages/glib.scm b/gnu/packages/glib.scm
index 0c2f548f62..6a9c2ac318 100644
--- a/gnu/packages/glib.scm
+++ b/gnu/packages/glib.scm
@@ -381,6 +381,42 @@ functions for strings and common data structures.")
                   (string-append doc html))
                  #t)))))))))
 
+(define (python-extension-suffix python triplet)
+  "Determine the suffix for C extensions for PYTHON when compiled
+for TRIPLET."
+  ;; python uses strings like 'x86_64-linux-gnu' instead of
+  ;; 'x86_64-unknown-linux-gnu'.
+  (define normalised-system
+    (string-replace-substring triplet "-unknown-" "-"))
+  (define major.minor (version-major+minor (package-version python)))
+  (define majorminor (string-delete #\. major.minor))
+  (string-append
+    ;; If guix' python package used "--with-pydebug", a #\d would
+    ;; need to be added, likewise "--with-pymalloc" and "--with-wide-unicode"
+    ;; would require a #\m and #\u, see cpython's configure.ac.
+    ".cpython-" majorminor "-" normalised-system
+    (if (target-mingw? triplet)
+        ".dll"
+        ".so")))
+
+(define (correct-library-name-phase python name)
+  "Return a G-exp evaluating to a phase renaming the python extension NAME
+from what Meson thinks its name should be to what python expects its name
+to be.  NAME must not include the platform-specific suffix.  This can only
+be used when cross-compiling."
+  #~(lambda _
+      (define name #$name)
+      (define native-suffix
+        #$(python-extension-suffix python
+                                   (nix-system->gnu-triplet (%current-system))))
+      (define target-suffix
+        #$(python-extension-suffix python (%current-target-system)))
+      (define native-name
+        (string-append name native-suffix))
+      (define target-name
+        (string-append name target-suffix))
+      (rename-file native-name target-name)))
+
 (define gobject-introspection
   (package
     (name "gobject-introspection")
@@ -431,19 +467,11 @@ functions for strings and common data structures.")
                ;; Meson gives python extensions an incorrect name, see
                ;; <https://github.com/mesonbuild/meson/issues/7049>.
                 #~((add-after 'install 'rename-library
-                     (lambda* (#:key build target #:allow-other-keys)
-                       (define dir
-                         (string-append #$output
-                                        "/lib/gobject-introspection/giscanner"))
-                       ;; python uses strings like 'x86_64-linux-gnu', not
-                       ;; 'x86_64-unknown-linux-gnu'.
-                       (define (normalise-system system)
-                         ((@ (ice-9 string-fun) string-replace-substring)
-                          system "-unknown-" "-"))
-                       (define (extension system)
-                         (string-append dir "/_giscanner.cpython-39-"
-                                        (normalise-system system) ".so"))
-                       (rename-file (extension build) (extension target)))))
+                     #$(correct-library-name-phase
+                         (this-package-input "python")
+                         #~(string-append #$output
+                                          "/lib/gobject-introspection/giscanner"
+                                          "/_giscanner"))))
                 #~()))))
     (native-inputs
      `(("glib" ,glib "bin")
-- 
2.33.0