Revert "mes: use array-based stack. WIP: will not survice gc."
[mes.git] / make.scm
1 #! /bin/sh
2 # -*- scheme -*-
3 exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$@"}
4 !#
5
6 (use-modules (srfi srfi-26)
7              (guix shell-utils))
8
9 ;; FIXME: .go dependencies
10 ;; workaround: always update .go before calculating hashes
11 ;;(use-modules ((mes make) #:select (sytem**)))
12 (define %scm-files
13   '("guix/make.scm"
14     "guix/records.scm"
15     "guix/shell-utils.scm"
16     "language/c99/compiler.scm"
17     "language/c99/info.scm"
18     "mes/as-i386.scm"
19     "mes/as.scm"
20     "mes/bytevectors.scm"
21     "mes/elf.scm"
22     "mes/guile.scm"
23     "mes/M1.scm"))
24 (define %go-files (map (compose (cut string-append <> ".go") (cut string-drop-right <> 4)) %scm-files))
25 (setenv "srcdir" ".")
26 (setenv "host" %host-type)
27 (with-directory-excursion "guile"
28   (apply system* `("guile"
29                    "--no-auto-compile"
30                    "-L" "."
31                    "-C" "."
32                    "-s"
33                    "../build-aux/compile-all.scm"
34                    ,@%scm-files)))
35
36 (use-modules (srfi srfi-1)
37              (ice-9 curried-definitions)
38              (ice-9 match)
39              (guix make))
40
41 (define crt1.hex2 (m1.as "lib/crt1.c"))
42 (add-target crt1.hex2)
43
44 (add-target crt1.mlibc-o)
45
46 (define %HEX2-FLAGS
47   '("--LittleEndian"
48     "--Architecture=1"
49     "--BaseAddress=0x1000000"))
50 (define %HEX2 (PATH-search-path "hex2"))
51
52 (define* (LINK.hex2 #:key (hex2 %HEX2) (hex2-flags %HEX2-FLAGS) (crt1 crt1.hex2) (libc libc-mes.hex2) debug?)
53   (method (name "LINK.hex2")
54           (build (lambda (o t)
55                    (let* ((input-files (map target-file-name (target-inputs t)))
56                           ;; FIXME: snarf inputs
57                           (input-files (filter (lambda (f) (and (string-suffix? "hex2" f)
58                                                                 (not (member f (cdr input-files)))))
59                                                input-files)))
60                      (format #t "  ~a\t ~a -> ~a\n" (method-name o) (string-join input-files) (target-file-name t))
61                      (with-output-to-file (target-file-name t)
62                        (lambda _
63                          (set-port-encoding! (current-output-port) "ISO-8859-1")
64                          (display
65                           (apply assert-gulp-pipe*
66                                  `(,hex2
67                                    ,@hex2-flags
68                                    "-f"
69                                    ,(if (not debug?) "stage0/elf32-0header.hex2"
70                                         "stage0/elf32-header.hex2")
71                                    ,@(if crt1 `("-f" ,(target-file-name crt1)) '())
72                                    ,@(if libc `("-f" ,(target-file-name libc)) '())
73                                    ,@(append-map (cut list "-f" <>) input-files)
74                                    "-f"
75                                    ,(if (not debug?) "stage0/elf-0footer.hex2"
76                                         "stage0/elf32-footer-single-main.hex2"))))))
77                      (chmod (target-file-name t) #o755))))
78           (inputs `(,(store #:add-file "stage0/elf32-0header.hex2")
79                     ,@(if crt1 (target-inputs crt1) '())
80                     ,@(if libc (target-inputs libc) '())
81                     ,(store #:add-file "stage0/elf-0footer.hex2")))))
82
83 (define* (bin.mescc input-file-name #:key (cc %MESCC) (hex2 %HEX2) (m1 %M1) (crt1 crt1.hex2) (libc libc-mes.hex2) (dependencies '()) (defines '()) (includes '()))
84   (let* ((base-name (base-name input-file-name ".c"))
85          ;;(foo (format (current-error-port) "bin[~s .c] base=~s\n" input-file-name base-name))
86          (suffix (cond ((not libc) ".0-guile")
87                        ((eq? libc libc-mes.hex2) ".guile")
88                        ((eq? libc libc+tcc-mes.hex2) ".tcc-guile")
89                        (else ".mini-guile")))
90          (target-file-name (string-append base-name suffix))
91          (hex2-target (m1.as input-file-name #:m1 m1 #:cc cc #:defines defines #:includes includes #:dependencies dependencies)))
92     (target (file-name target-file-name)
93             (inputs `(,hex2-target
94                       ,@(if crt1 (list crt1) '())
95                       ,@(if libc (list libc) '())))
96             (method (LINK.hex2 #:hex2 hex2 #:crt1 crt1 #:libc libc #:debug? (eq? libc libc-mes.hex2))))))
97
98 ;;(define mini-libc-mes.E (m1.as "lib/mini-libc-mes.c"))
99
100 (define libc-mes.hex2 (m1.as "lib/libc-mes.c"))
101 (add-target libc-mes.hex2)
102
103 (define mini-libc-mes.hex2 (m1.as "lib/mini-libc-mes.c"))
104 (add-target mini-libc-mes.hex2)
105
106 (define libc+tcc-mes.hex2 (m1.as "lib/libc+tcc-mes.c"))
107 (add-target libc+tcc-mes.hex2)
108
109 (add-target (bin.mescc "stage0/exit-42.c" #:libc #f))
110 (add-target (check "stage0/exit-42.0-guile" #:exit 42))
111
112 (add-target (cpp.mescc "lib/mini-libc-mes.c"))
113 (add-target (compile.mescc "lib/mini-libc-mes.c"))
114
115 (add-target (bin.mescc "stage0/exit-42.c" #:libc mini-libc-mes.hex2))
116 (add-target (check "stage0/exit-42.mini-guile" #:exit 42))
117
118 (add-target (cpp.mescc "lib/libc-mes.c"))
119 (add-target (compile.mescc "lib/libc-mes.c"))
120
121 (add-target (bin.mescc "stage0/exit-42.c"))
122 (add-target (check "stage0/exit-42.guile" #:exit 42))
123
124 (define* (add-scaffold-test name #:key (exit 0) (libc libc-mes.hex2) (libc-gcc libc-gcc.mlibc-o) (includes '()))
125   (add-target (bin.gcc (string-append "scaffold/tests/" name ".c") #:libc libc-gcc #:includes includes))
126   (add-target (check (string-append "scaffold/tests/" name ".mlibc-gcc") #:exit exit))
127
128   (add-target (bin.mescc (string-append "scaffold/tests/" name ".c") #:libc libc #:includes includes))
129   (add-target (check (string-append "scaffold/tests/" name "." (cond ((not libc) "0-")
130                                                                      ((eq? libc mini-libc-mes.hex2) "mini-")
131                                                                      ((eq? libc libc+tcc-mes.hex2) "tcc-")
132                                                                      (else "")) "guile") #:exit exit)))
133
134 (add-target (compile.gcc "lib/crt1.c" #:libc #f))
135 (add-target (compile.gcc "lib/libc-gcc.c" #:libc #f))
136 (add-target (compile.gcc "lib/libc+tcc-gcc.c" #:libc #f))
137 ;;(add-target (compile.gcc "lib/libc+tcc-mes.c" #:libc #f))
138
139 ;;(add-scaffold-test "t" #:libc mini-libc-mes.hex2)
140 (add-scaffold-test "t")
141 ;;(add-scaffold-test "t" #:libc libc+tcc-mes.hex2)
142
143 ;; tests/00: exit, functions without libc
144 (add-scaffold-test "00-exit-0" #:libc #f)
145 (add-scaffold-test "01-return-0" #:libc #f)
146 (add-scaffold-test "02-return-1" #:libc #f #:exit 1)
147 (add-scaffold-test "03-call" #:libc #f)
148 (add-scaffold-test "04-call-0" #:libc #f)
149 (add-scaffold-test "05-call-1" #:libc #f #:exit 1)
150 (add-scaffold-test "06-call-!1" #:libc #f)
151 (add-scaffold-test "07-include" #:libc #f #:includes '("scaffold/tests") #:exit 42)
152
153 (add-target (group "check-scaffold-tests/0" #:dependencies (filter (target-prefix? "check-scaffold/tests/0") %targets)))
154
155 ;; tests/10: control without libc
156 (for-each
157  (cut add-scaffold-test <> #:libc #f)
158  '("10-if-0"
159    "11-if-1"
160    "12-if-=="
161    "13-if-!="
162    "14-if-goto"
163    "15-if-!f"
164    "16-if-t"))
165
166 (add-target (group "check-scaffold-tests/1" #:dependencies (filter (target-prefix? "check-scaffold/tests/1") %targets)))
167
168 ;; tests/20: loop without libc
169 (for-each
170  (cut add-scaffold-test <> #:libc #f)
171  '("20-while"
172    "21-char[]"
173    "22-while-char[]"
174    "23-pointer"))
175
176 (add-target (group "check-scaffold-tests/2" #:dependencies (filter (target-prefix? "check-scaffold/tests/2") %targets)))
177
178 ;; tests/30: call, compare: mini-libc-mes.c
179 (for-each
180  (cut add-scaffold-test <> #:libc mini-libc-mes.hex2)
181  '("30-strlen"
182    "31-eputs"
183    "32-compare"
184    "33-and-or"
185    "34-pre-post"
186    "35-compare-char"
187    "36-compare-arithmetic"
188    "37-compare-assign"
189    "38-compare-call"))
190
191 (add-target (group "check-scaffold-tests/3" #:dependencies (filter (target-prefix? "check-scaffold/tests/3") %targets)))
192
193 ;; tests/40: control: mini-libc-mes.c
194 (for-each
195  (cut add-scaffold-test <> #:libc mini-libc-mes.hex2)
196  '("40-if-else"
197    "41-?"
198    "42-goto-label"
199    "43-for-do-while"
200    "44-switch"
201    "45-void-call"))
202
203 (add-target (group "check-scaffold-tests/4" #:dependencies (filter (target-prefix? "check-scaffold/tests/4") %targets)))
204
205 ;; tests/50: libc-mes.c
206 (for-each
207  add-scaffold-test
208  '("50-assert"
209    "51-strcmp"
210    "52-itoa"
211    "54-argv"))
212
213 (add-target (group "check-scaffold-tests/5" #:dependencies (filter (target-prefix? "check-scaffold/tests/5") %targets)))
214
215 ;; tests/60: building up to scaffold/m.c, scaffold/micro-mes.c
216 (for-each
217  add-scaffold-test
218  '("60-math"
219    "61-array"
220    "63-struct-cell"
221    "64-make-cell"
222    "65-read"
223    "66-local-char-array"))
224
225 (add-target (group "check-scaffold-tests/6" #:dependencies (filter (target-prefix? "check-scaffold/tests/6") %targets)))
226
227 ;; tests/70: and beyond src/mes.c -- building up to 8cc.c, pcc.c, tcc.c, libguile/eval.c
228 (for-each
229  add-scaffold-test
230  '("70-printf"
231    "71-struct-array"
232    "72-typedef-struct-def"
233    "73-union"
234    "74-multi-line-string"
235    "75-struct-union"
236    "76-pointer-arithmetic"
237    "77-pointer-assign"
238    "78-union-struct"
239    "79-int-array"
240    "7a-struct-char-array"
241    "7b-struct-int-array"
242    "7c-dynarray"
243    "7d-cast-char"
244    "7e-struct-array-access"
245    "7f-struct-pointer-arithmetic"
246    "7g-struct-byte-word-field"
247    "7h-struct-assign"
248    "7i-struct-struct"
249    "7j-strtoull"
250    "7k-for-each-elem"
251    "7l-struct-any-size-array"
252    "7m-struct-char-array-assign"
253    "7n-struct-struct-array"))
254
255 (add-target (group "check-scaffold-tests/7" #:dependencies (filter (target-prefix? "check-scaffold/tests/7") %targets)))
256
257 (add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets)))
258
259 ;; tests/80: and beyond tinycc; building GNU GCC and dependencies
260 (for-each
261  (cut add-scaffold-test <> #:libc libc+tcc-mes.hex2 #:libc-gcc libc+tcc-gcc.mlibc-o)
262  '("80-setjmp"
263    "81-qsort"
264    "82-define"))
265
266 (add-target (group "check-scaffold-tests/8" #:dependencies (filter (target-prefix? "check-scaffold/tests/8") %targets)))
267
268 (add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets)))
269
270 (add-target (cpp.mescc "lib/libc+tcc-mes.c"))
271 (add-target (compile.mescc "lib/libc+tcc-mes.c"))
272
273 (define* (add-tcc-test name)
274   (add-target (bin.gcc (string-append "scaffold/tinycc/" name ".c") #:libc libc-gcc.mlibc-o #:includes '("scaffold/tinycc")))
275   (add-target (check (string-append "scaffold/tinycc/" name ".mlibc-gcc") #:baseline (string-append "scaffold/tinycc/" name ".expect")))
276
277   (add-target (bin.mescc (string-append "scaffold/tinycc/" name ".c") #:includes '("scaffold/tinycc")))
278   (add-target (check (string-append "scaffold/tinycc/" name ".guile") #:baseline (string-append "scaffold/tinycc/" name ".expect"))))
279 (map
280  add-tcc-test
281  '("00_assignment"
282    "01_comment"
283    "02_printf"
284    "03_struct"
285    "04_for"
286    "05_array"
287    "06_case"
288    "07_function"
289    "08_while"
290    "09_do_while"
291
292    "10_pointer"
293    "11_precedence"
294    "12_hashdefine"
295    "13_integer_literals"
296    "14_if"
297    "15_recursion"
298    "16_nesting"
299    "17_enum"
300    "18_include"
301    "19_pointer_arithmetic"
302
303    "20_pointer_comparison"
304    "21_char_array"
305    ;;"22_floating_point"       ; float
306    ;;"23_type_coercion"        ; float
307    ;;"24_math_library"         ; float
308    "25_quicksort"
309    ;;"27_sizeof"               ; float
310    ;;"28_strings"              ; TODO: strncpy strchr strrchr memset memcpy memcmp
311    "29_array_address"
312
313    ;;"30_hanoi"                ; fails with GCC
314    "31_args"
315    ;;"32_led"                  ; unsupported: (decl (decl-spec-list (stor-spec (static)) (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (ident "d") (p-expr (fixed "32"))))))
316    ;;"34_array_assignment"     ; fails with GCC
317    "33_ternary_op"
318    "35_sizeof"
319    ;;"36_array_initialisers"   ; unspported: (decl (decl-spec-list (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (ident "Array") (p-expr (fixed "10"))) (initzer (initzer-list (initzer (p-expr (fixed "12"))) (initzer (p-expr (fixed "34"))) (initzer (p-expr (fixed "56"))) (initzer (p-expr (fixed "78"))) (initzer (p-expr (fixed "90"))) (initzer (p-expr (fixed "123"))) (initzer (p-expr (fixed "456"))) (initzer (p-expr (fixed "789"))) (initzer (p-expr (fixed "8642"))) (initzer (p-expr (fixed "9753"))))))))
320    ;; "37_sprintf"             ; integer formatting unsupported
321    ;;"38_multiple_array_index" ; unspported: (decl (decl-spec-list (type-spec (fixed-type "int"))) (init-declr-list (init-declr (array-of (array-of (ident "a") (p-expr (fixed "4"))) (p-expr (fixed "4"))))))
322    ;;"39_typedef"              ; unsupported: (decl (decl-spec-list (stor-spec (typedef)) (type-spec (typename "MyFunStruct"))) (init-declr-list (init-declr (ptr-declr (pointer) (ident "MoreFunThanEver")))))
323
324    ;;"40_stdio"                ; f* functions
325    "41_hashif"
326    ;;"42_function_pointer"     ; f* functions
327    "43_void_param"
328    "44_scoped_declarations"
329    "45_empty_for"           ; unsupported
330    ;;"46_grep"                 ; f* functions
331    "47_switch_return"
332    "48_nested_break"
333    ;;"49_bracket_evaluation"   ; float
334
335    "50_logical_second_arg"
336    ;;"51_static"               ; unsupported: (decl (decl-spec-list (stor-spec (static)) (type-spec (fixed-type "int"))) (init-declr-list (init-declr (ident "fred") (initzer (p-expr (fixed "1234"))))))
337    ;;"52_unnamed_enum"         ; unsupported: (decl (decl-spec-list (stor-spec (typedef)) (type-spec (enum-def (enum-def-list (enum-defn (ident "e")) (enum-defn (ident "f")) (enum-defn (ident "g")))))) (init-declr-list (init-declr (ident "h"))))
338    "54_goto"
339    ;;"55_lshift_type"          ; unsigned
340    ))
341
342 (add-target (group "check-scaffold-tinycc" #:dependencies (filter (target-prefix? "check-scaffold/tinycc") %targets)))
343
344 ;;(add-target (group "check-scaffold" #:dependencies (filter (target-prefix? "check-scaffold") %targets)))
345
346 (add-target (bin.gcc "scaffold/main.c"))
347 (add-target (check "scaffold/main.gcc" #:exit 42))
348
349 (add-target (bin.gcc "scaffold/main.c" #:libc #f))
350 (add-target (check "scaffold/main.mlibc-gcc" #:exit 42))
351
352 (add-target (bin.mescc "scaffold/main.c" #:libc mini-libc-mes.hex2))
353 (add-target (check "scaffold/main.mini-guile" #:exit 42))
354
355 (add-target (bin.mescc "scaffold/main.c"))
356 (add-target (check "scaffold/main.guile" #:exit 42))
357
358
359 (add-target (bin.gcc "scaffold/hello.c"))
360 (add-target (check "scaffold/hello.gcc" #:exit 42))
361
362 (add-target (bin.gcc "scaffold/hello.c" #:libc libc-gcc.mlibc-o))
363 (add-target (check "scaffold/hello.mlibc-gcc" #:exit 42))
364
365 (add-target (bin.mescc "scaffold/hello.c" #:libc mini-libc-mes.hex2))
366 (add-target (check "scaffold/hello.mini-guile" #:exit 42))
367
368 (add-target (bin.mescc "scaffold/hello.c"))
369 (add-target (check "scaffold/hello.guile" #:exit 42))
370
371
372 (add-target (bin.gcc "scaffold/m.c"))
373 (add-target (check "scaffold/m.gcc" #:exit 255))
374
375 (add-target (bin.gcc "scaffold/m.c" #:libc libc-gcc.mlibc-o))
376 (add-target (check "scaffold/m.mlibc-gcc" #:exit 255))
377
378 (add-target (bin.mescc "scaffold/m.c"))
379 (add-target (check "scaffold/m.guile" #:exit 255))
380
381 (add-target (bin.gcc "scaffold/micro-mes.c" #:libc libc-gcc.mlibc-o))
382 (add-target (check "scaffold/micro-mes.mlibc-gcc" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
383
384 (add-target (bin.mescc "scaffold/micro-mes.c"))
385 (add-target (check "scaffold/micro-mes.guile" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
386
387 (add-target (group "check-scaffold" #:dependencies (filter (target-prefix? "check-scaffold") %targets)))
388
389 (define snarf-bases
390   '("gc" "lib" "math" "mes" "posix" "reader" "vector"))
391
392 (define bla
393   `(,@(map (cut string-append "src/" <> ".c") snarf-bases)
394     ,@(map (cut string-append "src/" <> ".mes.h") snarf-bases)
395     ,@(map (cut string-append "src/" <> ".mes.i") snarf-bases)
396     ,@(map (cut string-append "src/" <> ".mes.environment.i") snarf-bases)))
397
398 (define gcc-snarf-targets
399   (list
400    (add-target (snarf "src/gc.c" #:mes? #f))
401    (add-target (snarf "src/lib.c" #:mes? #f))
402    (add-target (snarf "src/math.c" #:mes? #f))
403    (add-target (snarf "src/mes.c" #:mes? #f))
404    (add-target (snarf "src/posix.c" #:mes? #f))
405    (add-target (snarf "src/reader.c" #:mes? #f))
406    (add-target (snarf "src/vector.c" #:mes? #f))))
407
408 (define mes-snarf-targets
409   (list
410    (add-target (snarf "src/gc.c"))
411    (add-target (snarf "src/lib.c" #:mes? #t))
412    (add-target (snarf "src/math.c" #:mes? #t))
413    (add-target (snarf "src/mes.c" #:mes? #t))
414    (add-target (snarf "src/posix.c" #:mes? #t))
415    (add-target (snarf "src/reader.c" #:mes? #t))
416    (add-target (snarf "src/vector.c" #:mes? #t))))
417
418 (add-target (bin.gcc "src/mes.c" #:dependencies gcc-snarf-targets
419                      #:defines `("MES_C_READER=1"
420                                  "MES_C_DEFINE=1"
421                                  "MES_FIXED_PRIMITIVES=1"
422                                  "MES_FULL=1"
423                                  "POSIX=1"
424                                  ,(string-append "VERSION=\"" %version "\"")
425                                  ,(string-append "MODULEDIR=\"" (string-append %prefix (if (string-null? %prefix) "" "/") %moduledir "/") "\"")
426                                  ,(string-append "PREFIX=\"" %prefix "\""))
427                      #:includes '("src")))
428
429 (add-target (bin.gcc "src/mes.c" #:libc libc-gcc.mlibc-o
430                      #:dependencies mes-snarf-targets
431                      #:defines `("MES_C_READER=1"
432                                  "MES_C_DEFINE=1"
433                                  "MES_FIXED_PRIMITIVES=1"
434                                  "MES_FULL=1"
435                                  ,(string-append "VERSION=\"" %version "\"")
436                                  ,(string-append "MODULEDIR=\"" (string-append %prefix (if (string-null? %prefix) "" "/") "/" %moduledir "/") "\"")
437                                  ,(string-append "PREFIX=\"" %prefix "\""))
438                      #:includes '("src")))
439
440 (add-target (bin.mescc "src/mes.c" #:dependencies mes-snarf-targets
441                        #:defines `("MES_C_READER=1"
442                                    "MES_C_DEFINE=1"
443                                    "MES_FIXED_PRIMITIVES=1"
444                                    "MES_FULL=1"
445                                    ,(string-append "VERSION=\"" %version "\"")
446                                    ,(string-append "MODULEDIR=\"" (string-append %prefix (if (string-null? %prefix) "" "/") %moduledir "/") "\"")
447                                    ,(string-append "PREFIX=\"" %prefix "\""))
448                        #:includes '("src")))
449
450 (define mes-tests
451   '("tests/read.test"
452     "tests/base.test"
453     "tests/closure.test"
454     "tests/quasiquote.test"
455     "tests/let.test"
456     "tests/scm.test"
457     "tests/display.test"
458     "tests/cwv.test"
459     "tests/math.test"
460     "tests/vector.test"
461     "tests/srfi-1.test"
462     "tests/srfi-13.test"
463     "tests/srfi-14.test"
464     "tests/optargs.test"
465     "tests/fluids.test"
466     "tests/catch.test"
467     "tests/psyntax.test"
468     "tests/pmatch.test"
469     "tests/let-syntax.test"
470     "tests/guile.test"
471     "tests/record.test"
472     ;;sloooowwww
473     ;;"tests/match.test"
474     ;;"tests/peg.test"
475     ))
476
477 (define (add-guile-test o)
478   (add-target (target (file-name o)))
479   (add-target (check o)))
480
481 (define (add-mes.gcc-test o)
482   (add-target (target (file-name o)))
483   (add-target (check o #:dependencies (list (get-target "src/mes.mlibc-gcc")))))
484
485 (define (add-mes.guile-test o)
486   (add-target (target (file-name o)))
487   (add-target (check o #:dependencies (list (get-target "src/mes.guile")))))
488
489 (for-each add-guile-test (map (cut string-append <> "-guile") mes-tests))
490
491 ;; takes long, and should always pass if...
492 ;;(for-each add-mes.gcc-test mes-tests)
493
494 ;; ...mes.guile passes :-)
495 (for-each add-mes.guile-test mes-tests)
496
497 ;; FIXME: run tests/base.test
498 (setenv "MES" "src/mes.guile")
499
500 (add-target (install "guile/mescc.scm" #:dir "bin" #:substitutes #t))
501 (add-target (install "scripts/mescc.mes" #:dir "bin" #:substitutes #t))
502 (add-target (install "scripts/repl.mes" #:dir "bin" #:substitutes #t))
503 (define bootstrap? #f)
504 (if bootstrap?
505     (add-target (install "src/mes.mes" #:dir "bin" #:installed-name "mes"))
506     (add-target (install "src/mes.guile" #:dir "bin" #:installed-name "mes")))
507
508 (define* ((install-dir #:key dir) name)
509   (add-target (install name  #:dir (string-append dir "/" (dirname name)))))
510
511 (add-target (install "module/mes/base-0.mes" #:dir (string-append %moduledir "/mes") #:substitutes #t))
512 (add-target (install "module/language/c99/compiler.mes" #:dir (string-append %moduledir "/language/c99") #:substitutes #t))
513
514 (define %module-dir "share/mes")
515 (for-each
516  (lambda (f)
517    ((install-dir #:dir (string-append %module-dir)) f))
518  '("module/language/c99/compiler.mes"
519    "module/language/c99/compiler.scm"
520    "module/language/c99/info.mes"
521    "module/language/c99/info.scm"
522    "module/language/paren.mes"
523    "module/mes/M1.mes"
524    "module/mes/M1.scm"
525    "module/mes/as-i386.mes"
526    "module/mes/as-i386.scm"
527    "module/mes/as.mes"
528    "module/mes/as.scm"
529    ;;"module/mes/base-0.mes"
530    "module/mes/base.mes"
531    "module/mes/bytevectors.mes"
532    "module/mes/bytevectors.scm"
533    "module/mes/catch.mes"
534    "module/mes/display.mes"
535    "module/mes/elf.mes"
536    "module/mes/elf.scm"
537    "module/mes/fluids.mes"
538    "module/mes/getopt-long.mes"
539    "module/mes/getopt-long.scm"
540    "module/mes/guile.mes"
541    "module/mes/lalr.mes"
542    "module/mes/lalr.scm"
543    "module/mes/let.mes"
544    "module/mes/match.mes"
545    "module/mes/match.scm"
546    "module/mes/optargs.mes"
547    "module/mes/optargs.scm"
548    "module/mes/peg.mes"
549    "module/mes/peg/cache.scm"
550    "module/mes/peg/codegen.scm"
551    "module/mes/peg/simplify-tree.scm"
552    "module/mes/peg/string-peg.scm"
553    "module/mes/peg/using-parsers.scm"
554    "module/mes/pmatch.mes"
555    "module/mes/pmatch.scm"
556    "module/mes/posix.mes"
557    "module/mes/pretty-print.mes"
558    "module/mes/pretty-print.scm"
559    "module/mes/psyntax-0.mes"
560    "module/mes/psyntax-1.mes"
561    "module/mes/psyntax.mes"
562    "module/mes/psyntax.pp"
563    "module/mes/psyntax.ss"
564    "module/mes/quasiquote.mes"
565    "module/mes/quasisyntax.mes"
566    "module/mes/quasisyntax.scm"
567    "module/mes/read-0.mes"
568    "module/mes/repl.mes"
569    "module/mes/scm.mes"
570    "module/mes/syntax.mes"
571    "module/mes/syntax.scm"
572    "module/mes/test.mes"
573    "module/mes/tiny-0.mes"
574    "module/mes/type-0.mes"
575    "module/nyacc/lalr.mes"
576    "module/nyacc/lang/c99/cpp.mes"
577    "module/nyacc/lang/c99/parser.mes"
578    "module/nyacc/lang/c99/pprint.mes"
579    "module/nyacc/lang/calc/parser.mes"
580    "module/nyacc/lang/util.mes"
581    "module/nyacc/lex.mes"
582    "module/nyacc/parse.mes"
583    "module/nyacc/util.mes"
584    "module/rnrs/arithmetic/bitwise.mes"
585    "module/srfi/srfi-0.mes"
586    "module/srfi/srfi-1.mes"
587    "module/srfi/srfi-1.scm"
588    "module/srfi/srfi-13.mes"
589    "module/srfi/srfi-14.mes"
590    "module/srfi/srfi-16.mes"
591    "module/srfi/srfi-16.scm"
592    "module/srfi/srfi-26.mes"
593    "module/srfi/srfi-26.scm"
594    "module/srfi/srfi-43.mes"
595    "module/srfi/srfi-9.mes"
596    "module/sxml/xpath.mes"
597    "module/sxml/xpath.scm"))
598
599 (define* ((install-guile-dir #:key dir) name)
600   (add-target (install (string-append "guile/" name) #:dir (string-append dir "/" (dirname name)))))
601
602 (for-each
603  (lambda (f)
604    ((install-guile-dir #:dir (string-append %guiledir)) f))
605  %scm-files)
606
607 (for-each
608  (lambda (f)
609    ((install-guile-dir #:dir (string-append %godir)) f))
610  %go-files)
611
612 (add-target (install "lib/crt1.hex2" #:dir "lib"))
613 (add-target (install "lib/libc-mes.M1" #:dir "lib"))
614 (add-target (install "lib/libc-mes.hex2" #:dir "lib"))
615 (add-target (install "lib/libc+tcc-mes.M1" #:dir "lib"))
616 (add-target (install "lib/libc+tcc-mes.hex2" #:dir "lib"))
617 (add-target (install "lib/mini-libc-mes.M1" #:dir "lib"))
618 (add-target (install "lib/mini-libc-mes.hex2" #:dir "lib"))
619
620 (add-target (install "lib/crt1.mlibc-o" #:dir "lib"))
621 (add-target (install "lib/libc-gcc.mlibc-o" #:dir "lib"))
622 (add-target (install "lib/libc+tcc-gcc.mlibc-o" #:dir "lib"))
623
624 (for-each
625  (lambda (f)
626    ((install-dir #:dir "share/") f))
627  '("include/alloca.h"
628    "include/assert.h"
629    "include/ctype.h"
630    "include/dlfcn.h"
631    "include/errno.h"
632    "include/fcntl.h"
633    "include/features.h"
634    "include/inttypes.h"
635    "include/libgen.h"
636    "include/limits.h"
637    "include/locale.h"
638    "include/math.h"
639    "include/mlibc.h"
640    "include/setjmp.h"
641    "include/signal.h"
642    "include/stdarg.h"
643    "include/stdbool.h"
644    "include/stdint.h"
645    "include/stdio.h"
646    "include/stdlib.h"
647    "include/stdnoreturn.h"
648    "include/string.h"
649    "include/strings.h"
650    "include/sys/cdefs.h"
651    "include/sys/mman.h"
652    "include/sys/stat.h"
653    "include/sys/time.h"
654    "include/sys/timeb.h"
655    "include/sys/types.h"
656    "include/sys/ucontext.h"
657    "include/sys/wait.h"
658    "include/time.h"
659    "include/unistd.h"))
660
661 (for-each
662  (compose add-target (cut install <> #:dir "share/doc/mes"))
663  '("AUTHORS"
664    ;;"ChangeLog"
665    "BOOTSTRAP"
666    "COPYING"
667    "HACKING"
668    "INSTALL"
669    "NEWS"
670    "README"
671    "doc/ANNOUNCE-0.11"))
672
673 (add-target (install "doc/fosdem/fosdem.pdf" #:dir "share/doc/mes"))
674
675 (define (main args)
676   (cond ((member "all-go" args) #t)
677         ((member "clean-go" args) (map delete-file (filter file-exists? %go-files)))
678         ((member "clean" args) (clean))
679         ((member "list" args) (display (string-join (map target-file-name %targets) "\n" 'suffix)))
680         ((member "help" args) (format #t "Usage: ./make.scm [TARGET]...
681
682 Targets:
683     all
684     all-go
685     check
686     clean
687     clean-go
688     help~a
689     install
690     list
691 "
692                                       (string-join (filter (negate (cut string-index <> #\/)) (map target-file-name %targets)) "\n    " 'prefix)))
693         (else
694          (let ((targets (match args
695                           (() (filter (conjoin (negate install-target?)
696                                                (negate check-target?))
697                                       %targets))
698                           ((? (cut member "all" <>)) (filter (conjoin (negate install-target?)
699                                                                       (negate check-target?))
700                                                              %targets))
701                           ((? (cut member "check" <>)) (filter check-target? %targets))
702                           ((? (cut member "install" <>)) (filter install-target? %targets))
703                           (_ (filter-map (cut get-target <>) args)))))
704            ;;((@@ (guix make) store) #:print 0)
705            (for-each build targets)
706            (exit %status)))))
707
708 (main (cdr (command-line)))