-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMakefile
184 lines (150 loc) · 4.68 KB
/
Makefile
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
CODEDIRS=./src/grammar ./src/grammar/rolls ./src/grammar/util ./src/grammar/operations ./src/grammar/external
INCDIRS=./src/grammar ./src/grammar/external/pcg-c/include
PCG_SRC=./src/grammar/external/pcg-c/src
$(info CC is $(CC))
ifeq ($(CC),g++)
STANDARD= -std=c++11
else ifeq ($(CC),clang++)
STANDARD= -std=c++11
else ifeq ($(filter $(CC),gcc cc),gcc cc)
STANDARD= -std=c99
else
STANDARD= -std=c99
endif
.DEFAULT_GOAL := all
OPT=-O3 \
$(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \
-Wcast-qual -Wdisabled-optimization -Winit-self \
-Wmissing-declarations -Wmissing-include-dirs \
-Wredundant-decls -Wshadow -Wsign-conversion \
-Wundef -Wno-unused -Wformat=2 \
-Wconversion -Wimplicit-fallthrough
ifneq ($(shell uname -s), Darwin)
OPT := $(OPT) -Wl,-z,nodlopen -Wl,-z,noexecstack \
-Wl,-z,relro
endif
# -ffast-math # Problematic for Python
# YACC/LEX fails for the following, so disabled:
# -Wswitch-default -Wstrict-overflow=5
# EMCC fails for the following, so disabled:
# -Wlogical-op
# === DEBUG OPTIONS ====
ASAN_FLAGS= -fsanitize=address \
-fsanitize-recover=address \
-fsanitize-address-use-after-scope \
-fno-omit-frame-pointer -static-libasan -g
GDB_FLAGS= -g -gdwarf-5
DEBUG?=0
ifeq ($(DEBUG), 1)
# Valgrind
OPT=-O0 $(GDB_FLAGS)
PARSER_DEBUG:=--debug --verbose
else
ifeq ($(DEBUG), 2)
# ASAN
OPT=-O0 $(ASAN_FLAGS)
PARSER_DEBUG:=
else
ifeq ($(DEBUG), 3)
# ASAN
OPT=-O0 $(GDB_FLAGS) -lefence
PARSER_DEBUG:=
else
# USUAL
PARSER_DEBUG:=
endif
endif
endif
USE_SECURE_RANDOM?=0
ifeq ($(USE_SECURE_RANDOM), 1)
#$(shell echo "Using Fast, but Cryptographically insecure random fn")
ARC4RANDOM:=-lbsd `pkg-config --libs libbsd`
else
#$(shell echo abc) "Using Cryptographically Secure, but slow random fn")
ARC4RANDOM:=
endif
USE_CLT=0
YACC_FALLBACK?=0
ifeq ($(YACC_FALLBACK), 1)
#$(shell echo USING YACC)
PARSER:=yacc
else
#$(shell echo USING BISON)
PARSER:=bison --yacc
endif
LEX_FALLBACK?=0
ifeq ($(LEX_FALLBACK), 1)
#$(shell echo USING LEX)
LEXER:=lex
else
#$(shell echo USING FLEX)
LEXER:=flex -f -Ca -Ce -Cr
endif
# add flags and the include paths
DEFS=-DUSE_SECURE_RANDOM=${USE_SECURE_RANDOM} -DJUST_YACC=${YACC_FALLBACK} -DUSE_CLT=${USE_CLT}
CFLAGS=$(foreach D,$(INCDIRS),-I$(D)) $(OPT) $(DEFS)
# add flags to build for shared library and add include paths
SHAREDCFLAGS=-fPIC -c $(foreach D,$(INCDIRS),-I$(D)) $(ARC4RANDOM) $(DEFS)
# generate list of c files and remove y.tab.c from src/grammar directory
CFILES=$(foreach D,$(CODEDIRS),$(wildcard $(D)/*.c)) build/lex.yy.c build/y.tab.c
CFILES:=$(filter-out ./src/grammar/y.tab.c, $(CFILES))
# Create object files from the above c files.
OBJECTS=$(patsubst %.c,%.o,$(CFILES))
# To be under build/* as in grammar/*
OBJECTS:=$(subst ./src/grammar/,,$(OBJECTS))
OBJECTS:=$(addprefix build/,$(OBJECTS))
# Exception: Lex/Yacc generated files
OBJECTS:=$(subst build/build/,build/,$(OBJECTS))
# TODO: Would be nice to automatically get these.
CFILE_SUBDIRS=rolls util operations external
all: clean yacc lex compile shared
echo "== Build Complete =="
install: all
mkdir -p /usr/local/bin/
cp build/dice /usr/local/bin/dice
yacc:
mkdir -p build
$(foreach BD,$(CFILE_SUBDIRS),mkdir -p build/$(BD))
$(PARSER) -d src/grammar/dice.yacc $(PARSER_DEBUG)
mv y.tab.c build/y.tab.c
mv y.tab.h build/y.tab.h
mv y.output build/y.output | true # Only present with verbose
lex:
$(LEXER) src/grammar/dice.lex
mv lex.yy.c build/lex.yy.c
# Executable
compile: pcg
# FLEX creates warning when run with -f
# MacOS creates warnings for signs.
$(CC) $(CFLAGS) $(CFILES) $(ARC4RANDOM) \
-Wno-error=implicit-function-declaration \
-Wno-sign-conversion -Wno-sign-compare -lm \
-Wno-implicit-int-conversion -lpcg_random -L$(PCG_SRC) \
-Wno-undef -Wno-conversion # For pcg-c
# PCG Submodule
pcg:
make -C src/grammar/external/pcg-c all
# Shared Lib
shared: $(OBJECTS)
$(CC) -shared -o build/dice.so $^ $(ARC4RANDOM) -lm -lpcg_random -L$(PCG_SRC)
cp build/dice.so build/libdice.so
# Linux
mv ./a.out build/dice | true
# Windows
mv ./a.exe build/dice | true
# hardcode for lex and yacc files
build/y.tab.o:
$(CC) $(SHAREDCFLAGS) -c build/y.tab.c -o $@
build/lex.yy.o:
$(CC) $(SHAREDCFLAGS) -c build/lex.yy.c -o $@
# Wildcard everything else
build/*/%.o:src/grammar/*/%.c
$(CC) $(SHAREDCFLAGS) -c $^ -o $@
build/%.o:src/grammar/%.c
$(CC) $(SHAREDCFLAGS) -c -o $@ $^
test: pip
python3 -m pytest tests/python/ -s
include src/*/target.mk
clean: perl_clean python_clean clean_js
rm -rf build
rm src/grammar/test_dice.yacc | true