blob: eb7a91840e44f5cd3d6a08b0d461b430d3ce3477 (
about) (
plain)
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
|
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
;;;
;;; 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 guile-build-system)
#:use-module ((guix build gnu-build-system) #:prefix gnu:)
#:use-module (guix build utils)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-26)
#:use-module (ice-9 match)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 regex)
#:use-module (guix build utils)
#:export (target-guile-effective-version
%standard-phases
guile-build))
(define* (target-guile-effective-version #:optional guile)
"Return the effective version of GUILE or whichever 'guile' is in $PATH.
Return #false if it cannot be determined."
(let* ((pipe (open-pipe* OPEN_READ
(if guile
(string-append guile "/bin/guile")
"guile")
"-c" "(display (effective-version))"))
(line (read-line pipe)))
(and (zero? (close-pipe pipe))
(string? line)
line)))
(define (file-sans-extension file) ;TODO: factorize
"Return the substring of FILE without its extension, if any."
(let ((dot (string-rindex file #\.)))
(if dot
(substring file 0 dot)
file)))
(define %scheme-file-regexp
;; Regexp to match Scheme files.
"\\.(scm|sls)$")
(define %documentation-file-regexp
;; Regexp to match README files and the likes.
"^(README.*|.*\\.html|.*\\.org|.*\\.md)$")
(define* (set-locale-path #:key inputs native-inputs
#:allow-other-keys)
"Set 'GUIX_LOCPATH'."
(match (assoc-ref (or native-inputs inputs) "locales")
(#f #t)
(locales
(setenv "GUIX_LOCPATH" (string-append locales "/lib/locale"))
#t)))
(define* (invoke-each commands
#:key (max-processes (current-processor-count))
report-progress)
"Run each command in COMMANDS in a separate process, using up to
MAX-PROCESSES processes in parallel. Call REPORT-PROGRESS at each step.
Raise an error if one of the processes exit with non-zero."
(define total
(length commands))
(define processes
(make-hash-table))
(define (wait-for-one-process)
(match (waitpid WAIT_ANY)
((pid . status)
(let ((command (hashv-ref processes pid)))
(hashv-remove! processes command)
(unless (zero? (status:exit-val status))
(format (current-error-port)
"process '~{~a ~}' failed with status ~a~%"
command status)
(exit 1))))))
(define (fork-and-run-command command)
(match (primitive-fork)
(0
(dynamic-wind
(const #t)
(lambda ()
(apply execlp command))
(lambda ()
(primitive-exit 127))))
(pid
(hashv-set! processes pid command)
#t)))
(let loop ((commands commands)
(running 0)
(completed 0))
(match commands
(()
(or (zero? running)
(let ((running (- running 1))
(completed (+ completed 1)))
(wait-for-one-process)
(report-progress total completed)
(loop commands running completed))))
((command . rest)
(if (< running max-processes)
(let ((running (+ 1 running)))
(fork-and-run-command command)
(loop rest running completed))
(let ((running (- running 1))
(completed (+ completed 1)))
(wait-for-one-process)
(report-progress total completed)
(loop commands running completed)))))))
(define* (report-build-progress total completed
#:optional (log-port (current-error-port)))
"Report that COMPLETED out of TOTAL files have been completed."
(format log-port "compiling...\t~5,1f% of ~d files~%" ;FIXME: i18n
(* 100. (/ completed total)) total)
(force-output log-port))
(define* (build #:key outputs inputs native-inputs
(source-directory ".")
(compile-flags '())
(scheme-file-regexp %scheme-file-regexp)
(not-compiled-file-regexp #f)
target
#:allow-other-keys)
"Build files in SOURCE-DIRECTORY that match SCHEME-FILE-REGEXP. Files
matching NOT-COMPILED-FILE-REGEXP, if true, are not compiled but are
installed; this is useful for files that are meant to be included."
(let* ((out (assoc-ref outputs "out"))
(guile (assoc-ref (or native-inputs inputs) "guile"))
(effective (target-guile-effective-version guile))
(module-dir (string-append out "/share/guile/site/"
effective))
(go-dir (string-append out "/lib/guile/"
effective "/site-ccache/"))
(guild (string-append guile "/bin/guild"))
(flags (if target
(cons (string-append "--target=" target)
compile-flags)
compile-flags)))
(if target
(format #t "Cross-compiling for '~a' with Guile ~a...~%"
target effective)
(format #t "Compiling with Guile ~a...~%" effective))
(format #t "compile flags: ~s~%" flags)
;; Make installation directories.
(mkdir-p module-dir)
(mkdir-p go-dir)
;; Compile .scm files and install.
(setenv "GUILE_AUTO_COMPILE" "0")
(setenv "GUILE_LOAD_COMPILED_PATH"
(string-append go-dir
(match (getenv "GUILE_LOAD_COMPILED_PATH")
(#f "")
(path (string-append ":" path)))))
(let ((source-files
(with-directory-excursion source-directory
(find-files "." scheme-file-regexp))))
(invoke-each
(filter-map (lambda (file)
(and (or (not not-compiled-file-regexp)
(not (string-match not-compiled-file-regexp
file)))
(cons* guild
"guild" "compile"
"-L" source-directory
"-o" (string-append go-dir
(file-sans-extension file)
".go")
(string-append source-directory "/" file)
flags)))
source-files)
#:max-processes (parallel-job-count)
#:report-progress report-build-progress)
(for-each
(lambda (file)
(install-file (string-append source-directory "/" file)
(string-append module-dir
"/" (dirname file))))
source-files))
#t))
(define* (install-documentation #:key outputs
(documentation-file-regexp
%documentation-file-regexp)
#:allow-other-keys)
"Install files that mactch DOCUMENTATION-FILE-REGEXP."
(let* ((out (assoc-ref outputs "out"))
(doc (string-append out "/share/doc/"
(strip-store-file-name out))))
(for-each (cut install-file <> doc)
(find-files "." documentation-file-regexp))
#t))
(define %standard-phases
(modify-phases gnu:%standard-phases
(delete 'bootstrap)
(delete 'configure)
(add-before 'install-locale 'set-locale-path
set-locale-path)
(replace 'build build)
(add-after 'build 'install-documentation
install-documentation)
(delete 'check)
(delete 'strip)
(delete 'validate-runpath)
(delete 'install)))
(define* (guile-build #:key (phases %standard-phases)
#:allow-other-keys #:rest args)
"Build the given Guile package, applying all of PHASES in order."
(apply gnu:gnu-build #:phases phases args))
|