From d622a15d0889c18bdfd32b569479001f53567177 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Wed, 18 Dec 2019 16:06:14 +0100 Subject: Move the "binary" build system from the Gaming Channels to Nonguix. * nonguix/build-system/binary.scm: New file. * nonguix/build/binary-build-system.scm: New file. * nonguix/build/utils.scm: New file. * nonguix/utils.scm: New file. --- nonguix/build-system/binary.scm | 162 +++++++++++++++++++++++++++++ nonguix/build/binary-build-system.scm | 134 ++++++++++++++++++++++++ nonguix/build/utils.scm | 185 ++++++++++++++++++++++++++++++++++ nonguix/utils.scm | 40 ++++++++ 4 files changed, 521 insertions(+) create mode 100644 nonguix/build-system/binary.scm create mode 100644 nonguix/build/binary-build-system.scm create mode 100644 nonguix/build/utils.scm create mode 100644 nonguix/utils.scm (limited to 'nonguix') diff --git a/nonguix/build-system/binary.scm b/nonguix/build-system/binary.scm new file mode 100644 index 0000000..795b30c --- /dev/null +++ b/nonguix/build-system/binary.scm @@ -0,0 +1,162 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2019 Julien Lepiller +;;; +;;; This file is not part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (nonguix build-system binary) + #:use-module (guix store) + #:use-module (guix utils) + #:use-module (guix derivations) + #:use-module (guix search-paths) + #:use-module (guix build-system) + #:use-module (guix build-system gnu) + #:use-module (guix packages) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (nonguix utils) + #:export (%binary-build-system-modules + default-patchelf + default-glibc + lower + binary-build + binary-build-system)) + +;; Commentary: +;; +;; Standard build procedure for binary packages. This is implemented as an +;; extension of `gnu-build-system'. +;; +;; Code: + +(define %binary-build-system-modules + ;; Build-side modules imported by default. + `((nonguix build binary-build-system) + (nonguix build utils) + ,@%gnu-build-system-modules)) + +(define (default-patchelf) + "Return the default patchelf package." + + ;; Do not use `@' to avoid introducing circular dependencies. + (let ((module (resolve-interface '(gnu packages elf)))) + (module-ref module 'patchelf))) + +(define (default-glibc) + "Return the default glibc package." + ;; Do not use `@' to avoid introducing circular dependencies. + (let ((module (resolve-interface '(gnu packages base)))) + (module-ref module 'glibc))) + +(define* (lower name + #:key source inputs native-inputs outputs system target + (patchelf (default-patchelf)) + (glibc (default-glibc)) + #:allow-other-keys + #:rest arguments) + "Return a bag for NAME." + (define private-keywords + '(#:source #:target #:patchelf #:inputs #:native-inputs)) + + (and (not target) ;XXX: no cross-compilation + (bag + (name name) + (system system) + (host-inputs `(,@(if source + `(("source" ,source)) + '()) + ,@inputs + + ;; Keep the standard inputs of 'gnu-build-system'. + ,@(standard-packages))) + (build-inputs `(("patchelf" ,patchelf) + ;; If current system is i686, the *32 packages will be the + ;; same as the non-32, but that's OK. + ("libc32" ,(to32 glibc)) + ,@native-inputs)) + (outputs outputs) + (build binary-build) + (arguments (strip-keyword-arguments private-keywords arguments))))) + +(define* (binary-build store name inputs + #:key (guile #f) + (outputs '("out")) + (patchelf-plan '()) + (install-plan '(("" ".*"))) + (search-paths '()) + (out-of-source? #t) + (validate-runpath? #t) + (patch-shebangs? #t) + (strip-binaries? #t) + (strip-flags ''("--strip-debug")) + (strip-directories ''("lib" "lib64" "libexec" + "bin" "sbin")) + (phases '(@ (nonguix build binary-build-system) + %standard-phases)) + (system (%current-system)) + (imported-modules %binary-build-system-modules) + (modules '((nonguix build binary-build-system) + (guix build utils) + (nonguix build utils)))) + "Build SOURCE using PATCHELF, and with INPUTS. This assumes that SOURCE +provides its own binaries." + (define builder + `(begin + (use-modules ,@modules) + (binary-build #:source ,(match (assoc-ref inputs "source") + (((? derivation? source)) + (derivation->output-path source)) + ((source) + source) + (source + source)) + #:system ,system + #:outputs %outputs + #:inputs %build-inputs + #:patchelf-plan ,patchelf-plan + #:install-plan ,install-plan + #:search-paths ',(map search-path-specification->sexp + search-paths) + #:phases ,phases + #:out-of-source? ,out-of-source? + #:validate-runpath? ,validate-runpath? + #:patch-shebangs? ,patch-shebangs? + #:strip-binaries? ,strip-binaries? + #:strip-flags ,strip-flags + #:strip-directories ,strip-directories))) + + (define guile-for-build + (match guile + ((? package?) + (package-derivation store guile system #:graft? #f)) + (#f ; the default + (let* ((distro (resolve-interface '(gnu packages commencement))) + (guile (module-ref distro 'guile-final))) + (package-derivation store guile system #:graft? #f))))) + + (build-expression->derivation store name builder + #:system system + #:inputs inputs + #:modules imported-modules + #:outputs outputs + #:guile-for-build guile-for-build)) + +(define binary-build-system + (build-system + (name 'binary) + (description "The standard binary build system") + (lower lower))) + +;;; binary.scm ends here diff --git a/nonguix/build/binary-build-system.scm b/nonguix/build/binary-build-system.scm new file mode 100644 index 0000000..372e16b --- /dev/null +++ b/nonguix/build/binary-build-system.scm @@ -0,0 +1,134 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2019 Julien Lepiller +;;; +;;; This file is not part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (nonguix build binary-build-system) + #:use-module ((guix build gnu-build-system) #:prefix gnu:) + #:use-module (nonguix build utils) + #:use-module (guix build utils) + #:use-module (ice-9 match) + #:export (%standard-phases + binary-build)) + +;; Commentary: +;; +;; Builder-side code of the standard binary build procedure. +;; +;; Code: + +(define* (install #:key install-plan outputs #:allow-other-keys) + "Copy files from the \"source\" build input to the \"out\" output according to INSTALL-PLAN. + +An INSTALL-PLAN is made of three elements: + +- A source path which is a file or directory from the \"source\" build input. +- Patterns of the files to copy (only useful if the source path is a directory). +- The target destination. + +If the target ends with a slash, it represents the target directory. If not, it +represent the target full path, which only makes sense for single files." + (define (install-file file target) + (let ((target (string-append (assoc-ref outputs "out") + "/" target + (if (string-suffix? "/" target) + (string-append "/" file) + "")))) + (mkdir-p (dirname target)) + (copy-file file target))) + + (define (install-file-pattern pattern target) + (for-each + (lambda (file) + (install-file file target)) + (find-files "." pattern))) + + (define (install plan) + (match plan + ((file-or-directory files target) + (if (file-is-directory? file-or-directory) + (with-directory-excursion file-or-directory + (for-each + (lambda (pattern) + (install-file-pattern pattern target)) + files)) + (install-file file-or-directory target))))) + + (for-each install install-plan) + #t) + +(define* (patchelf #:key inputs outputs patchelf-plan #:allow-other-keys) + "Set the interpreter and the RPATH of files as per the PATCHELF-PLAN. + +The PATCHELF-PLAN elements are lists of: + +- The file to patch. +- The inputs (as strings) to include in the rpath, e.g. \"mesa\". + +Both executables and dynamic libraries are accepted. +The inputs are optional when the file is an executable." + (define (binary-patch binary interpreter runpath) + (unless (string-contains binary ".so") + (invoke "patchelf" "--set-interpreter" interpreter binary)) + (when runpath + (let ((rpath (string-join + (map + (lambda (input-or-output) + (cond + ((assoc-ref outputs input-or-output) + (string-append (assoc-ref outputs input-or-output) "/lib")) + ((assoc-ref inputs input-or-output) + (string-append (assoc-ref inputs input-or-output) "/lib")) + (else (error (format #f "`~a' not found among the inputs nor the outputs." input-or-output))))) + runpath) + ":"))) + (invoke "patchelf" "--set-rpath" rpath binary))) + #t) + (let ((interpreter (car (find-files (assoc-ref inputs "libc") "ld-linux.*\\.so"))) + (interpreter32 (car (find-files (assoc-ref inputs "libc32") "ld-linux.*\\.so")))) + (for-each + (lambda (plan) + (match plan + ((binary runpath) + (binary-patch binary (if (64-bit? binary) + interpreter + interpreter32) + runpath)) + ((binary) + (binary-patch binary (if (64-bit? binary) + interpreter + interpreter32) + #f)))) + patchelf-plan)) + #t) + +(define %standard-phases + ;; Everything is as with the GNU Build System except for the `configure' + ;; , `build', `check' and `install' phases. + (modify-phases gnu:%standard-phases + (delete 'bootstrap) + (delete 'configure) + (delete 'build) + (delete 'check) + (add-before 'install 'patchelf patchelf) + (replace 'install install))) + +(define* (binary-build #:key inputs (phases %standard-phases) + #:allow-other-keys #:rest args) + "Build the given package, applying all of PHASES in order." + (apply gnu:gnu-build #:inputs inputs #:phases phases args)) + +;;; binary-build-system.scm ends here diff --git a/nonguix/build/utils.scm b/nonguix/build/utils.scm new file mode 100644 index 0000000..b520769 --- /dev/null +++ b/nonguix/build/utils.scm @@ -0,0 +1,185 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2019 Pierre Neidhardt +;;; +;;; This file is not part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (nonguix build utils) + #:use-module (ice-9 match) + #:use-module (ice-9 binary-ports) + #:use-module (guix build utils) + #:export (64-bit? + make-desktop-entry-file + make-wrapper)) + +(define (64-bit? file) + "Return true if ELF file is in 64-bit format, false otherwise. +See https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header." + (with-input-from-file file + (lambda () + (= 2 + (array-ref (get-bytevector-n (current-input-port) 5) 4))) + #:binary #t)) + +(define* (make-desktop-entry-file destination #:key + (type "Application") ; One of "Application", "Link" or "Directory". + (version "1.1") + name + (generic-name name) + (no-display #f) + comment + icon + (hidden #f) + only-show-in + not-show-in + (d-bus-activatable #f) + try-exec + exec + path + (terminal #f) + actions + mime-type + (categories "Application") + implements + keywords + (startup-notify #t) + startup-w-m-class + #:rest all-args) + "Create a desktop entry file at DESTINATION. +You must specify NAME. + +Values can be booleans, numbers, strings or list of strings. + +Additionally, locales can be specified with an alist where the key is the +locale. The #f key specifies the default. Example: + + #:name '((#f \"I love Guix\") (\"fr\" \"J'aime Guix\")) + +produces + + Name=I love Guix + Name[fr]=J'aime Guix + +For a complete description of the format, see the specifications at +https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html." + (define (escape-semicolon s) + (string-join (string-split s #\;) "\\;")) + (define* (parse key value #:optional locale) + (set! value (match value + (#t "true") + (#f "false") + ((? number? n) n) + ((? string? s) (escape-semicolon s)) + ((? list? value) + (catch 'wrong-type-arg + (lambda () (string-join (map escape-semicolon value) ";")) + (lambda args (error "List arguments can only contain strings: ~a" args)))) + (_ (error "Value must be a boolean, number, string or list of strings")))) + (format #t "~a=~a~%" + (if locale + (format #f "~a[~a]" key locale) + key) + value)) + + (define key-error-message "This procedure only takes key arguments beside DESTINATION") + + (unless name + (error "Missing NAME key argument")) + (unless (member #:type all-args) + (set! all-args (append (list #:type type) all-args))) + (mkdir-p (dirname destination)) + + (with-output-to-file destination + (lambda () + (format #t "[Desktop Entry]~%") + (let loop ((args all-args)) + (match args + (() #t) + ((_) (error key-error-message)) + ((key value . ...) + (unless (keyword? key) + (error key-error-message)) + (set! key + (string-join (map string-titlecase + (string-split (symbol->string + (keyword->symbol key)) + #\-)) + "")) + (match value + (((_ . _) . _) + (for-each (lambda (locale-subvalue) + (parse key + (if (and (list? (cdr locale-subvalue)) + (= 1 (length (cdr locale-subvalue)))) + ;; Support both proper and improper lists for convenience. + (cadr locale-subvalue) + (cdr locale-subvalue)) + (car locale-subvalue))) + value)) + (_ + (parse key value))) + (loop (cddr args)))))))) + +(define* (make-wrapper wrapper real-file #:rest vars) + "Like `wrap-program' but create WRAPPER around REAL-FILE. +The wrapper automatically changes directory to that of REAL-FILE. + +Example: + + (make-wrapper \"bin/foo\" \"sub-dir/original-foo\" + '(\"PATH\" \":\" = (\"/gnu/.../bar/bin\")) + '(\"CERT_PATH\" suffix (\"/gnu/.../baz/certs\" + \"/qux/certs\"))) + +will create 'bin/foo' with the following +contents: + + #!location/of/bin/bash + export PATH=\"/gnu/.../bar/bin\" + export CERT_PATH=\"$CERT_PATH${CERT_PATH:+:}/gnu/.../baz/certs:/qux/certs\" + cd sub-dir + exec -a $0 sub-dir/original-foo \"$@\"." + (define (export-variable lst) + ;; Return a string that exports an environment variable. + (match lst + ((var sep '= rest) + (format #f "export ~a=\"~a\"" + var (string-join rest sep))) + ((var sep 'prefix rest) + (format #f "export ~a=\"~a${~a:+~a}$~a\"" + var (string-join rest sep) var sep var)) + ((var sep 'suffix rest) + (format #f "export ~a=\"$~a${~a+~a}~a\"" + var var var sep (string-join rest sep))) + ((var '= rest) + (format #f "export ~a=\"~a\"" + var (string-join rest ":"))) + ((var 'prefix rest) + (format #f "export ~a=\"~a${~a:+:}$~a\"" + var (string-join rest ":") var var)) + ((var 'suffix rest) + (format #f "export ~a=\"$~a${~a:+:}~a\"" + var var var (string-join rest ":"))))) + + (mkdir-p (dirname wrapper)) + (call-with-output-file wrapper + (lambda (port) + (format port + "#!~a~%~a~%cd \"~a\"~%exec -a \"$0\" \"~a\" \"$@\"~%" + (which "bash") + (string-join (map export-variable vars) "\n") + (dirname real-file) + (canonicalize-path real-file)))) + (chmod wrapper #o755)) diff --git a/nonguix/utils.scm b/nonguix/utils.scm new file mode 100644 index 0000000..2a514b6 --- /dev/null +++ b/nonguix/utils.scm @@ -0,0 +1,40 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2019 Pierre Neidhardt +;;; +;;; This file is not part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (nonguix utils) + #:use-module (srfi srfi-26) + #:use-module (ice-9 match) + #:use-module (ice-9 textual-ports) + #:use-module (ice-9 popen) + #:use-module (guix utils) + #:use-module (guix packages) + #:export (getenv*)) + +(define-public (to32 package64) + "Build package for i686-linux. +Only x86_64-linux and i686-linux are supported. +- If i686-linux, return the package unchanged. +- If x86_64-linux, return the 32-bit version of the package." + (match (%current-system) + ("x86_64-linux" + (package + (inherit package64) + (name (string-append (package-name package64) "32")) + (arguments `(#:system "i686-linux" + ,@(package-arguments package64))))) + (_ package64))) -- cgit v1.2.3