# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD+Patents license found in the
# LICENSE file in the root directory of this source tree.

-include makefile.inc

SRC=$(wildcard *.cpp)
OBJ=$(SRC:.cpp=.o)


############################
# Building

default: libfaiss.a

all: libfaiss.a libfaiss.$(SHAREDEXT)

libfaiss.a: $(OBJ)
	ar r $@ $^

libfaiss.$(SHAREDEXT): $(OBJ)
	$(CXX) $(SHAREDFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

%.o: %.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(CPUFLAGS) -c $< -o $@

clean:
	rm -f libfaiss.*
	rm -f $(OBJ)


############################
# Installing

install: libfaiss.a libfaiss.$(SHAREDEXT) installdirs
	cp libfaiss.a libfaiss.$(SHAREDEXT) $(DESTDIR)$(libdir)
	cp *.h $(DESTDIR)$(includedir)/faiss/

installdirs:
	$(MKDIR_P) $(DESTDIR)$(libdir) $(DESTDIR)$(includedir)/faiss

uninstall:
	rm $(DESTDIR)$(libdir)/libfaiss.a
	rm $(DESTDIR)$(libdir)/libfaiss.$(SHAREDEXT)
	rm -rf $(DESTDIR)$(includedir)/faiss


#############################
# Dependencies

-include depend

# The above makefile.dep is generated by the following target:
depend:
	for i in $(SRC); do \
		$(CXXCPP) $(CPPFLAGS) -MM $$i; \
	done > depend


#############################
# Tests

test: libfaiss.a py
	make -C tests run
	PYTHONPATH=./python/build/`ls python/build | grep lib` \
	$(PYTHON) -m unittest discover tests/ -v


#############################
# Demos

demos: libfaiss.a
	make -C demos


#############################
# Misc

misc/test_blas: misc/test_blas.cpp
	$(CXX) $(CXXFLAG) $(LDFLAGS) -o $@ $^ $(LIBS)


#############################
# Python

py:
	$(MAKE) -C python build


.PHONY: all clean default demos install installdirs py test uninstall
