include ./../../linux.mk

# 纠正 ROOT_DIR：release/src/system 只有三级深度，linux.mk 假设四级
ORIG_ROOT := $(ROOT_DIR)
ROOT := $(realpath $(CURDIR)/../../..)
SYS_SRC := $(ROOT)/src/system
PUB_SRC := $(ROOT)/src/public

LIB := $(ROOT)/release/$(BUILD_TYPE)/lib/libsystem.a
LIB_OBJ := $(CURDIR)/libsystem/obj/app_modules.o
LIB_FLAGS := $(C_FLAGS) -I$(SYS_SRC)/inc -I$(PUB_SRC)/liblog/inc -I$(PUB_SRC)/libtask/inc -I$(ROOT)/release/inc

$(LIB): $(LIB_OBJ)
	@mkdir -p $(dir $@)
	$(AR) rcs $@ $^
	@echo "[libsystem] built"

$(LIB_OBJ): $(SYS_SRC)/app_modules.c
	@mkdir -p $(dir $@)
	$(CC) $(LIB_FLAGS) -c $< -o $@

.PHONY: clean-sys
clean-sys:
	rm -rf $(CURDIR)/libsystem/obj $(LIB)

SUBDIRS := ./libmodbus_m ./RTU

.PHONY: all clean rebuild

all: $(LIB)
	@for d in $(SUBDIRS); do [ -f "$$d/makefile" ] && (cd "$$d" && $(MAKE) -f makefile all) || true; done

clean: clean-sys
	@for d in $(SUBDIRS); do [ -f "$$d/makefile" ] && (cd "$$d" && $(MAKE) -f makefile clean) || true; done

rebuild: clean all
