From b96825876eea5c0dff9414d88a8dc3302b2d2f7f Mon Sep 17 00:00:00 2001
From: "Jan (janneke) Nieuwenhuizen" <janneke@gnu.org>
Date: Sat, 18 Apr 2020 17:05:48 +0200
Subject: [PATCH] gnu: perl: Actually produce a host perl when cross-compiling.

* gnu/packages/perl.scm (perl)[native-inputs]: When cross-compiling, add
`perl-cross' input.
[arguments]: When cross-compiling, use it to produce binary for host.
---
 gnu/packages/perl.scm | 85 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 80 insertions(+), 5 deletions(-)

diff --git a/gnu/packages/perl.scm b/gnu/packages/perl.scm
index d6a75506ec..67f4cb969c 100644
--- a/gnu/packages/perl.scm
+++ b/gnu/packages/perl.scm
@@ -11,7 +11,7 @@
 ;;; Copyright © 2016 Alex Sassmannshausen <alex@pompo.co>
 ;;; Copyright © 2016, 2018, 2020 Roel Janssen <roel@gnu.org>
 ;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
-;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+;;; Copyright © 2016, 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
 ;;; Copyright © 2017 Raoul J.P. Bonnal <ilpuccio.febo@gmail.com>
 ;;; Copyright © 2017, 2018 Marius Bakke <mbakke@fastmail.com>
 ;;; Copyright © 2017 Adriano Peluso <catonano@gmail.com>
@@ -48,6 +48,7 @@
   #:use-module (gnu packages)
   #:use-module (guix packages)
   #:use-module (guix download)
+  #:use-module (guix git-download)
   #:use-module (guix utils)
   #:use-module (guix build-system gnu)
   #:use-module (guix build-system perl)
@@ -120,10 +121,72 @@
                (("-std=c89")
                 "-std=gnu89"))
              #t))
-         (replace 'configure
-           (lambda* (#:key configure-flags #:allow-other-keys)
-             (format #t "Perl configure flags: ~s~%" configure-flags)
-             (apply invoke "./Configure" configure-flags)))
+         ,@(if (%current-target-system)
+               `((add-after 'unpack 'unpack-cross
+                   (lambda* (#:key inputs #:allow-other-keys)
+                     (let ((cross-checkout (assoc-ref %build-inputs "perl-cross")))
+                       (invoke "chmod" "-R" "+w" ".")
+                       (copy-recursively cross-checkout "."))
+                     (let ((bash (assoc-ref %build-inputs "bash")))
+                       (substitute* '("Makefile.config.SH"
+                                      "cnf/config.guess"
+                                      "cnf/config.sub"
+                                      "cnf/configure"
+                                      "cnf/configure_misc.sh"
+                                      "miniperl_top")
+                         (("! */bin/sh") (string-append "! " bash "/bin/bash"))
+                         ((" /bin/sh") (string-append bash "/bin/bash")))
+                       (substitute* '("cnf/configure_tool.sh")
+                         (( "[\t ]*result \"BSD\".*")
+                          "\
+			result \"BSD\"
+			;;
+			*-gnu)
+			result \"GNU\"
+"))
+                       (substitute* '("ext/Errno/Errno_pm.PL")
+                         (( "\\$cpp < errno.c") "gcc -E errno.c")))
+                     #t))
+                 (replace 'configure
+                   (lambda* (#:key configure-flags outputs #:allow-other-keys)
+                     (let* ((out (assoc-ref outputs "out"))
+                            (configure-flags
+                             (cons*
+                              ;; `perl-cross' confuses target and host
+                              (string-append "--target=" ,(%current-target-system))
+                              (string-append "--prefix=" out)
+                              "-Dbyteorder=1234"
+                              (filter (negate
+                                       (lambda (x) (or (string-prefix? "-d" x)
+                                                       (string-prefix? "-Dcc=" x))))
+                                      configure-flags))))
+                       (format (current-error-port)
+                               "running ./configure ~a\n" (string-join configure-flags))
+                       (apply invoke (cons "./configure" configure-flags))
+                       (substitute* "config.sh"
+                         (("^libs=.*") "libs='-ldl -lpthread -lm'\n")
+                         (("^perllibs=.*") "perllibs='-ldl -lpthread -lm'\n"))
+                       (substitute* "config.h"
+                         (("^# HAS_NANOSLEEP") "/* #undef HAS_NANOSLEEP */"))
+                       #t)))
+                 (add-after 'build 'touch-non-built-files-for-install
+                   (lambda _
+                     ;; `make install' wants to install this; it wasn't built...
+                     (mkdir-p "cpan/Pod-Usage/blib/script")
+                     (with-output-to-file "cpan/Pod-Usage/blib/script/pod2text"
+                       (lambda _ (display "")))
+                     (with-output-to-file "cpan/Pod-Usage/blib/script/pod2usage"
+                       (lambda _ (display "")))
+                     (with-output-to-file "cpan/Pod-Checker/blib/script/podchecker"
+                       (lambda _ (display "")))
+                     (mkdir-p "cpan/Pod-Parser/blib/script")
+                     (with-output-to-file "cpan/Pod-Parser/blib/script/podselect"
+                       (lambda _ (display "")))
+                     #t)))
+               `((replace 'configure
+                   (lambda* (#:key configure-flags #:allow-other-keys)
+                     (format #t "Perl configure flags: ~s~%" configure-flags)
+                     (apply invoke "./Configure" configure-flags)))))
          (add-after 'install 'remove-extra-references
            (lambda* (#:key inputs outputs #:allow-other-keys)
              (let* ((out     (assoc-ref outputs "out"))
@@ -152,6 +215,18 @@
                                              "/lib',\n"))))
                          config2)
                #t))))))
+    (native-inputs
+     (if (%current-target-system)
+         `(("perl-cross"
+            ,(origin
+               (method git-fetch)
+               (uri (git-reference
+                     (url "https://github.com/arsv/perl-cross")
+                     (commit "1.3.3")))
+               (file-name (git-file-name "perl-cross" "1.3.3"))
+               (sha256
+                (base32 "065qbl1x44maykaj8p8za0b6qxj74bz7fi2zsrlydir1mqb1js3d")))))
+         '()))
     (native-search-paths (list (search-path-specification
                                 (variable "PERL5LIB")
                                 (files '("lib/perl5/site_perl")))))
-- 
2.26.0

