DOM formatter
[elinks/fonseca.git] / Makefile.lib
blob7f02f1309ead32a8d2df0dfb31a9815de2c9a8ec
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)$(patsubst $(srcdir)%,%,$(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 # $(INSTALL_DATA) in cmd_installdata doesn't use the directory part of
70 # $(2) when it forms the output file name, so don't use it here either.
71 quiet_cmd_uninstall = "     [$(UNINSTALL_COLOR)UNINSTALL$(END_COLOR)]   $(3)/$(notdir $(2))"
72       cmd_uninstall = $(RM) $(3)/$(notdir $(2))
74 #############################################################################
75 # Special handling of conditional variables
77 SUBDIRS += $(SUBDIRS-yes) $(SUBDIRS-unlessno) $(SUBDIRS-unless)
78 OBJS    += $(OBJS-yes) $(OBJS-unlessno) $(OBJS-unless)
80 ALTDIRS  = $(SUBDIRS-no) $(SUBDIRS-) $(SUBDIRS-unlessyes)
81 ALTOBJS  = $(OBJS-no) $(OBJS-) $(OBJS-unlessyes)
83 ifneq ($(findstring cleanall,$(MAKECMDGOALS)),)
84 INCLUDE_ALL=1
85 endif
87 ifneq ($(findstring init,$(MAKECMDGOALS)),)
88 INCLUDE_ALL=1
89 ifndef SRC
90 SRC = $(shell cd $(top_srcdir) && pwd)
91 endif
92 endif
94 ifdef INCLUDE_ALL
95 SUBDIRS += $(ALTDIRS)
96 OBJS    += $(ALTOBJS)
97 endif
100 #############################################################################
101 # Internal build rules
103 # All files in $(OBJS) and any $(subdir)/lib.o are linked into lib.o.
104 # Sort them to filter out duplicated and get uniform order.
105 LIB_O_DEPS = \
106  $(sort $(filter-out $(LIB_O_NAME),$(OBJS))) \
107  $(foreach subdir,$(sort $(SUBDIRS)),$(wildcard $(subdir)/$(LIB_O_NAME)))
109 $(LIB_O_NAME): $(LIB_O_DEPS)
110         $(call cmd,ld_objs)
112 DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src))
113 DEP_FILES = $(DEP_FILES_1:%.o=%.P)
114 DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)
116 ifneq ($(strip $(OBJS)),)
117 -include $(DEP_FILES)
118 ALL_OBJS = $(LIB_O_DEPS) $(LIB_O_NAME)
119 endif
121 %.o: $(srcdir)%.c
122         $(call mcmd,compile)
123         @-if test -e .deps/$(*F).pp; then \
124                 cp .deps/$(*F).pp .deps/$(*F).P; \
125                 tr ' ' '\012' < .deps/$(*F).pp \
126                         | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
127                         >> .deps/$(*F).P; \
128                 rm .deps/$(*F).pp; \
129         fi
131 CLEAN += $(PROGS) $(OBJS) $(LIB_O_NAME)
134 #############################################################################
135 # The main default rules
137 all-default: $(ALL_OBJS) $(PROGS) $(MAN1) $(MAN5)
139 # Ensure that Makefiles in subdirs are created before we recursive into them
140 init-recursive: init-default
142 init-default:
143         @$(foreach subdir,$(sort $(SUBDIRS)), \
144                 $(MKINSTALLDIRS) $(subdir) >/dev/null; \
145                 test -e "$(subdir)/Makefile" \
146                 || echo 'include $(SRC)/$(RELPATH)/$(subdir)/Makefile' > $(subdir)/Makefile;)
148 clean-default cleanall-default:
149         $(Q)-test -z "$(CLEAN)" || $(RM) $(CLEAN)
151 check-default:
152 ifneq ($(SPARSE),)
153         @$(foreach file, $(wildcard *.c), \
154                 $(call ncmd,sparse,$(file));)
155 endif
157 install-default: all-default
158 ifdef PROGS
159         @$(MKINSTALLDIRS) $(DESTDIR)$(bindir)
160         @$(foreach file,$(PROGS), \
161                 $(call ncmd,installprog,$(file),$(DESTDIR)$(bindir));)
162 endif
163 ifdef MAN1
164         @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man1
165         @$(foreach file,$(MAN1), \
166                 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man1);)
167 endif
168 ifdef MAN5
169         @$(MKINSTALLDIRS) $(DESTDIR)$(mandir)/man5
170         @$(foreach file,$(MAN5), \
171                 $(call ncmd,installdata,$(file),$(DESTDIR)$(mandir)/man5);)
172 endif
174 uninstall-default:
175 ifdef PROGS
176         @$(foreach file,$(PROGS), \
177                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(bindir));)
178 endif           
179 ifdef MAN1
180         @$(foreach file,$(MAN1), \
181                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man1);)
182 endif
183 ifdef MAN5
184         @$(foreach file,$(MAN5), \
185                 $(call ncmd,uninstall,$(file),$(DESTDIR)$(mandir)/man5);)
186 endif
187 ##############################################################################
188 # Auto-testing infrastructure
190 test-default:
192 ifdef TEST_PROGS
193 TESTDEPS-$(CONFIG_DEBUG) += $(top_builddir)/src/util/memdebug.o
194 TESTDEPS-unless$(CONFIG_SMALL) += $(top_builddir)/src/util/fastfind.o
196 # Add most of the basic utility library to the test dependencies.
197 TESTDEPS += \
198  $(top_builddir)/src/intl/charsets.o \
199  $(top_builddir)/src/osdep/stub.o \
200  $(top_builddir)/src/util/conv.o \
201  $(top_builddir)/src/util/error.o \
202  $(top_builddir)/src/util/file.o \
203  $(top_builddir)/src/util/hash.o \
204  $(top_builddir)/src/util/memory.o \
205  $(top_builddir)/src/util/string.o \
206  $(top_builddir)/src/util/time.o
208 TESTDEPS += $(TESTDEPS-yes) $(TESTDEPS-unlessno)
210 TEST_LIB=$(top_srcdir)/test/libtest.sh
211 export TEST_LIB
213 # This is a very general rule but as long as we don't put test programs in src/
214 # it should work.
215 %: %.o $(TESTDEPS)
216         $(call cmd,link)
218 TESTS = $(wildcard $(srcdir)test-*)
220 $(TESTS): $(addsuffix .o,$(TEST_PROGS)) $(TEST_PROGS)
221         @echo "*** $(notdir $@) ***"; \
222         $(call shellquote,$(SHELL)) $@ $(TEST_OPTS)
224 test-default: $(TESTS)
226 clean-test:
227         @rm -fr trash
229 CLEAN += $(TEST_PROGS) $(addsuffix .o,$(TEST_PROGS))
230 clean-default: clean-test
231 endif
233 .PHONY: $(TESTS)
234 .NOPARALLEL:
237 #############################################################################
238 # Basic recursion and dependencies setup
240 RULES    = all install clean cleanall init check test uninstall
242 RULES_LOCAL = $(addsuffix -local,$(RULES))
243 RULES_REC   = $(addsuffix -recursive,$(RULES))
245 .PHONY: $(RULES) $(RULES_LOCAL) $(RULES_REC) $(addsuffix -default,$(RULES))
247 # The -recursive rules descend all subdirs.
248 # If make -k was used and a sub-Make fails, then keep building the
249 # remaining subdirectories, but return an error at the end.
250 $(RULES_REC):
251 ifneq (,$(findstring k,$(MAKEFLAGS)))
252         @suberr=0; \
253         $(foreach subdir,$(sort $(SUBDIRS)), \
254                 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || suberr=1;) \
255         exit $$suberr
256 else
257         @$(foreach subdir,$(sort $(SUBDIRS)), \
258                 $(call ncmd,recmake,$(subdir),$(subst -recursive,,$@)) || exit 1;)
259 endif
261 # Dummy -local rules
262 $(RULES_LOCAL):
264 # Default deps
265 rule_deps = $(1)-recursive $(1)-default $(1)-local
266 all:      $(call rule_deps,all)
267 install:  $(call rule_deps,install)
268 clean:    $(call rule_deps,clean)
269 cleanall: $(call rule_deps,cleanall)
270 init:     $(call rule_deps,init)
271 check:    $(call rule_deps,check)
272 test:     $(call rule_deps,test)
273 uninstall: $(call rule_deps,uninstall)
275 #############################################################################
276 # Misc
278 # Tell versions [3.59,3.63) of GNU make to not export all variables.
279 # Otherwise a system limit (for SysV at least) may be exceeded.
280 .NOEXPORT:
282 # Shell quote;
283 # Result of this needs to be placed inside ''
284 # XXX: Placed here because Vim cannot highlight things right afterwards
285 shq = $(subst ','\'',$(1))
286 # This has surrounding ''
287 shellquote = '$(call shq,$(1))'
289 # vim:syntax=make