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

ROOT := $(realpath $(CURDIR)/../../../..)

INC_PATH := -I$(ROOT)/release/inc \
            -I$(ROOT)/src/system/inc \
            -I$(ROOT)/src/system/libweb_server/inc \
            -I$(ROOT)/src/public/libdatacenter/inc \
            -I$(ROOT)/src/public/liblog/inc \
            -I$(ROOT)/src/public/libfunc/inc \
            -I$(ROOT)/src/public/libcmd/inc \
            -I$(ROOT)/src/public/libtask/inc \
            -I$(ROOT)/src/public/libmy_xxhash/inc \
            -I$(ROOT)/src/public/libcJSON/inc \
            -I$(ROOT)/src/protocol/libmodbus/inc \
            -I$(ROOT)/src/protocol/libmongoose/inc

LIBS := -L$(ROOT)/release/$(BUILD_TYPE)/lib \
        -Wl,--whole-archive \
        -lweb_server -lmodbus_m -lself_ptl -lcom_router -liec -lplc -licp67 \
        -ldatacenter -lxml -lcmd -ltask \
        -llog -lfunc -lmy_xxhash -lcJSON -lmd5 -lcomm \
        -Wl,--no-whole-archive \
        -lmodbus -l60870 \
        -lmongoose \
        -lpthread -lstdc++

O := $(ROOT)/release/$(BUILD_TYPE)/exe/RTU
S := $(ROOT)/src/system/RTU/src

C_OBJ   := $(CURDIR)/RTU/obj/main.o
CXX_OBJ := $(CURDIR)/RTU/obj/app_sys.o

.PHONY: all clean rebuild
all: $(O)

$(O): $(C_OBJ) $(CXX_OBJ)
	@mkdir -p $(dir $@)
	$(CXX) $^ $(LIBS) -o $@
	@echo "[RTU] built -> $(O)"

$(C_OBJ): $(S)/main.c
	@mkdir -p $(dir $@)
	$(CC) $(C_FLAGS) $(INC_PATH) -c $< -o $@

$(CXX_OBJ): $(S)/app_sys.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXX_FLAGS) $(INC_PATH) -c $< -o $@

clean:
	rm -rf $(CURDIR)/RTU/obj $(O)

rebuild: clean all
