-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend-build-main.rkt
275 lines (234 loc) · 7.63 KB
/
backend-build-main.rkt
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
#lang racket/base
#|
Routines for parsing and collecting `build` annotations, and generating
code for them.
|#
(require data/order
data/splay-tree
racket/contract/base
racket/dict
racket/function
racket/list
racket/match
"app-util.rkt"
"ir-ast.rkt"
"backend-build-writer.rkt"
"backend-util.rkt"
"util.rkt"
"util/order.rkt")
;;;
;;; parsing
;;;
(define (opt-name? s)
(regexp-match? #rx"^[a-z][a-z0-9-]*$" s))
(define (allowed-symbol? sym)
(opt-name? (symbol->string sym)))
(define (opt-value-number? v)
(any-pred-holds exact-integer? hexnum? v))
(define (opt-value-atom/c v)
(any-pred-holds boolean? opt-value-number? string? symbol? v))
(define (opt-value-atom-pred v)
(ormap
(lambda (p?)
(and (p? v) p?))
(list boolean? opt-value-number? string? symbol?)))
(define (opt-value-number->number v)
(if (hexnum? v) (hexnum-num v) v))
(define (opt-value-number-comparator x y)
(define x-n (opt-value-number->number x))
(define y-n (opt-value-number->number y))
(cond
((= x-n y-n) '=)
((< x-n y-n) '<)
(else '>)))
(define opt-value-number-order
(order 'number-order
opt-value-number?
opt-value-number-comparator))
(define (order-for-opt-value-atom v)
(cond
((boolean? v) boolean-order)
((opt-value-number? v) opt-value-number-order)
((string? v) string-order)
((symbol? v) symbol-order)
(else (assert #f))))
(define opt-value-set/c
(cons/c predicate/c ordered-dict?))
(define ir-opt-value/c
(or/c opt-value-atom/c opt-value-set/c))
(define opt-value/c
(or/c opt-value-atom/c list?))
;; The returned dictionary has Lispy, all lowercase strings as keys,
;; i.e., opt-name? holds for the keys. The ir-opt-value/c contract
;; holds for the values, which may be either atoms or sets of values.
;; Set elements are also ordered, and must all be of the same value
;; type.
(define-with-contract*
(-> (listof (list/c Id? syntax?))
ordered-dict?)
(parse-analyze-build-annos/ir build-lst)
(define h (make-splay-tree string-order
#:key-contract opt-name?
#:value-contract ir-opt-value/c))
(define (to-name id-ast n-stx)
(define s (symbol->string (syntax-e n-stx)))
(unless (opt-name? s)
(raise-syntax-error
#f
(format "illegal build option name for definition ~a"
(Id-name id-ast))
n-stx))
s)
(define (to-value id-ast opt-stx v-stx)
(define v
(syntax-case v-stx ()
((#:hex h-v)
(exact-integer? (syntax-e #'h-v))
(hexnum (syntax->datum #'h-v)))
(_
(syntax->datum v-stx))))
(define p? (opt-value-atom-pred v))
(unless p?
(raise-language-error
#f
"illegal value for build option"
opt-stx
v-stx
#:continued
(format "(for declaration ~a)" (Id-name id-ast))))
(when (and (symbol? v) (not (allowed-symbol? v)))
(raise-language-error
#f
"too exotic a symbol for build option"
opt-stx
v-stx
#:continued
(format "(for declaration ~a)" (Id-name id-ast))))
(values p? v))
(define (set-value-opt! id-ast opt-stx n-stx v-stx)
(define n (to-name id-ast n-stx))
(define-values (p? v) (to-value id-ast opt-stx v-stx))
(if (dict-has-key? h n)
(let ()
(define x-v (dict-ref h n))
(unless (equal? x-v v)
(error 'parse-analyze-build-annos
"conflicting redefinition of build option ~a for definition ~a (previously: ~s): ~s"
n (Id-name id-ast) x-v v-stx)))
(dict-set! h n v)))
(define (set-set-opt! id-ast opt-stx n-stx v-stx-lst)
(assert (not (null? v-stx-lst)))
(define n (to-name id-ast n-stx))
(define-values (type-p? v-h)
(if (dict-has-key? h n)
(let ((p (dict-ref h n)))
(define-values (type-p? v-h) (values (car p) (cdr p)))
(unless (ordered-dict? v-h)
(error 'parse-analyze-build-annos
"conflicting use of operator += with build option ~a for definition ~a (previously defined as non-set ~s): ~s"
n (Id-name id-ast) v-h opt-stx))
(values type-p? v-h))
(values #f #f)))
(for ((v-stx v-stx-lst))
(define-values (p? v) (to-value id-ast opt-stx v-stx))
(if type-p?
(unless (type-p? v)
(raise-syntax-error
#f
(format "type mismatch for definition ~a build option ~a value (expected ~a)" (Id-name id-ast) n (object-name type-p?))
opt-stx v-stx))
(set!-values
(type-p? v-h)
(values p? (make-splay-tree (order-for-opt-value-atom v)))))
(dict-set! v-h v #t))
(dict-set! h n (cons type-p? v-h)))
(define (parse-opt! id-ast build-stx opt-stx)
(syntax-case opt-stx ()
(n
(identifier? #'n)
(set-value-opt! id-ast opt-stx #'n #'#t))
((n v)
(identifier? #'n)
(set-value-opt! id-ast opt-stx #'n #'v))
((p n v more-v ...)
(and (eq? '+= (syntax-e #'p)) (identifier? #'n))
(set-set-opt! id-ast opt-stx #'n
(syntax->list #'(v more-v ...))))
(_
(raise-language-error
'build
"illegal (build ...) annotation sub-form"
build-stx
opt-stx
#:continued
(format "(for declaration ~a)" (Id-name id-ast))))))
(define (parse-build! id-ast build-stx)
(syntax-case build-stx ()
((_ . opt)
(for-each
(fix parse-opt! id-ast build-stx)
(syntax->list #'opt)))
(_
(raise-language-error
'build
"illegal (build ...) annotation form"
build-stx
#:continued
(format "(for declaration ~a)" (Id-name id-ast))))))
(for ((build build-lst))
(define-values (id-ast build-stx) (apply values build))
(parse-build! id-ast build-stx))
h)
;; Any returned lists are sorted. The (list/c string? opt-value/c)
;; part of the signature could be more accurately given as (list/c
;; opt-name? (or/c opt-value-atom/c (listof opt-value-atom/c))).
(define-with-contract*
(-> (listof (list/c Id? syntax?))
(listof (list/c string? opt-value/c)))
(parse-analyze-build-annos build-lst)
(define (mk-lst v)
(define h (cdr v))
(for/list (((k v) (in-dict h)))
k))
(define h (parse-analyze-build-annos/ir build-lst))
(for/list (((n v) (in-dict h)))
(list n (if (pair? v) (mk-lst v) v))))
;;;
;;; collection
;;;
(define-with-contract*
(-> list? (listof (list/c Id? syntax?)))
(defs-collect-build-annos def-lst)
(define lst null)
(define (add! id build-stx)
(set! lst (cons (list id build-stx) lst)))
(for ((def def-lst))
(assert (Def? def))
(define b (ast-anno-maybe def 'build))
(when b (add! (Def-id def) b)))
lst)
;;;
;;; code generation
;;;
(define-with-contract*
(-> list? list? path-string? (or/c #f output-port?) boolean?
void?)
(generate-build-file spec attrs path-stem out banner?)
(define targets '(gnu-make))
(let ()
(match-define (cons 'build (? list? opt-lst)) spec)
(for ([opt opt-lst])
(match opt
[(list 'targets (and (or 'c 'gnu-make 'qmake 'ruby) lst) ...)
(set! targets (remove-duplicates lst eq?))])))
(for ([target targets])
(define-values (writer sfx pfx) (get-writer-etc target))
(define path (path-add-suffix path-stem sfx))
(define filename (path-basename-as-string path))
(write-generated-output
path out
(thunk
(when banner?
(display-banner pfx filename))
(writer path attrs))))
(void))