aboutsummaryrefslogtreecommitdiff
path: root/ninja.m4
blob: 7a22ba03e5f63c5a29fff471e0de2d8c133f62cc (plain)
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
divert(-1)
changequote([,])

define([map], [ifelse([$2],,, [dnl
$1($2)[]map([$1], shift(shift($@)))dnl
])])

define([first], [$1 ])

divert(0)

# rules

cflags = -Wall -Wextra -g -I.
bin    = bin
leak   = valgrind -s --leak-check=full --show-leak-kinds=all

rule cc
  depfile = $out.d
  command = gcc -MD -MF $out.d $cflags $in -o $out
rule shared
  depfile = $out.d
  command = gcc -MD -MF $out.d $cflags -shared -fPIC $in -o $out
rule m4
  command = m4 $m4flags $in > $out
rule leak
  command = $leak $in

define([sbuild], [
build $bin/$1: cc $2
  cflags = $cflags $3
build $1: leak $bin/$1
])

build $bin/generate-parser: cc demos/generate-parser.c
  cflags = $cflags -rdynamic
rule parser_gen
  command = $leak $bin/generate-parser -o $$(echo $out | cut -f1 -d'.') -t $tabletype $in

define([gbuild], [ifelse(
[$1], [from-gram], [
build $bin/$2-gram.c: gram_parse $3/$2.g | $bin/gram-parser
build $bin/$2-gram.so: shared $bin/$2-gram.c],

[$1], [from-defs], [
build $bin/$2-gram.so: shared $3/$2-defs.c])

build $bin/$2.c $bin/$2.h: parser_gen $bin/$2-gram.so | $bin/generate-parser tables
  tabletype = lalr-table
build $bin/$2-parser: cc $3/$2-skeleton.c $5 | $bin/$2.c $bin/$2.h
build $2: phony $bin/$2-parser
])

# building tools

build $bin/slr-table.so:  shared slr-table.c
build $bin/clr-table.so:  shared clr-table.c
build $bin/lalr-table.so: shared clr-table.c
  cflags = $cflags -D_LAZY_LALR
build tables: phony $bin/slr-table.so $bin/clr-table.so $bin/lalr-table.so

gbuild(from-defs, gram, demos/sample-files, lalr-table)
rule gram_parse
  command = $leak $bin/gram-parser < $in > $out

# build actual

gbuild(from-gram, lbp, demos/sample-files, lalr-table, util/dict.c)
rule lbp_parse
  command = $leak $bin/lbp-parser < $in
build lbp_test: lbp_parse demos/sample-files/lbp-code.lbp | lbp

gbuild(from-gram, calc, demos/sample-files, lalr-table)
rule calc_parse
  command = $leak $bin/calc-parser 5+3
build calc_test: calc_parse | calc

define([standalone_table], [
[util-tables, util-tables.c, -D_UTIL_TABLES_STANDALONE],
[slr-table,   slr-table.c, -D_SLR_TABLE_STANDALONE],
[clr-table,   clr-table.c, -D_CLR_TABLE_STANDALONE],
[lalr-table,  clr-table.c, -D_CLR_TABLE_STANDALONE -D_LAZY_LALR],
[lr-parser,   lr-parser.c, -D_LR_PARSER_STANDALONE]])

map([sbuild], standalone_table)

# targets

rule ninja_clean
  command = ninja -t clean
build clean: ninja_clean

# default map([first], standalone_table)
default calc_test

# regeneration

rule regen
  command = m4 ninja.m4 > build.ninja
  generator = 1
  description = Regen build.ninja

build build.ninja: regen ninja.m4
build configure:   regen