1ifndef COMPONENT
2COMPONENT := fatfs
3endif
4
5COMPONENT_LIB := lib$(COMPONENT).a
6TEST_PROGRAM := test_$(COMPONENT)
7
8STUBS_LIB_DIR := ../../../components/spi_flash/sim/stubs
9STUBS_LIB_BUILD_DIR := $(STUBS_LIB_DIR)/build
10STUBS_LIB := libstubs.a
11
12SPI_FLASH_SIM_DIR := ../../../components/spi_flash/sim
13SPI_FLASH_SIM_BUILD_DIR := $(SPI_FLASH_SIM_DIR)/build
14SPI_FLASH_SIM_LIB := libspi_flash.a
15
16WEAR_LEVELLING_DIR := ../../../components/wear_levelling/test_wl_host
17WEAR_LEVELLING_BUILD_DIR := $(WEAR_LEVELLING_DIR)/build
18WEAR_LEVELLING_LIB := libwl.a
19
20include Makefile.files
21
22all: test
23
24ifndef SDKCONFIG
25SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h))
26SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h
27else
28SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG)))
29endif
30
31INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR) ../../../tools/catch)
32
33CPPFLAGS += $(INCLUDE_FLAGS) -g -m32
34CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32
35
36# Build libraries that this component is dependent on
37$(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB): force
38	$(MAKE) -C $(STUBS_LIB_DIR) lib SDKCONFIG=$(SDKCONFIG)
39
40$(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB): force
41	$(MAKE) -C $(SPI_FLASH_SIM_DIR) lib SDKCONFIG=$(SDKCONFIG)
42
43$(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB): force
44	$(MAKE) -C $(WEAR_LEVELLING_DIR) lib SDKCONFIG=$(SDKCONFIG)
45
46# Create target for building this component as a library
47CFILES := $(filter %.c, $(SOURCE_FILES))
48CPPFILES := $(filter %.cpp, $(SOURCE_FILES))
49
50CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1}))
51CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1}))
52
53ifndef BUILD_DIR
54BUILD_DIR := build
55endif
56
57OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o))))
58
59define COMPILE_C
60$(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG)
61	mkdir -p $(BUILD_DIR)
62	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1}
63endef
64
65define COMPILE_CPP
66$(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG)
67	mkdir -p $(BUILD_DIR)
68	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1}
69endef
70
71$(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG)
72	mkdir -p $(BUILD_DIR)
73	$(AR) rcs $@ $^
74
75lib: $(BUILD_DIR)/$(COMPONENT_LIB)
76
77$(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile))))
78$(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile))))
79
80# Create target for building this component as a test
81TEST_SOURCE_FILES = \
82	test_fatfs.cpp \
83	main.cpp \
84
85TEST_OBJ_FILES = $(filter %.o, $(TEST_SOURCE_FILES:.cpp=.o) $(TEST_SOURCE_FILES:.c=.o))
86
87$(TEST_PROGRAM): lib $(TEST_OBJ_FILES) $(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB) $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB) $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB) partition_table.bin $(SDKCONFIG)
88	g++ $(LDFLAGS) $(CXXFLAGS) -o $@  $(TEST_OBJ_FILES) -L$(BUILD_DIR) -l:$(COMPONENT_LIB) -L$(WEAR_LEVELLING_BUILD_DIR) -l:$(WEAR_LEVELLING_LIB) -L$(SPI_FLASH_SIM_BUILD_DIR) -l:$(SPI_FLASH_SIM_LIB) -L$(STUBS_LIB_BUILD_DIR) -l:$(STUBS_LIB)
89
90test: $(TEST_PROGRAM)
91	./$(TEST_PROGRAM)
92
93# Create other necessary targets
94partition_table.bin: partition_table.csv
95	python ../../../components/partition_table/gen_esp32part.py --verify $< $@
96
97force:
98
99# Create target to cleanup files
100clean:
101	$(MAKE) -C $(STUBS_LIB_DIR) clean
102	$(MAKE) -C $(SPI_FLASH_SIM_DIR) clean
103	$(MAKE) -C $(WEAR_LEVELLING_DIR) clean
104	rm -f $(OBJ_FILES) $(TEST_OBJ_FILES) $(TEST_PROGRAM) $(COMPONENT_LIB) partition_table.bin
105
106.PHONY: all lib test clean force
107