Debian Codegraph @

GNUmakefile

raw
1 ########################################################################
2 # Installation directories
3 ########################################################################
4
5 # We use GNU makefile conventions for directory names with one notable
6 # exception: prefix is not /usr/local in order to keep the traditional
7 # default installation location for beep.
8
9 PACKAGE_TARNAME = beep
10 PACKAGE_VERSION = 1.4.9
11
12 DESTDIR=
13 prefix=/usr
14 exec_prefix=$(prefix)
15 bindir=$(exec_prefix)/bin
16 sbindir=$(exec_prefix)/sbin
17 datarootdir=$(prefix)/share
18 mandir=$(datarootdir)/man
19 man1dir=$(mandir)/man1
20 docdir=$(datarootdir)/doc
21 pkgdocdir=$(docdir)/$(PACKAGE_TARNAME)
22 contribdir=$(pkgdocdir)/contrib
23
24
25 ########################################################################
26 # Tools
27 ########################################################################
28
29 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
30
31 # Avoid running GNU make builtin rules based on $(CC) by mistake
32 CC = false
33
34 DOT = $(call pathsearch,dot)
35 DOXYGEN = $(call pathsearch,doxygen)
36 EGREP = $(GREP) -E
37 GIT = $(call pathsearch,git)
38 GREP = $(call pathsearch,grep)
39 INSTALL = $(call pathsearch,install)
40 MKDIR_P = mkdir -p
41 PANDOC = $(call pathsearch,pandoc)
42 PYTHON3 = $(call pathsearch,python3)
43 SED = $(call pathsearch,sed)
44 SLOCCOUNT = $(call pathsearch,sloccount)
45
46
47 ########################################################################
48 # This needs to be the first rule
49 ########################################################################
50
51 .PHONY: all
52 all: all-local
53
54
55 # Have make forget rules for known suffixes
56 .SUFFIXES:
57
58
59 ########################################################################
60 # Variables to add to later
61 ########################################################################
62
63 # targets to build for the "all" target
64 all_TARGETS =
65
66 # targets to build for the "check" target
67 check_TARGETS =
68
69 bin_PROGRAMS =
70 check_PROGRAMS =
71 contrib_DATA =
72 contrib_SCRIPTS =
73 sbin_PROGRAMS =
74 CLEANFILES =
75 HTML_DATA =
76 man1_DATA =
77 pkgdoc_DATA =
78
79
80 ########################################################################
81 # Define compilers and their flags
82 ########################################################################
83
84 # CPPFLAGS common to all compilers
85 CPPFLAGS_COMMON =
86 CPPFLAGS_COMMON += -DPACKAGE_TARNAME='"$(PACKAGE_TARNAME)"'
87 CPPFLAGS_COMMON += -DPACKAGE_VERSION='"$(PACKAGE_VERSION)"'
88
89 comma := ,
90
91 # If supported by COMPILER_gcc, add given flags to CFLAGS_gcc.
92 # Example usage:
93 # $(eval $(call CHECK_CFLAGS_gcc,-fasynchronous-unwind-tables))
94 define CHECK_CFLAGS_gcc
95 CFLAGS_gcc += $$(if $$(shell if $$(COMPILER_gcc) $(1) -x c -o compile-check.gcc-o -c - < /dev/null > /dev/null 2>&1; then echo yes; else :; fi; rm -f compile-check.gcc-o > /dev/null 2>&1),$(1))
96 endef
97
98 COMPILER_gcc = gcc
99 LINKER_gcc = gcc
100 CPPFLAGS_gcc =
101 CFLAGS_gcc =
102 CFLAGS_gcc += -std=gnu99 -pedantic
103 CFLAGS_gcc += -O -g
104 CFLAGS_gcc += -Wa,-adhlns=$(@:-o=-lst)
105 $(eval $(call CHECK_CFLAGS_gcc,-Wall -Wextra -Werror -Werror=format-security))
106 $(eval $(call CHECK_CFLAGS_gcc,-Wp$$(comma)-D_FORTIFY_SOURCE=2))
107 $(eval $(call CHECK_CFLAGS_gcc,-Wp$$(comma)-D_GLIBCXX_ASSERTIONS))
108 $(eval $(call CHECK_CFLAGS_gcc,-fasynchronous-unwind-tables))
109 $(eval $(call CHECK_CFLAGS_gcc,-fstack-protector-strong))
110 $(eval $(call CHECK_CFLAGS_gcc,-fstack-clash-protection))
111 $(eval $(call CHECK_CFLAGS_gcc,-fcf-protection))
112 CFLAGS_gcc += -save-temps=obj
113 LDFLAGS_gcc =
114 LIBS_gcc =
115
116 ifneq ($(call pathsearch,$(COMPILER_gcc)),)
117 ifneq ($(COMPILER_gcc)),no)
118 COMPILERS += gcc
119 endif
120 endif
121
122 COMPILER_clang = clang
123 LINKER_clang = clang
124 CPPFLAGS_clang =
125 CFLAGS_clang += -Wall -Wextra
126 CFLAGS_clang += -Weverything
127 CFLAGS_clang += -Wno-padded
128 CFLAGS_clang += -std=gnu99 -pedantic
129 CFLAGS_clang += -Werror
130 CFLAGS_clang += -fsanitize=undefined
131 CFLAGS_clang += -O -g
132 LDFLAGS_clang =
133 LIBS_clang =
134
135 ifneq ($(call pathsearch,$(COMPILER_clang)),)
136 ifneq ($(COMPILER_clang),no)
137 COMPILERS += clang
138 endif
139 endif
140
141
142 ########################################################################
143 # Detect libraries
144 ########################################################################
145
146
147 ########################################################################
148 # Define executables and their flags
149 ########################################################################
150
151 check_PROGRAMS += issue-6-benchmark
152 issue_6_benchmark_OBJS = issue-6-benchmark.o
153 issue_6_benchmark_LIBS = -lm
154
155 bin_PROGRAMS += beep
156 beep_OBJS =
157 beep_OBJS += beep-log.o
158 beep_OBJS += beep-main.o
159 beep_OBJS += beep-library.o
160 beep_OBJS += beep-usage.o
161 beep_OBJS += beep-drivers.o
162
163 # The drivers here use `__attribute__((constructor))` functions to
164 # register themselves with `beep_drivers_register()`, so the last one
165 # listed here will be linked last and have its constructor called at
166 # the latest time, and thus will have its `driver_detect()` function
167 # called first.
168
169 # beep_OBJS += beep-driver-noop.o
170 beep_OBJS += beep-driver-console.o
171 beep_OBJS += beep-driver-evdev.o
172
173 beep_LIBS =
174
175 beep-log.clang-o : override CFLAGS_clang += -D_GNU_SOURCE
176 beep-log.gcc-o : override CFLAGS_gcc += -D_GNU_SOURCE
177
178 beep-log.clang-o : override CFLAGS_clang += -Wno-format-nonliteral
179
180 # sbin_PROGRAMS += beep-foo
181 # beep_foo_OBJS =
182 # beep_foo_OBJS += beep-log.o
183 # beep_foo_OBJS += beep.o
184 # beep_foo_LIBS =
185 # beep_foo_LIBS += -lm
186
187
188 ########################################################################
189 # Built sources
190 ########################################################################
191
192 CLEANFILES += beep-usage.c
193 beep-usage.c: beep-usage.txt
194 echo '/* Auto-generated from `$<`. Modify that file instead. */' > $@
195 echo '#include "beep-usage.h"' >> $@
196 echo 'char beep_usage[] =' >> $@
197 set -e; IFS=""; while read line; do \
198 printf ' "%s\\n"\n' "$${line}" >> $@; \
199 done < $<
200 echo ' ;' >> $@
201
202 CLEANFILES += beep-usage.txt
203
204
205 ########################################################################
206 # Compile and Link rules including automatic dependency generation
207 #
208 # For details on the automatic dependency generation, see
209 # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
210 ########################################################################
211
212
213 # CALL: LINK_RULE <compiler> <executable> <executable_as_varname_part> <dircomponent>
214 # To be called from PER_COMPILER. Defines the per-executable rules.
215 define LINK_RULE
216 $(4)_ALL_PROGRAMS += $(2).$(1)
217
218 $(2).$(1): $(patsubst %.o,%.$(1)-o,$($(3)_OBJS))
219 @: echo "LINK_RULE $$@: $$^"
220 $(LINKER_$(1)) $(CFLAGS) $(CFLAGS_$(1)) $(LDFLAGS) $(LDFLAGS_$(1)) -o $$@ $$^ $($(3)_LIBS) $(LIBS_$(1)) $(LIBS)
221
222 $$(patsubst %.o,.deps/%.$(1)-o.dep,$($(3)_OBJS))):
223
224 -include $$(wildcard $$(patsubst %.o,.deps/%.$(1)-o.dep,$($(3)_OBJS)))
225 endef
226
227
228 # CALL: PER_COMPILER <compiler>
229 # To be called for each compiler. Defines the per-compiler rules for each executable.
230 define PER_COMPILER
231 $(foreach exec,$(bin_PROGRAMS),$(eval $(call LINK_RULE,$(1),$(exec),$(subst -,_,$(exec)),bin)))
232 $(foreach exec,$(check_PROGRAMS),$(eval $(call LINK_RULE,$(1),$(exec),$(subst -,_,$(exec)),check)))
233 $(foreach exec,$(sbin_PROGRAMS),$(eval $(call LINK_RULE,$(1),$(exec),$(subst -,_,$(exec)),sbin)))
234
235 %.$(1)-o: %.c | .deps
236 $$(COMPILER_$(1)) -MT $$@ -MMD -MP -MF .deps/$$*.$(1)-o.dep $$(CPPFLAGS) $$(CPPFLAGS_COMMON) $$(CPPFLAGS_$(1)) $$(CFLAGS_COMMON) $$(CFLAGS) $$(CFLAGS_$(1)) -o $$@ -c $$<
237
238 %.h-check.$(1)-o: %.h | .deps
239 $$(COMPILER_$(1)) -MT $$@ -MMD -MP -MF .deps/$$*.h-check.$(1)-o.dep $$(CPPFLAGS) $$(CPPFLAGS_COMMON) $$(CPPFLAGS_$(1)) $$(CFLAGS_COMMON) $$(CFLAGS) $$(CFLAGS_$(1)) -o $$@ -c $$<
240 endef
241
242 $(foreach compiler,$(COMPILERS),$(eval $(call PER_COMPILER,$(compiler))))
243
244 .deps:
245 @$(MKDIR_P) $@
246
247
248 # For each executable, take the first from COMPILERS to determine the
249 # variant to use as the default executables.
250 %: $(firstword $(foreach comp,$(COMPILERS),%.$(comp)))
251 cp -f $< $@
252
253
254 ########################################################################
255 # Generate doc and similar files
256 ########################################################################
257
258 man1_DATA += beep.1
259 CLEANFILES += beep.1
260
261 HTML_DATA += html/README.html
262 HTML_DATA += html/INSTALL.html
263 HTML_DATA += html/DEVELOPMENT.html
264 HTML_DATA += html/PACKAGING.html
265 HTML_DATA += html/PERMISSIONS.html
266
267 .PHONY: html
268 html: $(HTML_DATA)
269 @mkdir -p html
270 cp -f pandoc.css html/
271
272 html/%.html: %.md
273 @mkdir -p html
274 @if test -f $(PANDOC); then \
275 echo PANDOC $< -o $@; \
276 $(PANDOC) --from gfm --to html --standalone -M pagetitle="$$($(SED) -n 1p $<)" -M title="" -c pandoc.css $< -o $@; \
277 else \
278 echo "You need to install pandoc to generate the HTML files."; \
279 exit 1; \
280 fi
281
282 DEFAULT_FREQ = 440
283 DEFAULT_LENGTH = 200
284 DEFAULT_DELAY = 100
285
286 REPLACEMENTS =
287 REPLACEMENTS += -e s/@PACKAGE_TARNAME@/$(PACKAGE_TARNAME)/g
288 REPLACEMENTS += -e s/@PACKAGE_VERSION@/$(PACKAGE_VERSION)/g
289
290 REPLACEMENTS += -e s/@DEFAULT_FREQ@/$(DEFAULT_FREQ)/g
291 REPLACEMENTS += -e s/@DEFAULT_LENGTH@/$(DEFAULT_LENGTH)/g
292 REPLACEMENTS += -e s/@DEFAULT_DELAY@/$(DEFAULT_DELAY)/g
293
294 REPLACEMENTS += -e 's|[@]pkgdocdir@|$(pkgdocdir)|g'
295
296 CLEANFILES += beep-config.h
297 BUILT_SOURCES += beep-config.h
298 beep-main.clang-o : beep-config.h
299 beep-main.gcc-o : beep-config.h
300
301 CLEANFILES += Doxyfile
302 CLEANFILES += Doxyfile.new
303
304 %: %.in GNUmakefile
305 $(SED) $(REPLACEMENTS) < $< > [email protected]
306 @if $(EGREP) '@([A-Za-z][A-Za-z0-9_]*)@' [email protected]; then \
307 echo "Error: GNUmakefile fails to substitute some of the variables in \`$<'."; \
308 exit 1; \
309 fi
310 mv -f [email protected] $@
311
312 CLEANFILES += doxygen.stamp
313 .PHONY: doxygen.stamp
314 doxygen.stamp: Doxyfile $(wildcard *.c) $(wildcard *.h)
315 $(DOXYGEN) $<
316 echo > $@
317
318 html: doxygen.stamp
319
320 .PHONY: serve-html
321 serve-html: html
322 $(PYTHON3) -m http.server --directory html/dox/html
323
324 pkgdoc_DATA += CHANGELOG
325 pkgdoc_DATA += COPYING
326 pkgdoc_DATA += CREDITS
327 pkgdoc_DATA += README.md
328 pkgdoc_DATA += PERMISSIONS.md
329
330 contrib_SCRIPTS += contrib/failure-beeps
331 contrib_SCRIPTS += contrib/success-beeps
332
333 ########################################################################
334 # Generic targets
335 ########################################################################
336
337 all_TARGETS += $(bin_PROGRAMS) $(bin_ALL_PROGRAMS)
338 all_TARGETS += $(sbin_PROGRAMS) $(sbin_ALL_PROGRAMS)
339 all_TARGETS += $(man1_DATA)
340
341 .PHONY: all-local
342 all-local: $(all_TARGETS)
343
344 SLOC_SOURCES =
345 SLOC_SOURCES += beep*.[ch]
346 SLOC_SOURCES += gen-freq-table
347 SLOC_SOURCES += tests/run-tests
348 SLOC_SOURCES += tests/*.sh
349 SLOC_SOURCES += GNUmakefile
350
351 .PHONY: sloccount
352 sloccount:
353 @if test -e $(SLOCCOUNT); then \
354 $(SLOCCOUNT) --details $(SLOC_SOURCES); \
355 $(SLOCCOUNT) $(SLOC_SOURCES); \
356 else \
357 echo "sloccount not found"; \
358 fi
359
360 SPLINT = splint
361 SPLINT_FLAGS += -standard
362 # SPLINT_FLAGS += -checks
363 # SPLINT_FLAGS += -strict
364 SPLINT_FLAGS += +posixstrictlib
365 SPLINT_FLAGS += +gnuextensions
366 SPLINT_FLAGS += -preproc
367 SPLINT_FLAGS += -syntax
368 SPLINT_FLAGS += -D__signed__=signed
369 SPLINT_FLAGS += $(CPPFLAGS_COMMON)
370
371 .PHONY: lint
372 lint:
373 $(SPLINT) $(SPLINT_FLAGS) beep*.c beep*.h
374
375 check_TARGETS += $(all_TARGETS)
376 check_TARGETS += $(check_PROGRAMS) $(check_ALL_PROGRAMS)
377 check_TARGETS += $(foreach compiler,$(COMPILERS),$(foreach header,$(wildcard beep-*.h),$(basename $(header)).h-check.$(compiler)-o))
378
379 .PHONY: check-targets
380 check-targets: $(check_TARGETS)
381
382 .PHONY: check
383 check: $(foreach compiler,$(COMPILERS),beep.$(compiler)) $(check_TARGETS)
384 env PACKAGE_VERSION="${PACKAGE_VERSION}" \
385 /bin/bash tests/run-tests tests $(foreach compiler,$(COMPILERS),beep.$(compiler))
386
387 .PHONY: clean
388 clean:
389 rm -f $(bin_PROGRAMS) $(bin_ALL_PROGRAMS)
390 rm -f $(check_PROGRAMS) $(check_ALL_PROGRAMS)
391 rm -f $(sbin_PROGRAMS) $(sbin_ALL_PROGRAMS)
392 rm -f $(CLEANFILES)
393 rm -f $(foreach comp,$(COMPILERS),*.$(comp)-o)
394 rm -f *.dep
395 rm -rf .deps
396 rm -f *.lst *.gcc-lst
397 rm -f tests/*.new tests/*.actual
398 rm -rf html
399 rm -f *.o *.i *.s
400
401 .PHONY: doc
402 doc: $(pkgdoc_DATA)
403
404 .PHONY: install
405 install: all
406 ifneq (,$(bin_PROGRAMS))
407 $(INSTALL) -m 0755 -d $(DESTDIR)$(bindir)
408 $(INSTALL) -m 0755 -p $(bin_PROGRAMS) $(DESTDIR)$(bindir)/
409 endif
410 ifneq (,$(sbin_PROGRAMS))
411 $(INSTALL) -m 0755 -d $(DESTDIR)$(sbindir)
412 $(INSTALL) -m 0755 -p $(sbin_PROGRAMS) $(DESTDIR)$(sbindir)/
413 endif
414 ifneq (,$(man1_DATA))
415 $(INSTALL) -m 0755 -d $(DESTDIR)$(man1dir)
416 $(INSTALL) -m 0644 -p $(man1_DATA) $(DESTDIR)$(man1dir)/
417 endif
418 ifneq (,$(pkgdoc_DATA))
419 $(INSTALL) -m 0755 -d $(DESTDIR)$(pkgdocdir)
420 $(INSTALL) -m 0644 -p $(pkgdoc_DATA) $(DESTDIR)$(pkgdocdir)/
421 endif
422 ifneq (,$(contrib_DATA)$(contrib_SCRIPTS))
423 $(INSTALL) -m 0755 -d $(DESTDIR)$(contribdir)
424 endif
425 ifneq (,$(contrib_DATA))
426 $(INSTALL) -m 0644 -p $(contrib_DATA) $(DESTDIR)$(contribdir)/
427 endif
428 ifneq (,$(contrib_SCRIPTS))
429 $(INSTALL) -m 0755 -p $(contrib_SCRIPTS) $(DESTDIR)$(contribdir)/
430 endif
431
432 .PHONY: uninstall
433 uninstall:
434 for f in $(bin_PROGRAMS); do rm -f "$(DESTDIR)$(bindir)/$$f"; done
435 for f in $(sbin_PROGRAMS); do rm -f "$(DESTDIR)$(sbindir)/$$f"; done
436 for f in $(man1_DATA); do rm -f "$(DESTDIR)$(man1dir)/$$f"; done
437 for f in $(pkgdoc_DATA); do rm -f "$(DESTDIR)$(pkgdocdir)/$$f"; done
438
439
440 ########################################################################
441 # Development helpers
442 ########################################################################
443
444 # Only have make deal with those variables and rules if this is a git
445 # repo, and if the git executable has been found.
446 GIT_INFO_EXCLUDE = $(firstword $(wildcard .git/info/exclude))
447 ifneq ($(strip $(GIT_INFO_EXCLUDE)),)
448 ifdef GIT
449
450 # List all references to documentation in the git repo
451 .PHONY: refs
452 refs:
453 $(GIT) grep -n -E '((http|https)://[a-zA-Z0-9\._/-]+|([A-Z]+\.md)|([a-zA-Z][a-zA-Z0-9_-]+\([0-9]+\)))'
454
455 # List all TODOs and FIXMEs in the git repo
456 .PHONY: todo fixme
457 todo fixme:
458 $(GIT) grep -n -E '(TODO:|FIXME:|\\todo\s|@todo\s)'
459
460 # Generate a kind of dist tarball to help with preparing for release
461 PACKAGE_TARBASE := $(PACKAGE_TARNAME)-$(shell $(GIT) describe --tags | $(SED) 's/^v\([0-9]\)/\1/')
462 .PHONY: dist
463 dist:
464 $(GIT) archive --format=tar.gz --verbose --prefix=$(PACKAGE_TARBASE)/ --output=$(PACKAGE_TARBASE).tar.gz HEAD
465
466 endif
467 endif
468
469
470 ########################################################################
471 # End of GNUmakefile
472 ########################################################################