forked from apaz-cli/pgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·116 lines (92 loc) · 3.16 KB
/
build
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
#!/bin/sh
# Generate utf8 include with xxd
if command -v xxd >/dev/null; then
xxd -i src/utf8.h | sed 's/\([0-9a-f]\)$/\0, 0x00/' > src/strutf8.xxd
xxd -i src/arena.h | sed 's/\([0-9a-f]\)$/\0, 0x00/' > src/strarena.xxd
else
echo "xxd is not installed." 1>&2
echo "This is fine, until you want to change src/utf8.h or src/arena.h." 1>&2
echo "Continuing." 1>&2
fi
if [ "$1" = "install" ]; then
# Build release
cc src/pgen.c -o pgen -O3 -march=native
# Install binary
sudo cp pgen /bin/pgen
echo "Installed pgen."
elif [ "$1" = "profile" ]; then
# Build pgen and generate tokenizer and parser.
cc src/pgen.c -o pgen
./pgen -u examples/pl0.tok examples/pl0.peg -o examples/pl0.h
cd examples/
# Build pl0 tokenizer/parser for profiling
cc pl0.c -static -O3 -march=native -ggdb3
# Profile with Callgrind
valgrind --tool=callgrind --collect-jumps=yes --dump-instr=yes ./a.out 2>/dev/null
callgrind_annotate callgrind.out* > calls
nano calls
# Profile with Cachegrind
valgrind --tool=cachegrind --branch-sim=yes ./a.out 2>/dev/null
cg_annotate cachegrind.out.* > cache
nano cache
# Dump the instructions
objdump -D a.out > dump
nano dump
# Make a flamegraph
#flamegraph ./a.out
# Clean up
rm dump calls cache cachegrind.out.* callgrind.out.* 2>/dev/null
cd ..
elif [ "$1" = "macrocheck" ]; then
# Build pgen and examples
cc src/pgen.c -o pgen
./pgen examples/pl0.peg -o examples/pl0.h
# Check
echo '#include <limits.h>' > .empty.c
echo '#include <stddef.h>' >> .empty.c
echo '#include <stdint.h>' >> .empty.c
echo '#include <stdio.h>' >> .empty.c
echo '#include <stdlib.h>' >> .empty.c
echo '#include <string.h>' >> .empty.c
echo '#include <inttypes.h>' >> .empty.c
cc -dM -E .empty.c | sort > .predef.c
cc -dM -E examples/pl0.h | sort > .nowdef.c
MACRS="$(diff .predef.c .nowdef.c | grep '>' | sed 's/^<\ //g' | cut -d' ' -f2- \
| grep -v '#define PGEN_' \
| grep -v '#define PL0_' \
| grep -v '#define UTF8_' \
| grep -v '#define recover' \
| grep -v '#define PRI_CODEPOINT')"
rm .empty.c .predef.c .nowdef.c
if [ ! "$MACRS" = "" ]; then
echo "Forgot to undef the following macros:"
printf "%s\n" "$MACRS"
exit 1;
fi
else
# Build debug
cc src/pgen.c -o pgen --std=c99 -g -Wall -Wextra -Wpedantic -Wno-unused-parameter -Wno-unused-variable -Wconversion
if [ ! "$?" -eq 0 ]; then exit 1; fi
# Test
if [ "$1" = "test" ]; then
shift;
# Generate pl0 example
./pgen examples/pl0.peg -o examples/pl0.h $@
if [ ! "$?" -eq 0 ]; then exit 1; fi
# Build and run pl0 example
cd examples/
cc pl0.c -g -Wconversion
if [ ! "$?" -eq 0 ]; then exit 1; fi
./a.out > ../.testast.json
if [ ! "$?" -eq 0 ]; then exit 1; fi
cd ..
DIFF="$(diff .refast.json .testast.json)"
if [ ! "$DIFF" = "" ]; then
echo "The test AST was different than expected.\nDiff:"
echo "$DIFF"
fi
echo "Built and tested pgen."
else
echo "Built pgen."
fi
fi