1 ### The build commands and verbosity
3 # If we are verbose, we will show commands prefixed by $(Q) (which acts as
4 # @ in the non-verbose mode), and we will show the "real" cmds instead of
5 # their quiet versions (which are used in the non-verbose mode).
6 # Inspired by the Linux kernel build system.
19 INFO_COLOR = $(shell tput setaf 5)
20 CC_COLOR = $(shell tput setaf 6)
21 LD_COLOR = $(shell tput setaf 2)
22 PO_COLOR = $(shell tput setaf 6)
23 LINK_COLOR = $(shell tput bold;tput setaf 4)
24 INSTALL_COLOR = $(shell tput setaf 3)
25 END_COLOR = $(shell tput sgr0)
28 # Show the command (quiet or non-quiet version based on the assignment
29 # just above) and then execute it.
30 ncmd = $(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
31 cmd = @$(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
32 mcmd = @$(if $($(mquiet)cmd_$(1)),echo $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
33 ecmd = @$(if $($(mquiet)cmd_$(1)),printf "%-38s " $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
35 quiet_cmd_compile = ' [$(CC_COLOR)CC$(END_COLOR)] $(RELPATH)$@'
36 masq_cmd_compile = $(COMPILE) -o $(@) -c $< $(2)
37 cmd_compile = $(COMPILE) -o $(@) -Wp,-MD,.deps/$(*F).pp -c $< $(2)
39 # Rule to compile a set of .o files into one .o file
40 quiet_cmd_ld_objs = " [$(LD_COLOR)LD$(END_COLOR)] $(RELPATH)$@"
41 cmd_ld_objs = $(LD) -r -o $@ $(filter $(OBJS), $^) \
42 $(foreach subdir,$(sort $(filter-out src,$(SUBDIRS))), \
43 `test -e $(subdir)/lib.o && echo $(subdir)/lib.o`)
45 quiet_cmd_link = ' [$(LINK_COLOR)LINK$(END_COLOR)] $(RELPATH)$@'
46 cmd_link = $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(2) $(LIBS)
48 quiet_cmd_sparse = ' [SPARSE] $(RELPATH)$(2)'
49 cmd_sparse = $(SPARSE) $(DEFS) $(INCLUDES) $(AM_CFLAGS) $(CFLAGS) $(SPARSE_FLAGS) $(2)
52 quiet_cmd_recmake = "[$(INFO_COLOR)MAKE $(3)$(END_COLOR)] $(RELPATH)$(2)"
53 cmd_recmake = $(MAKE) -C $(2) $(3)
55 quiet_cmd_installdata = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
56 cmd_installdata = $(INSTALL_DATA) $(2) $(3)
58 quiet_cmd_installprog = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
59 cmd_installprog = $(INSTALL_PROGRAM) $(2) $(3)
62 ### Internal build rules
64 DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
65 DEP_FILES = $(DEP_FILES_1:%.o=%.P)
67 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
75 @-cp .deps/$(*F).pp .deps/$(*F).P; \
76 tr ' ' '\012' < .deps/$(*F).pp \
77 | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
82 SUBDIRS += $(SUBDIRS-yes)
89 ifneq ($(findstring cleanall,$(MAKECMDGOALS)),)
93 ifneq ($(findstring init,$(MAKECMDGOALS)),)
94 # FIXME: Detect when $(subdir)/Makefile is $(srcdir)/$(subdir)/Makefile
95 # and error out so the user won't overwrite the 'master' Makefiles.
101 SUBDIRS += $(SUBDIRS-no)
104 SUBDIRS += $(SUBDIRS-)
117 lib.o: $(sort $(OBJS)) $(foreach subdir,$(sort $(filter-out src,$(SUBDIRS))), $(wildcard $(subdir)/lib.o))
121 CLEAN += $(OBJS) $(LIB_O)
126 all-default: $(LIB_O) $(PROGS) $(MAN1) $(MAN5)
128 # Ensure that Makefiles in subdirs are created before we recursive into them
129 init-recursive: init-default
132 @$(foreach subdir,$(sort $(SUBDIRS)), \
133 $(MKINSTALLDIRS) $(subdir) >/dev/null; \
134 echo 'include $(SRC)/$(RELPATH)/$(subdir)/Makefile' > $(subdir)/Makefile;)
136 clean-default: clean-test
137 @-test -z "$(CLEAN)" || $(RM) $(CLEAN)
139 cleanall-default: clean-default
143 @$(foreach file, $(wildcard *.c), \
144 $(call ncmd,sparse,$(file));)
147 ##############################################################################
149 # Auto-testing infrastructure
156 TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o
157 TESTDEPS += $(TESTDEPS-yes)
159 TESTS = $(wildcard $(srcdir)test-*)
161 $(TEST_PROGS): $(TESTDEPS)
162 $(call cmd,link,$@.o)
164 # We cannot use $$@.o in the rule above so ensure that all test programs are
165 # built before linking.
166 $(TESTS): $(addsuffix .o,$(TEST_PROGS)) $(TEST_PROGS)
167 @echo "*** $(notdir $@) ***"; \
168 $(call shellquote,$(SHELL)) $@ $(TEST_OPTS)
170 test-default: $(TESTS)
175 CLEAN += $(TEST_PROGS) $(addsuffix .o,$(TEST_PROGS))
181 # sparse is architecture-neutral, which means that we need to tell it
182 # explicitly what architecture to check for. Fix this up for yours..
183 SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
184 install-default: all-default
186 @$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
187 @$(foreach file,$(PROGS), \
188 $(call ncmd,installprog,$(file),$(DESTDIR)$(bindir));)
191 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
192 @$(foreach file,$(MAN1), \
193 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man1);)
196 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man5
197 @$(foreach file,$(MAN5), \
198 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man5);)
203 RULES = all install clean cleanall init check test
204 RECRULES = $(addsuffix -recursive,$(RULES))
208 # The -recursive rules decend all subdirs.
211 @$(foreach subdir,$(sort $(SUBDIRS)), \
212 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || exit 1;)
215 # Setup the default sub commands dependency. First decend subdirs then do all
216 # the default stuff and finally do any local hooks.
217 recdeps = $1-recursive $1-default $1-local
218 all: $(call recdeps,all)
219 check: $(call recdeps,check)
220 cleanall: $(call recdeps,cleanall)
221 clean: $(call recdeps,clean)
222 init: $(call recdeps,init)
223 install: $(call recdeps,install)
224 test: $(call recdeps,test)
226 # Dummy rules for local hooks
227 $(addsuffix -local,$(RULES)):
229 # Tell versions [3.59,3.63) of GNU make to not export all variables.
230 # Otherwise a system limit (for SysV at least) may be exceeded.
234 # Result of this needs to be placed inside ''
235 # XXX: Placed here because Vim cannot highlight things right afterwards
236 shq = $(subst ','\'',$(1))
237 # This has surrounding ''
238 shellquote = '$(call shq,$(1))'