all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 7229f32d128b8a37abad1b0910d0ae13fe44d173 15472 bytes (raw)
name: guix/build/asdf-build-system.scm 	 # note: path name is non-authoritative(*)

  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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2016 Andy Patterson <ajpatter@uwaterloo.ca>
;;;
;;; 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 asdf-build-system)
  #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  #:use-module (guix build utils)
  #:use-module (guix build lisp-utils)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 receive)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:export (%standard-phases/sbcl
            %standard-phases/ecl
            %standard-phases/source
            asdf-build
            asdf-build/source))

;; Commentary:
;;
;; System for building ASDF packages; creating executable programs and images
;; from them.
;;
;; Code:

(define %object-prefix "/lib")

(define %source-install-prefix
  (string-append %install-prefix "/source"))

(define %system-install-prefix
  (string-append %install-prefix "/systems"))

(define (output-path->package-name path)
  (package-name->name+version (strip-store-file-name path)))

(define (outputs->name outputs)
  (output-path->package-name
   (assoc-ref outputs "out")))

(define (wrap-source-registry registry)
  `(:source-registry
    ,@registry
    :inherit-configuration))

(define (wrap-output-translations translations)
  `(:output-translations
    ,@translations
    :inherit-configuration))

(define (source-directory output name)
  (string-append output %source-install-prefix "/" name))

(define (library-directory output lisp)
  (string-append output %object-prefix
                 "/" lisp))

(define (output-translation source-output
                            source-name
                            object-output
                            lisp)
  "Return a translation for the system's source output
to it's binary output."
  `((,(source-directory source-output source-name)
     :**/ :*.*.*)
    (,(library-directory object-output lisp)
     :**/ :*.*.*)))

(define (source-registry source-path)
  `(:tree ,source-path))

(define (lisp-dependency-names lisp inputs)
  (map first (lisp-dependencies lisp inputs)))

