summaryrefslogtreecommitdiff
path: root/Makefile
blob: 125d7be3b6b741545110e44d9a73dab9f74421c5 (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
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
# SPDX-License-Identifier: GPL-2.0-only
.SUFFIXES:
MAKEFLAGS += -Rr
ifeq ("$(origin CC)", "default")
        CC := cc
endif
ifeq ("$(origin V)", "command line")
	SAY =
else
	SAY = @echo '$(strip $(1))'
endif

PACKAGE := micoforia
DEFINES = \
	$(call DEFINE,PACKAGE,$(PACKAGE)) \
	$(call DEFINE,AUTHOR,Andre Noll) \
	$(call DEFINE,COPYRIGHT_YEAR,2019) \
	$(call DEFINE,SLOGAN,Minimal Containers for Instant Access) \
	$(call DEFINE,EMAIL,maan@tuebingen.mpg.de) \
	$(call DEFINE,URL,https://people.tuebingen.mpg.de/maan/$(PACKAGE)/) \
	$(call DEFINE,CLONE_URL,https://git.tuebingen.mpg.de/$(PACKAGE)) \
	$(call DEFINE,GITWEB_URL,https://git.tuebingen.mpg.de/$(PACKAGE).git) \
	$(call DEFINE,HOME_URL,https://people.tuebingen.mpg.de/maan/) \
	$(call DEFINE,LICENSE,GNU GPL version 3) \
	$(call DEFINE,LICENSE_URL,https://www.gnu.org/licenses/gpl-3.0-standalone.html)
DEFINE = -D$(1)='"$(2)"'
# LOGLEVELS is special because it is defined unquoted
LOGLEVELS := LL_DEBUG,LL_INFO,LL_NOTICE,LL_WARNING,LL_ERROR,LL_CRIT,LL_EMERG
CPP_DEFINES := $(DEFINES) -DLOGLEVELS='$(LOGLEVELS)'
DEFINE = -D "$(1)=$(2)"
M4_DEFINES := $(DEFINES)

.ONESHELL:
.SHELLFLAGS := -ec
PREFIX ?= /usr/local
INSTALL ?= install
MKDIR_P := mkdir -p
RM := rm -f
CHMOD := chmod
B := build
all := micoforia micoforia.8
all: $(all)
www := $(B)/index.html
www: $(www)

units := micoforia util version micoforia.lsg
deps := $(addprefix $(B)/, $(addsuffix .d, $(units)))
objs := $(addprefix $(B)/, $(addsuffix .o, $(units)))

ifeq ($(findstring clean, $(MAKECMDGOALS)),)
-include $(deps)
-include $(B)/config.mak
endif

M4 += $(M4_DEFINES) defs.m4

XCPPFLAGS := $(CPP_DEFINES) -I$(B) -Wunused-macros

XCFLAGS :=
XCFLAGS += -fno-strict-aliasing
XCFLAGS += -g
XCFLAGS += -Os
XCFLAGS += -Wundef -W -Wuninitialized
XCFLAGS += -Wchar-subscripts
XCFLAGS += -Werror-implicit-function-declaration
XCFLAGS += -Wmissing-noreturn
XCFLAGS += -Wbad-function-cast
XCFLAGS += -Wredundant-decls
XCFLAGS += -Wno-sign-compare -Wno-unknown-pragmas
XCFLAGS += -Wdeclaration-after-statement
XCFLAGS += -Wformat -Wformat-security -Wmissing-format-attribute
XCFLAGS += -fsanitize=undefined
XCFLAGS += -fdata-sections -ffunction-sections
XCFLAGS += -Wstrict-prototypes
XCFLAGS += -Wshadow
XCFLAGS += -Wunused -Wall
XCFLAGS += -Wformat-signedness
XCFLAGS += -Wdiscarded-qualifiers

XLDFLAGS := -lubsan -Wl,--gc-sections
version_file := $(B)/version.c
GIT_VERSION := $(shell $(MKDIR_P) $(B) && ./version-gen.sh $(PACKAGE) $(version_file))

CC_CMD = $(CC) -c -o $@ $(XCPPFLAGS) $(CPPFLAGS) \
	$(XCFLAGS) $(CFLAGS) -MMD -MP -MF $(B)/$(*F).d -MT $@

$(objs): m7a.h $(B)/micoforia.lsg.h

$(B):
	@$(MKDIR_P) $@

$(B)/config.h.in: configure.ac | $(B)
	$(call SAY, AH $<)
	cd $(B)
	autoheader -f ../configure.ac
$(B)/configure.sh: configure.ac | $(B)
	$(call SAY, AC $<)
	cd $(B)
	autoconf ../configure.ac > configure.sh
	$(CHMOD) 755 configure.sh
$(B)/config.status: $(B)/configure.sh | $(B)
	$(call SAY, SH $<)
	cd $(B)
	if test -x config.status; then \
		./config.status --quiet --recheck; \
	else \
		./configure.sh --no-create; \
	fi
$(B)/config.mak $(B)/config.h: $(B)/config.status config.mak.in $(B)/config.h.in
	$(call SAY, CS $@)
	cd $(B)
	ln -f ../config.mak.in
	./config.status -q
	test -f config.h && touch config.h

$(B)/index.html $(B)/micoforia.suite: $(B)/%: %.m4 Makefile $(B)/config.mak
	$(call SAY, M4 $<)
	$(M4) $< > $@
$(B)/%.lsg.c: $(B)/%.suite
	$(call SAY, LSGC $<)
	$(LOPSUBGEN) --gen-c --output-dir $(B) < $<
$(B)/%.lsg.h: $(B)/%.suite
	$(call SAY, LSGH $<)
	$(LOPSUBGEN) --gen-header --output-dir $(B) < $<
%.8: $(B)/%.suite $(B)/version.c
	$(call SAY, LSGM $<)
	$(LOPSUBGEN) --gen-man=$(*F).8 --version-string $(GIT_VERSION) < $<

$(B)/%.o: %.c | $(B)
	$(call SAY, CC $<)
	$(CC_CMD) $<
$(B)/%.o: $(B)/%.c
	$(call SAY, CC $<)
	$(CC_CMD) $<
micoforia: $(objs)
	$(call SAY, LD $@)
	$(CC) -o $@ $^ $(XLDFLAGS) $(LDFLAGS) -llopsub -lmnl -lutil -lcap

mandir := $(datarootdir)/man/man8
INSTALL ?= install
INSTALL_PROGRAM ?= $(INSTALL) -m 755
INSTALL_DATA ?= $(INSTALL) -m 644
ifneq ($(findstring strip, $(MAKECMDGOALS)),)
	strip_option := -s
endif
install install-strip: all
	$(MKDIR_P) $(DESTDIR)$(sbindir) $(DESTDIR)$(mandir)
	$(INSTALL_PROGRAM) $(strip_option) micoforia $(DESTDIR)$(sbindir)
	$(INSTALL_DATA) micoforia.8 $(DESTDIR)$(mandir)

clean:
	$(RM) $(B)/*.o $(all) $(www)
distclean: clean
	$(RM) -r $(B)
maintainer-clean:
	git clean -dfqx > /dev/null 2>&1

README: README.m4 Makefile $(B)/config.mak
	@$(M4) $<

.PRECIOUS: $(B)/%.lsg.c $(B)/%.lsg.h $(B)/%.8
.PHONY: all www clean install distclean maintainer-clean README
-include Makefile.local