include ../../buildconf.mk

VDEVADM_SRCS := vdevadm.c vdevadm-ctx.c vdevadm-device.c vdevadm-info.c \
	vdevadm-trigger.c vdevadm-settle.c vdevadm-control.c vdevadm-monitor.c

VDEVADM_UDEV_CFLAGS := $(shell pkg-config --cflags libudev 2>/dev/null)
VDEVADM_UDEV_LIBS := $(shell pkg-config --libs libudev 2>/dev/null)
ifeq ($(VDEVADM_UDEV_LIBS),)
VDEVADM_UDEV_LIBS := -ludev
endif

VDEVADM_OBJS := $(patsubst %.c,$(BUILD_USRBIN)/vdevadm-objs/%.o,$(VDEVADM_SRCS))
VDEVADM_BIN := $(BUILD_USRBIN)/vdevadm
VDEVADM_INSTALL := $(DESTDIR)$(BINDIR)/vdevadm

HELPERS_DIR := $(ROOT_DIR)/vdevd/helpers/LINUX
INC := -I$(ROOT_DIR) -I$(HELPERS_DIR) -I$(BUILD_LIBVDEV_HEADERS) $(VDEVADM_UDEV_CFLAGS)
LIBVDEV := $(BUILD_LIBVDEV)/libvdev.a

.PHONY: all install uninstall clean

all: $(VDEVADM_BIN)

$(BUILD_USRBIN)/vdevadm-objs/%.o: %.c vdevadm.h
	@mkdir -p "$(dir $@)"
	$(CC) $(CFLAGS) $(DEFS) $(INC) -o "$@" -c "$<"

$(VDEVADM_BIN): $(VDEVADM_OBJS) $(LIBVDEV)
	@mkdir -p "$(dir $@)"
	$(CC) -o $@ $(VDEVADM_OBJS) $(LIBVDEV) $(LDFLAGS) $(VDEVADM_UDEV_LIBS) -lpthread -lrt

install: $(VDEVADM_BIN)
	@mkdir -p "$(DESTDIR)$(BINDIR)"
	install -m 0755 $(VDEVADM_BIN) $(VDEVADM_INSTALL)

uninstall:
	rm -f $(VDEVADM_INSTALL)

clean:
	rm -rf $(BUILD_USRBIN)/vdevadm-objs $(VDEVADM_BIN)

$(LIBVDEV):
	$(MAKE) -C $(LIBVDEV_ROOT) PREFIX=
