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 END_COLOR = $(shell tput sgr0)
14 # sparse is architecture-neutral, which means that we need to tell it
15 # explicitly what architecture to check for. Fix this up for yours..
16 SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__
19 #############################################################################
22 # If we are verbose, we will show the "real" cmds instead of
23 # their quiet versions (which are used in the non-verbose mode).
24 # Inspired by the Linux kernel build system.
33 # Show the command (quiet or non-quiet version based on the assignment
34 # just above) and then execute it.
35 ncmd = $(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
36 cmd = @$(if $($(quiet)cmd_$(1)),echo $($(quiet)cmd_$(1)) &&) $(cmd_$(1))
37 mcmd = @$(if $($(mquiet)cmd_$(1)),echo $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
38 ecmd = @$(if $($(mquiet)cmd_$(1)),printf "%-38s " $($(mquiet)cmd_$(1)) &&) $(cmd_$(1))
40 quiet_cmd_compile = ' [$(CC_COLOR)CC$(END_COLOR)] $(RELPATH)$@'
41 masq_cmd_compile = $(COMPILE) -o $(@) -c $< $(2)
42 cmd_compile = $(COMPILE) -o $(@) -Wp,-MD,.deps/$(*F).pp -c $< $(2)
44 # Rule to compile a set of .o files into one .o file
45 quiet_cmd_ld_objs = " [$(LD_COLOR)LD$(END_COLOR)] $(RELPATH)$@"
46 cmd_ld_objs = $(LD) -r -o $@ $(filter $(OBJS), $^) \
47 $(foreach subdir,$(sort $(filter-out src,$(SUBDIRS))), \
48 `test -e $(subdir)/$(LIB_O_NAME) && echo $(subdir)/$(LIB_O_NAME)`)
50 quiet_cmd_link = ' [$(LINK_COLOR)LINK$(END_COLOR)] $(RELPATH)$@'
51 cmd_link = $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(2) $(LIBS)
53 quiet_cmd_sparse = ' [SPARSE] $(RELPATH)$(2)'
54 cmd_sparse = $(SPARSE) $(DEFS) $(INCLUDES) $(AM_CFLAGS) $(CFLAGS) $(SPARSE_FLAGS) $(2)
57 quiet_cmd_recmake = "[$(INFO_COLOR)MAKE $(3)$(END_COLOR)] $(RELPATH)$(2)"
58 cmd_recmake = $(MAKE) -C $(2) $(3)
60 quiet_cmd_installdata = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
61 cmd_installdata = $(INSTALL_DATA) $(2) $(3)
63 quiet_cmd_installprog = " [$(INSTALL_COLOR)INSTALL$(END_COLOR)] $(RELPATH)$(2) -> $(3)"
64 cmd_installprog = $(INSTALL_PROGRAM) $(2) $(3)
67 #############################################################################
68 # Special handling of conditional variables
70 SUBDIRS += $(SUBDIRS-yes)
73 ALTDIRS = $(SUBDIRS-no) $(SUBDIRS-)
74 ALTOBJS = $(OBJS-no) $(OBJS-)
76 ifneq ($(findstring cleanall,$(MAKECMDGOALS)),)
80 ifneq ($(findstring init,$(MAKECMDGOALS)),)
81 # FIXME: Detect when $(subdir)/Makefile is $(srcdir)/$(subdir)/Makefile
82 # and error out so the user won't overwrite the 'master' Makefiles.
92 #############################################################################
93 # Internal build rules
95 # All files in $(OBJS) and any $(subdir)/lib.o are linked into lib.o
97 $(sort $(filter-out $(LIB_O_NAME),$(OBJS))) \
98 $(foreach subdir,$(sort $(SUBDIRS)),$(wildcard $(subdir)/$(LIB_O_NAME)))
100 $(LIB_O_NAME): $(LIB_O_DEPS)
103 DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
104 DEP_FILES = $(DEP_FILES_1:%.o=%.P)
105 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
107 ifneq ($(strip $(OBJS)),)
108 -include $(DEP_FILES)
109 OBJS += $(LIB_O_NAME)
114 @-cp .deps/$(*F).pp .deps/$(*F).P; \
115 tr ' ' '\012' < .deps/$(*F).pp \
116 | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
120 CLEAN += $(PROG) $(OBJS)
123 #############################################################################
124 # The main default rules
126 all-default: $(OBJS) $(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 cleanall-default:
137 @-test -z "$(CLEAN)" || $(RM) $(CLEAN)
141 @$(foreach file, $(wildcard *.c), \
142 $(call ncmd,sparse,$(file));)
145 install-default: all-default
147 @$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
148 @$(foreach file,$(PROGS), \
149 $(call ncmd,installprog,$(file),$(DESTDIR)$(bindir));)
152 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
153 @$(foreach file,$(MAN1), \
154 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man1);)
157 @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man5
158 @$(foreach file,$(MAN5), \
159 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man5);)
163 ##############################################################################
164 # Auto-testing infrastructure
169 TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o
170 TESTDEPS += $(TESTDEPS-yes)
172 TESTS = $(wildcard $(srcdir)test-*)
174 $(TEST_PROGS): $(TESTDEPS)
175 $(call cmd,link,$@.o)
177 # We cannot use $$@.o in the rule above so ensure that all test programs are
178 # built before linking.
179 $(TESTS): $(addsuffix .o,$(TEST_PROGS)) $(TEST_PROGS)
180 @echo "*** $(notdir $@) ***"; \
181 $(call shellquote,$(SHELL)) $@ $(TEST_OPTS)
183 test-default: $(TESTS)
188 CLEAN += $(TEST_PROGS) $(addsuffix .o,$(TEST_PROGS))
189 clean-default: clean-test
196 #############################################################################
197 # Basic recursion and dependencies setup
199 RULES = all install clean cleanall init check test
201 RULES_LOCAL = $(addsuffix -local,$(RULES))
202 RULES_REC = $(addsuffix -recursive,$(RULES))
204 .PHONY: $(RULES) $(RULES_LOCAL) $(RULES_REC) $(addsuffix -default,$(RULES))
206 # The -recursive rules decend all subdirs.
208 @$(foreach subdir,$(sort $(SUBDIRS)), \
209 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || exit 1;)
215 rule_deps = $(1)-recursive $(1)-default $(1)-local
216 all: $(call rule_deps,all)
217 install: $(call rule_deps,install)
218 clean: $(call rule_deps,clean)
219 cleanall: $(call rule_deps,cleanall)
220 init: $(call rule_deps,init)
221 check: $(call rule_deps,check)
222 test: $(call rule_deps,test)
224 #############################################################################
227 # Tell versions [3.59,3.63) of GNU make to not export all variables.
228 # Otherwise a system limit (for SysV at least) may be exceeded.
232 # Result of this needs to be placed inside ''
233 # XXX: Placed here because Vim cannot highlight things right afterwards
234 shq = $(subst ','\'',$(1))
235 # This has surrounding ''
236 shellquote = '$(call shq,$(1))'