(define (copy-files-to-output outputs output name)
  "Copy all files from OUTPUT to \"out\". Create an extra link to any
system-defining files in the source to a convenient location. This is done
before any compiling so that the compiled source locations will be valid."
  (let* ((out (assoc-ref outputs output))
         (source (getcwd))
         (target (source-directory out name))
         (system-path (string-append out %system-install-prefix)))
    (copy-recursively source target)
    (mkdir-p system-path)
    (for-each
     (lambda (file)
       (symlink file
                (string-append system-path "/" (basename file))))
     (find-files target "\\.asd$"))
    #t))

(define* (install #:key outputs #:allow-other-keys)
  "Copy and symlink all the source files."
  (copy-files-to-output outputs "out" (outputs->name outputs)))

(define* (copy-source #:key outputs build-in-tree lisp
                      image? binary?
                      #:allow-other-keys)
  "Copy the source to the output named by BUILD-IN-TREE."
  (when (and build-in-tree (not image?) (not binary?))
    (let* ((out (assoc-ref outputs "out"))
           (name (remove-lisp-from-name (output-path->package-name out) lisp)))
      (copy-files-to-output outputs build-in-tree name)))
  #t)

(define* (build #:key outputs inputs lisp
                compile-dependencies
                source-input
                build-in-tree
                image?
                binary?
                #:allow-other-keys)
  "Compile the system."

  (unless (or binary? image?)
    (let* ((out (assoc-ref outputs "out"))
           (name (remove-lisp-from-name (output-path->package-name out) lisp))
           (source (cond
                    (source-input
                     (assoc-ref inputs source-input))
                    (build-in-tree (assoc-ref outputs build-in-tree))
                    (else (assoc-ref inputs name))))
           (source-name (or source-input name))
           (source-path (string-append source %source-install-prefix "/"
                                       source-name))
           (translations (wrap-output-translations
                          `(,(output-translation source
                                                 source-name
                                                 out
                                                 lisp))))
           (registry (map (match-lambda
                            ((_ . path) (source-registry path)))
                          (lisp-dependencies lisp inputs))))

      (setenv "ASDF_OUTPUT_TRANSLATIONS"
              (replace-escaped-macros (format #f "~S" translations)))
      (setenv "CL_SOURCE_REGISTRY"
              (replace-escaped-macros
               (format #f "~S" (wrap-source-registry
                                `(,(source-registry source-path)
                                  ,@registry)))))

      (setenv "HOME" out) ; ecl's asdf sometimes wants to create $HOME/.cache

      (parameterize ((%lisp (string-append
                             (assoc-ref inputs lisp) "/bin/" lisp)))
        (compile-system name lisp compile-dependencies))

      ;; As above, ecl will sometimes create this even though it doesn't use it

      (let ((cache-directory (string-append out "/.cache")))
        (when (directory-exists? cache-directory)
          (delete-file-recursively cache-directory)))))
  #t)

(define* (check #:key lisp tests? outputs inputs
                compile-dependencies
                image?
                binary?
                #:allow-other-keys)
  "Test the system."

  (if (and tests? (not image?) (not binary?))
      (parameterize ((%lisp (string-append
                             (assoc-ref inputs lisp) "/bin/" lisp)))
        (test-system
         (remove-lisp-from-name (outputs->name outputs) lisp)
         lisp
         compile-dependencies))
      (format #t "test suite not run~%"))
  #t)

(define* (patch-asd-files #:key outputs
                          inputs
                          lisp
                          special-dependencies
                          image?
                          binary?
                          test-only-systems
                          #:allow-other-keys)
  "Patch any asd files created by the compilation process so that they
can find their dependencies. Exclude any TEST-ONLY-SYSTEMS which were only
included to run tests. Add any SPECIAL-DEPENDENCIES which the LISP
implementation itself provides."
  (unless (or image? binary?)
    (let* ((out (assoc-ref outputs "out"))
           (name (remove-lisp-from-name (output-path->package-name out) lisp))
           (registry (lset-difference
                      (lambda (input system)
                        (match input
                          ((name . path) (string=? name system))))
                      (lisp-dependencies lisp inputs)
                      test-only-systems))
           (lisp-systems (map first registry)))

      (for-each
       (lambda (asd-file)
         (patch-asd-file asd-file registry lisp
                         (append lisp-systems special-dependencies)))
       (find-files out "\\.asd$"))))
  #t)

(define* (symlink-asd-files #:key outputs lisp
                            image? binary?
                            #:allow-other-keys)
  "Create an extra reference to the system in a convenient location."
  (unless (or image? binary?)
    (let* ((out (assoc-ref outputs "out")))
      (for-each
       (lambda (asd-file)
         (receive (new-asd-file asd-file-directory)
             (bundle-asd-file out asd-file lisp)
           (mkdir-p asd-file-directory)
           (symlink asd-file new-asd-file)))

       (find-files out "\\.asd$"))))
  #t)

(define* (generate-binary #:key outputs
                          inputs
                          image-dependencies
                          entry-program
                          lisp
                          binary?
                          #:allow-other-keys)
  "Generate a binary program for the system, either in \"bin\" if the package
also contains a library system, or in \"out\" otherwise."
  (define output (if binary? "out" "bin"))
  (generate-executable #:outputs outputs
                       #:inputs inputs
                       #:image-dependencies image-dependencies
                       #:entry-program entry-program
                       #:lisp lisp
                       #:output output
                       #:needs-own-system? (not binary?)
                       #:type "program")
  (and=>
   (assoc-ref outputs output)
   (lambda (bin)
     (let* ((full-name (outputs->name outputs))
            (name (if binary? full-name
                      (remove-lisp-from-name full-name lisp)))
            (bin-directory (string-append bin "/bin")))
       (with-directory-excursion bin-directory
         (rename-file (string-append name "-exec")
                      name)))))
  #t)

(define* (generate-image #:key outputs
                         inputs
                         image-dependencies
                         lisp
                         image?
                         #:allow-other-keys)
  "Generate an image for the system, possibly standalone, either in \"image\"
if the package also contains a library system, or in \"out\" otherwise."
  (define output (if image? "out" "image"))
  (generate-executable #:outputs outputs
                       #:inputs inputs
                       #:image-dependencies image-dependencies
                       #:entry-program '(nil)
                       #:lisp lisp
                       #:output output
                       #:needs-own-system? (not image?)
                       #:type "image")
  (and=>
   (assoc-ref outputs output)
   (lambda (image)
     (let* ((full-name (outputs->name outputs))
            (name (if image? full-name
                      (remove-lisp-from-name full-name lisp)))
            (bin-directory (string-append image "/bin")))
       (with-directory-excursion bin-directory
         (rename-file (string-append name "-exec--all-systems.image")
                      (string-append name ".image"))))))
  #t)

(define* (generate-executable #:key outputs
                              image-dependencies
                              entry-program
                              lisp
                              output
                              inputs
                              type
                              needs-own-system?
                              #:allow-other-keys)
  "Generate an executable by using asdf's TYPE-op, containing whithin the
image all IMAGE-DEPNDENCIES, and running ENTRY-PROGRAM in the case of an
executable."
  (and=>
   (assoc-ref outputs output)
   (lambda (out)
     (let* ((bin-directory (string-append out "/bin"))
            (full-name (outputs->name outputs))
            (name (if needs-own-system?
                      (remove-lisp-from-name full-name lisp)
                      full-name)))
       (mkdir-p out)
       (with-directory-excursion out
         (generate-executable-wrapper-system name
                                             image-dependencies
                                             needs-own-system?)
         (generate-executable-entry-point name entry-program))

       (setenv "CL_SOURCE_REGISTRY"
               (replace-escaped-macros
                (format
                 #f "~S"
                 (wrap-source-registry
                  `(,(source-registry (assoc-ref outputs "out"))
                    ,(source-registry out)
                    ,@(map (lambda (dependency)
                             (source-registry (assoc-ref inputs dependency)))
                           image-dependencies))))))

       (setenv "ASDF_OUTPUT_TRANSLATIONS"
               (replace-escaped-macros
                (format
                 #f "~S"
                 (wrap-output-translations
                  `(((,out :**/ :*.*.*)
                     (,bin-directory :**/ :*.*.*)))))))

       (parameterize ((%lisp (string-append
                              (assoc-ref inputs lisp) "/bin/" lisp)))
         (generate-executable-for-system type name lisp))

       (delete-file (string-append out "/" name "-exec.asd"))
       (delete-file (string-append out "/" name "-exec.lisp"))))))

(define* (cleanup-files/sbcl #:key outputs binary? image? lisp
                             #:allow-other-keys)
  "Remove any compiled files which are not a part of the final bundle."
  (unless (or binary? image?)
    (let ((out (assoc-ref outputs "out")))
      (for-each
       (lambda (file)
         (unless (string-suffix? "--system.fasl" file)
           (delete-file file)))
       (find-files out "\\.fasl$"))))
  #t)

(define* (cleanup-files/ecl #:key outputs binary? image?
                             #:allow-other-keys)
  "Remove any compiled files which are not a part of the final bundle."
  (unless (or binary? image?)
    (let ((out (assoc-ref outputs "out")))
      (for-each delete-file
                (append (find-files out "\\.fas$")
                        (find-files out "\\.o$")))))
  #t)

(define %standard-phases/source
  (modify-phases gnu:%standard-phases
    (delete 'configure)
    (delete 'check)
    (delete 'build)
    (replace 'install install)))

(define %standard-phases
  (modify-phases gnu:%standard-phases
    (delete 'configure)
    (delete 'install)
    (replace 'build build)
    (add-before 'build 'copy-source copy-source)
    (replace 'check check)
    (add-after 'check 'link-dependencies patch-asd-files)
    (add-after 'link-dependencies 'create-symlinks symlink-asd-files)
    (add-after 'create-symlinks 'cleanup cleanup-files/sbcl)
    (add-after 'cleanup 'generate-binary generate-binary)
    (add-after 'generate-binary 'generate-image generate-image)))

(define %standard-phases/sbcl
  (modify-phases %standard-phases
    ;; stripping sbcl binaries removes their entry program and extra systems
    (delete 'strip)))

(define %standard-phases/ecl
  (modify-phases %standard-phases
    (replace 'cleanup cleanup-files/ecl)))

(define* (asdf-build #:key inputs
                     (phases %standard-phases)
                     #:allow-other-keys
                     #:rest args)
  (apply gnu:gnu-build
         #:inputs inputs
         #:phases phases
         args))

(define* (asdf-build/source #:key inputs
                            (phases %standard-phases/source)
                            #:allow-other-keys
                            #:rest args)
  (apply gnu:gnu-build
         #:inputs inputs
         #:phases phases
         args))

;;; asdf-build-system.scm ends here

debug log:

solving 7229f32 ...
found 7229f32 in https://yhetil.org/guix/20160927041532.27097-2-ajpatter@uwaterloo.ca/

applying [1/1] https://yhetil.org/guix/20160927041532.27097-2-ajpatter@uwaterloo.ca/
diff --git a/guix/build/asdf-build-system.scm b/guix/build/asdf-build-system.scm
new file mode 100644
index 0000000..7229f32

Checking patch guix/build/asdf-build-system.scm...
Applied patch guix/build/asdf-build-system.scm cleanly.

index at:
100644 7229f32d128b8a37abad1b0910d0ae13fe44d173	guix/build/asdf-build-system.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.