forked from larcenists/larceny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sch
485 lines (416 loc) · 19.1 KB
/
setup.sch
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
;;; This file is the entry point for building a copy of Larceny from
;;; scratch. It should be R5RS compatible Scheme; part of its job is
;;; to load the implementation-specific extensions to Scheme based on
;;; the host system (as indicated by the user in the 'scheme: keyword
;;; argument to the setup procedure).
(load "src/Build/petit-unix-defns.sch")
;; A KeySpec is one of
;; (list Symbol 'required)
;; (list Symbol 'with-default Any)
;; (list Symbol 'flag #f)
;; parse-keys : [Listof Any] [Listof KeySpec] (Symbol Any ... -> X) -> X
;; Parses ARGL according to KEYSPECS. If the result satisf
(define (parse-keys argl keyspecs proc)
;; filter : (X -> Bool) [Listof X] -> [Listof X]
;; returns list containing only elements from LST satisfying PRED?
(define (filter pred? lst)
(let loop ((lst lst))
(cond ((null? lst) lst)
((pred? (car lst)) (cons (car lst) (loop (cdr lst))))
(else (loop (cdr lst))))))
(define spec-key car)
(define spec-type cadr)
(define spec-default caddr)
(define (make-assoc-list)
(let loop ((argl argl)
(required-keys (map spec-key
(filter (lambda (spec)
(case (spec-type spec)
((with-default flag) #f)
(else #t)))
keyspecs))))
(cond ((null? argl)
(cond ((null? required-keys) '())
(else (error "Missing required keys: " required-keys))))
((assq (car argl) keyspecs)
=> (lambda (spec)
(define (next rest)
(loop rest
(filter (lambda (k) (not (eq? k (spec-key spec))))
required-keys)))
(case (spec-type spec)
((flag)
(cons (list (car argl) #t)
(next (cdr argl))))
(else (if (null? (cdr argl))
(error "Key without value: " (car argl))
(cons (list (car argl) (cadr argl))
(next (cddr argl))))))))
(else (error "Unknown Key: " (car argl)
" Known Keys: " (map car keyspecs))))))
(let ((alst (make-assoc-list)))
(apply proc
(map (lambda (spec)
(cond ((assq (spec-key spec) alst) => cadr)
((case (spec-type spec)
((with-default flag) #t)
(else #f)) (spec-default spec))
(else (error "Missing Key: " (spec-key spec)))))
keyspecs))))
;; Syntactic form for defining procedures that use keyword parameters
;; (as opposed to positional parameters)
;; If you don't pass a keyword+value, the parameter is given
;; the default value of (if #f #f)
;;
;; Special:
;; FLAG keywords: just passed on their own, with no associated value.
;; WITH-DEFAULT keywords: have a default value defined by the procedure writer
;; REQUIRED keywords: have no default value; calls error if you don't pass them
;;
;; Examples:
;; ((lambda-keyed (a: b:) (- a: b:)) 'b: 4 'a: 6) ; ==> 2
;; ((lambda-keyed ((required a:) b:) (+ a: b:)) 'a: 1)
;; ; ==> <<error in call to +>>
;; ((lambda-keyed ((required a:) b:) (+ a: b:)) 'b: 1)
;; ; ==> <<error: Missing required keys: (a:)>>
;; ((lambda-keyed ((with-default 3 a:) b:) (+ a: b:)) 'b: 1) ; ==> 4
;; ((lambda-keyed ((flag help) a b) (if help "Adds a and b" (+ a b)))
;; 'a 1 'b 2) ; ==> 3
;; ((lambda-keyed ((flag help) a b) (if help "Adds a and b" (+ a b))) 'help)
;; ; ==> "Adds a and b"
(define-syntax lambda-keyed
(syntax-rules (flag with-default required)
((lambda-keyed "help" () (KEYSPECS ...) (ARGS ...) (BODY ...))
(lambda argl
(parse-keys argl (quasiquote (KEYSPECS ...))
(lambda (ARGS ...) BODY ...))))
((lambda-keyed "help"
((with-default DEFAULT KEY-ARG) KEY-ARGS ...)
(KEYSPECS ...) (ARGS ...) (BODY ...))
(lambda-keyed "help" (KEY-ARGS ...)
((KEY-ARG with-default (unquote DEFAULT)) KEYSPECS ...)
(KEY-ARG ARGS ...) (BODY ...)))
((lambda-keyed "help" ((flag KEY-ARG) KEY-ARGS ...)
(KEYSPECS ...) (ARGS ...) (BODY ...))
(lambda-keyed "help" (KEY-ARGS ...)
((KEY-ARG flag #f) KEYSPECS ...)
(KEY-ARG ARGS ...) (BODY ...)))
((lambda-keyed "help" ((required KEY-ARG) KEY-ARGS ...)
(KEYSPECS ...) (ARGS ...) (BODY ...))
(lambda-keyed "help" (KEY-ARGS ...)
((KEY-ARG required) KEYSPECS ...)
(KEY-ARG ARGS ...) (BODY ...)))
((lambda-keyed "help" (KEY-ARG KEY-ARGS ...)
(KEYSPECS ...) (ARGS ...) (BODY ...))
(lambda-keyed "help" (KEY-ARGS ...)
((KEY-ARG with-default (unquote (if #f #f))) KEYSPECS ...)
(KEY-ARG ARGS ...) (BODY ...)))
((lambda-keyed (KEY-ARGS ...) BODY ...)
(lambda-keyed "help" (KEY-ARGS ...) () () (BODY ...)))))
(define-syntax define-keyed
(syntax-rules ()
((define-keyed (PROC ARGS ...) BODY ...)
(define PROC (lambda-keyed (ARGS ...) BODY ...)))))
;; setup : oneof ['help]
;; -> Void {'scheme: Symbol, 'host: Symbol, ['target: Symbol]}
;; -> Void
;; Main entry to set up the build for Petit compiler and runtime.
(define-keyed (setup (with-default 'larceny scheme:)
;; inferred for scheme: 'larceny
(with-default #f host:)
;; inherits host: if not given
(with-default #f target:)
;; usually safe to leave alone
(with-default #f c-compiler:)
;; current choices are: flat1, flat4
;; future choices include: record1, record2
(with-default #f string-rep:)
(flag help)
;; Options for generated runtime
(with-default #t rof-collector:)
(flag dof-collector)
(flag gclib-large-table)
;; Build host options
(flag exit-on-error)
(flag quiet-load)
(flag verbose-load)
(flag always-source)
;; Target variation options
(flag native)
(flag nasm)
(flag sassy)
(flag common)
;; Deprecated options
(flag code-cov)
(flag rebuild-code-cov))
(define (displn arg) (display arg) (newline))
(define (help-text)
(displn "To setup Larceny, call ")
(displn "(setup 'scheme: HOST-SCHEME 'host: PLATFORM ['target: PLATFORM])")
(displn "e.g., (setup 'scheme: 'larceny 'host: 'macosx)")
(displn "Example host schemes: 'larceny 'mzscheme")
(displn "Example platforms: 'macosx 'solaris"))
(set! *exit-on-error* exit-on-error)
(cond (help (help-text)))
;; Fail fast in case user didn't know 'scheme: defaults to 'larceny
;; This expression should have no side-effects on Larceny, but
;; should fail spectacularly on almost any other imaginable Scheme.
(cond ((eq? scheme: 'larceny)
((environment-get (interaction-environment) 'current-larceny-root))))
(set! *always-source* always-source)
(set! *runtime:additional-features*
(append
(if dof-collector '("DOF_COLLECTOR") '())
(if rof-collector: '("ROF_COLLECTOR") '())
(if gclib-large-table '("GCLIB_LARGE_TABLE") '())
*runtime:additional-features*))
(cond (quiet-load (set! *verbose-load* #f))
(verbose-load (set! *verbose-load* #t)))
;; If on Larceny, allow more leeway for omitted options. Need to
;; add arch: to keys above (w/ reasonable implication logic).
(cond ((and (eq? scheme: 'larceny)
(not host:))
(let ((os-name (cdr (assq 'os-name (system-features))))
(arch-name (cdr (assq 'arch-name (system-features))))
(arch-endianness
(cdr (assq 'arch-endianness (system-features)))))
(cond
((and (equal? os-name "Linux") (eq? arch-endianness 'little))
(if (equal? arch-name "ARM")
(set! host: 'linux-arm-el)
(set! host: 'linux-el)))
((and (equal? os-name "Linux") (eq? arch-endianness 'big))
(set! host: 'linux-be))
((and (equal? os-name "SunOS") (eq? arch-endianness 'big))
(set! host: 'solaris))
((and (equal? os-name "Win32") (eq? arch-endianness 'little))
(set! host: 'win32))
((and (equal? os-name "MacOS X") (eq? arch-endianness 'little))
(set! host: 'macosx-el))
((and (equal? os-name "MacOS X") (eq? arch-endianness 'big))
(set! host: 'macosx))
;; Fill in other host: clauses here
))))
(cond ((and (not help) (or (not scheme:) (not host:)))
(help-text))
(else
(let* ((host: (case host:
;; Felix prefers "linux86" and "macosx86",
;; so those aliases are supported here
;;
;; Lars notes that "linux-el" as a synonym for "linux-x86-el"
;; is unreasonable given that Linux runs on so many platforms.
((linux86) 'linux-el)
((macosx86) 'macosx-el)
(else host:)))
(target: (cond (target: target:)
(common 'clr-el) ; FIXME
((eq? host: 'linux-arm-el)
(error 'setup.sch "An explicit target: value is required for ARM Linux, either linux-arm-el-hardfp or linux-arm-el-softfp"))
(else host:))))
(setup-real! scheme: host: target: c-compiler: string-rep:
(or native sassy nasm) code-cov rebuild-code-cov
sassy nasm)))))
;; Can't use parameters for *host-dir* and such, because we have not
;; loaded the compatibility files yet at the time we get here.
;; But this is just for setting things up anyway.
;;
;; setup-real! : Symbol ... -> Void
;; Sets global variables (based on Scheme impl. running on and Target OS),
;; then calls UNIX-INITIALIZE
(define (setup-real! host-scheme host-arch target-arch
c-compiler-choice string-rep-choice
native code-cov rebuild-code-cov
sassy nasm)
(define (platform->endianness sym)
(case sym
((macosx solaris clr-be) 'big)
((macosx-el linux-el linux-arm-el linux-arm-el-hardfp cygwin win32 clr-el) 'little)
(else (error 'platform->endianness "Unhandled case: ~a" sym))))
(define (platform->os sym)
(case sym
((macosx macosx-el solaris linux-el cygwin linux-arm-el linux-arm-el-hardfp) 'unix)
((win32) 'win32)
((clr-be) 'unix) ; FIXME
((clr-el) 'win32) ; FIXME
(else (error 'platform->os "Unhandled case: ~a" sym))))
;; Warn about "semi-working" cases
(cond ((and (not native)
(memq host-arch '(cygwin win32)))
(display "Warning: Petit/Standard-C on Windows is incomplete.")
(newline)
(display "Use at own risk, or try Petit/NASM")
(newline)
(newline)
;; In particular, control transfer points are not guaranteed
;; to be 4-byte aligned, and therefore on win32 we use the
;; CODEPTR_SHIFT2 feature to ensure they have a fixnum tag.
;; This almost works, except that addresses with significant
;; bits that are corrupted by the shift-by-2; thus things
;; break when dynamically loading compiled code on win32
;; non-native.
))
(case host-scheme
((plt-r5rs)
(set! *host-dir* "PLT-R5RS")
(set! *host-name* "PLT-R5RS"))
((mzscheme)
(set! *host-dir* "MzScheme")
(set! *host-name* "MzScheme"))
((larceny)
(set! *host-dir* "Larceny")
(set! *host-name* "Larceny"))
((chez petite)
;; Felix hasn't gotten his hands on Chez yet,
;; but will assume that Petite is a subset of Chez.
(set! *host-dir* "Petite")
(set! *host-name* "Chez"))
(else (error 'setup "Unknown host scheme: ~a" host-scheme)))
(set! *host:os* host-arch)
(set! *change-feature-set*
(case target-arch
((macosx-el) (cond (sassy 'features-x86-sassy-macosx)
(else 'features-petit-macosx-el)))
((macosx) 'features-petit-macosx)
((solaris) (cond (native 'features-sparc-solaris)
(else 'features-petit-solaris)))
((linux-arm-el-hardfp)
(cond (native 'features-arm-el-hardfp-linux)
(else (error 'setup.sch "Only the native system is supported on ARM Linux"))))
((linux-el) (cond (sassy 'features-x86-sassy-linux)
(nasm 'features-x86-nasm-linux)
(native 'features-x86-nasm-linux)
(else 'features-petit-linux)))
((cygwin) 'features-petit-cygwin)
((win32) (cond (sassy 'features-x86-sassy-win32)
(nasm 'features-x86-nasm-win32)
(native 'features-x86-nasm-win32)
(else 'features-petit-win32)))
;; if client says we're using unix or clr,
;; then just use value set by features.sch
((unix clr-be clr-el)
*change-feature-set*)
(else
(error 'setup.sch "Must add support for target-arch"))
))
(case target-arch
((cygwin win32)
(set! *twobit-executable-name* "twobit.bin.exe")
(set! *petit-executable-name* "petit.bin.exe")))
(set! *make:larceny-target*
(case target-arch
((macosx
macosx-el) "petitmacosx")
((solaris) "petitsparcsolaris")
((cygwin) "petitcygwinmswindows")
((linux-el linux-arm-el-hardfp) "petitdebianlinux")
((clr-be clr-le) "common"))) ; FIXME
(set! *host:endianness* (platform->endianness host-arch))
(set! *target:endianness* (platform->endianness target-arch))
(set! *target:os* (platform->os target-arch))
(cond (sassy
(case target-arch
((win32)
(set! *target:machine* 'x86-sass)
(set! *target:machine-source* "IAssassin")
(set! *makefile-configuration* 'x86-win32-static-visualc)
(set! *heap-type* 'sassy)
(set! *runtime-type* 'sassy-native))
((macosx-el)
(set! *target:machine* 'x86-sass)
(set! *target:machine-source* "IAssassin")
(set! *makefile-configuration* 'sassy-macosx-static-gcc-nasm)
(set! *heap-type* 'sassy)
(set! *runtime-type* 'sassy-native))
((linux-el)
(set! *target:machine* 'x86-sass)
(set! *target:machine-source* "IAssassin")
(set! *makefile-configuration* 'sassy-unix-static-gcc-nasm)
(set! *heap-type* 'sassy)
(set! *runtime-type* 'sassy-native))))
(native
(case target-arch
((solaris)
(set! *target:machine* 'sparc)
(set! *target:machine-source* "Sparc")
(set! *makefile-configuration* 'sparc-solaris-static-gcc)
(set! *heap-type* 'sparc-native)
(set! *runtime-type* 'sparc-native))
((linux-arm-el-hardfp)
(set! *target:machine* 'arm)
(set! *target:machine-source* "Fence")
(set! *makefile-configuration* 'arm-hardfp-linux-gcc-v4-gas)
(set! *heap-type* 'arm-native)
(set! *runtime-type* 'arm-native))
;; Win32 native is just Petit with extasm of NASM rather than C
((win32)
(set! *target:machine* 'x86-nasm)
(set! *target:machine-source* "Standard-C")
(set! *makefile-configuration* #f)
(set! *heap-type* 'petit)
(set! *runtime-type* 'petit))
;; Linux86 native is just Petit with extasm of NASM rather than C
((linux-el)
(set! *target:machine* 'x86-nasm)
(set! *target:machine-source* "Standard-C")
(set! *makefile-configuration* #f)
(set! *heap-type* 'petit)
(set! *runtime-type* 'petit))
(else
(error "Unsupported architecture for native setup: "
target-arch))))
((memq target-arch '(clr-be clr-el))
(set! *target:machine* 'clr)
(set! *target:machine-source* "IL")
(set! *makefile-configuration* #f)
(set! *heap-type* 'clr)
(set! *runtime-type* 'clr))
(else
(set! *target:machine* 'standard-c)
(set! *target:machine-source* "Standard-C")
(set! *makefile-configuration* #f)
(set! *heap-type* 'petit)
(set! *runtime-type* 'petit)
))
(set! *code-coverage* (or code-cov rebuild-code-cov))
(set! *rebuild-code-coverage* rebuild-code-cov)
;; [usually #f; user may override with e.g. 'mwcc aka CodeWarrior]
(set! *host:c-compiler* (or c-compiler-choice
(and native (not sassy)
(eq? target-arch 'win32)
'nasm+msvc)
(and native (not sassy)
(eq? target-arch 'linux-el)
'nasm+gcc)
#f))
(set! *target:string-rep*
(case string-rep-choice
((flat4 #f)
'flat4)
((flat1)
(if (or (and native (eq? target-arch 'solaris))
sassy)
'flat1
(error "Unsuported architecture for flat1 string-rep: "
target-arch)))
(else (error "Unsuported string representation: "
string-rep-choice))))
(if (or (eq? *target:machine* 'x86-nasm)
(eq? *target:machine* 'x86-sass))
(set! *globals-table* "globals-nasm.cfg"))
(if (eq? *target:machine* 'arm)
(set! *globals-table* "globals-fence.cfg"))
(case target-arch
((clr-be clr-el)
(load "src/Build/dotnet.sch")
(larceny-setup "Larceny" *target:os* *target:endianness*)) ; FIXME
(else
(unix-&-win32-initialize))))
(define (setup-load-build . args)
(apply setup args)
(build-config-files)
(load-compiler)
(build-runtime)
(build-heap)
(build-executable)
(build-larceny-files))