.PHONY: build clean

# 依赖库
RTU_LINUX_MODULES_PATH := $(shell pwd)/../../rtu_linux_modules

# 头文件
INCS += -I.
INCS += -I$(RTU_LINUX_MODULES_PATH)/swapi
INCS += -I./include

# 源文件
SRCS += $(filter-out $(RTU_LINUX_MODULES_PATH)/swapi/testLib.c, $(wildcard $(RTU_LINUX_MODULES_PATH)/swapi/*.c))
SRCS += $(filter-out ./lwjson/example_stream.c, $(wildcard ./lwjson/*.c))
SRCS += $(RTU_LINUX_MODULES_PATH)/swapi/subjects/serial/serial.c mcu_ctrl_board.c

# .o文件
OBJS = $(SRCS:.c=.o)

# 编译器
CC = gcc
CFLAGS = -Wall -fPIC -O2 -g
DEFINS = -D_GNU_SOURCE

# 单元测试时使用用, 开启调试模式, 正式编译时需要注释掉
# DEFINS += -D_DEBUG

target ?= x86_64
ifeq ($(target),armv7hf)
  CC := arm-linux-gnueabihf-gcc
	AR := arm-linux-gnueabihf-ar
endif

# 库文件
LIBS += -Wl,-Bdynamic -lc -lm -ldl -lpthread

# 编译和清理
build : libmcu_ctrl_board.a mcb_test.out

%.o : %.c
	$(CC) $(DEFINS) $(CFLAGS) -c $< $(INCS) -o $@

libmcu_ctrl_board.a : $(OBJS)
	$(AR) -cr $@ $(OBJS)

mcb_test.out : $(OBJS)
	$(CC) $(DEFINS) $(CFLAGS) $(OBJS) mcb_test.c $(INCS) $(LIBS) -o $@

clean :
	rm -rf $(OBJS) *.out *.a config/ log/ status/ var/
