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