ifndef OBJ_DIR
	OBJ_DIR := obj
endif

CFLAGS := -Wall \
	-g \
	-O \
	-fPIC


DIRS := ./include \
	./swupdate/include \
	./delta \
	./common

SRCS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.c))
LIBS = -liniparser -lmodem_utils -lnv_utils -lrt
$(warning "UNISOC_FSCRYPT=$(UNISOC_FSCRYPT)")
ifeq ($(UNISOC_FSCRYPT), true)
LIBS += -ltrusty -lunisoc-fscrypt
CFLAGS += -DUNISOC_FSCRYPT=1
else
SRCS_TMP := $(SRCS)
SRCS := $(filter-out unisoc_encrypted.c, $(SRCS_TMP))
endif
$(warning "UNISOC_RFNV_HW_COMP=$(UNISOC_RFNV_HW_COMP)")
ifeq ($(UNISOC_RFNV_HW_COMP), true)
CFLAGS += -DUNISOC_RFNV_HW_COMP=1
else
SRCS_TMP := $(SRCS)
SRCS := $(filter-out rfnv_hw_compatibility.c, $(SRCS_TMP))
endif
OBJS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(SRCS)))
TARGET := libswupdate-support.a
TARGET_INC := ./include

all:$(TARGET)

$(TARGET):dir $(OBJS)
	$(AR) rcs $(TARGET) $(OBJS)

$(OBJS):$(SRCS)
	$(foreach var,$^,\
		$(CC) $(CFLAGS) $(LIBS) $(LIBPATH) $(addprefix -I,$(DIRS)) -c $(var) -o $(patsubst %.c,$(OBJ_DIR)/%.o,$(notdir $(var)));\
	)

install:
ifdef INSTALL_LIB_DIR
	install -m 0777 $(TARGET) $(INSTALL_LIB_DIR)
endif
ifdef INSTALL_INC_DIR
	install -m 0777 $(TARGET_INC)/bsdiff_patch.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/rdiff_patch.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/file_verify.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/diff_utils.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/install_local_file.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/ab_support.h $(INSTALL_INC_DIR)
	install -m 0777 $(TARGET_INC)/sys_mtd.h $(INSTALL_INC_DIR)
ifeq ($(UNISOC_FSCRYPT), true)
	install -m 0777 $(TARGET_INC)/unisoc_encrypted.h $(INSTALL_INC_DIR)
endif
ifeq ($(UNISOC_RFNV_HW_COMP), true)
	install -m 0777 $(TARGET_INC)/rfnv_hw_compatibility.h $(INSTALL_INC_DIR)
endif
endif

clean:
	rm -f $(OBJ_DIR)/*.o
	rm -f $(TARGET)
	rm -f *.o

dir:
	mkdir -p $(OBJ_DIR)
