1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
| | ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016, 2018 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2019 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
;;; Copyright © 2020, 2022 Hartmut Goebel <h.goebel@crazy-compilers.com>
;;; Copyright © 2023 Pierre-Henry Fröhring <phfrohring@deeplinks.com>
;;;
;;; This file is 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 <http://www.gnu.org/licenses/>.
(define-module (guix build rebar-build-system)
#:use-module ((guix build gnu-build-system)
#:prefix gnu:)
#:use-module ((guix build utils)
#:hide (delete))
#:use-module (ice-9 match)
#:use-module (ice-9 ftw)
#:use-module (ice-9 string-fun)
#:use-module (ice-9 receive)
#:use-module (ice-9 regex)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:export (rebar-build %standard-phases))
;;
;; Builder-side code of the standard build procedure for Erlang packages using
;; rebar3.
;;
;;
;; utils
;;
(define (utils-list-directories directory)
"Return file names of the sub-directory of DIRECTORY."
(scandir directory
(lambda (file)
(and (not (member file
'("." "..")))
(file-is-directory? (string-append directory "/" file))))))
;;
;; source
;;
(define source->name
car)
(define source->tar
cadr)
;;
;; tar
;;
(define* (tar-unpack tar)
(invoke "tar" "xvf" tar)
(invoke "tar" "xvf" "contents.tar.gz")
;; Prevent an error message at install phase.
;; .
;; rebar3 compile produces a symlinks like so in _build/:
;; priv -> ../../../../priv
;; priv -> ../../../../include
;;
;; The install phase copies whatever has been built to the output directory.
;; If the priv/ directory is absent, then an error `i/o error:
;; _build/…/priv: No such file or directory' occurs. So, we make sure that a
;; directory exists.
(for-each (lambda (dir)
(mkdir-p dir))
(list "priv" "include")))
;;;
;;; pkg-name
;;;
(define pkg-name-prefix
"erlang-")
(define pkg-name-prefix-re
(format #f "^~a(.*)" pkg-name-prefix))
(define (pkg-name? name)
(string-match (format #f "^~a" pkg-name-prefix) name))
(define (pkg-name->name name)
(regexp-substitute #f
(string-match pkg-name-prefix-re name) 1))
(define* (pkg-name->lib-name name)
"erlang-a-b-1.0.2 -> a_b-1.0.2"
(let ((elements (string-split (pkg-name->name name) #\-)))
(string-append (string-join (drop-right elements 1) "_") "-"
(last elements))))
(define* (pkg-name->build-dir name
#:key (profile "default"))
(string-append (format #f "_build/~a/lib/" profile)
(car (string-split (pkg-name->lib-name name) #\-))))
(define (lib-name-no-version lib-name)
(car (string-split lib-name #\-)))
;;
;; phase
;;
(define* (phase-unpack #:key source #:allow-other-keys)
(tar-unpack source))
(define (phase-configure-HOME . ignored_args)
"In some cases, it is needed for the environment variable HOME to be defined
as a directory with write permission.
Examples of errors:
- Could not write to \"/homeless-shelter/.cache/rebar3/hex\". Please ensure the path is writeable.
"
(let ((HOME "HOME")
(tmp "/tmp"))
(setenv HOME tmp)
(format #t "~a=~a\n" HOME tmp)))
(define* (phase-configure-dependencies #:key name
version
inputs
sources-erlang
(install-profile "default")
#:allow-other-keys)
;; If source in sources-erlang, then install it under _checkouts/.
;; see: https://rebar3.org/docs/configuration/dependencies/#checkout-dependencies
(let ((_checkouts "_checkouts"))
(mkdir-p _checkouts)
(for-each (lambda (source)
(match source
((name tar)
(let ((src (string-append _checkouts "/" name)))
(mkdir-p src)
(with-directory-excursion src
(tar-unpack tar))))
(_ #f))) sources-erlang))
;; If input in inputs is an Erlang package, then install it under _build/.
(let ((_build (format #f "_build/~a/checkouts" install-profile)))
(mkdir-p _build)
(match inputs
(((_ . dirs) ..1)
(for-each (lambda (dir)
(let ((elib (string-append dir "/lib/erlang/lib")))
(when (directory-exists? elib)
(for-each (lambda (dirname)
(let ((src (string-append elib "/" dirname))
(dest (string-append _build "/"
(lib-name-no-version
dirname))))
(copy-recursively src dest
#:log #f)
(mkdir-p (string-append dest "/priv"))))
(utils-list-directories elib))))) dirs))
(_ #f))))
(define* (phase-build #:key name
(rebar-flags '()) #:allow-other-keys)
(apply invoke
`("rebar3" "compile"
,@rebar-flags)))
(define* (phase-check #:key target
(rebar-flags '())
(tests? (not target))
(test-target "eunit")
#:allow-other-keys)
(if tests?
(apply invoke
`("rebar3" ,test-target
,@rebar-flags))
(format #t "test suite not run~%")))
(define* (phase-install #:key name outputs
(install-profile "default") #:allow-other-keys)
(let* ((build-dir (pkg-name->build-dir name
#:profile install-profile))
(out-build-dir (string-append (assoc-ref outputs "out")
"/lib/erlang/lib/"
(pkg-name->lib-name name))))
(mkdir-p out-build-dir)
(copy-recursively build-dir out-build-dir
#:follow-symlinks? #t)))
;;
;; rebar
;;
(define rebar-%standard-phases
(modify-phases gnu:%standard-phases
(replace 'unpack
phase-unpack)
(add-after 'unpack 'configure-HOME
phase-configure-HOME)
(delete 'bootstrap)
(delete 'configure)
(add-before 'build 'configure-dependencies
phase-configure-dependencies)
(replace 'build
phase-build)
(replace 'check
phase-check)
(replace 'install
phase-install)))
(define* (rebar-build #:key inputs
(phases rebar-%standard-phases)
#:allow-other-keys #:rest args)
"Build the given Erlang package, applying all of PHASES in order."
(apply gnu:gnu-build
#:inputs inputs
#:phases phases
args))
;;
;; Exported bindings.
;;
(define %standard-phases rebar-%standard-phases)
;;; rebar-build-system.scm ends here
|