build: Fix snarf dependencies.
[mes.git] / make.scm
1 #! /usr/bin/env guile
2 !#
3
4 (set! %load-path (cons "guile" %load-path))
5 (set! %load-path (cons "../guix" %load-path))
6 (set! %load-compiled-path (cons "guile" %load-compiled-path))
7 (set! %load-compiled-path (cons "../guix" %load-compiled-path))
8
9 (use-modules (guix shell-utils))
10
11 ;; FIXME: .go dependencies
12 ;; workaround: always update .go before calculating hashes
13 ;;(use-modules ((mes make) #:select (sytem**)))
14 (let* ((scm-files '("guix/make.scm"
15                     "guix/records.scm"
16                     "guix/shell-utils.scm"
17                     "language/c99/compiler.scm"
18                     "mes/as-i386.scm"
19                     "mes/as.scm"
20                     "mes/elf.scm"
21                     "mes/M1.scm")))
22   (setenv "srcdir" "guile")
23   (setenv "host" %host-type)
24   (with-directory-excursion "guile"
25     (apply system* `("guile"
26                      "--no-auto-compile"
27                      "-L" "."
28                      "-C" "."
29                      "-s"
30                      "../build-aux/compile-all.scm"
31                      ,@scm-files))))
32
33 (use-modules (srfi srfi-1)
34              (srfi srfi-26)
35              (ice-9 curried-definitions)
36              (ice-9 match)
37              (guix make))
38
39 (add-target (bin.mescc "stage0/exit-42.c" #:libc #f))
40 (add-target (check "stage0/exit-42.0-guile" #:signal 11))  ; FIXME: segfault
41
42 (add-target (bin.mescc "stage0/exit-42.c" #:libc mini-libc-mes.E))
43 (add-target (check "stage0/exit-42.mini-guile" #:exit 42))
44
45 (add-target (bin.mescc "stage0/exit-42.c"))
46 (add-target (check "stage0/exit-42.guile" #:exit 42))
47
48 (define* (add-scaffold-test name #:key (exit 0) (libc libc-mes.E))
49   (add-target (bin.gcc (string-append "scaffold/tests/" name ".c") #:libc #f))
50   (add-target (check (string-append "scaffold/tests/" name ".mlibc-gcc") #:exit exit))
51
52   (add-target (bin.mescc (string-append "scaffold/tests/" name ".c") #:libc libc))
53   (add-target (check (string-append "scaffold/tests/" name "." (cond ((not libc) "0-")
54                                                                      ((eq? libc mini-libc-mes.E) "mini-")
55                                                                      (else "")) "guile") #:exit exit)))
56
57 ;; tests/00: exit, functions without libc
58 (add-scaffold-test "00-exit-0" #:libc #f)
59 (add-scaffold-test "01-return-0" #:libc #f)
60 (add-scaffold-test "02-return-1" #:libc #f #:exit 1)
61 (add-scaffold-test "03-call" #:libc #f)
62 (add-scaffold-test "04-call-0" #:libc #f)
63 (add-scaffold-test "05-call-1" #:libc #f #:exit 1)
64 (add-scaffold-test "06-call-!1" #:libc #f)
65
66 (add-target (group "check-scaffold-tests/0" #:dependencies (filter (target-prefix? "check-scaffold/tests/0") %targets)))
67
68 ;; tests/10: control without libc
69 (for-each
70  (cut add-scaffold-test <> #:libc #f)
71  '("10-if-0"
72    "11-if-1"
73    "12-if-=="
74    "13-if-!="
75    "14-if-goto"
76    "15-if-!f"
77    "16-if-t"))
78
79 (add-target (group "check-scaffold-tests/1" #:dependencies (filter (target-prefix? "check-scaffold/tests/1") %targets)))
80
81 ;; tests/20: loop without libc
82 (for-each
83  (cut add-scaffold-test <> #:libc #f)
84  '("20-while"
85    "21-char[]"
86    "22-while-char[]"))
87
88 (add-target (group "check-scaffold-tests/2" #:dependencies (filter (target-prefix? "check-scaffold/tests/2") %targets)))
89
90 ;; tests/30: call, compare: mini-libc-mes.c
91 (for-each
92  (cut add-scaffold-test <> #:libc mini-libc-mes.E)
93  '("30-strlen"
94    "31-eputs"
95    "32-compare"
96    "33-and-or"
97    "34-pre-post"
98    "35-compare-char"
99    "36-compare-arithmetic"
100    "37-compare-assign"
101    "38-compare-call"))
102
103 (add-target (group "check-scaffold-tests/3" #:dependencies (filter (target-prefix? "check-scaffold/tests/3") %targets)))
104
105 ;; tests/40: control: mini-libc-mes.c
106 (for-each
107  (cut add-scaffold-test <> #:libc mini-libc-mes.E)
108  '("40-if-else"
109    "41-?"
110    "42-goto-label"
111    "43-for-do-while"
112    "44-switch"
113    "45-void-call"))
114
115 (add-target (group "check-scaffold-tests/4" #:dependencies (filter (target-prefix? "check-scaffold/tests/4") %targets)))
116
117 ;; tests/50: libc-mes.c
118 (for-each
119  add-scaffold-test
120  '("50-assert"
121    "51-strcmp"
122    "52-itoa"
123    "54-argv"))
124
125 (add-target (group "check-scaffold-tests/5" #:dependencies (filter (target-prefix? "check-scaffold/tests/5") %targets)))
126
127 ;; tests/60: building up to scaffold/m.c, scaffold/micro-mes.c
128 (for-each
129  add-scaffold-test
130  '("60-math"
131    "61-array"
132    "63-struct-cell"
133    "64-make-cell"
134    "65-read"))
135
136 (add-target (group "check-scaffold-tests/6" #:dependencies (filter (target-prefix? "check-scaffold/tests/6") %targets)))
137
138 ;; tests/70: and beyond src/mes.c -- building up to 8cc.c, pcc.c, tcc.c, libguile/eval.c
139 (for-each
140  add-scaffold-test
141  '("70-printf"
142    "71-struct-array"))
143
144 (add-target (group "check-scaffold-tests/7" #:dependencies (filter (target-prefix? "check-scaffold/tests/7") %targets)))
145
146 (add-target (group "check-scaffold-tests" #:dependencies (filter (target-prefix? "check-scaffold/tests") %targets)))
147
148
149 (define* (add-tcc-test name)
150   (add-target (bin.gcc (string-append "scaffold/tinycc/" name ".c") #:libc #f #:includes '("scaffold/tinycc")))
151   (add-target (check (string-append "scaffold/tinycc/" name ".mlibc-gcc") #:baseline (string-append "scaffold/tinycc/" name ".expect")))
152
153   (add-target (bin.mescc (string-append "scaffold/tinycc/" name ".c") #:includes '("scaffold/tinycc")))
154   (add-target (check (string-append "scaffold/tinycc/" name ".guile") #:baseline (string-append "scaffold/tinycc/" name ".expect"))))
155 (map
156  add-tcc-test
157  '("00_assignment"
158    "01_comment"
159    "02_printf"
160    "03_struct"
161    "04_for"
162    "05_array"
163    "06_case"
164    "07_function"
165    "08_while"
166    "09_do_while"
167
168    "10_pointer"
169    "11_precedence"
170    "12_hashdefine"
171    "13_integer_literals"
172    "14_if"
173    "15_recursion"
174    "16_nesting"
175    "17_enum"
176    "18_include"
177    "19_pointer_arithmetic"
178
179    "20_pointer_comparison"
180    "21_char_array"
181    ;;"22_floating_point"       ; float
182    ;;"23_type_coercion"        ; float
183    ;;"24_math_library"         ; float
184    "25_quicksort"
185    ;;"27_sizeof"               ; float
186    ;;"28_strings"              ; TODO: strncpy strchr strrchr memset memcpy memcmp
187    "29_array_address"
188
189    ;;"30_hanoi"                ; fails with GCC
190    "31_args"
191    ;;"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"))))))
192    ;;"34_array_assignment"     ; fails with GCC
193    "33_ternary_op"
194    "35_sizeof"
195    ;;"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"))))))))
196    ;; "37_sprintf"             ; integer formatting unsupported
197    ;;"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"))))))
198    ;;"39_typedef"              ; unsupported: (decl (decl-spec-list (stor-spec (typedef)) (type-spec (typename "MyFunStruct"))) (init-declr-list (init-declr (ptr-declr (pointer) (ident "MoreFunThanEver")))))
199
200    ;;"40_stdio"                ; f* functions
201    "41_hashif"
202    ;;"42_function_pointer"     ; f* functions
203    "43_void_param"
204    "44_scoped_declarations"
205    ;; "45_empty_for"           ; unsupported
206    ;;"46_grep"                 ; f* functions
207    "47_switch_return"
208    "48_nested_break"
209    ;;"49_bracket_evaluation"   ; float
210
211    "50_logical_second_arg"
212    ;;"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"))))))
213    ;;"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"))))
214    "54_goto"
215    ;;"55_lshift_type"          ; unsigned
216    ))
217
218 (add-target (group "check-scaffold-tinycc" #:dependencies (filter (target-prefix? "check-scaffold/tinycc") %targets)))
219
220 ;;(add-target (group "check-scaffold" #:dependencies (filter (target-prefix? "check-scaffold") %targets)))
221
222 (add-target (bin.gcc "scaffold/hello.c"))
223 (add-target (check "scaffold/hello.gcc" #:exit 42))
224
225 (add-target (bin.gcc "scaffold/hello.c" #:libc #f))
226 (add-target (check "scaffold/hello.mlibc-gcc" #:exit 42))
227
228 (add-target (bin.mescc "scaffold/hello.c" #:libc mini-libc-mes.E))
229 (add-target (check "scaffold/hello.mini-guile" #:exit 42))
230
231 (add-target (bin.mescc "scaffold/hello.c"))
232 (add-target (check "scaffold/hello.guile" #:exit 42))
233
234
235 (add-target (bin.gcc "scaffold/m.c"))
236 (add-target (check "scaffold/m.gcc" #:exit 255))
237
238 (add-target (bin.gcc "scaffold/m.c" #:libc #f))
239 (add-target (check "scaffold/m.mlibc-gcc" #:exit 255))
240
241 (add-target (bin.mescc "scaffold/m.c"))
242 (add-target (check "scaffold/m.guile" #:exit 255))
243
244 (add-target (bin.gcc "scaffold/micro-mes.c" #:libc #f))
245 (add-target (check "scaffold/micro-mes.mlibc-gcc" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
246
247 (add-target (bin.mescc "scaffold/micro-mes.c"))
248 (add-target (check "scaffold/micro-mes.guile" #:exit 6)) ; arg1 arg2 arg3 arg4 arg5
249
250 (define snarf-bases
251   '("gc" "lib" "math" "mes" "posix" "reader" "vector"))
252
253 (define bla
254   `(,@(map (cut string-append "src/" <> ".c") snarf-bases)
255     ,@(map (cut string-append "src/" <> ".mes.h") snarf-bases)
256     ,@(map (cut string-append "src/" <> ".mes.i") snarf-bases)
257     ,@(map (cut string-append "src/" <> ".mes.environment.i") snarf-bases)))
258
259 (define gcc-snarf-targets
260   (list
261    (add-target (snarf "src/gc.c" #:mes? #f))
262    (add-target (snarf "src/lib.c" #:mes? #f))
263    (add-target (snarf "src/math.c" #:mes? #f))
264    (add-target (snarf "src/mes.c" #:mes? #f))
265    (add-target (snarf "src/posix.c" #:mes? #f))
266    (add-target (snarf "src/reader.c" #:mes? #f))
267    (add-target (snarf "src/vector.c" #:mes? #f))))
268
269 (define mes-snarf-targets
270   (list
271    (add-target (snarf "src/gc.c"))
272    (add-target (snarf "src/lib.c" #:mes? #t))
273    (add-target (snarf "src/math.c" #:mes? #t))
274    (add-target (snarf "src/mes.c" #:mes? #t))
275    (add-target (snarf "src/posix.c" #:mes? #t))
276    (add-target (snarf "src/reader.c" #:mes? #t))
277    (add-target (snarf "src/vector.c" #:mes? #t))))
278
279 (define VERSION "0.8")
280 (define PREFIX (or (getenv "PREFIX") "/usr/local"))
281 (define DATADIR (or (getenv "DATADIR") (string-append PREFIX " /share")))
282 (define MODULEDIR (or (getenv "MODULEDIR") (string-append DATADIR "/module/")))
283
284 (add-target (bin.gcc "src/mes.c" #:dependencies gcc-snarf-targets
285                      #:defines `("FIXED_PRIMITIVES=1"
286                                  "MES_FULL=1"
287                                  "POSIX=1"
288                                  ,(string-append "VERSION=\"" VERSION "\"")
289                                  ,(string-append "MODULEDIR=\"" MODULEDIR "\"")
290                                  ,(string-append "PREFIX=\"" PREFIX "\""))))
291
292 (add-target (bin.gcc "src/mes.c" #:libc #f
293                      #:dependencies mes-snarf-targets
294                      #:defines `("FIXED_PRIMITIVES=1"
295                                  "MES_FULL=1"
296                                  ,(string-append "VERSION=\"" VERSION "\"")
297                                  ,(string-append "MODULEDIR=\"" MODULEDIR "\"")
298                                  ,(string-append "PREFIX=\"" PREFIX "\""))))
299
300 (add-target (bin.mescc "src/mes.c" #:dependencies mes-snarf-targets
301                        #:defines `("FIXED_PRIMITIVES=1"
302                                    "MES_FULL=1"
303                                  ,(string-append "VERSION=\"" VERSION "\"")
304                                  ,(string-append "MODULEDIR=\"" MODULEDIR "\"")
305                                  ,(string-append "PREFIX=\"" PREFIX "\""))))
306
307 (define mes-tests
308   '("tests/read.test"
309     "tests/base.test"
310     "tests/closure.test"
311     "tests/quasiquote.test"
312     "tests/let.test"
313     "tests/scm.test"
314     "tests/display.test"
315     "tests/cwv.test"
316     "tests/math.test"
317     "tests/vector.test"
318     "tests/srfi-1.test"
319     "tests/srfi-13.test"
320     "tests/srfi-14.test"
321     "tests/optargs.test"
322     "tests/fluids.test"
323     "tests/catch.test"
324     "tests/psyntax.test"
325     "tests/pmatch.test"
326     "tests/let-syntax.test"
327     "tests/guile.test"
328     "tests/record.test"
329     ;;sloooowwww
330     ;;"tests/match.test"
331     ;;"tests/peg.test"
332     ))
333
334 (define (add-mes.gcc-test o)
335   (add-target (target (file-name o)))
336   (add-target (check o #:dependencies (list (get-target "src/mes.mlibc-gcc")))))
337
338 (define (add-mes.guile-test o)
339   (add-target (target (file-name o)))
340   (add-target (check o #:dependencies (list (get-target "src/mes.guile")))))
341
342 ;; takes long, and should always pass if...
343 ;;(for-each add-mes.gcc-test mes-tests)
344
345 ;; ...mes.guile passes :-)
346 (for-each add-mes.guile-test mes-tests)
347
348 ;; FIXME: run tests/base.test
349 (setenv "MES" "src/mes.guile")
350
351 (define (main args)
352   (cond ((member "clean" args) (clean))
353         ((member "help" args) (format #t "Usage: ./make.scm [TARGET]...
354
355 Targets:
356     all
357     check
358     clean
359     help~a
360 "
361                                       ;;(string-join (map target-file-name %targets) "\n    " 'prefix)
362                                       (string-join (filter (negate (cut string-index <> #\/)) (map target-file-name %targets)) "\n    " 'prefix)))
363         (else
364          (let ((targets (match args
365                           (() (filter (negate check-target?) %targets))
366                           ((? (cut member "all" <>)) (filter (negate check-target?) %targets))
367                           ((? (cut member "check" <>)) (filter check-target? %targets))
368                           (_ (filter-map (cut get-target <>) args)))))
369            (for-each build targets)
370            ;;((@@ (mes make) store) #:print 0)
371            (exit %status)))))
372
373 (main (cdr (command-line)))