1 ### The build commands and verbosity
5 INFO_COLOR = $(shell tput setaf 5)
6 CC_COLOR = $(shell tput setaf 6)
7 LD_COLOR = $(shell tput setaf 2)
8 PO_COLOR = $(shell tput setaf 6)
9 LINK_COLOR = $(shell tput bold;tput setaf 4)
10 INSTALL_COLOR = $(shell tput setaf 3)
11 UNINSTALL_COLOR = $(shell tput setaf 1)
12 END_COLOR = $(shell tput sgr0)
15 # sparse is architecture-neutral, which means that we need to tell it
16 # explicitly what architecture to check for. Fix this up for yours..
17 SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
20 #############################################################################
23 # If we are verbose, we will show the "real" cmds instead of
24 # their quiet versions (which are used in the non-verbose mode).
25 # Inspired by the Linux kernel build system.
36 # Show the command (quiet or non-quiet version based on the assignment
37 # just above) and then execute it.
38 ncmd = $(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
39 cmd = @$(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
40 mcmd = @$(if $($(mquiet)cmd_$(1)),echo $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
41 ecmd = @$(if $($(mquiet)cmd_$(1)),printf "%-38s " $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
43 quiet_cmd_compile = ' [$(CC_COLOR)CC$(END_COLOR)] $(RELPATH)$@'
44 masq_cmd_compile = $(COMPILE) -o $(@) -c $< $(2)
45 cmd_compile = $(COMPILE) -o $(@) -Wp,-MD,.deps/$(*F).pp -c $< $(2)
47 # Rule to compile a set of .o files into one .o file
48 quiet_cmd_ld_objs = " [$(LD_COLOR)LD$(END_COLOR)] $(RELPATH)$@"
49 cmd_ld_objs = $(LD) -r -o $@ $(filter $(OBJS), $^) \
50 $(foreach subdir,$(sort $(filter-out src,$(SUBDIRS))), \
51 `test -e $(subdir)/$(LIB_O_NAME) && echo $(subdir)/$(LIB_O_NAME)`)
53 quiet_cmd_link = ' [$(LINK_COLOR)LINK$(END_COLOR)] $(RELPATH)$@'
54 cmd_link = $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
56 quiet_cmd_sparse = ' [SPARSE] $(RELPATH)$(2)'
57 cmd_sparse = $(SPARSE) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(SPARSE_FLAGS) $(2)
60 quiet_cmd_recmake = "[$(INFO_COLOR)MAKE $(3)$(END_COLOR)] $(RELPATH)$(2)"
61 cmd_recmake = $(MAKE) -C $(2) $(3)
63 quiet_cmd_installdata = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
64 cmd_installdata = $(INSTALL_DATA) $(2) $(3)
66 # Install a data file from the source (not build) directory
67 quiet_cmd_installsrcdata = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
68 cmd_installsrcdata = $(INSTALL_DATA) $(srcdir)$(2) $(3)
70 quiet_cmd_installprog = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
71 cmd_installprog = $(INSTALL_PROGRAM) $(2) $(3)
73 quiet_cmd_uninstall = " [$(UNINSTALL_COLOR)UNINSTALL$(END_COLOR)] $(3)/$(2)"
74 cmd_uninstall = $(UNINSTALL) $(3)/$(2)
76 #############################################################################
77 # Special handling of conditional variables
79 SUBDIRS += $(SUBDIRS-yes)
82 ALTDIRS = $(SUBDIRS-no) $(SUBDIRS-)
83 ALTOBJS = $(OBJS-no) $(OBJS-)
85 ifneq ($(findstring cleanall,$(MAKECMDGOALS)),)
89 ifneq ($(findstring init,$(MAKECMDGOALS)),)
92 SRC = $(shell cd $(top_srcdir) && pwd)
102 #############################################################################
103 # Internal build rules
105 # All files in $(OBJS) and any $(subdir)/lib.o are linked into lib.o.
106 # Sort them to filter out duplicated and get uniform order.
108 $(sort $(filter-out $(LIB_O_NAME),$(OBJS))) \
109 $(foreach subdir,$(sort $(SUBDIRS)),$(wildcard $(subdir)/$(LIB_O_NAME)))
111 $(LIB_O_NAME): $(LIB_O_DEPS)
114 DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
115 DEP_FILES = $(DEP_FILES_1:%.o=%.P)
116 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
118 ifneq ($(strip $(OBJS)),)
119 -include $(DEP_FILES)
120 ALL_OBJS = $(LIB_O_DEPS) $(LIB_O_NAME)
125 @-if test -e .deps/$(*F).pp; then \
126 cp .deps/$(*F).pp .deps/$(*F).P; \
127 tr ' ' '\012' < .deps/$(*F).pp \
128 | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
133 CLEAN += $(PROGS) $(OBJS) $(LIB_O_NAME)
136 #############################################################################
137 # The main default rules
139 all-default: $(ALL_OBJS) $(PROGS) $(MAN1) $(MAN5)
141 # Ensure that Makefiles in subdirs are created before we recursive into them
142 init-recursive: init-default
145 @$(foreach subdir,$(sort $(SUBDIRS)), \
146 $(MKINSTALLDIRS) $(subdir) >/dev/null; \
147 test -e "$(subdir)/Makefile" \
148 || echo 'include $(SRC)/$(RELPATH)/$(subdir)/Makefile' > $(subdir)/Makefile;)
150 clean-default cleanall-default:
151 $(Q)-test -z "$(CLEAN)" || $(RM) $(CLEAN)
155 @$(foreach file, $(wildcard *.c), \
156 $(call ncmd,sparse,$(file));)
159 install-default: all-default
161 @$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
162 @$(foreach file,$(PROGS), \
163 $(call ncmd,installprog,$(file),$(DESTDIR)$(bindir));)
166 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
167 @$(foreach file,$(MAN1), \
168 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man1);)
171 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man5
172 @$(foreach file,$(MAN5), \
173 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man5);)
178 @$(foreach file,$(PROGS), \
179 $(call ncmd,uninstall,$(file),$(DESTDIR)$(bindir));)
182 @$(foreach file,$(MAN1), \
183 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man1);)
186 @$(foreach file,$(MAN5), \
187 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man5);)
189 ##############################################################################
190 # Auto-testing infrastructure
195 TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o
196 TESTDEPS += $(TESTDEPS-yes)
198 TEST_LIB=$(top_srcdir)/test/libtest.sh
201 # This is a very general rule but as long as we don't put test programs in src/
206 TESTS = $(wildcard $(srcdir)test-*)
208 $(TESTS): $(addsuffix .o,$(TEST_PROGS)) $(TEST_PROGS)
209 @echo "*** $(notdir $@) ***"; \
210 $(call shellquote,$(SHELL)) $@ $(TEST_OPTS)
212 test-default: $(TESTS)
217 CLEAN += $(TEST_PROGS) $(addsuffix .o,$(TEST_PROGS))
218 clean-default: clean-test
225 #############################################################################
226 # Basic recursion and dependencies setup
228 RULES = all install clean cleanall init check test uninstall
230 RULES_LOCAL = $(addsuffix -local,$(RULES))
231 RULES_REC = $(addsuffix -recursive,$(RULES))
233 .PHONY: $(RULES) $(RULES_LOCAL) $(RULES_REC) $(addsuffix -default,$(RULES))
235 # The -recursive rules descend all subdirs.
236 # If make -k was used and a sub-Make fails, then keep building the
237 # remaining subdirectories, but return an error at the end.
239 ifneq (,$(findstring k,$(MAKEFLAGS)))
241 $(foreach subdir,$(sort $(SUBDIRS)), \
242 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || suberr=1;) \
245 @$(foreach subdir,$(sort $(SUBDIRS)), \
246 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || exit 1;)
253 rule_deps = $(1)-recursive $(1)-default $(1)-local
254 all: $(call rule_deps,all)
255 install: $(call rule_deps,install)
256 clean: $(call rule_deps,clean)
257 cleanall: $(call rule_deps,cleanall)
258 init: $(call rule_deps,init)
259 check: $(call rule_deps,check)
260 test: $(call rule_deps,test)
261 uninstall: $(call rule_deps,uninstall)
263 #############################################################################
266 # Tell versions [3.59,3.63) of GNU make to not export all variables.
267 # Otherwise a system limit (for SysV at least) may be exceeded.
271 # Result of this needs to be placed inside ''
272 # XXX: Placed here because Vim cannot highlight things right afterwards
273 shq = $(subst ','\'',$(1))
274 # This has surrounding ''
275 shellquote = '$(call shq,$(1))'