Subject: [PATCH 3/4] gnu: make-nsis: Use cross-compilation.

From: Jean-Pierre De Jesus DIAZ <[email-masked]>
Date: Fri, 22 Dec 2023 13:37:16 +0100

* gnu/packages/installers.scm (make-nsis): Use `#:target' keyword
  argument in order to cross-compile the package.

Change-Id: I403254a08f43bcd21e475807ea592c2f3908ff6b
---
 gnu/packages/installers.scm | 123 +++++++++++-------------------------
 1 file changed, 37 insertions(+), 86 deletions(-)

diff --git a/gnu/packages/installers.scm b/gnu/packages/installers.scm
index 9879359be7..67e91b908d 100644
--- a/gnu/packages/installers.scm
+++ b/gnu/packages/installers.scm
@@ -1,5 +1,6 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2019 Carl Dong <[email-masked]>
+;;; Copyright © 2023 Foundation Devices, Inc. <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -21,17 +22,14 @@ (define-module (gnu packages installers)
   #:use-module (gnu packages)
   #:use-module (gnu packages build-tools)
   #:use-module (gnu packages compression)
-  #:use-module (gnu packages cross-base)
   #:use-module (guix packages)
   #:use-module (guix download)
+  #:use-module (guix gexp)
   #:use-module (guix build-system scons)
   #:use-module (guix utils))
 
 (define (make-nsis machine target-arch nsis-target-type)
-  (let* ((triplet (string-append machine "-" "w64-mingw32"))
-         (xbinutils (cross-binutils triplet))
-         (xlibc (cross-libc triplet))
-         (xgcc (cross-gcc triplet #:libc xlibc)))
+  (let ((target (string-append machine "-" "w64-mingw32")))
     (package
       (name (string-append "nsis-" machine))
       (version "3.09")
@@ -44,88 +42,41 @@ (define (make-nsis machine target-arch nsis-target-type)
                   "1qjnvhnj89a40ac10k2mbln0yqscdmawmgw71816i465x734dn0c"))
                 (patches (search-patches "nsis-env-passthru.patch"))))
       (build-system scons-build-system)
-      (native-inputs `(("xgcc" ,xgcc)
-                       ("xbinutils" ,xbinutils)
-                       ("mingw-w64" ,xlibc)))
-      (inputs (list zlib))
       (arguments
-       `(#:scons ,scons-python2
-         #:modules ((srfi srfi-1)
-                    (srfi srfi-26)
-                    (guix build utils)
-                    (guix build scons-build-system))
-         #:tests? #f
-         #:scons-flags `("UNICODE=yes"
-                         "SKIPUTILS=MakeLangId,Makensisw,NSIS Menu,SubStart,zip2exe"
-                         "SKIPDOC=COPYING"
-                         "STRIP_CP=no"
-                         ,(string-append "PREFIX=" %output)
-                         ,(string-append "TARGET_ARCH=" ,target-arch)
-                         ,(string-append "XGCC_W32_PREFIX=" ,triplet "-")
-                         ,(string-append "PREFIX_PLUGINAPI_INC=" (assoc-ref %build-inputs "mingw-w64") "/include/")
-                         ,(string-append "PREFIX_PLUGINAPI_LIB=" (assoc-ref %build-inputs "mingw-w64") "/lib/"))
-         #:build-targets '("makensis"
-                           "stubs"
-                           "plugins"
-                           "utils")
-         #:install-targets '("install-stubs"
-                             "install-plugins"
-                             "install-data"
-                             "install-utils"
-                             "install-compiler"
-                             "install-conf")
-         #:phases (modify-phases %standard-phases
-                    (add-before 'build 'fix-env
-                      (lambda _
-                        (define* (filter-delimited-string delimited-string predicate #:optional (delimiter #\:))
-                          ;; Given a DELIMITED-STRING delimited by DELIMITER,
-                          ;; only keep items that satisfy PREDICATE
-                          (string-join
-                           (filter predicate (string-split delimited-string delimiter))
-                           (string delimiter)))
-                        (define (mingw-path? path)
-                          (string-prefix? (assoc-ref %build-inputs "mingw-w64") path))
-                        (for-each
-                         (lambda (env-name)
-                           (let ((env-val (getenv env-name)))
-                             ;; Remove all mingw-w64 paths from env vars meant
-                             ;; for native toolchain
-                             (setenv env-name
-                                     (filter-delimited-string env-val (negate mingw-path?)))
-                             ;; Add the removed paths back into CROSS_-prefixed
-                             ;; version of env vars
-                             (setenv (string-append "CROSS_" env-name)
-                                     (filter-delimited-string env-val mingw-path?))))
-                         '("C_INCLUDE_PATH" "CPLUS_INCLUDE_PATH" "LIBRARY_PATH"))
-                        ;; Hack to place mingw-w64 path at the end of search
-                        ;; paths.  Could probably use a specfile and dirafter
-                        (setenv "CROSS_C_INCLUDE_PATH"
-                                (string-join
-                                 `(,@(map (cut string-append
-                                               (assoc-ref %build-inputs "xgcc")
-                                               "/lib/gcc/" ,triplet "/"
-                                               ,(package-version xgcc) <>)
-                                          '("/include"
-                                            "/include-fixed"))
-                                   ,(getenv "CROSS_C_INCLUDE_PATH"))
-                                 ":"))
-                        (setenv "CROSS_CPLUS_INCLUDE_PATH"
-                                (string-join
-                                 `(,@(map (cut string-append (assoc-ref %build-inputs "xgcc") <>)
-                                          `("/include/c++"
-                                            ,(string-append "/include/c++/" ,triplet)
-                                            "/include/c++/backward"
-                                            ,@(map (cut string-append "/lib/gcc/" ,triplet "/" ,(package-version xgcc) <>)
-                                                   '("/include"
-                                                     "/include-fixed"))))
-                                   ,(getenv "CROSS_CPLUS_INCLUDE_PATH"))
-                                 ":"))))
-                    (add-before 'build 'fix-target-detection
-                      (lambda _
-                        ;; NSIS target detection is screwed up, manually change
-                        ;; it ourselves
-                        (substitute* "Source/build.cpp" (("m_target_type=TARGET_X86UNICODE")
-                                                         (string-append "m_target_type=" ,nsis-target-type))))))))
+       (list #:target target
+             #:scons scons-python2
+             #:tests? #f
+             #:scons-flags
+             #~(let ((xlibc (assoc-ref %build-inputs "cross-libc")))
+                 (list "UNICODE=yes"
+                       "SKIPUTILS=MakeLangId,Makensisw,NSIS Menu,SubStart,zip2exe"
+                       "SKIPDOC=COPYING"
+                       "STRIP_CP=no"
+                       (string-append "PREFIX=" #$output)
+                       (string-append "TARGET_ARCH=" #$target-arch)
+                       (string-append "XGCC_W32_PREFIX=" #$target "-")
+                       (string-append "PREFIX_PLUGINAPI_INC=" xlibc "/include/")
+                       (string-append "PREFIX_PLUGINAPI_LIB=" xlibc "/lib/")))
+             #:build-targets #~'("makensis"
+                                 "stubs"
+                                 "plugins"
+                                 "utils")
+             #:install-targets #~'("install-stubs"
+                                   "install-plugins"
+                                   "install-data"
+                                   "install-utils"
+                                   "install-compiler"
+                                   "install-conf")
+             #:phases
+             #~(modify-phases %standard-phases
+                 (add-before 'build 'fix-target-detection
+                   (lambda _
+                     ;; NSIS target detection is screwed up, manually change
+                     ;; it ourselves
+                     (substitute* "Source/build.cpp"
+                      (("m_target_type=TARGET_X86UNICODE")
+                       (string-append "m_target_type=" #$nsis-target-type))))))))
+      (native-inputs (list zlib))
       (home-page "https://nsis.sourceforge.io/Main_Page")
       (synopsis "System to create Windows installers")
       (description
-- 
2.41.0