# a minimal build for static linking

SRC_DIR=thirdparty/src

.PHONY: all clean-src glog leveldb kyotocabinet marisa opencc yaml-cpp gtest

all: glog leveldb kyotocabinet marisa opencc yaml-cpp gtest

# note: this won't clean output files under include/, lib/ and bin/.
clean-src:
	cd $(SRC_DIR)/glog; make distclean || true
	rm -r $(SRC_DIR)/gtest/build || true
	cd $(SRC_DIR)/leveldb; make clean || true
	cd $(SRC_DIR)/kyotocabinet; make distclean || true
	cd $(SRC_DIR)/marisa-trie; make distclean || true
	rm -r $(SRC_DIR)/opencc/build || true
	rm -r $(SRC_DIR)/yaml-cpp/build || true

glog:
	cd $(SRC_DIR)/glog; ./configure --disable-dependency-tracking --enable-static --disable-shared && make
	cp -R $(SRC_DIR)/glog/src/glog thirdparty/include/
	cp $(SRC_DIR)/glog/.libs/libglog.a thirdparty/lib/

leveldb:
	cd $(SRC_DIR)/leveldb; make
	cp -R $(SRC_DIR)/leveldb/include/leveldb thirdparty/include/
	cp $(SRC_DIR)/leveldb/libleveldb.a thirdparty/lib/

kyotocabinet:
	cd $(SRC_DIR)/kyotocabinet; chmod +x configure; ./configure --disable-debug && make
	cp $(SRC_DIR)/kyotocabinet/kc*.h thirdparty/include/
	cp $(SRC_DIR)/kyotocabinet/libkyotocabinet.a thirdparty/lib/

marisa:
	cd $(SRC_DIR)/marisa-trie; ./configure --disable-debug --disable-dependency-tracking --enable-static && make
	cp -R $(SRC_DIR)/marisa-trie/lib/marisa thirdparty/include/
	cp $(SRC_DIR)/marisa-trie/lib/marisa.h thirdparty/include/
	cp $(SRC_DIR)/marisa-trie/lib/.libs/libmarisa.a thirdparty/lib/

opencc:
	cd $(SRC_DIR)/opencc; mkdir -p build && \
	cd build && cmake \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DBUILD_SHARED_LIBS:BOOL=OFF \
	.. && \
	make
	mkdir -p thirdparty/include/opencc/ && \
	cp $(SRC_DIR)/opencc/src/*.h* thirdparty/include/opencc/
	cp $(SRC_DIR)/opencc/build/src/libopencc.a thirdparty/lib/

yaml-cpp:
	cd $(SRC_DIR)/yaml-cpp; mkdir -p build && \
	cd build && cmake \
	-DCMAKE_BUILD_TYPE=Release \
	.. && \
	make
	cp -R $(SRC_DIR)/yaml-cpp/include/yaml-cpp thirdparty/include/
	cp $(SRC_DIR)/yaml-cpp/build/libyaml-cpp.a thirdparty/lib/

gtest:
	cd $(SRC_DIR)/gtest; mkdir -p build && \
	cd build && cmake .. && \
	make
	cp -R $(SRC_DIR)/gtest/include/gtest thirdparty/include/
	cp $(SRC_DIR)/gtest/build/libgtest*.a thirdparty/lib/
