#  SPDX-License-Identifier: BSD-3-Clause
#  Copyright (C) 2017 Intel Corporation
#  All rights reserved.
#

SPDK_ROOT_DIR := $(abspath $(CURDIR)/../spdk)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
include $(SPDK_ROOT_DIR)/mk/spdk.modules.mk

LIBZVFS := libzvfs.so

C_SRCS := \
	common/utils.c \
	spdk_engine/io_engine.c \
	fs/zvfs.c \
	fs/zvfs_inode.c \
	fs/zvfs_path_entry.c \
	fs/zvfs_open_file.c \
	fs/zvfs_sys_init.c \
	proto/ipc_proto.c \
	hook/zvfs_hook_init.c \
	hook/zvfs_hook_fd.c \
	hook/zvfs_hook_rw.c \
	hook/zvfs_hook_seek.c \
	hook/zvfs_hook_stat.c \
	hook/zvfs_hook_sync.c \
	hook/zvfs_hook_fcntl.c \
	hook/zvfs_hook_dir.c \
	hook/zvfs_hook_mmap.c \

# 指定头文件搜索路径
CFLAGS += -I$(abspath $(CURDIR)) -fPIC

# SPDK 库依赖
SPDK_LIB_LIST = $(ALL_MODULES_LIST) event event_bdev

# 链接选项
LDFLAGS += -shared -Wl,-soname,$(LIBZVFS) -Wl,-z,nodelete \
	-Wl,--disable-new-dtags \
	-Wl,-rpath,$(SPDK_ROOT_DIR)/build/lib \
	-Wl,-rpath,$(SPDK_ROOT_DIR)/dpdk/build/lib

# 系统库
SYS_LIBS += -ldl

# 获取 SPDK 库的链接参数
SPDK_LIBS = $(call spdk_lib_list_to_linker_args,$(SPDK_LIB_LIST))

DEPS = $(OBJS:.o=.d)

all: $(LIBZVFS)
	$(MAKE) -C daemon

# 构建目标文件
$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# 构建共享库
$(LIBZVFS): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(SPDK_LIBS) $(SYS_LIBS)

clean:
	$(CLEAN_C) $(LIBZVFS)
	rm -f $(DEPS) $(OBJS) $(LIBZVFS)
	$(MAKE) -C daemon clean

include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk
