1# Pull in autoconf variables
2-include $(topdir)/autoconf/variables.mak
3
4# Now that we have autoconf vars, overwrite $(topdir) with absolute path
5# version instead of relative version we inherited. The easy way to do this
6# would be to use $(abspath $(topdir)), but abspath is a gmake-3.81 feature.
7# So we let autoconf figure it out for us.
8topdir := $(abstopdir)
9
10# Older (pre-3.79) gmake does not have $(CURDIR)
11ifeq ($(CURDIR),)
12  CURDIR := $(shell pwd)
13endif
14
15# By default we do pretty-printing only
16V := @
17VV := @
18NPD := --no-print-directory
19
20# Check verbose flag
21ifeq ($(strip $(VERBOSE)),1)
22  V :=
23  NPD :=
24endif
25ifeq ($(strip $(VERBOSE)),2)
26  V :=
27  VV :=
28  NPD :=
29endif
30
31# Relative path to this dir from $(topdir)
32RELDIR := $(patsubst /%,%,$(subst $(topdir),,$(CURDIR)))
33ifneq ($(strip $(RELDIR)),)
34  RELDIR := $(RELDIR)/
35endif
36
37# Strip extensions
38STRIPEXT = $(foreach file,$(1),$(basename $(file)))
39
40# Convert a list of sources to a list of objects in OBJDIR
41SRC2OBJ = $(foreach obj,$(call STRIPEXT,$(1)),$(dir $(obj))$(OBJDIR)/$(notdir $(obj)).o)
42
43# All objects, derived from all sources
44OBJS = $(call SRC2OBJ,$(SRCS))
45
46# Dependency files, derived from all sources
47DEPS = $(foreach dep,$(call STRIPEXT,$(SRCS)),$(DEPDIR)/$(dep).P)
48
49# Default target: Build all subdirs, then reinvoke make to build local
50# targets. This is a little gross, but necessary to force make to build
51# subdirs first when running in parallel via 'make -jN'. Hopefully I will
52# discover a cleaner way to solve this someday.
53.PHONY: all
54all: all-subdirs
55	$(VV)+$(MAKE) $(NPD) all-targets
56
57# 'all-targets' is supplied by lower level Makefile. It represents
58# all targets to be built at that level. We list it here with a do-nothing
59# action in order to suppress the "Nothing to do for all-targets" message
60# when all targets are up to date.
61.PHONY: all-targets
62all-targets:
63	@#
64
65# standard install target: Same logic as 'all'.
66.PHONY: install
67install: all-subdirs
68	$(VV)+$(MAKE) $(NPD) all-targets
69	$(VV)+$(MAKE) $(NPD) all-install
70
71# 'all-install' is extended by lower-level Makefile to perform any
72# required install actions.
73.PHONY: all-install
74all-install:
75	@#
76
77# no-op targets for use by lower-level Makefiles when a particular
78# component is not being installed.
79.PHONY: install- uninstall-
80install-:
81uninstall-:
82
83# standard uninstall target: Depends on subdirs to force recursion,
84# then reinvokes make to uninstall local targets. Same logic as 'install'.
85.PHONY: uninstall
86uninstall: all-subdirs
87	$(VV)+$(MAKE) $(NPD) all-uninstall
88
89# 'all-uninstall' is extended by lower-level Makefiles to perform
90# any required uninstall actions.
91.PHONY: all-uninstall
92all-uninstall:
93	@#
94
95# Typical clean target: Remove all objects and dependency files.
96.PHONY: clean
97clean:
98	$(V)find . -depth \
99	  \( -name $(OBJDIR) -o -name $(DEPDIR) -o -name \*.a \) \
100          -exec $(ECHO) "  CLEAN" \{\} \; -exec $(RMF) \{\} \;
101
102# Remove all files that show as unversioned in svn
103.PHONY: svnclean
104svnclean:
105	$(V)rm -rfv `svn status --no-ignore 2>/dev/null | sed -e '/^[?I]/ s/^[?I] *//p' -e d`
106
107# Template rule to build a subdirectory
108.PHONY: %_DIR
109%_DIR:
110	@$(ECHO) "       " $(RELDIR)$*
111	$(VV)+$(MAKE) -C $* $(NPD) $(MAKECMDGOALS)
112
113# Collective all-subdirs target depends on subdir rule
114.PHONY: all-subdirs
115all-subdirs: $(foreach subdir,$(SUBDIRS),$(subdir)_DIR)
116
117# Echo with no newline
118# Pipline here is silly, but should be more portable
119# than 'echo -n' or 'echo ...\c'. Cannot use autoconf
120# to figure this out since 'make install' may be run
121# with root's shell when ./configure was run with user's
122# shell. Could also use 'printf' but not certain how
123# universal that is.
124define ECHO_N
125	$(ECHO) $(1) | tr -d '\n'
126endef
127
128# How to build dependencies
129MAKEDEPEND = $(CC) -M $(CPPFLAGS) $< > $(df).d
130ifeq ($(strip $(NODEPS)),)
131  define DEPENDS
132	if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi; \
133	  $(MAKEDEPEND); \
134	  $(call ECHO_N,$(OBJDIR)/) > $(df).P; \
135	  $(SED) -e 's/#.*//' -e '/^$$/ d' < $(df).d >> $(df).P; \
136	  $(SED) -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
137	         -e '/^$$/ d' -e 's/$$/ :/' < $(df).d >> $(df).P; \
138	  $(RMF) $(df).d
139  endef
140else
141  DEPENDS :=
142endif
143
144# Rule to build *.o from *.c and generate dependencies for it
145$(OBJDIR)/%.o: %.c
146	@$(ECHO) "  CXX  " $(RELDIR)$<
147	$(VV)if test ! -d $(OBJDIR); then mkdir -p $(OBJDIR); fi
148	$(V)$(CXX) $(CXXFLAGS) -c -o $@ $<
149	$(VV)$(DEPENDS)
150
151# Rule to build *.o from *.cpp and generate dependencies for it
152$(OBJDIR)/%.o: %.cpp
153	@$(ECHO) "  CXX  " $(RELDIR)$<
154	$(VV)if test ! -d $(OBJDIR); then mkdir -p $(OBJDIR); fi
155	$(V)$(CXX) $(CXXFLAGS) -c -o $@ $<
156	$(VV)$(DEPENDS)
157
158# Rule to build *.o from *.m and generate dependencies for it
159$(OBJDIR)/%.o: %.m
160	@$(ECHO) "  OBJC " $(RELDIR)$<
161	$(VV)if test ! -d $(OBJDIR); then mkdir -p $(OBJDIR); fi
162	$(V)$(OBJC) $(OBJCFLAGS) -c -o $@ $<
163	$(VV)$(DEPENDS)
164
165# Rule to link an executable
166define LINK
167	@$(ECHO) "  LD   " $(RELDIR)$@
168	$(V)$(LD) $(LDFLAGS) $+ -o $@ $(LIBS)
169endef
170
171# Rule to generate an archive (library)
172MAKELIB=$(call ARCHIVE,$@,$(OBJS))
173define ARCHIVE
174	@$(ECHO) "  AR   " $(RELDIR)$(1)
175	$(VV)$(RMF) $(1)
176	$(V)$(AR) rc $(1) $(2)
177	$(V)$(RANLIB) $(1)
178endef
179
180# How to generate a *.nib from a *.xib
181%.nib: %.xib
182	@$(ECHO) "  NIB  " $(RELDIR)$<
183	$(VV)if test ! -d $(OBJDIR); then mkdir -p $(OBJDIR); fi
184	$(V)$(NIB) $(NIBFLAGS) --compile $@ $<
185
186# Rule to create a directory during install
187define MKDIR
188   $(if $(wildcard $(DESTDIR)$(1)),, \
189      @$(ECHO) "  MKDIR" $(DESTDIR)$(1))
190   $(if $(wildcard $(DESTDIR)$(1)),, \
191     $(V)$(MKINSTALLDIRS) $(DESTDIR)$(1))
192endef
193
194# Install a program file, given mode, src, and dest
195define INSTPROG
196   @$(ECHO) "  COPY " $(2) =\> $(DESTDIR)$(3)
197   $(V)$(INSTALL_PROGRAM) $(STRIP) -m $(1) $(2) $(DESTDIR)$(3)
198endef
199
200# Install a data file, given mode, src, and dest
201define INSTDATA
202   @$(ECHO) "  COPY " $(2) =\> $(DESTDIR)$(3)
203   $(V)$(INSTALL_DATA) -m $(1) $(2) $(DESTDIR)$(3)
204endef
205
206# Install a data file, given mode, src, and dest.
207# Existing dest file is preserved; new file is named *.new if dest exists.
208define INSTNEW
209   @$(ECHO) "  COPY " $(notdir $(2)) =\> $(DESTDIR)$(3)/$(notdir $(2))$(if $(wildcard $(DESTDIR)$(3)/$(notdir $(2))),.new,)
210   $(V)$(INSTALL_DATA) -m $(1) $(2) $(DESTDIR)$(3)/$(notdir $(2))$(if $(wildcard $(DESTDIR)$(3)/$(notdir $(2))),.new,)
211endef
212
213# Install a data file, given mode, src, and dest.
214# Existing dest file is renamed to *.orig if it exists.
215define INSTORIG
216   $(if $(wildcard $(DESTDIR)$(3)/$(notdir $(2))), \
217      @$(ECHO) "  MV   " $(DESTDIR)$(3)/$(notdir $(2)) =\> \
218         $(DESTDIR)$(3)/$(notdir $(2)).orig,)
219   $(if $(wildcard $(DESTDIR)$(3)/$(notdir $(2))), \
220      $(V)$(MV) $(DESTDIR)$(3)/$(notdir $(2)) $(DESTDIR)$(3)/$(notdir $(2)).orig,)
221   @$(ECHO) "  COPY " $(notdir $(2)) =\> $(DESTDIR)$(3)/$(notdir $(2))
222   $(V)$(INSTALL_SCRIPT) -m $(1) $(2) $(DESTDIR)$(3)
223endef
224
225# Make a symlink
226define SYMLINK
227   @$(ECHO) "  LN   " $(DESTDIR)/$(2) -\> $(1)
228   $(V)$(LN) -sf $(1) $(DESTDIR)/$(2)
229endef
230
231# Copy a file
232define COPY
233   @$(ECHO) "  CP   " $(1) =\> $(DESTDIR)/$(2)
234   $(V)$(CP) -fR $(1) $(DESTDIR)/$(2)
235endef
236
237# Uninstall a file
238define UNINST
239   @$(ECHO) "  RM   " $(DESTDIR)$(1)
240   $(V)$(RMF) $(DESTDIR)$(1)
241endef
242
243# Announce distro install
244define DISTINST
245   @$(ECHO) "  ------------------------------------------------------------"
246   @$(ECHO) "  $(1) distribution installation"
247   @$(ECHO) "  ------------------------------------------------------------"
248endef
249
250# Announce distro uninstall
251define DISTUNINST
252   @$(ECHO) "  ------------------------------------------------------------"
253   @$(ECHO) "  $(1) distribution uninstall"
254   @$(ECHO) "  ------------------------------------------------------------"
255endef
256
257# If DESTDIR is set, we do no chkconfig processing
258ifeq ($(DESTDIR),)
259define CHKCFG
260    $(if $(wildcard $(2)),@$(ECHO) "  CKCFG" $(1):$(2))
261    $(if $(wildcard $(2)),$(V)$(CHKCONFIG) --$(1) apcctrl)
262endef
263endif
264
265# How to massage dependency list from rst2html
266ifeq ($(strip $(NODEPS)),)
267  define RSTDEPENDS
268	  $(ECHO) $@: $< \\ > $(df).P;                             \
269	  $(SED) -e '$$q' -e 's/^.*$$/& \\/' < $(df).d >> $(df).P; \
270	  $(ECHO) $<: >> $(df).P;                                  \
271	  $(SED) -e 's/^.*$$/&:/' < $(df).d >> $(df).P;            \
272	  $(RMF) $(df).d
273  endef
274else
275  RSTDEPENDS :=
276endif
277
278# Build *.html from *.rst and generate dependencies for it
279%.html: %.rst
280	@$(ECHO) "  HTML " $<
281ifneq ($(strip $(RST2HTML)),)
282	$(VV)if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi;
283	$(V)$(RST2HTML) $(RST2HTMLOPTS) $< $@
284	$(VV)$(RSTDEPENDS)
285else
286	@$(ECHO) "--> Not building HTML due to missing rst2html"
287endif
288
289# Build *.pdf from *.rst
290%.pdf: %.rst
291	@$(ECHO) "  PDF  " $<
292ifneq ($(strip $(RST2PDF)),)
293	$(V)$(RST2PDF) $(RST2PDFOPTS) -o $@ $<
294else
295	@$(ECHO) "--> Not building PDF due to missing rst2pdf"
296endif
297
298# Format a manpage into plain text
299define MANIFY
300	@$(ECHO) "  MAN  " $(1) -\> $(2)
301	$(V)man ./$(1) | col -b > $(2)
302endef
303
304# Rule to build a Windows resource object from the source RC file
305# Includes substitution of version strings, if needed
306comma := ,
307RCVERSION = $(subst .,$(comma),$(firstword $(subst -, ,$(VERSION))).0)
308$(OBJDIR)/%.o: %.rc
309	@$(ECHO) "  RES  " $(RELDIR)$<
310	$(V)sed -e "s/\$$VERSION/$(VERSION)/" \
311       -e "s/FILEVERSION.*/FILEVERSION     $(RCVERSION)/" \
312       -e "s/PRODUCTVERSION.*/PRODUCTVERSION  $(RCVERSION)/" $< | \
313   $(RES) -I$(dir $<) -O coff -o $@
314
315