me
/
guix
Archived
1
0
Fork 0

gnu: node: Enable cross-compilation.

Node runs parts of itself on the host for bootstraping therefore for
cross-compiling support we need to fidle with the rpath in the build system,
as well as duplicating some of the dependencies as native-inputs and inputs.

* gnu/pakcages/node.scm (node)[arguments]: Refer to /bin/sh and /bin/env
directly instead of using (which).  Add new 'set-bootstrap-host-rpath phase
to correctly set the rpath for binaries that are meant to run on the host.
Pass --cross-compiling and --dest-cpu to configure script if needed.  Set the
CC_host, CXX_host, CC, CCX and PKG_CONFIG variable for cross-compilation.
Refer to the host python.  Do not return #t from any phases.
[native-inputs]: Add c-ares, http-parser, icu4c, libuv, nghttp2, openssl and
zlib.  Remove which.
[inputs]: Add bash and coreutils.
(llhttp-bootstrap)[arguments]: Refer to esbuild via (or native-inputs inputs).
(node-lts)[arguments]: Add new 'set-bootstrap-host-rpath phase to correctly
set the rpath for host binaries.  Pass --cross-compiling and --dest-cpu to
configure script if needed.  Set the CC_host, CXX_host, CCX and PKG_CONFIG
variable for cross-compilation.  Refer to the host python.  Do not return #t
from any phases.  Refer to /bin/sh and /bin/env directly instead of
using (which).  Do not return #t from any phases.
[native-inputs]: Hardcode native inputs instead of inheriting them from node.
[inputs]: Add bash and coreutils.
master
Pierre Langlois 2021-08-11 16:43:45 +01:00
parent 9f7c4f380f
commit 5ee38c4672
No known key found for this signature in database
GPG Key ID: A8FC9E447F4F7D54
1 changed files with 157 additions and 34 deletions

View File

