-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
235 lines (196 loc) · 6.62 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# GNU Make 3.81; MacOSX gcc 4.2.1; clang 19.6.0; MacOSX MinGW 4.3.0
# https://stackoverflow.com/questions/18136918/how-to-get-current-relative-directory-of-your-makefile
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
project := $(current_dir)
# dirs
src := src
test := test
build := build
bin := bin
backup := backup
doc := doc
media := media
#lemon := lemon
PREFIX := /usr/local
# files in $(bin)
install := $(project)-`date +%Y-%m-%d`
# extra stuff we should back up
extra :=
# John Graham-Cumming: rwildcard is a recursive wildcard
rwildcard=$(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) \
$(filter $(subst *,%,$2),$d))
java_srcs := $(call rwildcard, $(src), *.java)
all_c_srcs := $(call rwildcard, $(src), *.c)
c_re_srcs := $(call rwildcard, $(src), *.re.c)
c_rec_srcs := $(call rwildcard, $(src), *.re_c.c)
c_gperf_srcs := $(call rwildcard, $(src), *.gperf.c)
c_srcs := $(filter-out $(c_re_srcs) $(c_rec_srcs) $(c_gperf_srcs), $(all_c_srcs))
h_srcs := $(call rwildcard, $(src), *.h)
y_srcs := $(call rwildcard, $(src), *.y)
all_c_tests := $(call rwildcard, $(test), *.c)
c_re_tests := $(call rwildcard, $(test), *.re.c)
c_rec_tests := $(call rwildcard, $(test), *.re_c.c)
c_tests := $(filter-out $(c_re_tests) $(c_rec_tests), $(all_c_tests))
h_tests := $(call rwildcard, $(test), *.h)
icons := $(call rwildcard, $(media), *.ico)
# combinations
all_h := $(h_srcs) $(h_tests)
all_srcs := $(java_srcs) $(all_c_srcs) $(y_srcs)
all_tests := $(all_c_tests)
all_icons := $(icons)
java_class := $(patsubst $(src)/%.java, $(build)/%.class, $(java_srcs))
c_objs := $(patsubst $(src)/%.c, $(build)/%.o, $(c_srcs))
# must not conflict, eg, foo.c.re and foo.c would go to the same thing
c_re_builds := $(patsubst $(src)/%.re.c, $(build)/%.c, $(c_re_srcs))
c_re_test_builds := $(patsubst $(test)/%.re.c, $(build)/$(test)/%.c, $(c_re_tests))
c_rec_builds := $(patsubst $(src)/%.re_c.c, $(build)/%.c, $(c_rec_srcs))
c_rec_test_builds := $(patsubst $(test)/%.re_c.c, $(build)/%.c, $(c_rec_tests))
c_y_builds := $(patsubst $(src)/%.y, $(build)/%.c, $(y_srcs))
c_gperf_builds := $(patsubst $(src)/%.gperf.c, $(build)/%.c, $(c_gperf_srcs))
# together .re/.re_c/.y/.gperf.c
c_other_objs := $(patsubst $(build)/%.c, $(build)/%.o, $(c_re_builds) \
$(c_rec_builds) $(c_re_test_builds) $(c_rec_test_builds) $(c_y_builds) $(c_gperf_builds))
test_c_objs := $(patsubst $(test)/%.c, $(build)/$(test)/%.o, $(c_tests))
html_docs := $(patsubst $(src)/%.c, $(doc)/%.html, $(c_srcs))
cdoc := cdoc
re2c := re2c
mkdir := mkdir -p
cat := cat
zip := zip
bison := bison
#lemon := lemon
gperf := gperf
target := # -mwindows
optimize := -ffast-math
warnbasic := -Wall -pedantic -ansi # -std=c99
# Some stuff is really new.
warnclang := -Wextra \
-Weverything \
-Wno-comma \
-Wno-logical-op-parentheses \
-Wno-parentheses \
-Wno-poison-system-directories \
-Wno-documentation-unknown-command \
-Wno-documentation \
-Wno-shift-op-parentheses \
-Wno-empty-body \
-Wno-padded \
-Wno-switch-enum \
-Wno-missing-noreturn
warn := $(warnbasic) $(warnclang)
CC := clang # gcc
CF := $(target) $(optimize) $(warn)
OF := # -lm -framework OpenGL -framework GLUT or -lglut -lGLEW
# Jakob Borg and Eldar Abusalimov
# $(ARGS) is all the extra arguments; $(BRGS) is_all_the_extra_arguments
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
ifeq (backup, $(firstword $(MAKECMDGOALS)))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
BRGS := $(subst $(SPACE),_,$(ARGS))
ifneq (,$(BRGS))
BRGS := -$(BRGS)
endif
$(eval $(ARGS):;@:)
endif
ifeq (release, $(firstword $(MAKECMDGOALS)))
CF += -funroll-loops -Ofast -D NDEBUG # -O3
OF += -Ofast
else
CF += -g
endif
######
# compiles the programme by default
default: $(bin)/$(project)
# . . . success; executable is in $(bin)/$(project)
docs: $(html_docs)
# linking
$(bin)/$(project): $(c_objs) $(c_other_objs) $(test_c_objs)
# linking rule
@$(mkdir) $(bin)
$(CC) $(OF) -o $@ $^
# compiling
#$(lemon)/$(bin)/$(lem): $(lemon)/$(src)/lemon.c
# # compiling lemon
# @$(mkdir) $(lemon)/$(bin)
# $(CC) $(CF) -o $@ $<
$(c_objs): $(build)/%.o: $(src)/%.c $(all_h)
# c_objs rule
@$(mkdir) $(build)
$(CC) $(CF) -c -o $@ $<
$(c_other_objs): $(build)/%.o: $(build)/%.c $(all_h)
# c_other_objs rule
$(CC) $(CF) -c -o $@ $<
$(test_c_objs): $(build)/$(test)/%.o: $(test)/%.c $(all_h)
# test_c_objs rule
@$(mkdir) $(build)
@$(mkdir) $(build)/$(test)
$(CC) $(CF) -c -o $@ $<
# -8 made my file 32767 lines or longer
$(c_re_builds): $(build)/%.c: $(src)/%.re.c
# *.re.c build rule
@$(mkdir) $(build)
$(re2c) -W -T -o $@ $<
$(c_re_test_builds): $(build)/$(test)/%.c: $(test)/%.re.c
# *.re.c tests rule
@$(mkdir) $(build)
@$(mkdir) $(build)/$(test)
$(re2c) -W -T -o $@ $<
$(c_rec_builds): $(build)/%.c: $(src)/%.re_c.c
# *.re_c.c (conditions) build rule
@$(mkdir) $(build)
$(re2c) -W -T -c -o $@ $<
$(c_rec_test_builds): $(build)/$(test)/%.c: $(test)/%.re_c.c
# *.re_c.c (conditions) tests rule
@$(mkdir) $(build)
@$(mkdir) $(build)/$(test)
$(re2c) -W -T -c -o $@ $<
$(c_y_builds): $(build)/%.c: $(src)/%.y # $(lemon)/$(bin)/$(lem)
# .y rule
@$(mkdir) $(build)
$(bison) -o $@ $<
$(c_gperf_builds): $(build)/%.c: $(src)/%.gperf.c
# *.gperf.c build rule
@$(mkdir) $(build)
$(gperf) $@ --output-file $<
$(html_docs): $(doc)/%.html: $(src)/%.c $(src)/%.h
# docs rule
@$(mkdir) $(doc)
cat $^ | $(cdoc) > $@
######
# phoney targets
.PHONY: setup clean backup icon install uninstall test docs release
clean:
-rm -f $(c_objs) $(test_c_objs) $(c_other_objs) $(c_re_builds) \
$(c_rec_builds) $(html_docs)
-rm -rf $(bin)/$(test)
backup:
@$(mkdir) $(backup)
$(zip) $(backup)/$(project)-`date +%Y-%m-%dT%H%M%S`$(BRGS).zip \
readme.txt Makefile $(all_h) $(all_srcs) $(all_tests) $(all_icons)
icon: default
# . . . setting icon on a Mac.
cp $(media)/$(icon) $(bin)/$(icon)
-sips --addIcon $(bin)/$(icon)
-DeRez -only icns $(bin)/$(icon) > $(bin)/$(RSRC)
-Rez -append $(bin)/$(RSRC) -o $(bin)/$(project)
-SetFile -a C $(bin)/$(project)
setup: default icon
@$(mkdir) $(bin)/$(install)
cp $(bin)/$(project) readme.txt $(bin)/$(install)
rm -f $(bin)/$(install)-MacOSX.dmg
# or rm -f $(BDIR)/$(INST)-Win32.zip
hdiutil create $(bin)/$(install)-MacOSX.dmg -volname "$(project)" -srcfolder $(bin)/$(install)
# or zip $(BDIR)/$(INST)-Win32.zip -r $(BDIR)/$(INST)
rm -R $(bin)/$(install)
# this needs work
release: clean default
strip $(bin)/$(project)
# define NDEBUG
install: release
@$(mkdir) -p $(DESTDIR)$(PREFIX)/bin
cp $(bin)/$(project) $(DESTDIR)$(PREFIX)/bin/$(project)
uninstall:
rm -f $(DESTDIR)$(PREFIX)/bin/$(project)
docs: $(html_docs)