blob: 3a3fe68b59805fde2c39c973d65b578ff64548c5 (
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
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
|
;;; Chickadee Game Toolkit
;;; Copyright © 2021 David Thompson <davet@gnu.org>
;;;
;;; Chickadee 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.
;;;
;;; Chickadee 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 this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (chickadee cli bundle)
#:declarative? #f
#:use-module (chickadee cli)
#:use-module (chickadee cli play)
#:use-module (chickadee config)
#:use-module (ice-9 format)
#:use-module (ice-9 ftw)
#:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-37)
#:use-module (system base compile)
#:export (chickadee-bundle
%default-config))
(define (regular-file? file-name)
(eq? (stat:type (lstat file-name)) 'regular))
(define (scan-for-libraries directories)
(map (lambda (dir)
(cons dir
(scandir dir
(lambda (file-name)
(and (not (string=? file-name "."))
(not (string=? file-name ".."))
(regular-file? (string-append dir "/" file-name))
(string-contains file-name ".so"))))))
directories))
(define (soname lib)
(string-append "lib" lib ".so"))
(define (find-lib lib-name libraries)
(let ((prefix (soname lib-name)))
(let loop ((libraries libraries))
(match libraries
(() (error "no shared library found for" lib-name))
(((dir . files) . rest)
(or (let ((file (find (lambda (file-name)
(string-prefix? prefix file-name))
files)))
(and file (string-append dir "/" file)))
(loop rest)))))))
(define (find-bin name directories)
(let loop ((dirs directories))
(match dirs
(() (error "cannot find binary" name))
((dir . rest)
(or (let ((bin-name (string-append dir "/" name)))
(and (file-exists? bin-name) bin-name))
(loop rest))))))
(define (library-version-number lib file-name)
(string-drop (basename file-name)
(string-length (string-append (soname lib) "."))))
(define (copy-lib lib file-name destdir)
(define (scope-lib file-name)
(string-append destdir "/lib/" file-name))
(let* ((so (soname lib))
(version (string-split (library-version-number lib file-name) #\.))
(base-file-name (basename file-name))
(dest-file-name (scope-lib base-file-name)))
(format #t "copy ~a → ~a~%"
file-name dest-file-name)
(copy-file file-name dest-file-name)
(format #t "symlink ~a → ~a~%" (scope-lib so) base-file-name)
(symlink base-file-name (scope-lib so))
;; Create symlinks for all the possible version number fragments.
(for-each (lambda (n)
(let ((link-name (scope-lib
(string-append so "."
(string-join (take version
(+ n 1))
".")))))
(format #t "symlink ~a → ~a~%" link-name base-file-name)
(symlink base-file-name link-name)))
(iota (- (length version) 1)))))
(define (root-module)
(resolve-module '() #f #f #:ensure #f))
(define (loaded-modules)
(define (scan-submodules module)
(hash-fold (lambda (k m memo)
(if (module-filename m)
(cons (module-filename m)
(append (scan-submodules m)
memo))
(append (scan-submodules m) memo)))
'()
(module-submodules module)))
(delete-duplicates (cons* "ice-9/eval.scm"
"ice-9/i18n.scm"
"ice-9/posix.scm"
"ice-9/psyntax-pp.scm"
"ice-9/quasisyntax.scm"
"ice-9/match.upstream.scm"
"ice-9/networking.scm"
"ice-9/r6rs-libraries.scm"
"ice-9/r7rs-libraries.scm"
(scan-submodules (root-module)))
string=?))
(define (scm->go file-name)
(string-append (substring file-name 0 (- (string-length file-name) 4)) ".go"))
;; Gather up the compiled/source files of all the modules that are
;; being used right now.
(define (shake-tree)
(map (lambda (f)
(list f
(search-path %load-path f)
(search-path %load-compiled-path (scm->go f))))
(sort (loaded-modules) string<)))
;; Snarfed from Guix
(define (mkdir-p dir)
"Create directory DIR and all its ancestors."
(define absolute?
(string-prefix? "/" dir))
(define not-slash
(char-set-complement (char-set #\/)))
(let loop ((components (string-tokenize dir not-slash))
(root (if absolute?
""
".")))
(match components
((head tail ...)
(let ((path (string-append root "/" head)))
(catch 'system-error
(lambda ()
(mkdir path)
(loop tail path))
(lambda args
(if (= EEXIST (system-error-errno args))
(loop tail path)
(apply throw args))))))
(() #t))))
;; Also snarfed from Guix, with some simplifications.
(define* (copy-recursively source destination)
(define strip-source
(let ((len (string-length source)))
(lambda (file)
(substring file len))))
(file-system-fold (const #t) ; enter?
(lambda (file stat result) ; leaf
(let ((dest (string-append destination
(strip-source file))))
(format #t "copy ~a → ~a~%" file dest)
(case (stat:type stat)
((symlink)
(let ((target (readlink file)))
(symlink target dest)))
(else
(copy-file file dest)))))
(lambda (dir stat result) ; down
(let ((target (string-append destination
(strip-source dir))))
(mkdir-p target)))
(lambda (dir stat result) ; up
result)
(const #t) ; skip
(lambda (file stat errno result)
(format (current-error-port) "i/o error: ~a: ~a~%"
file (strerror errno))
#f)
#t
source
lstat))
;; Once again, snarfed from Guix.
(define* (delete-file-recursively dir
#:key follow-mounts?)
(let ((dev (stat:dev (lstat dir))))
(file-system-fold (lambda (dir stat result) ; enter?
(or follow-mounts?
(= dev (stat:dev stat))))
(lambda (file stat result) ; leaf
(delete-file file))
(const #t) ; down
(lambda (dir stat result) ; up
(rmdir dir))
(const #t) ; skip
(lambda (file stat errno result)
(format (current-error-port)
"warning: failed to delete ~a: ~a~%"
file (strerror errno)))
#t
dir
;; Don't follow symlinks.
lstat)))
(define (shell-escape str)
(let ((n (string-length str)))
(list->string
(cons #\'
(let loop ((i 0))
(cond
((= i n)
'(#\'))
((eqv? (string-ref str i) #\')
(cons* #\' #\\ #\' #\'
(loop (+ i 1))))
(else
(cons (string-ref str i)
(loop (+ i 1))))))))))
(define (install-modules destdir)
(let ((v (string-append (major-version) ".0")))
(for-each (match-lambda
((suffix source compiled)
(let ((go-dest (string-append destdir "/lib/guile/" v "/ccache/"
(scm->go suffix))))
(mkdir-p (dirname go-dest))
(when compiled
(format #t "copy ~a → ~a~%" compiled go-dest)
(copy-file compiled go-dest)))))
(shake-tree))))
(define (install-libraries destdir names system-libraries)
(for-each (lambda (lib)
(let ((file-name (find-lib lib system-libraries)))
(copy-lib lib file-name destdir)))
names))
(define (install-guile destdir directories)
(let ((src (find-bin "guile" directories))
(dest (string-append destdir "/bin/guile")))
(format #t "copy ~a → ~a~%" src dest)
(copy-file src dest)))
(define (install-assets dirs destdir)
(for-each (lambda (dir)
(let ((target (string-append destdir "/" dir)))
(copy-recursively dir target)))
dirs))
(define (install-chickadee-data destdir)
(let ((sharedir (string-append destdir "/share/chickadee")))
(mkdir sharedir)
(copy-recursively %datadir sharedir)))
(define (install-init.scm code method destdir)
(let ((init (string-append destdir "/init.scm")))
(format #t "copy ~a → ~a~%" code init)
(copy-file code init)
(case method
((play)
(let ((module (resolve-module '(chickadee-bundler) #f)))
(beautify-user-module! module)
;; Need to load all of the default modules for `chickadee play` so
;; that the tree shaker won't miss anything.
(for-each (lambda (name)
(module-use! module (resolve-interface name)))
%default-modules)
;; Compile the main file to load all of the modules that it uses
;; without executing the code.
(compile-file init #:env module)))
((manual)
(compile-file init)))))
(define (install-launcher name method args destdir)
(let ((exe (string-append destdir "/" name))
(args (case method
((play)
(let ((exp (with-output-to-string
(lambda ()
(write '(use-modules (chickadee cli play)))
(write `(chickadee-play "init.scm" ,@args))))))
(string-append "-c " (shell-escape exp))))
((manual)
"--no-auto-compile init.scm")
(else
(error "unsupported launch method" method)))))
(format #t "install ~a~%" exe)
(call-with-output-file exe
(lambda (port)
(format port "#!/bin/sh
rootdir=`dirname $(realpath $0)`
export PATH=\"$rootdir/bin:$PATH\"
export LD_LIBRARY_PATH=\"$rootdir/lib\"
export GUILE_LOAD_PATH=\"$rootdir/share/guile/3.0\"
export GUILE_LOAD_COMPILED_PATH=\"$rootdir/lib/guile/3.0/ccache\"
export CHICKADEE_DATADIR=\"$rootdir/share/chickadee\"
cd $rootdir
exec bin/guile ~a
" args)))
(chmod exe #o755)))
(define %default-config
'((asset-directories . ())
(binary-directories . ("/usr/bin"))
(bundle-name . "chickadee-bundle")
(launcher-name . "launch-game")
(libraries . ("ffi"
"FLAC"
"freetype"
"gc"
"gmp"
"guile-3.0"
"jbig"
"jpeg"
"mpg123"
"ogg"
"openal"
"png16"
"readline"
"SDL2-2.0"
"SDL2_image-2.0"
"sndfile"
"sndio"
"tiff"
"tinfo"
"unistring"
"vorbis"
"vorbisenc"
"vorbisfile"
"webp"
"z"))
(library-directories . ("/lib"
"/lib64"
"/lib/x86_64-linux-gnu"
"/usr/lib"
"/usr/lib/x86_64-linux-gnu"))
(method . play)
(play-args . ())))
(define %tmpdir (or (getenv "TMPDIR") "/tmp"))
(define (make-bundle user-config)
(let* ((config (append user-config %default-config))
(name (assq-ref config 'bundle-name))
(archive (string-append name ".tar.gz"))
(args (assq-ref config 'play-args))
(assets (assq-ref config 'asset-directories))
(bindirs (assq-ref config 'binary-directories))
(code (assq-ref config 'code))
(tmpdir (mkdtemp (string-append %tmpdir "/chickadee-bundle-XXXXXX")))
(destdir (string-append tmpdir "/" name))
(launcher (assq-ref config 'launcher-name))
(libraries (assq-ref config 'libraries))
(libdirs (assq-ref config 'library-directories))
(method (assq-ref config 'method)))
(mkdir destdir)
(mkdir (string-append destdir "/bin"))
(mkdir (string-append destdir "/lib"))
(mkdir (string-append destdir "/share"))
(install-init.scm code method destdir)
(install-launcher launcher method args destdir)
(install-assets assets destdir)
(install-guile destdir bindirs)
(install-chickadee-data destdir)
(install-libraries destdir libraries (scan-for-libraries libdirs))
(install-modules destdir)
(format #t "create ~a~%" archive)
(unless (zero? (system* "tar" "czf" archive "-C" tmpdir "chickadee-bundle"))
(format (current-error-port) "failed to create ~a archive~%" archive)
(exit 1))
(delete-file-recursively tmpdir)))
(define (display-help-and-exit)
(format #t "Usage: chickadee bundle [OPTIONS] [FILE]~%
Create a redistributable binary tarball using the settings in FILE, or
'bundle.scm' by default.~%")
(display "
--help display this help and exit")
(newline)
(exit 1))
(define %options
(list (option '("help") #f #f
(lambda (opt name arg result)
(display-help-and-exit)))))
(define %default-options '())
(define (chickadee-bundle . args)
(let ((opts (simple-args-fold args %options %default-options)))
(match (operands opts)
(()
(make-bundle (primitive-load "bundle.scm")))
((file-name)
(make-bundle (primitive-load file-name)))
(_
(leave "too many arguments specified. just pass a Scheme file name.")))))
|