Merge with git+ssh://pasky.or.cz/srv/git/elinks.git
[elinks.git] / Makefile.lib
blob446c1e62fa2f488ea71bd132712a28c72971d024
1 ### The build commands and verbosity
3 # Colorize the build.
4 ifdef MAKE_COLOR
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)
13 endif
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 #############################################################################
21 # Build recipies
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.
26 ifdef V
27         quiet =
28         mquiet = masq_
29         Q =
30 else
31         quiet = quiet_
32         mquiet = quiet_
33         Q = @
34 endif
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)
59 # Recursive make
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 quiet_cmd_installprog = "     [$(INSTALL_COLOR)INSTALL$(END_COLOR)]   $(RELPATH)$(2) -> $(3)"
67       cmd_installprog = $(INSTALL_PROGRAM) $(2) $(3)
69 quiet_cmd_uninstall = "     [$(UNINSTALL_COLOR)UNINSTALL$(END_COLOR)]   $(3)/$(2)"
70       cmd_uninstall = $(UNINSTALL) $(3)/$(2)
72 #############################################################################
73 # Special handling of conditional variables
75 SUBDIRS += $(SUBDIRS-yes)
76 OBJS    += $(OBJS-yes)
78 ALTDIRS  = $(SUBDIRS-no) $(SUBDIRS-)
79 ALTOBJS  = $(OBJS-no) $(OBJS-)
81 ifneq ($(findstring cleanall,$(MAKECMDGOALS)),)
82 INCLUDE_ALL=1
83 endif
85 ifneq ($(findstring init,$(MAKECMDGOALS)),)
86 INCLUDE_ALL=1
87 ifndef SRC
88 SRC = $(shell cd $(top_srcdir) && pwd)
89 endif
90 endif
92 ifdef INCLUDE_ALL
93 SUBDIRS += $(ALTDIRS)
94 OBJS    += $(ALTOBJS)
95 endif
98 #############################################################################
99 # Internal build rules
101 # All files in $(OBJS) and any $(subdir)/lib.o are linked into lib.o.
102 # Sort them to filter out duplicated and get uniform order.
103 LIB_O_DEPS = \
104  $(sort $(filter-out $(LIB_O_NAME),$(OBJS))) \
105  $(foreach subdir,$(sort $(SUBDIRS)),$(wildcard $(subdir)/$(LIB_O_NAME)))
107 $(LIB_O_NAME): $(LIB_O_DEPS)
108         $(call cmd,ld_objs)
110 DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
111 DEP_FILES = $(DEP_FILES_1:%.o=%.P)
112 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
114 ifneq ($(strip $(OBJS)),)
115 -include $(DEP_FILES)
116 ALL_OBJS = $(LIB_O_DEPS) $(LIB_O_NAME)
117 endif
119 %.o: $(srcdir)%.c
120         $(call mcmd,compile)
121         @-if test -e .deps/$(*F).pp; then \
122                 cp .deps/$(*F).pp .deps/$(*F).P; \
123                 tr ' ' '\012' < .deps/$(*F).pp \
124                         | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
125                         >> .deps/$(*F).P; \
126                 rm .deps/$(*F).pp; \
127         fi
129 CLEAN += $(PROGS) $(OBJS) $(LIB_O_NAME)
132 #############################################################################
133 # The main default rules
135 all-default: $(ALL_OBJS) $(PROGS) $(MAN1) $(MAN5)
137 # Ensure that Makefiles in subdirs are created before we recursive into them
138 init-recursive: init-default
140 init-default:
141         @$(foreach subdir,$(sort $(SUBDIRS)), \
142                 $(MKINSTALLDIRS) $(subdir) >/dev/null; \
143                 test -e "$(subdir)/Makefile" \
144                 || echo 'include $(SRC)/$(RELPATH)/$(subdir)/Makefile' > $(subdir)/Makefile;)
146 clean-default cleanall-default:
147         $(Q)-test -z "$(CLEAN)" || $(RM) $(CLEAN)
149 check-default:
150 ifneq ($(SPARSE),)
151         @$(foreach file, $(wildcard *.c), \
152                 $(call ncmd,sparse,$(file));)
153 endif
155 install-default: all-default
156 ifdef PROGS
157         @$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
158         @$(foreach file,$(PROGS), \
159                 $(call ncmd,installprog,$(file),$(DESTDIR)$(bindir));)
160 endif
161 ifdef MAN1
162         @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
163         @$(foreach file,$(MAN1), \
164                 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man1);)
165 endif
166 ifdef MAN5
167         @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man5
168         @$(foreach file,$(MAN5), \
169                 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man5);)
170 endif
172 uninstall-default:
173 ifdef PROGS
174         @$(foreach file,$(PROGS), \
175                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(bindir));)
176 endif           
177 ifdef MAN1
178         @$(foreach file,$(MAN1), \
179                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man1);)
180 endif
181 ifdef MAN5
182         @$(foreach file,$(MAN5), \
183                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man5);)
184 endif
185 ##############################################################################
186 # Auto-testing infrastructure
188 test-default:
190 ifdef TEST_PROGS
191 TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o
192 TESTDEPS += $(TESTDEPS-yes)
194 TEST_LIB=$(top_srcdir)/test/libtest.sh
195 export TEST_LIB
197 # This is a very general rule but as long as we don't put test programs in src/
198 # it should work.
199 %: %.o $(TESTDEPS)
200         $(call cmd,link)
202 TESTS = $(wildcard $(srcdir)test-*)
204 $(TESTS): $(addsuffix .o,$(TEST_PROGS)) $(TEST_PROGS)
205         @echo "*** $(notdir $@) ***"; \
206         $(call shellquote,$(SHELL)) $@ $(TEST_OPTS)
208 test-default: $(TESTS)
210 clean-test:
211         @rm -fr trash
213 CLEAN += $(TEST_PROGS) $(addsuffix .o,$(TEST_PROGS))
214 clean-default: clean-test
215 endif
217 .PHONY: $(TESTS)
218 .NOPARALLEL:
221 #############################################################################
222 # Basic recursion and dependencies setup
224 RULES    = all install clean cleanall init check test uninstall
226 RULES_LOCAL = $(addsuffix -local,$(RULES))
227 RULES_REC   = $(addsuffix -recursive,$(RULES))
229 .PHONY: $(RULES) $(RULES_LOCAL) $(RULES_REC) $(addsuffix -default,$(RULES))
231 # The -recursive rules descend all subdirs.
232 # If make -k was used and a sub-Make fails, then keep building the
233 # remaining subdirectories, but return an error at the end.
234 $(RULES_REC):
235 ifneq (,$(findstring k,$(MAKEFLAGS)))
236         @suberr=0; \
237         $(foreach subdir,$(sort $(SUBDIRS)), \
238                 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || suberr=1;) \
239         exit $$suberr
240 else
241         @$(foreach subdir,$(sort $(SUBDIRS)), \
242                 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || exit 1;)
243 endif
245 # Dummy -local rules
246 $(RULES_LOCAL):
248 # Default deps
249 rule_deps = $(1)-recursive $(1)-default $(1)-local
250 all:      $(call rule_deps,all)
251 install:  $(call rule_deps,install)
252 clean:    $(call rule_deps,clean)
253 cleanall: $(call rule_deps,cleanall)
254 init:     $(call rule_deps,init)
255 check:    $(call rule_deps,check)
256 test:     $(call rule_deps,test)
257 uninstall: $(call rule_deps,uninstall)
259 #############################################################################
260 # Misc
262 # Tell versions [3.59,3.63) of GNU make to not export all variables.
263 # Otherwise a system limit (for SysV at least) may be exceeded.
264 .NOEXPORT:
266 # Shell quote;
267 # Result of this needs to be placed inside ''
268 # XXX: Placed here because Vim cannot highlight things right afterwards
269 shq = $(subst ','\'',$(1))
270 # This has surrounding ''
271 shellquote = '$(call shq,$(1))'
273 # vim:syntax=make