-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameterization.patch
1324 lines (1321 loc) · 51.4 KB
/
parameterization.patch
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
diff --git a/guix/parameters.scm b/guix/parameters.scm
new file mode 100644
index 0000000000..f424d33588
--- /dev/null
+++ b/guix/parameters.scm
@@ -0,0 +1,1196 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2020 Ludovic Courtès <[email protected]>
+;;; Copyright © 2023 Sarthak Shah <[email protected]>
+;;;
+;;; 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 parameters)
+ #:use-module (guix diagnostics)
+ #:use-module (guix i18n)
+ #:use-module (guix packages)
+ #:use-module (guix profiles)
+ #:use-module (guix records)
+ #:use-module (guix transformations)
+ #:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-13)
+ #:use-module (srfi srfi-26)
+ #:use-module (srfi srfi-34)
+ #:use-module (srfi srfi-35)
+ #:use-module (ice-9 hash-table)
+ #:use-module (ice-9 match)
+ #:use-module (ice-9 receive)
+ ;? -> remove?
+ #:export (parameter-type;
+ package-parameter;
+ parameter-spec;
+ boolean-parameter-type;
+
+ parameter-variant;
+ parameter-variant-match;
+ parameter-spec-property;?
+ package-parameter-spec;
+ package-parameter-alist;
+ all-spec-parameters;
+ all-spec-parameters-with-types;
+ base-parameter-alist;
+ parameter-process-list;
+ package-override-plist;?
+ parameter-spec-validate;
+ package-resolve-parameter-list;
+ %global-parameters;
+ define-global-parameter;
+
+ package-with-parameters;
+ parameterize-package;
+ apply-variants;
+ parameter-spec-parameter-alist;
+ parameter-if;
+ parameter-if-all;?
+ parameter-match-any;?
+ parameter-match-all;?
+ parameter-match-case-all;?
+ parameter-match;
+ parameter-match-case;
+ parameter-modify-inputs;
+ ))
+
+;;; Commentary:
+;;;
+;;; This module provides a way to express high-level "package parameters",
+;;; which allow users to customize how packages are built. Parameters are an
+;;; interface that package developers define, where each parameter has a name
+;;; and type. The user interface then converts parameter values from string
+;;; to Scheme values and records them in the package properties.
+;;;
+;;; Package parameters are discoverable; their description is
+;;; internationalized. The possible values of a parameter can be enumerated,
+;;; and thus the Cartesian product of all possible parameter values for a
+;;; package can be enumerated as well.
+;;;
+;;; Code:
+
+(define (give-me-a-symbol ex)
+ "Take a string or symbol EX and return a symbol."
+ (cond ((symbol? ex) ex)
+ ((string? ex) (string->symbol ex))
+ (else (raise (formatted-message
+ (G_ "Not a symbol or a string: ~s")
+ ex)))))
+
+(define-record-type* <parameter-type> parameter-type
+ make-parameter-type
+ parameter-type?
+ this-parameter-type
+ (name parameter-type-name
+ (sanitize give-me-a-symbol))
+ (accepted-values parameter-type-accepted-values)
+ (negation parameter-type-negation
+ (default (car (parameter-type-accepted-values this-parameter-type)))
+ (thunked))
+ (default parameter-type-default
+ (default (match (parameter-type-accepted-values this-parameter-type)
+ [(first second . rest)
+ (if (not (parameter-type-negation this-parameter-type))
+ first
+ second)]
+ [oth (raise (formatted-message
+ (G_ "Bad accepted-values form: ~s")
+ oth))]))
+ (thunked))
+ (description parameter-type-description
+ (default "")))
+
+(define boolean-parameter-type
+ (parameter-type
+ (name 'boolean)
+ (accepted-values '(off on))
+ (description "Boolean Parameter Type")))
+
+;; Package parameter interface.
+(define-record-type* <package-parameter> package-parameter
+ make-package-parameter
+ package-parameter?
+ (name package-parameter-name
+ (sanitize give-me-a-symbol))
+ (type package-parameter-type
+ (default boolean-parameter-type))
+ (variants package-parameter-variants
+ (default '())
+ (sanitize sanitize-parametric-variants))
+ (dependencies package-parameter-dependencies
+ (default '())
+ (sanitize dependency-sanitizer)
+ (thunked))
+ (predicate package-parameter-predicate
+ (sanitize predicate-sanitizer)
+ (default (const #f)))
+ (description package-parameter-description (default "")))
+
+(define %global-parameters
+ (alist->hash-table '()))
+
+;; SANITIZERS
+
+(define (sanitize-parametric-variants ls)
+ "Raise an error if LS is not a list."
+ (cond ((list? ls) ls)
+ (else (raise (formatted-message
+ (G_ "Not a list: ~s")
+ ls)))))
+
+(define (predicate-sanitizer p)
+ (match p
+ [(? procedure? p) p]
+ [#t (and (warning
+ (G_ "Please use (const #t) instead of #t!~%"))
+ (const #t))]
+ [#f (and (warning
+ (G_ "Please use (const #f) instead of #f!~%"))
+ (const #f))]
+ [_ (raise (formatted-message
+ (G_ "Not a predicate: ~s")
+ p))]))
+
+
+;; % USEFUL HELPER FUNCTIONS %
+
+(define (return-list lst)
+ "Take a value LST, return LST if it a list and (list LST) otherwise."
+ (if (list? lst)
+ lst
+ (list lst)))
+
+(define (append-everything . things)
+ "Take a number of THINGS, and append them all."
+ (apply append
+ (map return-list things)))
+
+(define (get-parameter-sym psym)
+ "If the argument is a cons cell, return the CAR otherwise return the argument."
+ (match psym
+ [(a . b) a]
+ [a a]))
+
+(define* (merge-same-car lst #:optional (carry '()))
+ "Merge the cells of LST with the same value in their CAR."
+ (match lst
+ [((a . b) . rest)
+ (if (null? (filter (lambda (y) (equal? a (car y)))
+ carry))
+ (merge-same-car rest (cons (cons a b) carry))
+ (merge-same-car rest (assq-set! carry
+ a
+ (append (assq-ref carry a) b))))]
+ [() carry]))
+
+(define-syntax lambdize-lambdas
+ (syntax-rules (:cruise)
+ [(% :cruise x . rest)
+ (if (keyword? x)
+ (cons 'x (lambdize-lambdas . rest))
+ (cons x (lambdize-lambdas :cruise . rest)))]
+ [(% :cruise) '()]
+ [(% #:lambda fn . rest)
+ (cons #:lambda
+ (cons fn
+ (lambdize-lambdas :cruise . rest)))]
+ [(% x . rest)
+ (cons 'x (lambdize-lambdas . rest))]
+ [(%) '()]))
+
+(define-syntax parameter-variant
+ (syntax-rules ()
+ [(%) '()]
+ [(% psym variants ...)
+ (let ((parsed-variants
+ (parse-kw-list (lambdize-lambdas variants ...))))
+ (map (cut cons <>
+ parsed-variants)
+ (return-list 'psym)))]))
+
+(define* (parse-kw-list kw-lst)
+ "Parses a list of keywords, KW-LST and returns an alist."
+ (define (list-till-kw lst)
+ (receive (a b)
+ (break keyword? lst)
+ (cons a b)))
+ (define* (break-keywords lst)
+ (match lst
+ [((? keyword? key) vals ..1)
+ (match (list-till-kw vals)
+ [(first . rest)
+ (cons (cons (keyword->symbol key)
+ first)
+ (break-keywords rest))])]
+ [((? keyword? just-a-key)) ; (... #:key)
+ (cons (cons (keyword->symbol just-a-key) '())
+ '())]
+ [(singleton) '()]
+ [() '()]
+ [_ (raise (formatted-message
+ (G_ "Error trying to break keywords at ~a")
+ lst))]))
+ (merge-same-car (break-keywords kw-lst)))
+
+;; The lock here is used to signal when merge-same-car is to be used
+;; having a :lock means merge-same-car has been used further up the tree
+;; note that :lock is not a keyword but a symbol, as we are using keywords elsewhere
+(define-syntax parameter-variant-match
+ (syntax-rules (:lock)
+ ((% :lock (x ...))
+ (return-list
+ (parameter-variant x ...)))
+ ((% :lock (x ...) rest ...)
+ (append
+ (return-list (parameter-variant x ...))
+ (parameter-variant-match :lock rest ...)))
+ ((% rest ...)
+ (map
+ (lambda (ls) (cons (car ls)
+ (merge-same-car (cdr ls))))
+ (merge-same-car
+ (parameter-variant-match :lock rest ...))))))
+
+(define (local-sanitizer ls)
+ "Sanitize a list of local parameters, LS."
+ (if (list? ls)
+ (map (lambda (val)
+ (cond ((package-parameter? val) val)
+ ((symbol? val) (package-parameter (name val)))
+ ((string? val) (package-parameter (name (string->symbol val))))
+ (else (raise (formatted-message
+ (G_ "Not a parameter, symbol or string: ~s")
+ val)))))
+ ls)
+ (raise (formatted-message
+ (G_ "Spec's local field is not a list: ~s")
+ ls))))
+
+(define* (variant-sanitizer lv)
+ "Sanitize a list of variants."
+ ;; #:yes -> use default variant
+ ;; #:no -> don't use variant
+ ;; #:special -> use variant in cdr
+ (define (sym->parameter psym)
+ "Take a symbol PSYM and return the corresponding parameter."
+ (or (find (lambda (g) (eqv? psym
+ (package-parameter-name g)))
+ lv)
+ (hash-ref %global-parameters psym)
+ (raise (formatted-message
+ (G_ "sym->parameter: not a symbol: ~s")
+ psym))))
+ (define-macro (assq-ov! asslst key val)
+ `(set! ,asslst
+ (assq-set! ,asslst ,key ,val)))
+ (lambda (ls)
+ (let ((triad (parse-kw-list ls)))
+ (if (find (lambda (g) (not (or (eqv? (car g) 'yes)
+ (eqv? (car g) 'no)
+ (eqv? (car g) 'special))))
+ triad)
+ (raise (formatted-message
+ (G_ "Invalid keyword in use-variants: ~s")
+ (car g))))
+ (let ((vars-lst '()))
+ (map
+ (match-lambda
+ [('yes rest ...)
+ (map
+ (lambda (p)
+ (if (not (symbol? p))
+ (raise (formatted-message
+ (G_ "Not a symbol: ~s")
+ p))
+ (assq-ov! vars-lst p #:yes)))
+ rest)]
+ [('no rest ...)
+ (map
+ (lambda (p)
+ (if (not (symbol? p))
+ (raise (formatted-message
+ (G_ "Not a symbol: ~s")
+ p))
+ (assq-ov! vars-lst p #:no)))
+ rest)]
+ [('special rest ...)
+ (map
+ (lambda (x)
+ (assq-ov! vars-lst
+ (car x)
+ (cdr x)))
+ rest)]
+ [_ (error "wrongly formatted use-variant!")])
+ triad)
+ (map
+ (lambda (x)
+ (match (assq-ref vars-lst (package-parameter-name x))
+ [#f (assq-ov! vars-lst
+ (package-parameter-name x)
+ (package-parameter-variants x))]
+ [#:yes (assq-ov! vars-lst
+ (package-parameter-name x)
+ (package-parameter-variants x))]
+ [#:no #f] ; do nothing
+ [varn (assq-ov! vars-lst
+ (package-parameter-name x)
+ varn)]))
+ lv)
+ vars-lst))))
+
+(define (dependency-sanitizer deps)
+ "Sanitize the dependency-list of a package-parameter."
+ (unless (eqv? deps '())
+ (if (not (list? deps))
+ (raise (formatted-message
+ (G_ "Dependencies not a list: ~s")
+ deps)))
+ (if (keyword? (car deps))
+ (if (match (car deps)
+ [#:package (and (warning
+ (G_ "Package Dependencies are not supported!~%"))
+ #t)]
+ [#:parameter #t]
+ [_ #f])
+ (parse-kw-list deps)
+ (raise (formatted-message
+ (G_ "Bad dependency keyword: ~s")
+ (car deps))))
+ (dependency-sanitizer (cons #:parameter deps)))))
+
+(define-record-type* <parameter-spec> parameter-spec
+ make-parameter-spec
+ parameter-spec?
+ this-parameter-spec
+ (local parameter-spec-local
+ (default '())
+ (sanitize local-sanitizer)
+ (thunked))
+ (defaults parameter-spec-defaults
+ (default '())
+ (thunked))
+ (required parameter-spec-required
+ (default '())
+ (thunked))
+ (optional parameter-spec-optional
+ (default '())
+ (thunked))
+ (one-of parameter-spec-one-of
+ (default '())
+ (thunked))
+ (combinations-with-substitutes
+ parameter-spec-combinations-with-substitutes
+ (default parameter-spec-defaults)
+ (thunked))
+ (use-variants parameter-spec-use-variants
+ (default '())
+ (sanitize (variant-sanitizer
+ (parameter-spec-local this-parameter-spec)))
+ (thunked))
+ (parameter-alist parameter-spec-parameter-alist
+ (default (base-parameter-alist this-parameter-spec))
+ (thunked)))
+
+(define-syntax parameter-spec-property
+ (syntax-rules ()
+ [(parameter-spec-property body ...)
+ (cons 'parameter-spec
+ (parameter-spec body ...))]))
+
+(define (apply-variants pkg vars)
+ "Apply a list of variants, VARS to the given package PKG."
+ (define (exact-sub v)
+ (match v
+ [(lst ...) ; to traverse the tree
+ (map exact-sub v)]
+ [#:package-name
+ (package-name pkg)]
+ [#:package
+ pkg]
+ [#:parameter-value
+ (cdar vars)]
+ [x x]))
+ ;; substitute keywords - transforms
+ (define* (sub-kw-t in #:optional (ret '()))
+ (if (null? in)
+ (match (reverse ret)
+ [(a . rest)
+ (cons a (string-join rest "="))])
+ (sub-kw-t
+ (cdr in)
+ (cons
+ (exact-sub (car in))
+ ret))))
+ ;; substitute keywords
+ (define* (sub-kw in #:optional (ret '()))
+ (if (null? in)
+ (reverse ret)
+ (sub-kw
+ (cdr in)
+ (cons
+ (exact-sub (car in))
+ ret))))
+
+ (match vars
+ [(pcell (option optargs ...) . rest)
+ (match option
+ ['build-system
+ ;; halt execution if it does not match
+ (if (member (package-build-system the-package)
+ optargs) ; will be a list of build systems
+ (apply-variants pkg (cons pcell
+ rest))
+ pkg)]
+ ['transform
+ (apply-variants
+ ((options->transformation
+ (map sub-kw-t optargs))
+ pkg)
+ (cons pcell
+ rest))]
+ ['lambda
+ (apply-variants
+ ;; eval should normally be avoided
+ ;; but `lambda` as is defined evaluates
+ ;; code after substituting in keywords
+ ;; (primitive-eval (sub-kw (car optargs)))
+ (fold
+ (lambda (fn pack)
+ (case (car (procedure-minimum-arity fn))
+ [(0) (fn)]
+ [(1) (fn pack)]
+ [(2) (fn pack (cdr pcell))]
+ [else (raise (formatted-message
+ (G_ "Procedure ~s has invalid arity.")
+ fn))]))
+ pkg
+ optargs)
+ (cons pcell
+ rest))]
+ [oth
+ (raise (formatted-message
+ (G_ "Invalid Option: ")
+ oth))])]
+ [(pcell (option) . rest)
+ (apply-variants pkg (cons pcell rest))]
+ [(pcell) pkg]
+ [_ (raise (formatted-message
+ (G_ "Poorly formatted variant spec: ~s")
+ vars))]))
+
+(define-syntax package-with-parameters
+ (syntax-rules ()
+ [(% spec body ...)
+ (let* [(the-package-0 (package body ...))
+ (the-package (package
+ (inherit the-package-0)
+ (replacement (package-replacement the-package-0))
+ (location (package-location the-package-0))
+ (properties
+ (cons (cons 'parameter-spec
+ spec)
+ (package-properties the-package-0)))))]
+ (parameterize-package the-package
+ (parameter-spec-parameter-alist spec)
+ #:force-parameterization? #t))]))
+
+(define* (parameterize-package the-initial-package
+ the-initial-list
+ #:key (force-parameterization? #f))
+ "Evaluates THE-INITIAL-PACKAGE with the parameter-list THE-INITIAL-LIST."
+ (define-macro (assq-ov! asslst key val)
+ `(set! ,asslst
+ (assq-set! ,asslst ,key ,val)))
+
+ (define smoothen
+ (match-lambda
+ [(a . #:off)
+ (cons a
+ (parameter-type-negation
+ (package-parameter-type (parameter-spec-get-parameter spec a))))]
+ [(a . #:default)
+ (cons a
+ (parameter-type-default
+ (package-parameter-type (parameter-spec-get-parameter spec a))))]
+ [cell cell]))
+
+ (let* [(the-initial-spec
+ (package-parameter-spec the-initial-package))
+ (the-original-parameter-list
+ (package-parameter-alist the-initial-package))
+ (the-parameter-list
+ (package-resolve-parameter-list the-initial-package
+ the-initial-list))]
+ ;; exit and return the same package if no impactful changes
+ (if (and (not force-parameterization?)
+ (null? (filter (lambda (x)
+ (not (eqv? (assq-ref the-original-parameter-list
+ (car x))
+ (cdr x))))
+ the-parameter-list)))
+ the-initial-package
+ (let* [(the-spec ; this value gets called very often
+ (parameter-spec
+ (inherit the-initial-spec)
+ (parameter-alist
+ the-parameter-list)))
+ (the-package
+ (package
+ (inherit the-initial-package)
+ (replacement (package-replacement the-initial-package))
+ (location (package-location the-initial-package))
+ (properties (assq-set! (package-properties the-initial-package)
+ 'parameter-spec
+ the-spec))))
+ (the-variants
+ ;; first get list of normal variants (local, etc)
+ ;; then match over use-variants
+ ;; if cdr #:yes, check the-parameter-list for val
+ ;; if cdr #:no, purge from prev list
+ ;; if cdr #:special, /replace/ value
+ (let ((var-lst (parameter-spec-use-variants the-spec)))
+ (map (lambda (x)
+ (set! var-lst
+ (assq-set! var-lst
+ (car x)
+ (package-parameter-variants
+ (parameter-spec-get-parameter the-spec (car x))))))
+ (filter (lambda (x)
+ ((package-parameter-predicate
+ (parameter-spec-get-parameter
+ the-spec
+ (car x)))
+ the-package))
+ (filter
+ (lambda (x)
+ (not (assq-ref var-lst (car x)))) ; not in the variant-lst?
+ the-parameter-list)))
+ (map
+ (lambda (x)
+ (match (cdr x)
+ [#:yes (assq-ov! var-lst
+ (car x)
+ (package-parameter-variants
+ (parameter-spec-get-parameter the-spec (car x))))]
+ [#:no (set! var-lst
+ (assq-remove! var-lst
+ (car x)))]
+ [_ #f]))
+ var-lst)
+
+ var-lst))
+ (applicable-variants
+ (map (lambda (y)
+ (cons (cons (car y)
+ (assq-ref the-parameter-list (car y)))
+ (apply append
+ (map (lambda (x)
+ (return-list (cdr x)))
+ (cdr y)))))
+ ;; does it have values?
+ (filter (lambda (x) (not (null? (cdr x))))
+ (map ;; get list of applicable values
+ (lambda (x)
+ (let ((absv (assq-ref the-parameter-list (car x)))
+ ;; if absv is -ve, only -ve values allowed
+ ;; if absv is +ve, only +ve and _ allowed
+ (negv (parameter-type-negation
+ (package-parameter-type
+ (parameter-spec-get-parameter the-spec (car x)))))
+ (defv (parameter-type-default
+ (package-parameter-type
+ (parameter-spec-get-parameter the-spec (car x))))))
+ (cons (car x)
+ (filter
+ (lambda (ls)
+ (match (car ls)
+ ['_ (not (eqv? absv negv))]
+ [#:off (eqv? absv negv)]
+ [#:default (eqv? absv defv)]
+ [oth (eqv? absv oth)]))
+ (cdr x)))))
+ (filter (lambda (x) (assq-ref the-parameter-list (car x)))
+ the-variants)))))]
+ (fold (lambda (vlst pack)
+ (apply-variants pack vlst))
+ the-package
+ applicable-variants)))))
+
+(define (package-parameter-spec package)
+ "Takes a package PACKAGE and returns its parameter-spec."
+ (or (assq-ref (package-properties package) 'parameter-spec)
+ (parameter-spec))) ; returns empty spec
+
+(define (package-parameter-alist package)
+ "Takes a package PACKAGE and returns its parameter-list."
+ (parameter-spec-parameter-alist
+ (package-parameter-spec package)))
+
+;;; PROCESSING PIPELINE
+
+;; Convention:
+;; Works on Parameters? -> parameter-spec/fun
+;; Works on Parameter-Spec? -> parameter-spec/fun
+(define (parameter-spec-get-parameter pspec pcons)
+ "Takes a parameter cell PCONS and returns the corresponding package-parameter."
+ (let ((psym (get-parameter-sym pcons)))
+ (or (find (lambda (x)
+ (eqv? psym
+ (package-parameter-name x)))
+ (parameter-spec-local pspec))
+ (hash-ref %global-parameters psym)
+ (raise (formatted-message
+ (G_ "Parameter not found: ~s")
+ psym)))))
+
+(define (parameter-spec-negation-supported? pspec x)
+ "Is negation supported for the given parameter X?"
+ (let ((negv
+ (parameter-type-negation (package-parameter-type (parameter-spec-get-parameter pspec x)))))
+ (if negv
+ negv
+ '_)))
+
+(define (get-spec-deps pspec psym)
+ "Get the dependencies of the corresponding parameter to a given parameter symbol, PSYM."
+ (let ([p (parameter-spec-get-parameter pspec psym)])
+ (return-list
+ (assq-ref (package-parameter-dependencies p)
+ 'parameter))))
+
+;; 1. Fetching
+
+(define (base-parameter-alist pspec) ; returns base case
+ "Returns the BASE-PARAMETER-ALIST for a given parameter-spec PSPEC."
+ ;; '((a . psym) (b . #f) ...)
+ (let* ((v1 (parameter-process-list ; returns funneled list
+ (append-everything
+ (parameter-spec-defaults pspec)
+ (parameter-spec-required pspec))))
+ (v2 (parameter-process-list
+ (append-everything
+ (apply append
+ ;; XXX: change to a filter-map
+ (filter (cut car <>)
+ (map (cut get-spec-deps pspec <>)
+ (return-list v1))))
+ v1))))
+ ;; funnel will signal duplication err
+ ;; check if base case is valid
+ (parameter-spec-validate pspec v2)
+ v2))
+
+;; 2. Processing
+
+;; IMPORTANT CHANGE: Symbolic Negation no longer supported (psym!)
+(define (parameter-process-list lst)
+ "Processes and formats a list of parameters, LST."
+ (define (return-cell p)
+ (match p
+ [(a b) (cons a b)]
+ [(a . b) p]
+ [a (cons a '_)]))
+ (define (funnel plst)
+ (define* (group-val lst #:optional (carry '()))
+ (match lst
+ [((a . b) . rest)
+ (let ((v (assq-ref carry a)))
+ (group-val rest
+ (assq-set! carry
+ a
+ (cons b
+ (if v v '())))))]
+ [() carry]
+ [_ (raise (formatted-message
+ (G_ "Poorly formatted assoc-list in group-val! ~s")
+ lst))]))
+ (define (figure-out psym p)
+ (or (and (< (length p) 3)
+ (or (and (eq? (length p) 1) (car p))
+ (and (member '_ p)
+ (car (delq '_ p)))))
+ (raise (formatted-message
+ (G_ "Too many values for a single parameter: ~s with ~s")
+ psym p))))
+ (map (lambda (x) (cons (car x)
+ (figure-out (car x) ; for the error message
+ (delete-duplicates (cdr x)))))
+ (group-val plst)))
+ (funnel (map
+ return-cell
+ lst)))
+
+;; 3. Overriding
+
+(define (all-spec-parameters pspec) ; for the UI
+ "Returns all the parameters in a parameter-spec, PSPEC."
+ ;; '(sym-a sym-b ...)
+ (delete-duplicates
+ (map get-parameter-sym ; we do not care about the values
+ (append-everything ; works same as before
+ (map package-parameter-name
+ (parameter-spec-local pspec))
+ (parameter-spec-defaults pspec)
+ (parameter-spec-required pspec)
+ ;; We are NOT pulling dependencies at this phase
+ ;; They will not be influenced by the user parameter alist
+ (filter (lambda (x) (not (eqv? x '_)))
+ (apply append (parameter-spec-one-of pspec)))
+ (parameter-spec-optional pspec)))))
+
+(define* (all-spec-parameters-with-types pspec #:key (show-booleans? #t))
+ (if show-booleans?
+ (map (lambda (x)
+ (string-append
+ (symbol->string x)
+ ":"
+ (symbol->string
+ (parameter-type-name
+ (package-parameter-type (parameter-spec-get-parameter pspec (cons x #f)))))))
+ (all-spec-parameters pspec))
+ (map (lambda (x)
+ (string-append
+ (symbol->string x)
+ ((lambda (x)
+ (if (eqv? x 'boolean)
+ ""
+ (string-append ":" (symbol->string x))))
+ (parameter-type-name
+ (package-parameter-type (parameter-spec-get-parameter pspec (cons x #f)))))))
+ (all-spec-parameters pspec))))
+
+
+;; Now we compare it against the PLIST
+;; NOTE: This is the only instance where GLOBAL PARAMETERS may be used
+;; Since referring to the package is not possible, we pass it instead of pspec
+(define (package-override-plist pkg plist)
+ "Takes a package PKG and parameter-list PLIST and overrides PLIST according to the package."
+ (let* ((pspec (package-parameter-spec pkg))
+ (all-p (all-spec-parameters pspec))
+ (filtered-plist (filter (lambda (x) (or (member (car x) all-p)
+ (and (hash-ref %global-parameters (car x))
+ ((package-parameter-predicate
+ (hash-ref %global-parameters (car x)))
+ pkg))))
+ (parameter-process-list plist)))
+ (filtered-car (map car filtered-plist))
+ (remaining-p (filter (lambda (x) (not (member x filtered-car)))
+ all-p)))
+ (append-everything filtered-plist
+ (map (lambda (x) (if (parameter-spec-negation-supported? pspec x)
+ (cons x #:off)
+ (cons x '_)))
+ remaining-p))))
+
+;; 4. Funneling
+
+(define (override-spec-multi-match pspec plst)
+ "Overrides various keyword values in the parameter-list PLST."
+ (map
+ (match-lambda
+ [(a . '_)
+ (cons a
+ (cadr (parameter-type-accepted-values (package-parameter-type (parameter-spec-get-parameter pspec a)))))]
+ [(a . #:off)
+ (cons a
+ (parameter-type-negation (package-parameter-type (parameter-spec-get-parameter pspec a))))]
+ [(a . #:default)
+ (cons a
+ (parameter-type-default (package-parameter-type (parameter-spec-get-parameter pspec a))))]
+ [cell cell])
+ plst))
+
+;; 5. Validation
+
+(define (parameter-spec-validate pspec plst)
+ "Validates a parameter-list PLST against the parameter-spec PSPEC."
+ (define (process-multi-list lst)
+ (apply append
+ (map (lambda (x)
+ (parameter-process-list (list x)))
+ (filter (lambda (x) (not (eqv? x '_)))
+ lst))))
+
+ ;; We want all tests to run
+ (let ((works? #t))
+
+ (define (m+eqv? new-val orig-val)
+ (or (and (eqv? orig-val '_)
+ (not (eqv? new-val #:off)))
+ (eqv? orig-val new-val)))
+
+ (define (throw+f sym vals)
+ (raise (formatted-message
+ (G_ "Parameter Validation Error: ~a with values ~s~%")
+ sym vals))
+ (set! works? #f))
+
+ ;; first we check duplication
+ ;; a bit unnecessary
+ (define (validate/duplication)
+ (let ((symlst (map car plst)))
+ (unless (eqv? symlst (delete-duplicates symlst))
+ (throw+f "Duplicates" plst))))
+
+ ;; logic checking checks for:
+ ;; - presence of required parameters
+ ;; - 'one-of' conflicts
+ ;; - dependency satisfaction
+ (define (validate/logic)
+ (map ; required
+ (lambda (x)
+ (unless
+ (let ((new-val (assq-ref plst (car x))))
+ (m+eqv? (if (eqv?
+ new-val
+ (parameter-spec-negation-supported?
+ pspec
+ (car x)))
+ #:off new-val)
+ (cdr x)))
+ (throw+f "Unsatisfied Requirements" x)))
+ (parameter-process-list ; cannot have duplicates here!
+ (parameter-spec-required pspec)))
+ (map ; one-of
+ (lambda (ls)
+ (unless
+ (let ((satisfied (count
+ (lambda (x)
+ (let ((new-val (assq-ref plst (car x))))
+ (m+eqv?
+ (if
+ (eqv? new-val
+ (parameter-spec-negation-supported?
+ pspec
+ (car x)))
+ #:off new-val)
+ (cdr x))))
+ (process-multi-list ls)))) ; duplicates could happen!
+ (or (= satisfied 1)
+ (and (= satisfied 0)
+ (eqv? (car ls) '_))))
+ (throw+f "Unsatisfied One-Of" ls)))
+ (parameter-spec-one-of pspec))
+
+ (unless (not (member #f
+ (return-list
+ (map (lambda (x)
+ (let ((deps (package-parameter-dependencies
+ (parameter-spec-get-parameter pspec x))))
+ (if deps
+ (not
+ (member
+ #f
+ (map
+ (lambda (dep)
+ ;; 0. restructure d to a proper cell
+ (let ((d (car
+ (parameter-process-list
+ (return-list dep)))))
+ ;; 1. assq-ref
+ (m+eqv?
+ (assq-ref plst (car d))
+ (cdr d))))
+ (return-list
+ ;;; XXX: check for packages
+ ;; not doable in the current state as the validator
+ ;; does not take the entire package as an argument
+ ;; the validator will have to be heavily modified
+ (assq-ref deps 'parameter)))))
+ #t)))
+ ;; filter to check if parameter is not its negation
+ (filter (lambda (x)
+ (not (eqv? (cdr x)
+ (parameter-spec-negation-supported?
+ pspec
+ (car x)))))
+ plst)))))
+ (throw+f "Bad dependencies!" plst)))
+
+ (validate/duplication)
+
+ (validate/logic)
+
+ works?))
+
+;; need pkg instead of pspec for override-spec
+(define (package-resolve-parameter-list pkg plst)
+ "Resolves a parameter-list PLST against the package PKG."
+ (let* ([pspec (package-parameter-spec pkg)]
+ [proper-plst (override-spec-multi-match
+ pspec
+ (package-override-plist
+ pkg
+ (parameter-process-list plst)))])
+ (if (parameter-spec-validate pspec proper-plst)
+ proper-plst
+ (base-parameter-alist pspec))))
+
+;; %global-parameters: hash table containing global parameters ref'd by syms
+
+(define-syntax define-global-parameter
+ (syntax-rules ()
+ [(define-global-parameter parameter-definition)
+ (let ((gp-val parameter-definition))
+ (hash-set! %global-parameters
+ (package-parameter-name gp-val)
+ gp-val))]))
+
+(define-syntax parameter-inside?
+ (syntax-rules ()
+ [(% p plst)
+ (not
+ (eqv? (or (assq-ref plst (car p))
+ (error "Parameter not found!"))
+ (parameter-type-negation
+ (package-parameter-type
+ (parameter-spec-get-parameter
+ (package-parameter-spec this-package)
+ p)))))]))
+
+(define-syntax parameter-if
+ (syntax-rules ()
+ [(parameter-if property exp)
+ (let ((properties
+ (parameter-spec-parameter-alist
+ (package-parameter-spec this-package))))
+ (if (member
+ #t
+ (map (cut parameter-inside? <> properties)
+ (parameter-process-list (return-list property))))
+ exp
+ '()))]
+ [(parameter-if property exp exp-else)
+ (let ((properties
+ (parameter-spec-parameter-alist
+ (package-parameter-spec this-package))))
+ (if (member
+ #t
+ (map (cut parameter-inside? <> properties)
+ (parameter-process-list (return-list property))))
+ exp
+ exp-else))]))
+
+(define-syntax parameter-if-all
+ (syntax-rules ()
+ [(parameter-if-all property exp)
+ (let ((properties (parameter-spec-parameter-alist (package-parameter-spec this-package))))
+ (if (not (member
+ #f
+ (map (cut parameter-inside? <> properties)
+ (parameter-process-list (return-list property)))))