@ -6,7 +6,7 @@
;;; Copyright © 2017 Mike Gerwitz <mtg@gnu.org> ;;; Copyright © 2017 Mike Gerwitz <mtg@gnu.org>
;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr> ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
;;; Copyright © 2018, 2019, 2020, 2021 Marius Bakke <marius@gnu.org> ;;; Copyright © 2018, 2019, 2020, 2021 Marius Bakke <marius@gnu.org>
;;; Copyright © 2020 Pierre Langlois <pierre.langlois@gmx.com> ;;; Copyright © 2020, 2021 Pierre Langlois <pierre.langlois@gmx.com>
;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net> ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com> ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
;;; ;;;
@ -38,6 +38,7 @@
#:use-module (gnu packages) #:use-module (gnu packages)
#:use-module (gnu packages adns) #:use-module (gnu packages adns)
#:use-module (gnu packages base) #:use-module (gnu packages base)
#:use-module (gnu packages bash)
#:use-module (gnu packages compression) #:use-module (gnu packages compression)
#:use-module (gnu packages gcc) #:use-module (gnu packages gcc)
#:use-module (gnu packages icu4c) #:use-module (gnu packages icu4c)
@ -48,7 +49,9 @@
#:use-module (gnu packages pkg-config) #:use-module (gnu packages pkg-config)
#:use-module (gnu packages python) #:use-module (gnu packages python)
#:use-module (gnu packages tls) #:use-module (gnu packages tls)
#:use-module (gnu packages web)) #:use-module (gnu packages web)
#:use-module (ice-9 match)
#:use-module (srfi srfi-26))
(define-public node (define-public node
(package (package
@ -111,14 +114,15 @@
"test/parallel/test-stdio-closed.js" "test/parallel/test-stdio-closed.js"
"test/sequential/test-child-process-emfile.js") "test/sequential/test-child-process-emfile.js")
(("'/bin/sh'") (("'/bin/sh'")
(string-append "'" (which "sh") "'"))) (string-append "'" (assoc-ref inputs "bash") "/bin/sh'")))
;; Fix hardcoded /usr/bin/env references. ;; Fix hardcoded /usr/bin/env references.
(substitute* '("test/parallel/test-child-process-default-options.js" (substitute* '("test/parallel/test-child-process-default-options.js"
"test/parallel/test-child-process-env.js" "test/parallel/test-child-process-env.js"
"test/parallel/test-child-process-exec-env.js") "test/parallel/test-child-process-exec-env.js")
(("'/usr/bin/env'") (("'/usr/bin/env'")
(string-append "'" (which "env") "'"))) (string-append "'" (assoc-ref inputs "coreutils")
"/bin/env'")))
;; FIXME: These tests fail in the build container, but they don't ;; FIXME: These tests fail in the build container, but they don't
;; seem to be indicative of real problems in practice. ;; seem to be indicative of real problems in practice.
@ -155,23 +159,66 @@
;; TODO: Regenerate certs instead. ;; TODO: Regenerate certs instead.
(for-each delete-file (for-each delete-file
'("test/parallel/test-tls-passphrase.js" '("test/parallel/test-tls-passphrase.js"
"test/parallel/test-tls-server-verify.js")) "test/parallel/test-tls-server-verify.js"))))
#t)) (add-before 'configure 'set-bootstrap-host-rpath
(lambda* (#:key native-inputs inputs #:allow-other-keys)
(let* ((inputs (or native-inputs inputs))
(c-ares (assoc-ref inputs "c-ares"))
(http-parser (assoc-ref inputs "http-parser"))
(icu4c (assoc-ref inputs "icu4c"))
(nghttp2 (assoc-ref inputs "nghttp2"))
(openssl (assoc-ref inputs "openssl"))
(libuv (assoc-ref inputs "libuv"))
(zlib (assoc-ref inputs "zlib")))
(substitute* "deps/v8/gypfiles/v8.gyp"
(("'target_name': 'torque'," target)
(string-append target
"'ldflags': ['-Wl,-rpath="
c-ares "/lib:"
http-parser "/lib:"
icu4c "/lib:"
nghttp2 "/lib:"
openssl "/lib:"
libuv "/lib:"
zlib "/lib"
"'],"))))))
(replace 'configure (replace 'configure
;; Node's configure script is actually a python script, so we can't ;; Node's configure script is actually a python script, so we can't
;; run it with bash. ;; run it with bash.
(lambda* (#:key outputs (configure-flags '()) inputs (lambda* (#:key outputs (configure-flags '()) native-inputs inputs
#:allow-other-keys) #:allow-other-keys)
(let* ((prefix (assoc-ref outputs "out")) (let* ((prefix (assoc-ref outputs "out"))
(xflags ,(if (%current-target-system)
`'("--cross-compiling"
,(string-append
"--dest-cpu="
(match (%current-target-system)
((? (cut string-prefix? "arm" <>))
"arm")
((? (cut string-prefix? "aarch64" <>))
"arm64")
((? (cut string-prefix? "i686" <>))
"ia32")
((? (cut string-prefix? "x86_64" <>))
"x64")
((? (cut string-prefix? "powerpc64" <>))
"ppc64")
(_ "unsupported"))))
''()))
(flags (cons (string-append "--prefix=" prefix) (flags (cons (string-append "--prefix=" prefix)
configure-flags))) (append xflags configure-flags))))
(format #t "build directory: ~s~%" (getcwd)) (format #t "build directory: ~s~%" (getcwd))
(format #t "configure flags: ~s~%" flags) (format #t "configure flags: ~s~%" flags)
;; Node's configure script expects the CC environment variable to ;; Node's configure script expects the CC environment variable to
;; be set. ;; be set.
(setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc")) (setenv "CC_host" "gcc")
(setenv "CXX_host" "g++")
(setenv "CC" ,(cc-for-target))
(setenv "CXX" ,(cxx-for-target))
(setenv "PKG_CONFIG" ,(pkg-config-for-target))
(apply invoke (apply invoke
(string-append (assoc-ref inputs "python") (string-append (assoc-ref (or native-inputs inputs)
"python")
"/bin/python") "/bin/python")
"configure" flags)))) "configure" flags))))
(add-after 'patch-shebangs 'patch-npm-shebang (add-after 'patch-shebangs 'patch-npm-shebang
@ -181,29 +228,37 @@
(npm (string-append bindir "/npm")) (npm (string-append bindir "/npm"))
(target (readlink npm))) (target (readlink npm)))
(with-directory-excursion bindir (with-directory-excursion bindir
(patch-shebang target (list bindir)) (patch-shebang target (list bindir))))))
#t))))
(add-after 'install 'patch-node-shebang (add-after 'install 'patch-node-shebang
(lambda* (#:key outputs #:allow-other-keys) (lambda* (#:key outputs #:allow-other-keys)
(let* ((bindir (string-append (assoc-ref outputs "out") (let* ((bindir (string-append (assoc-ref outputs "out")
"/bin")) "/bin"))
(npx (readlink (string-append bindir "/npx")))) (npx (readlink (string-append bindir "/npx"))))
(with-directory-excursion bindir (with-directory-excursion bindir
(patch-shebang npx (list bindir)) (patch-shebang npx (list bindir)))))))))
#t)))))))
(native-inputs (native-inputs
`(("python" ,python-2) `(;; Runtime dependencies for binaries used as a bootstrap.
("c-ares" ,c-ares)
("http-parser" ,http-parser)
("icu4c" ,icu4c)
("libuv" ,libuv)
("nghttp2" ,nghttp2 "lib")
("openssl" ,openssl)
("zlib" ,zlib)
;; Regular build-time dependencies.
("perl" ,perl) ("perl" ,perl)
("pkg-config" ,pkg-config) ("pkg-config" ,pkg-config)
("procps" ,procps) ("procps" ,procps)
("util-linux" ,util-linux) ("python" ,python-2)
("which" ,which))) ("util-linux" ,util-linux)))
(native-search-paths (native-search-paths
(list (search-path-specification (list (search-path-specification
(variable "NODE_PATH") (variable "NODE_PATH")
(files '("lib/node_modules"))))) (files '("lib/node_modules")))))
(inputs (inputs
`(("c-ares" ,c-ares) `(("bash" ,bash)
("coreutils" ,coreutils)
("c-ares" ,c-ares)
("http-parser" ,http-parser) ("http-parser" ,http-parser)
("icu4c" ,icu4c) ("icu4c" ,icu4c)
("libuv" ,libuv) ("libuv" ,libuv)
@ -551,9 +606,10 @@ parser definition into a C output.")
#:phases #:phases
(modify-phases %standard-phases (modify-phases %standard-phases
(replace 'configure (replace 'configure
(lambda* (#:key inputs #:allow-other-keys) (lambda* (#:key native-inputs inputs #:allow-other-keys)
(let ((esbuild (string-append (assoc-ref inputs "esbuild") (let ((esbuild (string-append
"/bin/esbuild"))) (assoc-ref (or native-inputs inputs) "esbuild")
"/bin/esbuild")))
(invoke esbuild (invoke esbuild
"--platform=node" "--platform=node"
"--outfile=bin/generate.js" "--outfile=bin/generate.js"
@ -625,21 +681,74 @@ source files.")
"--with-intl=system-icu")) "--with-intl=system-icu"))
((#:phases phases) ((#:phases phases)
`(modify-phases ,phases `(modify-phases ,phases
(replace 'set-bootstrap-host-rpath
(lambda* (#:key native-inputs inputs #:allow-other-keys)
(let* ((inputs (or native-inputs inputs))
(c-ares (assoc-ref inputs "c-ares"))
(google-brotli (assoc-ref inputs "google-brotli"))
(icu4c (assoc-ref inputs "icu4c"))
(nghttp2 (assoc-ref inputs "nghttp2"))
(openssl (assoc-ref inputs "openssl"))
(libuv (assoc-ref inputs "libuv"))
(zlib (assoc-ref inputs "zlib"))
(host-binaries '("torque"
"bytecode_builtins_list_generator"
"gen-regexp-special-case"
"node_mksnapshot"
"mksnapshot")))
(substitute* '("node.gyp" "tools/v8_gypfiles/v8.gyp")
(((string-append "'target_name': '("
(string-join host-binaries "|")
")',")
target)
(string-append target
"'ldflags': ['-Wl,-rpath="
c-ares "/lib:"
google-brotli "/lib:"
icu4c "/lib:"
nghttp2 "/lib:"
openssl "/lib:"
libuv "/lib:"
zlib "/lib"
"'],"))))))
(replace 'configure (replace 'configure
;; Node's configure script is actually a python script, so we can't ;; Node's configure script is actually a python script, so we can't
;; run it with bash. ;; run it with bash.
(lambda* (#:key outputs (configure-flags '()) inputs (lambda* (#:key outputs (configure-flags '()) native-inputs inputs
#:allow-other-keys) #:allow-other-keys)
(let* ((prefix (assoc-ref outputs "out")) (let* ((prefix (assoc-ref outputs "out"))
(flags (cons (string-append "--prefix=" prefix) (xflags ,(if (%current-target-system)
configure-flags))) `'("--cross-compiling"
,(string-append
"--dest-cpu="
(match (%current-target-system)
((? (cut string-prefix? "arm" <>))
"arm")
((? (cut string-prefix? "aarch64" <>))
"arm64")
((? (cut string-prefix? "i686" <>))
"ia32")
((? (cut string-prefix? "x86_64" <>))
"x64")
((? (cut string-prefix? "powerpc64" <>))
"ppc64")
(_ "unsupported"))))
''()))
(flags (cons
(string-append "--prefix=" prefix)
(append xflags configure-flags))))
(format #t "build directory: ~s~%" (getcwd)) (format #t "build directory: ~s~%" (getcwd))
(format #t "configure flags: ~s~%" flags) (format #t "configure flags: ~s~%" flags)
;; Node's configure script expects the CC environment variable to ;; Node's configure script expects the CC environment variable to
;; be set. ;; be set.
(setenv "CC_host" "gcc")
(setenv "CXX_host" "g++")
(setenv "CC" ,(cc-for-target)) (setenv "CC" ,(cc-for-target))
(setenv "CXX" ,(cxx-for-target))
(setenv "PKG_CONFIG" ,(pkg-config-for-target))
(apply invoke (apply invoke
(string-append (assoc-ref inputs "python") (string-append (assoc-ref (or native-inputs inputs)
"python")
"/bin/python3") "/bin/python3")
"configure" flags)))) "configure" flags))))
(replace 'patch-files (replace 'patch-files
@ -652,14 +761,15 @@ source files.")
"test/parallel/test-stdio-closed.js" "test/parallel/test-stdio-closed.js"
"test/sequential/test-child-process-emfile.js") "test/sequential/test-child-process-emfile.js")
(("'/bin/sh'") (("'/bin/sh'")
(string-append "'" (which "sh") "'"))) (string-append "'" (assoc-ref inputs "bash") "/bin/sh'")))
;; Fix hardcoded /usr/bin/env references. ;; Fix hardcoded /usr/bin/env references.
(substitute* '("test/parallel/test-child-process-default-options.js" (substitute* '("test/parallel/test-child-process-default-options.js"
"test/parallel/test-child-process-env.js" "test/parallel/test-child-process-env.js"
"test/parallel/test-child-process-exec-env.js") "test/parallel/test-child-process-exec-env.js")
(("'/usr/bin/env'") (("'/usr/bin/env'")
(string-append "'" (which "env") "'"))) (string-append "'" (assoc-ref inputs "coreutils")
"/bin/env'")))
;; FIXME: These tests fail in the build container, but they don't ;; FIXME: These tests fail in the build container, but they don't
;; seem to be indicative of real problems in practice. ;; seem to be indicative of real problems in practice.
@ -707,20 +817,33 @@ source files.")
(copy-file (string-append llhttp "/src/http.c") (copy-file (string-append llhttp "/src/http.c")
"deps/llhttp/src/http.c") "deps/llhttp/src/http.c")
(copy-file (string-append llhttp "/include/llhttp.h") (copy-file (string-append llhttp "/include/llhttp.h")
"deps/llhttp/include/llhttp.h")) "deps/llhttp/include/llhttp.h"))))))))
#t)))))) (native-inputs
`(;; Runtime dependencies for binaries used as a bootstrap.
("c-ares" ,c-ares)
("google-brotli" ,google-brotli)
("icu4c" ,icu4c-67)
("libuv" ,libuv-for-node)
("nghttp2" ,nghttp2 "lib")
("openssl" ,openssl)
("zlib" ,zlib)
;; Regular build-time dependencies.
("perl" ,perl)
("pkg-config" ,pkg-config)
("procps" ,procps)
("python" ,python)
("util-linux" ,util-linux)))
(inputs (inputs
`(("c-ares" ,c-ares) `(("bash" ,bash)
("coreutils" ,coreutils)
("c-ares" ,c-ares)
("icu4c" ,icu4c-67) ("icu4c" ,icu4c-67)
("libuv" ,libuv-for-node) ("libuv" ,libuv-for-node)
("llhttp" ,llhttp-bootstrap) ("llhttp" ,llhttp-bootstrap)
("google-brotli" ,google-brotli) ("google-brotli" ,google-brotli)
("nghttp2" ,nghttp2 "lib") ("nghttp2" ,nghttp2 "lib")
("openssl" ,openssl) ("openssl" ,openssl)
("zlib" ,zlib))) ("zlib" ,zlib)))))
(native-inputs
(alist-replace "python" (list python-3)
(package-native-inputs node)))))
(define-public libnode (define-public libnode
(package/inherit node (package/inherit node