-
Notifications
You must be signed in to change notification settings - Fork 0
/
chicken-install.scm
1191 lines (1103 loc) · 47.3 KB
/
chicken-install.scm
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; chicken-install.scm
;
; Copyright (c) 2008-2021, The CHICKEN Team
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
; conditions are met:
;
; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
; disclaimer.
; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
; disclaimer in the documentation and/or other materials provided with the distribution.
; Neither the name of the author nor the names of its contributors may be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.
(declare
(uses chicken-ffi-syntax)) ; populate ##sys#chicken-ffi-macro-environment
(module main ()
(import (scheme))
(import (chicken base))
(import (chicken condition))
(import (chicken foreign))
(import (chicken keyword))
(import (chicken file))
(import (chicken file posix))
(import (chicken fixnum))
(import (chicken format))
(import (chicken irregex))
(import (chicken module))
(import (chicken tcp))
(import (chicken port))
(import (chicken platform))
(import (chicken internal))
(import (chicken io))
(import (chicken sort))
(import (chicken time))
(import (chicken pathname))
(import (chicken process))
(import (chicken process-context))
(import (chicken pretty-print))
(import (chicken string))
(define +defaults-version+ 2)
(define +module-db+ "modules.db")
(define +defaults-file+ "setup.defaults")
(define +short-options+ '(#\h #\k #\s #\r #\n #\u #\v))
(define +one-hour+ (* 60 60))
(define +internal-modules+ '(chicken.internal chicken.internal.syntax))
(include "mini-srfi-1.scm")
(include "egg-environment.scm")
(include "egg-information.scm")
(include "egg-compile.scm")
(include "egg-download.scm")
(define user-defaults #f)
(define quiet #t)
(define default-servers '())
(define default-locations '())
(define mappings '())
(define aliases '())
(define override '())
(define hacks '())
(define proxy-host #f)
(define proxy-port #f)
(define proxy-user-pass #f)
(define retrieve-only #f)
(define retrieve-recursive #f)
(define do-not-build #f)
(define no-install #f)
(define no-install-dependencies #f)
(define list-versions-only #f)
(define canonical-eggs '())
(define requested-eggs '())
(define dependencies '())
(define checked-eggs '())
(define run-tests #f)
(define force-install #f)
(define host-extension cross-chicken)
(define target-extension cross-chicken)
(define sudo-install #f)
(define sudo-program (or (get-environment-variable "SUDO") "sudo"))
(define update-module-db #f)
(define purge-mode #f)
(define keepfiles #f)
(define print-repository #f)
(define cached-only #f)
(define platform
(if (eq? (software-version) 'mingw32) 'windows 'unix))
(define current-status
(list ##sys#build-id default-prefix
(get-environment-variable "CSC_OPTIONS")
(get-environment-variable "LD_LIBRARY_PATH")
(get-environment-variable "DYLD_LIBRARY_PATH")
(get-environment-variable "CHICKEN_INCLUDE_PATH")
(get-environment-variable "DYLD_LIBRARY_PATH")))
(define (repo-path)
(if (and cross-chicken (not host-extension))
(##sys#split-path (destination-repository 'target))
(repository-path)))
(define (install-path)
(if (and cross-chicken (not host-extension))
(destination-repository 'target)
(destination-repository 'host)))
(define (build-script-extension mode platform)
(string-append "build"
(if (eq? mode 'target) ".target" "")
(if (eq? platform 'windows) ".bat" ".sh")))
(define (install-script-extension mode platform)
(string-append "install"
(if (eq? mode 'target) ".target" "")
(if (eq? platform 'windows) ".bat" ".sh")))
;;; validate egg-information tree
(define (egg-version? v)
(and (list? v)
(pair? v)
(null? (cdr v))
(let ((str (->string (car v))))
(irregex-match '(seq (+ numeric)
(? #\. (+ numeric)
(? #\. (+ numeric))))
str))))
(define (optname? x)
(and (list? x)
(or (null? x)
(string? (car x))
(symbol? (car x)))))
(define (nameprop? x)
(and (list? x) (or (symbol? (car x)) (string? (car x)))))
(define (name-or-predefd? x)
(or (optname? x)
(and (pair? x)
(pair? (car x))
(eq? 'predefined (caar x))
(optname? (cdar x)))))
;; ENTRY = (NAME TOPLEVEL? NESTED? NAMED? [VALIDATOR])
(define egg-info-items
`((synopsis #t #f #f)
(author #t #f #f)
(category #t #f #f)
(license #t #f #f)
(version #t #f #f ,egg-version?)
(dependencies #t #f #f ,list?)
(source-dependencies #f #f #f ,list?)
(component-dependencies #f #f #f ,list?)
(test-dependencies #t #f #f ,list?)
(build-dependencies #t #f #f ,list?)
(components #t #t #f)
(foreign-dependencies #t #f #f ,list?)
(platform #t #f #f)
(installed-files #t #f #f ,list?)
(maintainer #t #f #f)
(files #f #f #f ,list?)
(distribution-files #t #f #f ,list?) ;; handled by henrietta-cache
(source #f #f #f)
(csc-options #f #f #f)
(link-options #f #f #f)
(custom-build #f #f #f)
(linkage #f #f #f)
(objects #f #f #f)
(destination #f #f #f ,list?)
(install-name #f #f #f ,nameprop?)
(target #f #t #f)
(host #f #t #f)
(types-file #f #f #f ,name-or-predefd?)
(inline-file #f #f #f ,optname?)
(extension #f #t #t)
(c-object #f #t #t)
(generated-source-file #f #t #t)
(program #f #t #t)
(data #f #t #t)
(modules #f #f #f)
(component-options #t #f #f)
(cond-expand * #t #f)
(error * #f #f)
(c-include #f #f #t)
(scheme-include #f #f #t)))
(define (validate-egg-info info)
(define (validate info top?)
(for-each
(lambda (item)
(cond ((or (not (pair? item))
(not (list? item))
(not (symbol? (car item))))
(error "invalid egg information item" item))
((assq (car item) egg-info-items) =>
(lambda (a)
(apply (lambda (name toplevel nested named #!optional validator)
(cond ((and top?
(not (eq? toplevel '*))
(not toplevel))
(error "egg information item not allowed at toplevel"
item))
((and (not (eq? toplevel '*))
toplevel
(not top?))
(error "egg information item only allowed at toplevel" item))
((and named
(or (null? (cdr item))
(not (symbol? (cadr item)))))
(error "unnamed egg information item" item))
((and validator
(not (validator (cdr item))))
(error "egg information item has invalid structure" item)))
(when nested
(cond (named (validate (cddr item) #f))
((eq? name 'cond-expand)
(for-each
(lambda (clause)
(unless (and (list? clause)
(>= (length clause) 1))
(error "invalid syntax in `cond-expand' clause" clause))
(validate (cdr clause) top?))
(cdr item)))
(else (validate (cdr item) #f)))))
a)))
(else (error "unknown egg information item" item))))
info))
(validate info #t)
info)
;; utilities
;; Simpler replacement for SRFI-13's "string-suffix?"
(define (string-suffix? suffix s)
(let ((len-s (string-length s))
(len-suffix (string-length suffix)))
(and (not (< len-s len-suffix))
(string=? suffix
(substring s (- len-s len-suffix))))))
(define (d flag . args)
(let ((flag (and (not (string? flag)) flag))
(fstr (if (string? flag) flag (car args)))
(args (if (string? flag) args (cdr args))))
(when (or flag (not quiet))
(flush-output)
(let ((port (current-error-port)))
(apply fprintf port fstr args)
(flush-output port) ) )))
(define (version>=? v1 v2)
(define (version->list v)
(map (lambda (x) (or (string->number x) x))
(irregex-split "[-\\._]" (->string v))))
(let loop ((p1 (version->list v1))
(p2 (version->list v2)))
(cond ((null? p1) (null? p2))
((null? p2))
((number? (car p1))
(and (number? (car p2))
(or (> (car p1) (car p2))
(and (= (car p1) (car p2))
(loop (cdr p1) (cdr p2))))))
((number? (car p2)))
((string>? (car p1) (car p2)))
(else
(and (string=? (car p1) (car p2))
(loop (cdr p1) (cdr p2)))))))
;; load defaults file ("setup.defaults")
(define (load-defaults)
(let* ((cfg-dir (system-config-directory))
(user-file (and cfg-dir (make-pathname (list cfg-dir "chicken")
+defaults-file+)))
(deff (or user-defaults
(and user-file (file-exists? user-file))
(make-pathname host-sharedir +defaults-file+))))
(define (broken x)
(error "invalid entry in defaults file" deff x))
(cond ((not (file-exists? deff)) '())
(else
(for-each
(lambda (x)
(unless (and (list? x) (positive? (length x)))
(broken x))
(case (car x)
((version)
(cond ((not (pair? (cdr x))) (broken x))
((not (= (cadr x) +defaults-version+))
(error
(sprintf
"version of installed `~a' does not match chicken-install version (~a)"
+defaults-file+
+defaults-version+)
(cadr x)))
;; others are ignored
))
((server)
(set! default-servers
(append default-servers (cdr x))))
((map)
(set! mappings
(append
mappings
(map (lambda (m)
(let ((p (list-index (cut eq? '-> <>) m)))
(unless p (broken x))
(let-values (((from to) (split-at m p)))
(cons from (cdr to)))))
(cdr x)))))
((alias)
(set! aliases
(append
aliases
(map (lambda (a)
(if (and (list? a) (= 2 (length a)) (every string? a))
(cons (car a) (cadr a))
(broken x)))
(cdr x)))))
((override)
(set! override
(if (and (pair? (cdr x)) (string? (cadr x)))
(call-with-input-file (cadr x) read-list)
(cdr x))))
((location)
(set! default-locations
(append default-locations (cdr x))))
((hack)
(set! hacks (append hacks (list (eval (cadr x))))))
(else (broken x))))
(call-with-input-file deff read-list))))))
;; set variables with HTTP proxy information
(define (setup-proxy uri)
(and-let* (((string? uri))
(m (irregex-match "(http://)?([^:]+):?([0-9]*)" uri))
(port (irregex-match-substring m 3)))
(set! proxy-user-pass (get-environment-variable "proxy_auth"))
(set! proxy-host (irregex-match-substring m 2))
(set! proxy-port (or (string->number port) 80))))
;; apply egg->egg mappings loaded from defaults
(define (canonical x)
(cond ((symbol? x) (cons (symbol->string x) #f))
((string? x) (cons x #f))
((pair? x) x)
(else (error "internal error - bad egg spec" x))))
(define (apply-mappings eggs)
(define (same? e1 e2)
(equal? (car (canonical e1)) (car (canonical e2))))
(let ((eggs2
(delete-duplicates
(append-map
(lambda (egg)
(cond ((find (lambda (m) (find (cut same? egg <>) (car m)))
mappings) =>
(lambda (m) (map ->string (cdr m))))
(else (list egg))))
eggs)
same?)))
(unless (and (= (length eggs) (length eggs2))
(every (lambda (egg)
(find (cut same? <> egg) eggs2))
eggs))
(d "mapped ~s to ~s~%" eggs eggs2))
eggs2))
;; override versions, if specified in "overrides" file
(define (override-version egg)
(let ((name (string->symbol (if (pair? egg) (car egg) egg))))
(cond ((assq name override) =>
(lambda (a)
(if (and (pair? egg)
(pair? (cdr a))
(not (equal? (cadr a) (cdr egg))))
(warning
(sprintf
"version `~a' of extension `~a' overrides explicitly given version `~a'"
(cadr a) name (cdr egg)))
(d "overriding: ~a~%" a))
(if (null? (cdr a))
(and (pair? egg) (cdr egg))
(cadr a))))
((pair? egg) (cdr egg))
(else #f))))
;; "locate" egg: either perform HTTP download or copy from a file-system
;; location, also make sure it is up to date
(define (locate-egg name version)
(let* ((cached (make-pathname cache-directory name))
(now (current-seconds))
(status (make-pathname cached +status-file+))
(eggfile (make-pathname cached name +egg-extension+)))
(define (fetch lax)
(when (file-exists? cached)
(delete-directory cached #t))
(create-directory cached #t)
(fetch-egg-sources name version cached lax)
(with-output-to-file status (cut write current-status)))
(cond ((or (not (probe-dir cached))
(not (file-exists? eggfile)))
(d "~a not cached~%" name)
(when cached-only (error "extension not cached" name))
(fetch #f))
((and (file-exists? status)
(not (equal? current-status
(with-input-from-file status read))))
(d "status changed for ~a~%" name)
(cond (cached-only
(if force-install
(warning "cached egg does not match CHICKEN version" name)
(error "cached egg does not match CHICKEN version - use `-force' to install anyway" name)))
(else (fetch #f)))))
(let* ((info (validate-egg-info (load-egg-info eggfile)))
(vfile (make-pathname cached +version-file+))
(tfile (make-pathname cached +timestamp-file+))
(lversion (or (get-egg-property info 'version)
(and (file-exists? vfile)
(with-input-from-file vfile read)))))
(cond ((and (not cached-only)
(or (and (string? version)
(not (equal? version lversion)))
(and (or (not (file-exists? tfile))
(> (- now (with-input-from-file tfile read))
+one-hour+))
(not (check-remote-version name lversion cached)))))
(d "version of ~a out of date~%" name)
(fetch #t)
(let* ((info (validate-egg-info (load-egg-info eggfile))) ; new egg info (fetched)
(lversion (or (get-egg-property info 'version)
(and (file-exists? vfile)
(with-input-from-file vfile read)))))
(values cached lversion)))
(else (values cached version))))))
(define (resolve-location name)
(cond ((assoc name aliases) =>
(lambda (a)
(let ((new (cdr a)))
(d "resolving alias `~a' to: ~a~%" name new)
(resolve-location new))))
(else name)))
(define (fetch-egg-sources name version dest lax)
(print "fetching " name)
(let loop ((locs default-locations))
(cond ((null? locs)
(let ((tmpdir (create-temporary-directory)))
(let loop ((srvs (map resolve-location default-servers)))
(if (null? srvs)
(if lax
(print "no connection to server or egg not found remotely - will use cached version")
(begin
(delete-directory dest)
(delete-directory tmpdir)
(error "extension or version not found" name)))
(begin
(d "trying server ~a ...~%" (car srvs))
(receive (dir ver)
(try-download name (car srvs)
version: version
destination: tmpdir
tests: #t ;; Always fetch tests, otherwise cached eggs can't be tested later
proxy-host: proxy-host
proxy-port: proxy-port
proxy-user-pass: proxy-user-pass)
(cond (dir
(copy-egg-sources tmpdir dest)
(delete-directory tmpdir #t)
(when ver
(with-output-to-file
(make-pathname dest +version-file+)
(cut write ver)))
(with-output-to-file
(make-pathname dest +timestamp-file+)
(cut write (current-seconds))))
(else (loop (cdr srvs))))))))))
((probe-dir (make-pathname (car locs) name))
=> (lambda (dir)
(d "trying location ~a ...~%" dir)
(let* ((eggfile (make-pathname dir name +egg-extension+))
(info (validate-egg-info (load-egg-info eggfile)))
(rversion (get-egg-property info 'version)))
(if (or (not rversion)
(not version)
(version>=? rversion version))
(copy-egg-sources dir dest)
(loop (cdr locs))))))
(else (loop (cdr locs))))))
(define (copy-egg-sources from to)
;;XXX should probably be done manually, instead of calling tool
(let ((cmd (string-append
(copy-directory-command platform)
;; Don't quote the globbing character!
" " (make-pathname (qs* from platform #t) "*")
" " (qs* to platform #t))))
(d "~a~%" cmd)
(system+ cmd platform)))
(define (check-remote-version name lversion cached)
(let loop ((locs default-locations))
(cond ((null? locs)
(let loop ((srvs (map resolve-location default-servers)))
(and (pair? srvs)
(let ((versions (try-list-versions name (car srvs))))
(or (and versions
(every (cut version>=? lversion <>) versions))
(loop (cdr srvs)))))))
((probe-dir (make-pathname (car locs) name)) =>
(lambda (dir)
;; for locally available eggs, check set of files and
;; timestamps
(compare-trees dir cached)))
(else (loop (cdr locs))))))
(define (compare-trees there here)
(let walk ((there there)
(here here))
(let ((tfs (directory there))
(hfs (directory here)))
(every (lambda (f)
(and (member f hfs)
(let ((tf2 (string-append there "/" f))
(hf2 (string-append here "/" f)))
(and (<= (file-modification-time tf2)
(file-modification-time hf2))
(if (directory-exists? tf2)
(and (directory-exists? hf2)
(walk tf2 hf2))
(not (directory-exists? hf2)))))))
tfs))))
;; check installed eggs for already installed files
(define (matching-installed-files egg fnames)
(let ((eggs (glob (make-pathname (install-path) "*" +egg-info-extension+))))
(let loop ((eggs eggs) (same '()))
(cond ((null? eggs) same)
((string=? egg (pathname-file (car eggs)))
(loop (cdr eggs) same))
(else
(let* ((info (load-egg-info (car eggs)))
(files (assq 'installed-files info))
(mfiles (and files
(filter (lambda (fname)
(and (not (member fname same))
(member fname files)))
fnames))))
(loop (cdr eggs) (append (or mfiles '()) same))))))))
(define (check-installed-files name info)
(let ((bad (matching-installed-files name (cdr (assq 'installed-files info)))))
(unless (null? bad)
(flush-output)
(fprintf (current-error-port)
"\nthe extension `~a' will overwrite the following files:\n\n" name)
(for-each
(lambda (fname)
(fprintf (current-error-port) " ~a~%" fname))
bad)
(exit 1))))
;; retrieve eggs, recursively (if needed)
(define (retrieve-eggs eggs)
(for-each
(lambda (egg)
(cond ((assoc egg canonical-eggs) =>
(lambda (a)
;; push to front
(set! canonical-eggs (cons a (delete a canonical-eggs eq?)))))
(else
(let ((name (if (pair? egg) (car egg) egg))
(version (override-version egg)))
(let-values (((dir ver) (locate-egg name version)))
(when (or (not dir)
(null? (directory dir)))
(when dir (delete-directory dir))
(error "extension or version not found" name))
(d retrieve-only "~a located at ~a~%" egg dir)
(set! canonical-eggs
(cons (list name dir ver) canonical-eggs)))))))
eggs)
(when (or (not retrieve-only) retrieve-recursive)
(for-each
(lambda (e+d+v)
(unless (member (car e+d+v) checked-eggs)
(d "checking ~a ...~%" (car e+d+v))
(set! checked-eggs (cons (car e+d+v) checked-eggs))
(let* ((fname (make-pathname (cadr e+d+v) (car e+d+v) +egg-extension+))
(info (validate-egg-info (load-egg-info fname))))
(d "checking platform for `~a'~%" (car e+d+v))
(check-platform (car e+d+v) info)
(d "checking dependencies for `~a'~%" (car e+d+v))
(let-values (((missing upgrade)
(outdated-dependencies (car e+d+v) info)))
(set! missing (apply-mappings missing))
(set! dependencies
(cons (cons (car e+d+v)
(map (lambda (mu)
(if (pair? mu)
(car mu)
mu))
(append missing upgrade)))
dependencies))
(when (pair? missing)
(d " missing: ~a~%" (string-intersperse missing ", "))
(retrieve-eggs missing))
(when (and (pair? upgrade)
(or force-install
(replace-extension-question e+d+v upgrade)))
(let ((ueggs (unzip1 upgrade)))
(d " upgrade: ~a~%" (string-intersperse ueggs ", "))
;; XXX think about this...
#;(for-each
(lambda (e)
(d "removing previously installed extension `~a'" e)
(remove-extension e) )
ueggs)
(retrieve-eggs ueggs) ) ) ) ) ) )
canonical-eggs)))
(define (outdated-dependencies egg info)
(let ((ds (get-egg-dependencies info)))
(for-each (lambda (h) (set! ds (h egg ds))) hacks)
(let loop ((deps ds) (missing '()) (upgrade '()))
(if (null? deps)
(values (reverse missing) (reverse upgrade))
(let-values (((m u) (check-dependency (car deps))))
(loop (cdr deps)
(if m (cons m missing) missing)
(if u (cons u upgrade) upgrade)))))))
(define (get-egg-dependencies info)
(append (get-egg-property* info 'dependencies '())
(get-egg-property* info 'build-dependencies '())
(if run-tests
(get-egg-property* info 'test-dependencies '())
'())))
(define (check-dependency dep)
(cond ((or (symbol? dep) (string? dep))
(values (and (not (ext-version dep)) (->string dep))
#f))
((and (list? dep) (eq? 'or (car dep)))
(let scan ((ordeps (cdr dep)) (bestm #f) (bestu #f))
(if (null? ordeps)
(values (cond (bestu #f) ; upgrade overrides new
(bestm bestm)
(else #f))
bestu)
(let-values (((m u) (check-dependency (car ordeps))))
(if (and (not m) (not u))
(values #f #f)
(scan (cdr ordeps)
(if (and m (not bestm))
m
bestm)
(if (and u (not bestu))
u
bestu)))))))
((and (list? dep) (= 2 (length dep))
(or (string? (car dep)) (symbol? (car dep))))
(let ((v (ext-version (car dep))))
(cond ((not v)
(values (->string (car dep)) #f))
((not (version>=? v (->string (cadr dep))))
(cond ((string=? "chicken" (->string (car dep)))
(if force-install
(values #f #f)
(error
(string-append
"Your CHICKEN version is not recent enough to use this extension - version "
(cadr dep)
" or newer is required"))))
(else
(values #f
(cons (->string (car dep)) (->string (cadr dep)))))))
(else (values #f #f)))))
(else
(warning "invalid dependency syntax in extension meta information"
dep)
(values #f #f))))
(define (ext-version x)
(cond ((or (eq? x 'chicken) (equal? x "chicken"))
(chicken-version))
((let* ((sf (chicken.load#find-file
(make-pathname #f (->string x) +egg-info-extension+)
(repo-path))))
(and sf
(file-exists? sf)
(load-egg-info sf))) =>
(lambda (info)
(let ((a (assq 'version info)))
(if a
(->string (cadr a))
"0.0.0"))))
(else #f)))
(define (check-platform name info)
(unless cross-chicken
(and-let* ((platform (get-egg-property info 'platform)))
(or (let loop ((p platform))
(cond ((symbol? p)
(feature? p))
((not (list? p))
(error "invalid `platform' property" name platform))
((and (eq? 'not (car p)) (pair? (cdr p)))
(not (loop (cadr p))))
((eq? 'and (car p))
(every loop (cdr p)))
((eq? 'or (car p))
(any loop (cdr p)))
(else (error "invalid `platform' property" name platform))))
(error "extension is not targeted for this system" name)))))
(define (replace-extension-question e+d+v upgrade)
(print (string-intersperse
(append
(list "The following installed extensions are outdated, because `"
(car e+d+v)
"' requires later versions:\n\n")
(filter-map
(lambda (e)
(cond ((assq (string->symbol (car e)) override) =>
(lambda (a)
(when (and (pair? (cdr a))
(not (equal? (cadr a) (cdr e))))
(warning
(sprintf "version `~a' of extension `~a' overrides required version `~a'"
(cadr a) (car a) (cdr e))))
#f))
(else
(conc " " (car e) " ("
(or (ext-version (car e)) "unknown") " -> " (cdr e)
")" #\newline))))
upgrade))
""))
(let loop ()
(display "Do you want to replace the existing extensions? (yes/no/abort) ")
(flush-output)
(let ((r (trim (read-line))))
(cond ((string=? r "yes"))
((string=? r "no") #f)
((string=? r "abort") (exit 2))
(else (loop))))))
(define (trim str)
(define (left lst)
(cond ((null? lst) '())
((char-whitespace? (car lst)) (left (cdr lst)))
(else (cons (car lst) (left (cdr lst))))))
(list->string (reverse (left (reverse (left (string->list str)))))))
;; list available egg versions on servers
(define (list-egg-versions eggs)
(let ((srvs (map resolve-location default-servers)))
(let loop1 ((eggs eggs))
(unless (null? eggs)
(let* ((egg (car eggs))
(name (if (pair? egg) (car egg) egg)))
(let loop2 ((srvs srvs))
(and (pair? srvs)
(let ((versions (try-list-versions name (car srvs))))
(or (and versions
(begin
(printf "~a:" name)
(for-each (cut printf " ~a" <>) versions)
(newline)))
(loop2 (cdr srvs))))))
(loop1 (cdr eggs)))))))
;; perform installation of retrieved eggs
(define (install-eggs)
(for-each
(lambda (egg)
(let* ((name (car egg))
(dir (cadr egg))
(eggfile (make-pathname dir name +egg-extension+))
(info (load-egg-info eggfile))
(vfile (make-pathname dir +version-file+))
(ver (and (file-exists? vfile)
(with-input-from-file vfile read))))
(when (or host-extension
(and (not target-extension)
(not host-extension)))
(let-values (((build install info) (compile-egg-info eggfile
info
ver
platform
'host)))
(let ((bscript (make-pathname dir name
(build-script-extension 'host platform)))
(iscript (make-pathname dir name
(install-script-extension 'host
platform))))
(generate-shell-commands platform build bscript dir
(build-prefix 'host name info)
(build-suffix 'host name info)
keepfiles)
(generate-shell-commands platform install iscript dir
(install-prefix 'host name info)
(install-suffix 'host name info)
keepfiles)
(cond (do-not-build (print bscript "\n" iscript))
(else
(print "building " name)
(run-script dir bscript platform)
(unless (if (member name requested-eggs) no-install no-install-dependencies)
(check-installed-files name info)
(print "installing " name)
(run-script dir iscript platform sudo: sudo-install))
(when (and (member name requested-eggs)
run-tests
(not (test-egg egg platform)))
(exit 2)))))))
(when target-extension
(let-values (((build install info) (compile-egg-info eggfile
info
ver
platform
'target)))
(let ((bscript (make-pathname dir name
(build-script-extension 'target platform)))
(iscript (make-pathname dir name
(install-script-extension 'target
platform))))
(generate-shell-commands platform build bscript dir
(build-prefix 'target name info)
(build-suffix 'target name info)
keepfiles)
(generate-shell-commands platform install iscript dir
(install-prefix 'target name info)
(install-suffix 'target name info)
keepfiles)
(cond (do-not-build (print bscript "\n" iscript))
(else
(print "building " name " (target)")
(run-script dir bscript platform)
(unless (if (member name requested-eggs) no-install no-install-dependencies)
(print "installing " name " (target)")
(run-script dir iscript platform)))))))))
(order-installed-eggs)))
(define (order-installed-eggs)
(let* ((dag (reverse (sort-dependencies dependencies string=?)))
(ordered (filter-map (cut assoc <> canonical-eggs) dag)))
(unless quiet
(d "install order:~%")
(pp dag))
ordered))
(define (test-egg egg platform)
(let* ((name (car egg))
(dir (cadr egg))
(version (caddr egg))
(testdir (make-pathname dir "tests"))
(tscript (make-pathname testdir "run.scm")))
(if (and (directory-exists? testdir)
(file-exists? tscript))
(let ((old (current-directory))
(cmd (string-append (qs* default-csi platform)
" -s " (qs* tscript platform)
" " (qs* name platform)
" " (or version ""))))
(change-directory testdir)
(d "running: ~a~%" cmd)
(let ((r (system+ cmd platform)))
(flush-output (current-error-port))
(cond ((zero? r)
(change-directory old)
#t)
(else
(print "test script failed with nonzero exit status")
#f))))
#t)))
(define (run-script dir script platform #!key sudo (stop #t))
(d "running script ~a~%" script)
(exec (if (eq? platform 'windows)
script
(string-append
(if sudo
(string-append sudo-program " ")
"")
(let ((dyld (and (eq? (software-version) 'macosx)
(get-environment-variable "DYLD_LIBRARY_PATH"))))
(if dyld
(string-append "/usr/bin/env DYLD_LIBRARY_PATH="
(qs* dyld platform)
" ")
""))
"sh " script))
stop))
(define (exec cmd #!optional (stop #t))
(d "executing: ~s~%" cmd)
(let ((r (system+ cmd platform)))
(unless (zero? r)
(if stop
(error "shell command terminated with nonzero exit code" r cmd)
(print "shell command terminated with nonzero exit code " r ": " cmd)))
r))
;;; update module-db
(define (update-db)
(let* ((files (glob (make-pathname (install-path) "*.import.so")
(make-pathname (install-path) "*.import.scm")))
(dbfile (create-temporary-file)))
(print "loading import libraries ...")
(fluid-let ((##sys#warnings-enabled #f))
(for-each
(lambda (path)
(let* ((file (pathname-strip-directory path))
(import-name (pathname-strip-extension file))
(module-name (pathname-strip-extension import-name)))
(handle-exceptions ex
(print-error-message
ex (current-error-port)
(sprintf "Failed to import from `~a'" file))
(unless quiet (print "loading " file " ..."))
(eval `(import-syntax ,(string->symbol module-name))))))
files))
(print "generating database ...")
(let ((db
(sort
(concatenate
(filter-map
(lambda (m)
(and-let* ((mod (cdr m))
(mname (##sys#module-name mod))
((not (memq mname +internal-modules+)))
((not (eq? mname (current-module)))))
(unless quiet (print "processing " mname " ..."))
(let-values (((_ ve se) (##sys#module-exports mod)))
(append (map (lambda (se) (list (car se) 'syntax mname)) se)
(map (lambda (ve) (list (car ve) 'value mname)) ve)))))
##sys#module-table))
(lambda (e1 e2)
(string<? (symbol->string (car e1)) (symbol->string (car e2)))))))
(with-output-to-file dbfile
(lambda ()
(for-each (lambda (x) (write x) (newline)) db)))
(unless quiet (print "installing " +module-db+ " ..."))
(copy-file dbfile (make-pathname (install-path) +module-db+) #t)
(delete-file dbfile))))
;; purge cache for given (or all) eggs
(define (purge-cache eggs)
(cond ((null? eggs)
(when (file-exists? cache-directory)
(d "purging complete cache at ~a~%" cache-directory)
(delete-directory cache-directory #t)))
(else
(for-each
(lambda (egg)
(let* ((name (if (pair? egg) (car egg) egg))
(dname (make-pathname cache-directory name)))
(when (file-exists? dname)