1COMPONENTS_DIR=../..
2COMPILER_ICLUDE_DIR=$(shell echo `which xtensa-esp32-elf-gcc | xargs dirname | xargs dirname`/xtensa-esp32-elf)
3CFLAGS=-std=gnu99 -Og -ggdb -ffunction-sections -fdata-sections -nostdlib -Wall  -Werror=all -Wno-int-to-pointer-cast -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-macro-redefined -Wno-constant-conversion -Wno-incompatible-pointer-types-discards-qualifiers -Wno-typedef-redefinition -Wno-incompatible-pointer-types  -Wextra \
4-Wno-unused-parameter -Wno-sign-compare -Wno-address   -Wno-unused-variable -DESP_PLATFORM -D IDF_VER=\"v3.1\" -MMD -MP -DWITH_POSIX -DLWIP_NO_CTYPE_H=1
5INC_DIRS=-I . -I ./build/config -I $(COMPONENTS_DIR)/newlib/platform_include -I $(COMPONENTS_DIR)/newlib/include -I $(COMPONENTS_DIR)/driver/include -I $(COMPONENTS_DIR)/esp32/include -I $(COMPONENTS_DIR)/ethernet/include -I $(COMPONENTS_DIR)/freertos/include/esp_additions -I $(COMPONENTS_DIR)/freertos/include/esp_additions/freertos -I $(COMPONENTS_DIR)/freertos/include -I $(COMPONENTS_DIR)/heap/include -I $(COMPONENTS_DIR)/lwip/lwip/src/include  -I $(COMPONENTS_DIR)/lwip/include/apps -I $(COMPONENTS_DIR)/lwip/lwip/src/include/netif -I $(COMPONENTS_DIR)/lwip/lwip/src/include/posix -I $(COMPONENTS_DIR)/lwip/port/esp32/include -I $(COMPONENTS_DIR)/lwip/lwip/src/include/posix -I $(COMPONENTS_DIR)/lwip/include/apps/ping -I $(COMPONENTS_DIR)/lwip/include/apps/sntp  -I $(COMPONENTS_DIR)/soc/esp32/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/tcpip_adapter/include -I $(COMPONENTS_DIR)/esp_rom/include  -I $(COMPONENTS_DIR)/esp_common/include -I $(COMPONENTS_DIR)/esp_hw_support/include -I $(COMPONENTS_DIR)/xtensa/include -I $(COMPONENTS_DIR)/xtensa/esp32/include -I $(COMPONENTS_DIR)/esp_wifi/include -I $(COMPONENTS_DIR)/esp_event/include -I $(COMPONENTS_DIR)/freertos/port/xtensa/include  -I $(COMPONENTS_DIR)/esp_system/include -I $(COMPONENTS_DIR)/esp_timer/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/soc/include -I $(COMPONENTS_DIR)/soc/src/esp32/include -I $(COMPONENTS_DIR)/soc/esp32/include -I $(COMPONENTS_DIR)/esp_netif/include -I $(COMPONENTS_DIR)/esp_eth/include -I $(COMPONENTS_DIR)/esp_netif/lwip -I $(COMPONENTS_DIR)/hal/include -I $(COMPONENTS_DIR)/hal/esp32/include -I $(COMPILER_ICLUDE_DIR)/include
6TEST_NAME=test
7FUZZ=afl-fuzz
8GEN_CFG=generate_config
9LD=$(CC)
10ifeq ($(MODE),dhcp_client)
11    DEPENDENCY_INJECTION=-include dhcp_di.h
12    OBJECTS=dhcp.o def.o network_mock.o test_dhcp_client.o
13    SAMPLE_PACKETS=in_dhcp_client
14else ifeq ($(MODE),dhcp_server)
15    DEPENDENCY_INJECTION=-include dhcpserver_di.h
16    OBJECTS=dhcpserver.o def.o test_dhcp_server.o network_mock.o esp_netif_loopback_mock.o
17    SAMPLE_PACKETS=in_dhcp_server
18else ifeq ($(MODE),dns)
19    CFLAGS+=-DNOT_MOCK_DNS
20    DEPENDENCY_INJECTION=-include dns_di.h
21    OBJECTS=dns.o def.o test_dns.o network_mock.o
22    SAMPLE_PACKETS=in_dns
23else
24    $(error Please specify MODE: dhcp_server, dhcp_client, dns)
25endif
26
27ifeq ($(INSTR),off)
28    CC=gcc
29    CFLAGS+=-DINSTR_IS_OFF
30    TEST_NAME=test_sim
31else
32    CC=afl-clang-fast
33endif
34
35CFLAGS+=$(INC_DIRS)
36
37all: $(TEST_NAME)
38
39def.o: ../lwip/src/core/def.c $(GEN_CFG)
40	@echo "[CC] $<"
41	@$(CC) $(CFLAGS) -D BUILDING_DEF $(DEPENDENCY_INJECTION) -c $< -o $@
42
43dns.o: ../lwip/src/core/dns.c $(GEN_CFG)
44	@echo "[CC] $<"
45	@$(CC) $(CFLAGS) $(DEPENDENCY_INJECTION) -c $< -o $@
46
47dhcp.o: ../lwip/src/core/ipv4/dhcp.c $(GEN_CFG)
48	@echo "[CC] $<"
49	@$(CC) $(CFLAGS) $(DEPENDENCY_INJECTION) -c $< -o $@
50
51dhcpserver.o: ../apps/dhcpserver/dhcpserver.c $(GEN_CFG)
52	@echo "[CC] $<"
53	@$(CC) $(CFLAGS) $(DEPENDENCY_INJECTION) -c $< -o $@
54
55%.o: %.c $(GEN_CFG)
56	@echo "[CC] $<"
57	@$(CC) $(CFLAGS) -c $< -o $@
58
59.PHONY: $(GEN_CFG)
60$(GEN_CFG):
61	$(IDF_PATH)/tools/idf.py reconfigure
62
63$(TEST_NAME): $(OBJECTS)
64	@echo "[LD] $@"
65	@$(LD)  $(OBJECTS) -o $@ $(LDLIBS)
66
67fuzz: $(TEST_NAME)
68	@$(FUZZ) -t 5000+ -i "$(SAMPLE_PACKETS)" -o "out" -- ./$(TEST_NAME)
69