1# -*- Mode: makefile -*-
2TOPDIR := $(shell pwd)
3PREFIX := $(TOPDIR)/install
4
5COMPONENTS := bucket_engine \
6	ep-engine \
7	libconflate \
8	libvbucket \
9	libmemcached \
10	membase-cli \
11	memcached \
12	memcachetest \
13	moxi \
14	ns_server \
15	vbucketmigrator
16
17ifneq "$(DESTDIR)" ""
18LDFLAGS := -L$(DESTDIR)$(PREFIX)/lib $(LDFLAGS)
19CPPFLAGS := -I$(DESTDIR)$(PREFIX)/include $(CPPFLAGS)
20export LDFLAGS CPPFLAGS
21endif
22
23ifneq "$(COUCHBASE_DEBUG_BUILD)" ""
24WITH_DEBUG_FLAG=--with-debug
25endif
26
27ifndef DISABLE_COUCH
28
29ifneq "$(realpath couchdb/configure.ac)" ""
30BUILD_COUCH := 1
31COMPONENTS += couchdb
32endif
33
34endif
35
36ifneq "$(realpath portsigar/configure.ac)" ""
37COMPONENTS += sigar portsigar
38BUILD_SIGAR := 1
39endif
40
41ifdef FOR_WINDOWS
42COMPONENTS := $(filter-out memcachetest vbucketmigrator, $(COMPONENTS))
43endif
44
45BUILD_COMPONENTS := $(filter-out ns_server, $(COMPONENTS))
46
47MAKE_INSTALL_TARGETS := $(patsubst %, make-install-%, $(BUILD_COMPONENTS))
48MAKE_INSTALL_TARGETS_EX :=
49
50ifdef BUILD_COUCH
51ifneq "$(realpath geocouch/Makefile)" ""
52MAKE_INSTALL_TARGETS_EX += make-install-geocouch
53endif
54ifneq "$(realpath mccouch/Makefile)" ""
55MAKE_INSTALL_TARGETS_EX += make-install-mccouch
56endif
57endif
58
59TEST_COMPONENTS := $(filter-out libconflate membase-cli memcachetest moxi, $(BUILD_COMPONENTS))
60MAKE_TEST_TARGETS := $(patsubst %, make-test-%, $(TEST_COMPONENTS))
61MAKE_TEST_TARGETS_EX :=
62
63MAKEFILE_TARGETS := $(patsubst %, %/Makefile, $(BUILD_COMPONENTS))
64
65OPTIONS := --prefix=$(PREFIX)
66AUTOGEN := ./config/autorun.sh
67
68ifdef PREFER_STATIC
69LIBRARY_OPTIONS := --enable-static --disable-shared
70else
71NUKE_LA_FILES ?= false
72LIBRARY_OPTIONS := --disable-static --enable-shared
73endif
74
75all: do-install-all
76
77DIST_VERSION = `git describe`
78DIST_MANIFEST = manifest.xml
79
80dist:
81	for i in $(COMPONENTS); do (cd $$i && rm -f *.tar.gz && make dist || true); done
82	mkdir -p tmp/membase-server_src
83	rm -rf tmp/membase-server_src/*
84	(for i in $(COMPONENTS); do \
85         mkdir -p tmp/membase-server_src/$$i; \
86         (cd tmp/membase-server_src/$$i && \
87          tar --strip-components 1 -xzf ../../../$$i/$$i-*.tar.gz || \
88          tar --strip-components 1 -xzf ../../../$$i/*.tar.gz || true); \
89         done)
90	cp Makefile tmp/membase-server_src
91	if [ -f $(DIST_MANIFEST) ]; then cp $(DIST_MANIFEST) tmp/membase-server_src; fi
92	tar -C tmp -czf membase-server_src-$(DIST_VERSION).tar.gz membase-server_src
93
94test: $(MAKE_TEST_TARGETS) $(MAKE_TEST_TARGETS_EX)
95
96ifdef BUILD_SIGAR
97deps-for-portsigar: make-install-sigar
98portsigar/Makefile: AUTOGEN := ./bootstrap
99portsigar/Makefile: CONFIGURE_PREFIX := LDFLAGS="-L$(PREFIX)/lib $(LDFLAGS)"
100sigar/Makefile: AUTOGEN := ./autogen.sh
101portsigar_EXTRA_MAKE_OPTIONS := 'CPPFLAGS=-I$(TOPDIR)/sigar/include $(CPPFLAGS)'
102sigar_OPTIONS := $(LIBRARY_OPTIONS) $(sigar_OPTIONS)
103endif
104
105do-install-all: $(MAKE_INSTALL_TARGETS) make-install-ns_server $(MAKE_INSTALL_TARGETS_EX)
106
107-clean-common:
108	rm -rf install tmp
109	rm -f moxi*log
110	rm -f memcached*log
111
112clean: -clean-common
113	for i in $(COMPONENTS); do (cd $$i && make clean || true); done
114
115distclean: -clean-common
116	for i in $(COMPONENTS); do (cd $$i && make distclean || true); done
117	rm -rf install tmp
118	rm moxi*log
119	rm memcached*log
120
121clean-xfd: $(patsubst %, do-clean-xfd-%, $(COMPONENTS)) -clean-common
122	(cd icu4c && git clean -Xfd) || true
123	(cd spidermonkey && git clean -Xfd) || true
124
125clean-xfd-hard: $(patsubst %, do-hard-clean-xfd-%, $(COMPONENTS)) -clean-common
126	(cd icu4c && git clean -xfd) || true
127	(cd spidermonkey && git clean -xfd) || true
128
129do-clean-xfd-%:
130	(cd $* && git clean -Xfd)
131
132do-hard-clean-xfd-%:
133	(cd $* && git clean -xfd)
134
135CONFIGURE_TARGETS := $(patsubst %, %/configure, $(BUILD_COMPONENTS))
136
137ifdef AUTO_RECONFIG
138
139define define-configure-target-deps
140$(1)/configure: $(1)/.git/HEAD $(1)/.git/$(shell git --git-dir=$(1)/.git symbolic-ref -q HEAD || echo HEAD)
141endef
142# $(foreach comp, $(BUILD_COMPONENTS),$(eval $(info $(call define-configure-target-deps,$(comp)))))
143# $(error stop)
144$(foreach comp, $(BUILD_COMPONENTS),$(eval $(call define-configure-target-deps,$(comp))))
145
146endif
147
148$(CONFIGURE_TARGETS):
149	cd $(dir $@) && $(AUTOGEN_PREFIX) $(AUTOGEN)
150
151$(MAKEFILE_TARGETS): %/Makefile: | %/configure deps-for-%
152	cd $* && $(CONFIGURE_PREFIX) ./configure $(OPTIONS) $($*_OPTIONS) $($*_EXTRA_OPTIONS)
153
154$(MAKE_INSTALL_TARGETS): make-install-%: %/Makefile deps-for-%
155	(rm -rf tmp/$*; mkdir -p tmp/$*)
156	$(MAKE) -C $* install $($*_EXTRA_MAKE_OPTIONS)
157	if [ "x$(NUKE_LA_FILES)" = "xtrue" ]; then $(RM) -f $(DESTDIR)$(PREFIX)/lib/*.la; fi
158
159$(MAKE_TEST_TARGETS): make-test-%: make-install-%
160	$(MAKE) -C $* test
161
162$(patsubst %, deps-for-%, $(BUILD_COMPONENTS)):
163
164libmemcached_OPTIONS := $(LIBRARY_OPTIONS) --disable-dtrace --without-docs
165ifndef CROSS_COMPILING
166libmemcached_OPTIONS += --with-memcached=$(DESTDIR)$(PREFIX)/bin/memcached
167memcachetest_OPTIONS += --with-memcached=$(DESTDIR)$(PREFIX)/bin/memcached
168deps-for-libmemcached: make-install-memcached
169endif
170
171ifdef USE_TCMALLOC
172libmemcached_OPTIONS += --enable-tcmalloc
173endif
174
175# tar.gz _should_ have ./configure inside, but it doesn't
176# make-install-libmemcached: AUTOGEN := true
177
178libvbucket_OPTIONS :=  $(LIBRARY_OPTIONS) --without-docs $(WITH_DEBUG_FLAG)
179libvbucket_EXTRA_MAKE_OPTIONS := 'CPPFLAGS=-I$(TOPDIR)/libmemcached $(CPPFLAGS)'
180deps-for-libvbucket: make-install-libmemcached
181
182memcachetest_EXTRA_MAKE_OPTIONS := 'CPPFLAGS=-I$(TOPDIR)/memcached/include -I$(TOPDIR)/libmemcached -I$(TOPDIR)/libvbucket/include $(CPPFLAGS)'
183deps-for-memcachetest: make-install-memcached make-install-libmemcached make-install-libvbucket
184
185ep-engine_OPTIONS := --with-memcached=../memcached $(WITH_DEBUG_FLAG)
186deps-for-ep-engine: make-install-memcached
187
188bucket_engine_OPTIONS := --with-memcached=../memcached $(WITH_DEBUG_FLAG)
189bucket-engine_EXTRA_MAKE_OPTIONS := 'CPPFLAGS=-I$(TOPDIR)/memcached/include $(CPPFLAGS)'
190deps-for-bucket_engine: make-install-memcached
191
192moxi_OPTIONS := --enable-moxi-libvbucket \
193	--enable-moxi-libmemcached \
194	--without-check
195moxi_EXTRA_MAKE_OPTIONS := 'CPPFLAGS=-I$(TOPDIR)/libmemcached -I$(TOPDIR)/libvbucket/include -I$(PREFIX)/include $(CPPFLAGS)'
196ifndef CROSS_COMPILING
197moxi_OPTIONS += --with-memcached=$(DESTDIR)$(PREFIX)/bin/memcached
198endif
199deps-for-moxi: make-install-libconflate make-install-libvbucket make-install-libmemcached make-install-memcached
200
201libconflate_OPTIONS := $(LIBRARY_OPTIONS) --without-check $(WITH_DEBUG_FLAG)
202
203vbucketmigrator_OPTIONS := --without-sasl --with-isasl
204
205memcached_OPTIONS := --enable-isasl
206
207make-install-ns_server:
208	cd ns_server && ./configure "--prefix=$(PREFIX)"
209	$(MAKE) -C ns_server install "PREFIX=$(PREFIX)"
210
211make-install-geocouch:
212	$(MAKE) -C geocouch COUCH_SRC=../couchdb/src/couchdb
213	mkdir -p $(DESTDIR)$(PREFIX)/lib/couchdb/plugins/geocouch/ebin
214	cp -r geocouch/build/* $(DESTDIR)$(PREFIX)/lib/couchdb/plugins/geocouch/ebin
215	mkdir -p $(DESTDIR)$(PREFIX)/etc/couchdb/local.d
216	cp -r geocouch/etc/couchdb/local.d/* $(DESTDIR)$(PREFIX)/etc/couchdb/local.d
217	mkdir -p $(DESTDIR)$(PREFIX)/share/couchdb/www/script/test
218	cp -r geocouch/share/www/script/test/* $(DESTDIR)$(PREFIX)/share/couchdb/www/script/test
219
220make-install-mccouch:
221	$(MAKE) -C mccouch
222	mkdir -p $(DESTDIR)$(PREFIX)/lib/couchdb/plugins/mccouch/ebin
223	cp -r mccouch/ebin/* $(DESTDIR)$(PREFIX)/lib/couchdb/plugins/mccouch/ebin
224	mkdir -p $(DESTDIR)$(PREFIX)/etc/couchdb/local.d
225	cp -r mccouch/etc/couchdb/local.d/* $(DESTDIR)$(PREFIX)/etc/couchdb/local.d
226
227ifdef PLEASE_BUILD_COUCH_DEPS
228couchdb_OPTIONS := --with-js-lib=$(PREFIX)/lib --with-js-include=$(PREFIX)/include "PATH=$(PREFIX)/bin:$(PATH)"
229
230# it's necessary to pass this late. couchdb is using libtool and
231# libtool portably understands -rpath (NOTE: _single_ dash). Passing
232# it to configure fails, because a bunch of stuff is checked with
233# plain gcc versus with libtool wrapper.
234# NOTE: this doesn't work on Darwin and has issues on Solaris
235couchdb_EXTRA_MAKE_OPTIONS := "LDFLAGS=-R $(PREFIX)/lib $(LDFLAGS)"
236endif
237
238ifdef BUILD_COUCH
239couchdb/Makefile: AUTOGEN = ./bootstrap
240endif
241
242ifdef PLEASE_BUILD_COUCH_DEPS
243deps-for-couchdb: make-install-couchdb-deps
244endif
245
246WRAPPERS := $(patsubst %, $(PREFIX)/bin/%, memcached-wrapper moxi-wrapper)
247
248$(WRAPPERS): $(PREFIX)/bin/%: tlm/%.in
249	mkdir -p $(PREFIX)/bin
250	sed -e 's|@PREFIX@|$(PREFIX)|g' <$< >$@ || (rm $@ && false)
251	chmod +x $@
252
253WIN32_MAKE_TARGET := do-install-all
254WIN32_HOST := i586-mingw32msvc
255
256win32-cross:
257	$(MAKE) $(WIN32_MAKE_TARGET) FOR_WINDOWS=1 HOST=$(WIN32_HOST) CROSS_COMPILING=1
258
259ifdef FOR_WINDOWS
260
261WIN_FLAGS := 'LOCAL=$(PREFIX)'
262
263ifndef LIBS_PREFIX
264$(warning LIBS_PREFIX usually needs to be given so that I can find libcurl, libevent and libpthread)
265else
266
267OPTIONS += 'CFLAGS=-I$(LIBS_PREFIX)/include $(CFLAGS)' 'LDFLAGS=-L$(LIBS_PREFIX)/lib $(LDFLAGS)'
268LOCALINC := -I${PREFIX}/include
269LOCALINC += -I$(LIBS_PREFIX)/include
270ifdef NO_USECONDS_T
271LOCALINC += -Duseconds_t=unsigned
272endif
273
274WIN_FLAGS += 'LOCALINC=$(LOCALINC)' 'LIB=-L$(LIBS_PREFIX)/lib $(LIB)'
275
276endif
277
278ifdef HOST
279OPTIONS := --host=$(HOST) $(OPTIONS)
280WIN_FLAGS += CC=$(HOST)-gcc CXX=$(HOST)-g++
281endif
282
283libmemcached_OPTIONS += --without-memcached
284moxi_OPTIONS += --without-memcached
285
286memcached/Makefile:
287	touch $@
288
289ep-engine/Makefile:
290	touch $@
291
292bucket_engine/Makefile:
293	touch $@
294
295memcached/configure ep-engine/configure bucket_engine/configure:
296	@true
297
298membase-cli/Makefile:
299	touch $@
300
301make-install-membase-cli:
302	(cd membase-cli && mkdir -p $(PREFIX)/bin/simplejson && \
303	cp membase *.py LICENSE $(PREFIX)/bin && \
304	cp simplejson/LICENSE.txt simplejson/*.py $(PREFIX)/bin/simplejson)
305
306make-install-memcached:
307	(cd memcached && $(MAKE) -f win32/Makefile.mingw $(WIN_FLAGS) all \
308         && mkdir -p $(PREFIX)/lib/memcached \
309         && cp .libs/*.so $(PREFIX)/lib/memcached \
310         && cp memcached.exe mcstat.exe $(PREFIX)/bin)
311
312# hey, it's almost like Lisp
313EP_ENGINE_MARCH := $(strip $(if $(or $(findstring x86_64, $(HOST)), $(findstring amd64, $(HOST))), ,-march=i686))
314
315make-install-ep-engine:
316	chmod +x ep-engine/win32/config.sh
317	(cd ep-engine && $(MAKE) -f win32/Makefile.mingw "MARCH=$(EP_ENGINE_MARCH)" $(WIN_FLAGS) install)
318
319make-install-bucket_engine:
320	(cd bucket_engine && $(MAKE) -f win32/Makefile.mingw $(WIN_FLAGS) all \
321	 && cp .libs/bucket_engine.so "$(PREFIX)/lib/memcached")
322
323libmemcached/Makefile: fix-broken-libmemcached-tests
324
325fix-broken-libmemcached-tests:
326	patch -p1 -N -r /dev/null -t -d libmemcached <tlm/libmemcached-win32-fix.diff  || true
327
328endif
329
330AUTOCONF213 := autoconf213
331
332spidermonkey/configure:
333	(cd spidermonkey && $(AUTOCONF213))
334
335spidermonkey/Makefile: spidermonkey/configure
336	(cd spidermonkey && ./configure "--prefix=$(PREFIX)" --without-x)
337
338make-install-spidermonkey: spidermonkey/Makefile
339	$(MAKE) -C spidermonkey
340	$(MAKE) -C spidermonkey install
341
342icu4c/source/Makefile:
343	(cd icu4c/source && ./configure "--prefix=$(PREFIX)")
344
345make-install-icu4c: icu4c/source/Makefile
346	$(MAKE) -C icu4c/source install
347
348make-install-couchdb-deps: make-install-spidermonkey make-install-icu4c
349
350CHECK_COMPONENTS ?= $(COMPONENTS)
351
352CHECK_TARGETS := $(patsubst %, check-%, $(CHECK_COMPONENTS))
353
354MAKE_CHECK_TARGET := check
355
356check-fast: check-ns_server check-bucket_engine check-ep-engine check-ns_server check-libvbucket check-couchdb
357
358check: $(CHECK_TARGETS)
359
360check-memcached check-moxi check-bucket_engine check-ns_server: MAKE_CHECK_TARGET := test
361
362$(CHECK_TARGETS): check-%: make-install-%
363	$(MAKE) -C $* $(MAKE_CHECK_TARGET)
364
365replace-wrappers: $(WRAPPERS) all
366	mv $(PREFIX)/bin/memcached $(PREFIX)/bin/memcached.orig
367	mv $(PREFIX)/bin/moxi $(PREFIX)/bin/moxi.orig
368	sed -e 's|/bin/memcached|/bin/memcached.orig|g' <$(PREFIX)/bin/memcached-wrapper >$(PREFIX)/bin/memcached
369	sed -e 's|/bin/moxi|/bin/moxi.orig|g' <$(PREFIX)/bin/moxi-wrapper >$(PREFIX)/bin/moxi
370	chmod +x $(PREFIX)/bin/memcached $(PREFIX)/bin/moxi
371
372# Allow the user to override stuff for all projects (like
373# --with-erlang=)
374ifneq "$(realpath $(HOME)/.couchbase/build/Makefile.extra)" ""
375include $(HOME)/.couchbase/build/Makefile.extra
376endif
377
378# this thing can override settings and add components
379ifneq "$(realpath .repo/Makefile.extra)" ""
380include .repo/Makefile.extra
381endif
382