#ȡöĿ¼
TOPDIR = .
# NOTE: kept relative on purpose - an absolute pwd breaks make when the path
# contains spaces (e.g. '/Volumes/VMware Shared Folders/...' on the mac).

EXECS =  TPSIndex_test

.PHONY : everything deps objs clean veryclean rebuild

everything: $(EXECS)

LIBS := -lpthread

# ---- ƽ̨Զж cmakebuild/Makefile ͬmake PLATFORM=xxx ǿƸ ----
UNAME_S := $(shell uname -s)
UNAME_M := $(shell uname -m)
ifeq ($(origin PLATFORM),undefined)
  ifeq ($(UNAME_S),Darwin)
    PLATFORM := X86_mac
  else ifeq ($(UNAME_M),aarch64)
    PLATFORM := FT_aarch64
  else
    PLATFORM := X86_linux
  endif
endif

# ---- ƽ̨·cmakebuild/bin/<ƽ̨>/libFastUdx.aºԶӣ ----
UDX_LIB := $(TOPDIR)/../cmakebuild/bin/$(PLATFORM)/libFastUdx.a

# macOS  librtrt  libSystemLinux  -lrt
ifeq ($(UNAME_S),Darwin)
  RT_LIB :=
else
  RT_LIB := -lrt
endif

CC  = gcc
CXX = g++
LD  = g++

###define archive option
ARCHIVE  = ar


CFLAGS := -Wall -O3

INCDIR = $(TOPDIR)
CFLAGS += -I$(INCDIR) -I$(TOPDIR)/include -I$(TOPDIR)/src
CFLAGS += -L$(INCDIR) -L$(TOPDIR)/lib

CXXFLAGS := $(CFLAGS)
LDFLAGS += -MD -DLINUX -DUSE_LIB

#
RM-F := rm -f

	 
TPSIndextest_HEADER := $(wildcard $(TOPDIR)/*.h) \
		    $(wildcard $(TOPDIR)/src/*.h)

TPSIndextest_SOURCE := $(wildcard $(TOPDIR)/*.cpp)


OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(TPSIndex_SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) \
$(patsubst %.d,%.cpp,$(MISSING_DEPS)))


TestObj := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(TPSIndextest_SOURCE))) 
TestDEPS := $(patsubst %.o,%.d,$(TestObj))

deps : $(DEPS) $(TestDEPS)
objs : $(OBJS) $(TestObj)


clean :
	@$(RM-F) *.o $(OBJS) $(TestObj)
	@$(RM-F) *.d $(DEPS) $(TestDEPS)

veryclean: clean
	@$(RM-F) $(EXECS)

rebuild: veryclean everything

ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
	@$(RM-F) $(patsubst %.d,%.o,$@)
endif

-include $(DEPS) $(TestDEPS)


# ƽ̨ȱʧʱȷʾǻɬӱ
$(UDX_LIB):
	@echo "[FAIL] missing: $@"
	@echo "       build first: cd ../cmakebuild && bash build_makefile.sh $(PLATFORM)"
	@exit 1

TPSIndex_test : $(OBJS) $(TestObj) $(UDX_LIB)
	$(LD) -o fudx -I$(INCDIR) $(TestObj) $(OBJS) $(UDX_LIB) -lpthread $(RT_LIB)


