forked from pycket/pycket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-file-pycket.rkt
412 lines (390 loc) · 14.9 KB
/
compile-file-pycket.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
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
#lang racket/base
(require racket/path racket/list
racket/file
racket/cmdline
syntax/modread
(prefix-in c: compiler/compile-file))
(provide compile-file compile-lib-path)
(define compile-file
(case-lambda
[(src)
(with-module-reading-parameterization (lambda () (c:compile-file src)))]
[(src dest)
(with-module-reading-parameterization (lambda () (c:compile-file src dest)))]))
(define clean-count 0)
(define compiled-file-path (build-path "compiled" "pycket"))
(define racket-base-modules
'("racket/private/stx"
"racket/private/qq-and-or"
"racket/private/gen-temp"
"racket/private/cond"
"racket/private/member"
"racket/private/define-et-al"
"racket/private/ellipses"
"racket/private/sc"
"racket/private/stxcase"
"racket/private/template"
"racket/private/stxloc"
"racket/private/with-stx"
"racket/private/letstx-scheme"
"racket/private/stxcase-scheme"
"racket/private/qqstx"
"racket/private/norm-define"
"racket/private/define"
"racket/private/sort"
"racket/private/case"
"racket/private/logger"
"racket/private/more-scheme"
"racket/private/path"
"racket/private/path-list"
"racket/private/reading-param"
"racket/repl"
"racket/private/misc"
"racket/private/kw-prop-key"
"racket/private/name"
"racket/private/procedure-alias"
"racket/private/kw"
"racket/private/stxparamkey"
"racket/private/stxparam"
"racket/stxparam-exptime"
"racket/private/generic-methods"
"racket/private/struct-info"
"racket/private/define-struct"
"racket/require-transform"
"racket/provide-transform"
"racket/private/reqprov"
"racket/private/modbeg"
"racket/private/submodule"
"racket/private/reverse"
"racket/private/for"
"racket/private/map"
"racket/private/kernstruct"
"racket/private/top-int"
"racket/private/pre-base"
"racket/runtime-config"
"racket/private/hash"
"racket/private/list"
"racket/private/string"
"racket/private/kw-file"
"racket/private/namespace"
"racket/private/struct"
"racket/private/base"
"racket/base"
"racket/private/performance-hint"
"racket/private/executable-path"
"racket/private/generic-interfaces"
"racket/private/immediate-default"
"syntax/wrap-modbeg"
"racket/stxparam"
"racket/private/cert"
"racket/private/collect"
"racket/private/old-path"
"racket/private/kw-syntax-binding"))
(define racket-modules
(append racket-base-modules
'("racket/private/list-predicates"
"racket/private/math-predicates"
"racket/list"
"racket/private/arity"
"racket/private/norm-arity"
"racket/promise"
"racket/private/promise"
"racket/private/config"
"syntax/readerr"
"syntax/module-reader"
"syntax/modread"
"setup/cross-system"
"setup/dirs"
"setup/private/dirs"
"compiler/private/mach-o"
"compiler/private/winutf16"
"planet/private/define-config"
"planet/config"
"racket/cmdline"
"setup/path-relativize"
"pkg/path"
"racket/private/vector-wraps"
"racket/unsafe/ops"
"racket/flonum"
"racket/fixnum"
"syntax/private/boundmap"
"syntax/srcloc"
"syntax/stx"
"syntax/location"
"racket/path"
"racket/file"
"racket/private/vector-wraps"
"racket/unsafe/ops"
"racket/private/port"
"racket/private/portlines"
"racket/struct-info"
"racket/private/custom-write"
"racket/pretty"
"setup/main-collects"
"setup/path-to-relative"
"racket/contract/private/helpers"
"racket/contract/private/prop"
"racket/contract/private/blame"
"racket/contract/private/generate-base"
"racket/contract/private/generate"
"racket/contract/private/guts"
"racket/contract/private/misc"
"racket/contract/private/arrow-common"
"racket/contract/private/arr-i-parse"
"racket/contract/private/arr-util"
"racket/contract/private/arr-i"
"racket/contract/private/arr-d"
"racket/contract/private/and"
"racket/contract/private/opt-guts"
"racket/contract/private/list"
"racket/contract/private/opt"
"racket/contract/private/ds-helpers"
"racket/contract/private/ds" ;; exn:fail : Reference to an undefined variable : build-optres22.1
"racket/contract/private/arity-checking"
"racket/contract/private/application-arity-checking"
"racket/contract/private/arrow-higher-order"
"racket/contract/private/kwd-info-struct"
"racket/contract/private/box"
"racket/contract/private/basic-opters"
"racket/contract/private/opters"
"racket/contract/private/arrow-val-first"
"racket/contract/private/case-arrow" ;; fail
"racket/contract/private/hash"
"racket/contract/private/legacy"
"racket/contract/private/orc"
"racket/contract/private/out" ;; fail
"racket/contract/private/object" ;; needs classes
"racket/contract/private/parametric"
"racket/contract/parametric"
"racket/contract/private/provide" ;; fail
"racket/contract/private/struct-dc" ;; infinite exn:fail : abort-current-continuation: no such prompt exists
"racket/contract/private/struct-prop"
"racket/contract/private/rand"
"racket/contract/private/top-sort"
"racket/contract/private/types"
"racket/contract/private/unconstrained-domain-arrow"
"racket/contract/private/vector"
"racket/contract/private/base"
"racket/contract/private/exists"
"racket/contract/combinator"
"racket/contract/base"
"racket/private/serialize-structs"
"racket/private/so-search"
"racket/private/runtime-path-table"
"racket/private/this-expression-source-directory"
"racket/runtime-path" ;; exn:fail : Reference to an undefined variable : path->collects-relative10.1
"racket/unsafe/undefined"
"syntax/kerncase"
"racket/private/class-wrapped"
"racket/private/classidmap"
"racket/private/class-undef"
"racket/private/check"
"racket/private/custom-hash" ;; fail
"racket/private/generic" ;; fail
"racket/private/local"
"racket/private/place-local"
"racket/private/primitive-table"
"racket/private/sequence"
"racket/private/small-scheme"
"racket/private/streams"
"racket/private/stream-cons"
"racket/private/truncate-path"
"racket/private/unit-keywords"
"racket/private/unix-rand"
"racket/private/serialize" ;; fail
"racket/private/unit-compiletime" ;; fail
"racket/private/unit-utils" ;; fail
"racket/private/set" ;; fail
"racket/private/set-types" ;; fail
"racket/private/shared-body" ;; fail
"racket/private/tethered-installer" ;; fail
"racket/private/unit-contract-syntax" ;; fail
"racket/private/unit-contract" ;; fail
"racket/private/unit-runtime"
"racket/async-channel" ;; fail
"racket/bool"
"racket/block"
"racket/bytes"
"racket/control"
"racket/date" ;; fail
"racket/dict" ;; fail
"racket/engine" ;; fail
"racket/enter" ;; fail
"racket/exn"
"racket/fasl"
"racket/format" ;; fail
"racket/function"
"racket/future"
"racket/generator"
"racket/generic" ;; fail
"racket/hash" ;; fial
"racket/help"
"racket/include"
"racket/init" ;; fail
"racket/interactive"
"racket/kernel"
"racket/keyword-transform"
"racket/language-info"
"racket/lazy-require" ;; fail
"racket/linklet"
"racket/match" ;;fail
"racket/local"
"racket/main" ;; fail
"racket/logging" ;; fail
"racket/os" ;; fail
"racket/performance-hint" ;; fail
"racket/place" ;; fail
"racket/provide-syntax"
"racket/provide-transform"
"racket/prefab"
"racket/port" ;; fail
"racket/random" ;; fail
"racket/require"
"racket/require-syntax"
"racket/require-transform"
"racket/rerequire" ;; fail
"racket/sequence" ;; fail
"racket/serialize" ;; fail
"racket/set" ;; fail
"racket/shared" ;; fail
"racket/splicing"
"racket/stream" ;; fail
"racket/string"
"racket/surrogate" ;; fail
"racket/syntax"
"racket/system"
"racket/undefined"
"racket/tcp"
"racket/udp"
"racket/trait" ;; fail
"racket/vector"
"racket/unit" ;; fail
"racket/unit-exptime" ;; fail
"racket/load/lang/reader"
"racket/base/lang/reader"
"racket/lang/reader" ;; fail
"racket/kernel/init" ;; fail
"racket/place/private/prop"
"racket/place/private/th-place"
"racket/place/private/coercion"
"racket/place/private/async-bi-channel"
"racket/match/runtime"
"racket/match/stxtime"
"racket/match/syntax-local-match-introduce"
"racket/struct" ;; needs contracts
"syntax/strip-context" ;; needs contracts
"compiler/compile-file"
"syntax/private/modhelp"
"compiler/private/dep"
"compiler/compilation-path"
"setup/collects"
"syntax/private/modresolve-noctc"
"syntax/private/modcode-noctc"
"file/sha1"
"compiler/private/cm-minimal"
)))
(define (split s)
(define l (regexp-split "/" s))
(cons (last l) (drop-right l 1)))
(define (compile-lib-path p)
(let* ([p-list (split p)]
[mod-name (format "~a.rkt" (car p-list))]
[dirs (cdr p-list)]
[p (apply collection-file-path mod-name dirs)]
[compiled-file-name (format "~a_rkt.zo" (car p-list))]
[zo-path (build-path (path-only p) compiled-file-path compiled-file-name)])
(if (or (force-recompile)
(<= (file-or-directory-modify-seconds zo-path #f (lambda () -inf.0))
(file-or-directory-modify-seconds p #f)))
(begin
(printf "PYCKET COMPILE FILE -- compiling : ~a to ~s\n" p zo-path)
(compile-file p #;zo-path))
(printf "PYCKET COMPILE FILE -- PASS -- already exists : ~a\n" p))))
(define (compile-path p)
(let* ([path-to-file.rkt (string->path p)]
[p (path-only path-to-file.rkt)])
(printf "PYCKET COMPILE FILE -- path-to-file : ~a -- p : ~a\n" path-to-file.rkt p)
(if (not p)
(compile-file (build-path (current-directory) path-to-file.rkt))
(compile-file (build-path path-to-file.rkt)))))
(define (clean-file p)
(let* ([p-list (split p)]
[mod-name (format "~a.rkt" (car p-list))]
[dirs (cdr p-list)]
[p (apply collection-file-path (cons mod-name dirs))])
(let* ((d (path-only p))
(zo-name (format "~a_rkt.zo" (car p-list)))
(zo-path (build-path d compiled-file-path zo-name)))
(if (file-exists? zo-path)
(begin
(printf "REMOVING : ~a\n" zo-path)
(set! clean-count (add1 clean-count))
(delete-file zo-path))
(printf "DOESN'T EXIST : ~a\n" zo-path)))))
(define force-recompile (make-parameter #f))
(module+ main
(define batch #f)
(define clean #f)
(define lib-path? #f)
(define base-only #f)
(define stop-on-error? #f)
;; fake-parameterize
(define old-ns (current-namespace))
(define fails '())
(command-line
#:once-each
[("-b" "--batch") "compile the Racket modules statically listed here"
(set! batch #t)]
[("--clean") "remove all the generated pycket .zo files for racket modules"
(set! clean #t)]
[("--recompile") "clean and recompile the Racket libraries"
(begin (set! clean #t) (set! batch #t))]
[("-f" "--force") "force re-generation of existing zo files (disabled by default)"
(force-recompile #t)]
[("-l") "interpret paths as library paths"
(set! lib-path? #t)]
[("--stop-on-error") "stop on the first error"
(set! stop-on-error? #t)]
[("--base-only") "only compile modules needed for `racket/base`"
(set! base-only #t)]
#:args paths
;; do this with mutation because parameterization doesn't currently work
(current-namespace (make-base-namespace))
(let ([racket-modules (if base-only racket-base-modules racket-modules)])
(when clean
(for ([p (in-list racket-modules)])
(clean-file p))
(use-compiled-file-paths (list compiled-file-path)))
;; to do multiple
(when batch
(for ([p (in-list racket-modules)]
#:break (and stop-on-error? (not (null? fails))))
(with-handlers ([exn:fail?
(lambda (e)
(let* ((msg (lambda (n)
(string-append (substring (exn-message e) 0 n) " ...")))
(msg-short (msg 40))
(msg-long (msg (min (string-length (exn-message e)) 200)))
(mod.msg (cons p msg-short)))
(set! fails (cons mod.msg fails))
(printf "ERROR : ~a\n\n" (exn-message e))))])
(compile-lib-path p))))
;; to compile individual paths
(unless (null? paths)
(for ([p (in-list paths)])
(if lib-path?
(compile-lib-path p)
(compile-path p))))
(unless (zero? clean-count)
(printf "\n-- ~a out of ~a zo files removed --\n\n"
clean-count (length racket-modules)))
(if (null? fails)
(printf "All DONE without errors.\n")
(begin
(printf "\n-- Here are the failed libraries -- ~a / ~a failed --\n\n"
(length fails) (length racket-modules))
(for ([p (in-list fails)])
(printf "~a ------------ ~a\n" (car p) (cdr p)))
(printf "DONE.\n")))
(current-namespace old-ns))
))