5db9906acf95e5e88c3fc69278b1dbbba9f90d6e
[jackhill/guix/guix.git] / guix / build / chicken-build-system.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 raingloom <raingloom@riseup.net>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix build chicken-build-system)
20 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
21 #:use-module (guix build union)
22 #:use-module (guix build utils)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 ftw)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (rnrs io ports)
28 #:use-module (rnrs bytevectors)
29 #:export (%standard-phases
30 chicken-build))
31
32 ;; CHICKEN_EGG_CACHE is where sources are fetched and binaries are built
33 ;; CHICKEN_INSTALL_REPOSITORY is where dependencies are looked up
34 ;; its first component is also where new eggs are installed.
35
36 ;; TODO: deduplicate with go-build-system.scm ?
37 ;; TODO: the binary version should be defined in one of the relevant modules
38 ;; instead of being hardcoded everywhere. Tried to do that but got undefined
39 ;; variable errors.
40
41 (define (chicken-package? name)
42 (string-prefix? "chicken-" name))
43
44 (define* (setup-chicken-environment #:key inputs outputs #:allow-other-keys)
45 (setenv "CHICKEN_INSTALL_REPOSITORY"
46 (string-concatenate
47 ;; see TODO item about binary version above
48 (append (list (assoc-ref outputs "out") "/var/lib/chicken/11/")
49 (let ((oldenv (getenv "CHICKEN_INSTALL_REPOSITORY")))
50 (if oldenv
51 (list ":" oldenv)
52 '())))))
53 (setenv "CHICKEN_EGG_CACHE" (getcwd))
54 #t)
55
56 ;; This is copied from go-build-system.scm so it could probably be simplified.
57 ;; I used it because the source of the egg needs to be unpacked into a directory
58 ;; that is named after the egg and I knew that the go build system does that.
59 (define* (unpack #:key source egg-name unpack-path #:allow-other-keys)
60 "Relative to $CHICKEN_EGG_CACHE, unpack SOURCE in UNPACK-PATH, or EGG-NAME
61 when UNPACK-PATH is unset. If the SOURCE archive has a single top level
62 directory, it is stripped so that the sources appear directly under UNPACK-PATH.
63 When SOURCE is a directory, copy its content into UNPACK-PATH instead of
64 unpacking."
65 (define (unpack-maybe-strip source dest)
66 (let* ((scratch-dir (string-append (or (getenv "TMPDIR") "/tmp")
67 "/scratch-dir"))
68 (out (mkdir-p scratch-dir)))
69 (with-directory-excursion scratch-dir
70 (if (string-suffix? ".zip" source)
71 (invoke "unzip" source)
72 (invoke "tar" "-xvf" source))
73 (let ((top-level-files (remove (lambda (x)
74 (member x '("." "..")))
75 (scandir "."))))
76 (match top-level-files
77 ((top-level-file)
78 (when (file-is-directory? top-level-file)
79 (copy-recursively top-level-file dest #:keep-mtime? #t)))
80 (_
81 (copy-recursively "." dest #:keep-mtime? #t)))))
82 (delete-file-recursively scratch-dir)))
83
84 (when (string-null? egg-name)
85 (display "WARNING: The egg name is unset.\n"))
86 (when (string-null? unpack-path)
87 (set! unpack-path egg-name))
88 (let ((dest (string-append (getenv "CHICKEN_EGG_CACHE") "/" unpack-path)))
89 (mkdir-p dest)
90 (if (file-is-directory? source)
91 (copy-recursively source dest #:keep-mtime? #t)
92 (unpack-maybe-strip source dest)))
93 #t)
94
95 (define* (build #:key egg-name #:allow-other-keys)
96 "Build the Chicken egg named by EGG-NAME"
97 (invoke "chicken-install" "-cached" "-no-install" egg-name))
98
99 (define* (install #:key egg-name #:allow-other-keys)
100 "Install the already built egg named by EGG-NAME"
101 (invoke "chicken-install" "-cached" egg-name))
102
103 (define* (check #:key egg-name tests? #:allow-other-keys)
104 "Build and run tests for the Chicken egg EGG-NAME"
105 ;; there is no "-test-only" option, but we've already run install
106 ;; so this just runs tests.
107 ;; i think it's a fair assumption that phases won't be reordered.
108 (setenv "CHICKEN_REPOSITORY_PATH"
109 (string-append (getenv "CHICKEN_INSTALL_REPOSITORY")
110 ":"
111 (getenv "CHICKEN_REPOSITORY_PATH")))
112 (when tests?
113 (invoke "chicken-install" "-cached" "-test" "-no-install" egg-name)))
114
115 ;; It doesn't look like Chicken generates any unnecessary references.
116 ;; So we don't have to remove them either. Nice.
117
118 (define %standard-phases
119 (modify-phases gnu:%standard-phases
120 (replace 'unpack unpack)
121 (delete 'bootstrap)
122 (delete 'configure)
123 (delete 'patch-generated-file-shebangs)
124 (add-before 'unpack 'setup-chicken-environment setup-chicken-environment)
125 (replace 'build build)
126 (delete 'check)
127 (replace 'install install)
128 (add-after 'install 'check check)))
129
130 (define* (chicken-build #:key inputs (phases %standard-phases)
131 #:allow-other-keys #:rest args)
132 "Build the given Chicken package, applying all of PHASES in order."
133 (apply gnu:gnu-build #:inputs inputs #:phases phases args))