Saner handling if config.mk doesn't exist: use a default config.defaults.mk.
[wvstreams.git] / wvrules.mk
blob734ee4db9078bad0f2466f8298b34b2322a66f06
1 # wvrules.mk
3 # Copyright (C) 1998-2007 by Avery Pennarun <apenwarr@alumnit.ca>
4 # and contributors.
5 # Use, distribute, modify, and redistribute freely. (But if you're nice,
6 # you'll send all your changes back to me.)
8 # This is a complicated-looking set of Makefile rules that should make your
9 # own Makefiles simpler, by providing for several useful features (like
10 # autodependencies and a 'clean' target) without any extra effort.
12 # It will only work with GNU make.
15 # we need a default rule, since the 'includes' below can cause trouble
16 .PHONY: default all
17 default: all
19 # if WVSTREAMS_SRC is set assume everything else is set.
20 # For packages that use WvStreams use WVSTREAMS_SRC=. for distribution.
21 ifneq ($(WVSTREAMS),)
22 WVSTREAMS_SRC=$(WVSTREAMS)
23 WVSTREAMS_LIB=$(WVSTREAMS)
24 WVSTREAMS_INC=$(WVSTREAMS)/include
25 WVSTREAMS_BIN=$(WVSTREAMS)
26 endif
27 export WVSTREAMS WVSTREAMS_SRC WVSTREAMS_LIB WVSTREAMS_INC WVSTREAMS_BIN
29 SHELL=/bin/bash
31 include $(WVSTREAMS_SRC)/config.defaults.mk
32 -include $(WVSTREAMS_SRC)/config.mk
33 -include $(WVSTREAMS_SRC)/config.overrides.mk
34 -include $(WVSTREAMS_SRC)/local.mk
36 ifeq ($(wildcard $(WVSTREAMS_SRC)/config.mk),)
37 __junk:=$(shell echo "Warning: $(WVSTREAMS_SRC)/config.mk doesn't exist" >&2)
38 endif
40 include $(WVSTREAMS_SRC)/wvrules-$(COMPILER_STANDARD).mk
42 ifeq (${EXEEXT},.exe)
43 include $(WVSTREAMS_SRC)/wvrules-win32.mk
44 endif
46 ifeq (${WVTESTRUN},)
47 WVTESTRUN=$(WVSTREAMS_BIN)/wvtesthelper
48 endif
50 # macros that expand to the object files in the given directories
51 objects=$(sort $(foreach type,c cc,$(call objects_$(type),$1)))
52 objects_c=$(patsubst %.c,%.o,$(wildcard $(addsuffix /*.c,$1)))
53 objects_cc=$(patsubst %.cc,%.o,$(wildcard $(addsuffix /*.cc,$1)))
55 # default "test" rule does nothing...
56 .PHONY: test runtests
57 test:
58 runtests:
60 %/test:
61 $(MAKE) -C $(dir $@) test
63 INCFLAGS=$(addprefix -I,$(WVSTREAMS_INC) $(XPATH))
64 CPPFLAGS+=$(INCFLAGS) \
65 -D_BSD_SOURCE -D_GNU_SOURCE $(OSDEFINE) \
66 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
68 ifeq ($(VERBOSE),1)
69 COMPILE_MSG :=
70 LINK_MSG :=
71 DEPEND_MSG :=
72 SYMLINK_MSG :=
73 else
74 COMPILE_MSG = @echo compiling $@...;
75 LINK_MSG = @echo linking $@...;
76 #DEPEND_MSG = @echo " depending $@...";
77 DEPEND_MSG := @
78 SYMLINK_MSG := @
79 endif
81 # any rule that depends on FORCE will always run
82 .PHONY: FORCE
83 FORCE:
85 ifeq ($(LN_S),)
86 LN_S := ln -s
87 endif
88 ifeq ($(LN),)
89 LN := ln
90 endif
92 # Create symbolic links
93 # usage: $(wvlns,source,dest)
94 wvlns=$(SYMLINK_MSG)$(LN_S) -f $1 $2
96 # Create hard links
97 # usage: $(wvln,source,dest)
98 wvln=$(SYMLINK_MSG)$(LN) -f $1 $2
100 # usage: $(wvcc_base,outfile,infile,stem,compiler cflags,mode)
101 # eg: $(wvcc,foo.o,foo.cc,foo,$(CC) $(CFLAGS) -fPIC,-c)
102 DEPFILE = $(if $(filter %.o,$1),$(dir $1).$(notdir $(1:.o=.d)),/dev/null)
103 wvcc=$(call wvcc_base,$1,$2,$3,$(CC) $(CFLAGS) $($1-CFLAGS) $(CPPFLAGS) $($1-CPPFLAGS) $4,$(if $5,$5,-c))
104 wvcxx=$(call wvcc_base,$1,$2,$3,$(CXX) $(CXXFLAGS) $($1-CFLAGS) $(CPPFLAGS) $($1-CPPFLAGS) $($1-CXXFLAGS) $4,$(if $5,$5,-c))
106 %.so: SONAME=$@$(if $(SO_VERSION),.$(SO_VERSION))
108 wvsoname=$(if $($1-SONAME),$($1-SONAME),$(if $(SONAME),$(SONAME),$1))
109 define wvlink_so
110 $(LINK_MSG)$(WVLINK_CC) $(LDFLAGS) $($1-LDFLAGS) -Wl,-z,defs -Wl,-soname,$(call wvsoname,$1) -shared -o $1 $(filter %.o %.a %.so,$2) $($1-LIBS) $(LIBS) $(XX_LIBS)
111 $(if $(filter-out $(call wvsoname,$1),$1),$(call wvlns,$1,$(call wvsoname,$1)))
112 endef
114 ../%.so:; @echo "Shared library $@ does not exist!"; exit 1
115 ../%.a:; @echo "Library $@ does not exist!"; exit 1
116 ../%.o:; @echo "Object $@ does not exist!"; exit 1
117 /%.a:; @echo "Library $@ does not exist!"; exit 1
119 %.o: %.c; $(call wvcc ,$@,$<,$*)
120 %.fpic.o: %.c; $(call wvcc ,$@,$<,$*,-fPIC)
121 %.o: %.cc; $(call wvcxx,$@,$<,$*)
122 %.fpic.o: %.cc; $(call wvcxx,$@,$<,$*,-fPIC)
123 %.o: %.cpp; $(call wvcxx,$@,$<,$*)
124 %.fpic.o:%.cpp; $(call wvcxx,$@,$<,$*,-fPIC)
125 %.s: %.c; $(call wvcc ,$@,$<,$*,,-S)
126 %.s: %.cc; $(call wvcxx,$@,$<,$*,,-S)
127 %.s: %.cpp; $(call wvcxx,$@,$<,$*,,-S)
128 %.E: %.c; $(call wvcc,$@,$<,$*,,-E)
129 %.E: %.cc; $(call wvcxx,$@,$<,$*,,-E)
130 %.E: %.cpp; $(call wvcxx,$@,$<,$*,,-E)
132 %.moc: %.h; moc -o $@ $<
134 %: %.o; $(call wvlink,$@,$^)
135 %.t: %.t.o; $(call wvlink,$@,$(call reverse,$(filter %.o,$^)) $(filter-out %.o,$^) $(LIBWVTEST))
136 %.a %.libs:; $(call wvlink_ar,$@,$^)
137 %.so:; $(call wvlink_so,$@,$^)
139 # Force objects to be built before final binaries
140 $(addsuffix .o,$(basename $(wildcard *.c) $(wildcard *.cc) $(wildcard *.cpp))):
142 %.gz: FORCE %
143 @rm -f $@
144 gzip -f $*
145 @ls -l $@
148 # We automatically generate header dependencies for .c and .cc files. The
149 # dependencies are stored in the file ".filename.d", and we include them
150 # automatically here if they exist.
152 depfiles_sf = $(wildcard .*.d t/.*.d)
153 ifneq ($(depfiles_sf),)
154 -include $(depfiles_sf)
155 endif
159 # A macro for compiling subdirectories listed in the SUBDIRS variable.
160 # Tries to make the target ($@) in each subdir, unless the target is called
161 # "subdirs" in which case it makes "all" in each subdir.
163 define subdirs_func
164 +@OLDDIR="$$(pwd)"; set -e; \
165 for d in __fx__ $2; do \
166 if [ "$$d" = "__fx__" ]; then continue; fi; \
167 cd "$$d"; \
168 echo ; \
169 echo "--> Making $1 in $$(pwd)..."; \
170 $(MAKE) --no-print-directory $1 || exit 1; \
171 cd "$$OLDDIR"; \
172 done
173 @echo
174 @echo "--> Back in $$(pwd)..."
175 endef
177 subdirs = $(call subdirs_func,$(subst subdirs,all,$(if $1,$1,$@)),$(if $2,$2,$(SUBDIRS)))
179 define shell_reverse
180 revlist="" ; \
181 for word in $(1) ; do \
182 revlist="$${word} $${revlist}"; \
183 done ; \
184 echo "$${revlist}"
185 endef
186 reverse = $(shell $(call shell_reverse,$(1)))
188 clean_subdirs = $(call subdirs,clean,$(call reverse,$(SUBDIRS)),keep)
190 %: %/Makefile FORCE
191 @cd "$@"; echo; echo "--> Making all in $$(pwd)..."; \
192 $(MAKE) --no-print-directory all
194 subdirs: ${SUBDIRS}
197 # Auto-clean rule. Feel free to append to this in your own directory, by
198 # defining your own "clean" and/or "distclean" rules.
200 .PHONY: clean _wvclean
202 clean: _wvclean
204 _wvclean:
205 @echo '--> Cleaning $(shell pwd)...'
206 @rm -f *~ *.tmp *.o *.a *.so *.so.* *.libs *.dll *.lib *.moc *.d .*.d .depend \
207 .\#* .tcl_paths pkgIndex.tcl gmon.out core build-stamp \
208 wvtestmain
209 @rm -f $(patsubst %.t.cc,%.t,$(wildcard *.t.cc) $(wildcard t/*.t.cc)) \
210 t/*.o t/*~ t/.*.d t/.\#*
211 @rm -f valgrind.log.pid*
212 @rm -f semantic.cache tags
213 @rm -rf debian/tmp
215 distclean: clean
217 dist-hook:
219 dist: dist-hook
220 @echo '--> Making dist in ../build/$(PKGDIR)...'
221 @test -d ../build || mkdir ../build
222 @rsync -a --delete --force '$(shell pwd)/' '../build/$(PKGDIR)'
223 @find '../build/$(PKGDIR)' -name .svn -type d -print0 | xargs -0 rm -rf --
224 @find '../build/$(PKGDIR)' -name .cvsignore -type f -print0 | xargs -0 rm -f --
225 @$(MAKE) -C '../build/$(PKGDIR)' distclean
226 @rm -f '../build/$(PKGDIR).tar.gz'
227 @cd ../build; tar -zcf '$(PKGDIR).tar.gz' '$(PKGDIR)'
228 @echo '--> Created tarball in ../build/$(PKGDIR).tar.gz.'