Ok - I think this has all of the bits working necessary to compile WvStreams
[wvstreams.git] / wvrules.mk
blob00a8aeee913d452ed3691bdb3db04b0e06654b7f
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 all: CC CXX
21 # if WVSTREAMS_SRC is set assume everything else is set.
22 # For packages that use WvStreams use WVSTREAMS_SRC=. for distribution.
23 ifneq ($(WVSTREAMS),)
24 WVSTREAMS_SRC=$(WVSTREAMS)
25 WVSTREAMS_LIB=$(WVSTREAMS)
26 WVSTREAMS_INC=$(WVSTREAMS)/include
27 WVSTREAMS_BIN=$(WVSTREAMS)
28 endif
29 export WVSTREAMS WVSTREAMS_SRC WVSTREAMS_LIB WVSTREAMS_INC WVSTREAMS_BIN
31 SHELL=/bin/bash
33 include $(WVSTREAMS_SRC)/config.defaults.mk
34 -include $(WVSTREAMS_SRC)/config.mk
35 -include $(WVSTREAMS_SRC)/config.overrides.mk
36 -include $(WVSTREAMS_SRC)/local.mk
38 ifeq (${OS},SOLARIS)
39 _SOLARIS= _SOLARIS
40 AR=gar
41 endif
43 ifeq (${OS},MACOS)
44 _MACOS=_MACOS
45 endif
47 ifeq (${OS},WIN32)
48 _WIN32=_WIN32
49 endif
51 ifdef _WIN32
52 XPATH += $(WVSTREAMS)/win32 $(WVSTREAMS)/win32/cominclude
53 AR=i586-mingw32msvc-ar
54 LIBS += -lssl -lcrypto -lz -lole32 -lrpcrt4 -lwsock32 -lgdi32 -limagehlp \
55 -lstdc++
56 else
57 CFLAGS += -fPIC
58 CXXFLAGS += -fPIC
59 endif
61 include $(WVSTREAMS_SRC)/wvrules-$(COMPILER_STANDARD).mk
63 ifeq (${WVTESTRUN},)
64 WVTESTRUN=$(WVSTREAMS_BIN)/wvtestrunner.pl
65 endif
67 # macros that expand to the object files in the given directories
68 objects=$(sort $(foreach type,c cc,$(call objects_$(type),$1)))
69 objects_c=$(filter-out $(WV_EXCLUDES), \
70 $(patsubst %.c,%.o,$(wildcard $(addsuffix /*.c,$1))))
71 objects_cc=$(filter-out $(WV_EXCLUDES), \
72 $(patsubst %.cc,%.o,$(wildcard $(addsuffix /*.cc,$1))))
73 tests_cc=$(filter-out $(WV_EXCLUDES), \
74 $(patsubst %.cc,%,$(wildcard $(addsuffix /*.cc,$1))))
76 # default "test" rule does nothing...
77 .PHONY: test runtests
78 test:
79 runtests:
81 %/test:
82 $(MAKE) -C $(dir $@) test
84 INCFLAGS=$(addprefix -I,$(WVSTREAMS_INC) $(XPATH))
85 CPPFLAGS+=$(INCFLAGS) \
86 -D_BSD_SOURCE -D_GNU_SOURCE $(OSDEFINE) \
87 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 \
88 -DUNSTABLE
90 ifeq ($(VERBOSE),1)
91 COMPILE_MSG :=
92 LINK_MSG :=
93 DEPEND_MSG :=
94 SYMLINK_MSG :=
95 else
96 COMPILE_MSG = @echo compiling $@...;
97 LINK_MSG = @echo linking $@...;
98 #DEPEND_MSG = @echo " depending $@...";
99 DEPEND_MSG := @
100 SYMLINK_MSG := @
101 endif
103 # any rule that depends on FORCE will always run
104 .PHONY: FORCE
105 FORCE:
107 ifeq ($(LN_S),)
108 LN_S := ln -s
109 endif
110 ifeq ($(LN),)
111 LN := ln
112 endif
114 # Create symbolic links
115 # usage: $(wvlns,source,dest)
116 wvlns=$(SYMLINK_MSG)$(LN_S) -f $1 $2
118 # Create hard links
119 # usage: $(wvln,source,dest)
120 wvln=$(SYMLINK_MSG)$(LN) -f $1 $2
122 # usage: $(wvcc,outfile,infile,stem,extra_cflags,mode)
123 # eg: $(wvcc,foo.o,foo.cc,foo,-fPIC,-c)
125 define wvcc
126 ./CC $(if $5,$5,-c) $3 $($1-CFLAGS) $($1-CPPFLAGS) $4
127 endef
129 define wvcxx
130 ./CXX $(if $5,$5,-c) $3 $($1-CXXFLAGS) $($1-CPPFLAGS) $4
131 endef
133 %.so: SONAME=$@$(if $(SO_VERSION),.$(SO_VERSION))
135 wvsoname=$(if $($1-SONAME),$($1-SONAME),$(if $(SONAME),$(SONAME),$1))
136 ifdef _WIN32
137 define wvlink_so
138 @echo "Skipping $@ on win32 (can't build shared libraries)"
139 endef
140 endif
142 ifdef _SOLARIS
143 define wvlink_so
144 @echo "Solaris Link!"
145 $(LINK_MSG)$(WVLINK_CC) $(LDFLAGS) $($1-LDFLAGS) -shared -o $1 $(filter %.o %.a %.so,$2) $($1-LIBS) $(LIBS) $(XX_LIBS)
146 endef
147 else ifdef _MACOS
148 define wvlink_so
149 @echo "MacOS Link!"
150 $(LINK_MSG)$(WVLINK_CC) $(LDFLAGS) $($1-LDFLAGS) -dynamiclib -o $1 $(filter %.o %.a %.so,$2) $($1-LIBS) $(LIBS) $(XX_LIBS)
151 endef
152 else
153 define wvlink_so
154 $(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)
155 $(if $(filter-out $(call wvsoname,$1),$1),$(call wvlns,$1,$(call wvsoname,$1)))
156 endef
157 endif
159 ../%.so:; @echo "Shared library $@ does not exist!"; exit 1
160 ../%.a:; @echo "Library $@ does not exist!"; exit 1
161 ../%.o:; @echo "Object $@ does not exist!"; exit 1
162 /%.a:; @echo "Library $@ does not exist!"; exit 1
164 %.o: %.c; $(call wvcc ,$@,$<,$*)
165 %.fpic.o: %.c; $(call wvcc ,$@,$<,$*,-fPIC)
166 %.o: %.cc; $(call wvcxx,$@,$<,$*)
167 %.fpic.o: %.cc; $(call wvcxx,$@,$<,$*,-fPIC)
168 %.o: %.cpp; $(call wvcxx,$@,$<,$*)
169 %.fpic.o:%.cpp; $(call wvcxx,$@,$<,$*,-fPIC)
170 %.s: %.c; $(call wvcc ,$@,$<,$*,,-S)
171 %.s: %.cc; $(call wvcxx,$@,$<,$*,,-S)
172 %.s: %.cpp; $(call wvcxx,$@,$<,$*,,-S)
173 %.E: %.c; $(call wvcc,$@,$<,$*,,-E)
174 %.E: %.cc; $(call wvcxx,$@,$<,$*,,-E)
175 %.E: %.cpp; $(call wvcxx,$@,$<,$*,,-E)
177 %.moc: %.h; $(MOC) -o $@ $<
179 %: %.o; $(call wvlink,$@,$^)
180 %.t: %.t.o; $(call wvlink,$@,$(call reverse,$(filter %.o,$^)) $(filter-out %.o,$^) $(LIBWVTEST))
181 %.a %.libs:; $(call wvlink_ar,$@,$^)
182 %.so:; $(call wvlink_so,$@,$^)
184 # Force objects to be built before final binaries
185 $(addsuffix .o,$(basename $(wildcard *.c) $(wildcard *.cc) $(wildcard *.cpp))):
187 %.gz: FORCE %
188 @rm -f $@
189 gzip -f $*
190 @ls -l $@
193 # We automatically generate header dependencies for .c and .cc files. The
194 # dependencies are stored in the file ".filename.d", and we include them
195 # automatically here if they exist.
197 -include $(shell find . -name '.*.d') /dev/null
201 # A macro for compiling subdirectories listed in the SUBDIRS variable.
202 # Tries to make the target ($@) in each subdir, unless the target is called
203 # "subdirs" in which case it makes "all" in each subdir.
205 define subdirs_func
206 +@OLDDIR="$$(pwd)"; set -e; \
207 for d in __fx__ $2; do \
208 if [ "$$d" = "__fx__" ]; then continue; fi; \
209 cd "$$d"; \
210 echo ; \
211 echo "--> Making $1 in $$(pwd)..."; \
212 $(MAKE) --no-print-directory $1 || exit 1; \
213 cd "$$OLDDIR"; \
214 done
215 @echo
216 @echo "--> Back in $$(pwd)..."
217 endef
219 subdirs = $(call subdirs_func,$(subst subdirs,all,$(if $1,$1,$@)),$(if $2,$2,$(SUBDIRS)))
221 define shell_reverse
222 revlist="" ; \
223 for word in $(1) ; do \
224 revlist="$${word} $${revlist}"; \
225 done ; \
226 echo "$${revlist}"
227 endef
228 reverse = $(shell $(call shell_reverse,$(1)))
230 clean_subdirs = $(call subdirs,clean,$(call reverse,$(SUBDIRS)),keep)
232 %: %/Makefile FORCE
233 @cd "$@"; echo; echo "--> Making all in $$(pwd)..."; \
234 $(MAKE) --no-print-directory all
236 subdirs: ${SUBDIRS}
239 # Auto-clean rule. Feel free to append to this in your own directory, by
240 # defining your own "clean" and/or "distclean" rules.
242 .PHONY: clean _wvclean
244 clean: _wvclean
246 _wvclean:
247 @echo '--> Cleaning $(shell pwd)...'
248 @rm -f *~ *.tmp *.o *.a *.so *.so.* *.libs *.dll *.lib *.moc *.d .*.d .depend \
249 .\#* .tcl_paths pkgIndex.tcl gmon.out core build-stamp \
250 CC CXX \
251 wvtestmain
252 @rm -f $(patsubst %.t.cc,%.t,$(wildcard *.t.cc) $(wildcard t/*.t.cc)) \
253 t/*.o t/*~ t/.*.d t/.\#*
254 @rm -f valgrind.log.pid*
255 @rm -f semantic.cache tags
256 @rm -rf debian/tmp
258 dist-hook:
260 PKGDIR=$(WVPACKAGE_TARNAME)-$(WVPACKAGE_VERSION)
262 dist: config.mk dist-hook
263 @echo '--> Making dist in ../build/$(PKGDIR)...'
264 @test -d ../build || mkdir ../build
265 @rsync -a --delete --force '$(shell pwd)/' '../build/$(PKGDIR)'
266 cd ../build/$(PKGDIR) && git clean -d -f -x
267 cd ../build/$(PKGDIR) && git log > ChangeLog
268 cd ../build/$(PKGDIR) && ./autogen.sh
269 @find '../build/$(PKGDIR)' -name .git -type d -print0 | xargs -0 rm -rf --
270 @find '../build/$(PKGDIR)' -name .gitignore -type f -print0 | xargs -0 rm -f --
271 @rm -f '../build/$(PKGDIR).tar.gz'
272 @cd ../build; tar -zcf '$(PKGDIR).tar.gz' '$(PKGDIR)'
273 @echo '--> Created tarball in ../build/$(PKGDIR).tar.gz.'