
ROOT	= .
include makefiles/Makefile.defs

####################################
#### list of functions going to IRAM
# update: This was not working from the beginning:
#         everything has been in flash and working not so bad
#         so commenting here, leaving for possibly future
#         update.
#         In that case "*(.move2iram.*)" will be needed in
#         eagle.app.v6.common.ld's "dram0_0_seg :dram0_0_phdr"
#
# glue-esp
#IRAM	= glue2esp_err esp2glue_err pbuf_wrapper_get pbuf_wrapper_release glue2esp_linkoutput ethernet_input pbuf_alloc pbuf_free pbuf_ref
# glue-lwip2
#IRAM	+= glue2git_err git2glue_err new_linkoutput esp2glue_pbuf_freed esp2glue_alloc_for_recv esp2glue_ethernet_input
# lwIP
#IRAM	+= sys_check_timeouts
####################################

AUTO	= glue-lwip/lwip-err-t.h glue-lwip/lwip-git-hash.h

all: $(AUTO) $(LWIP_LIB_RELEASE)

glue-lwip/lwip-err-t.h: $(LWIP_ESP)/arch/cc.h
	( \
	echo "// script-generated, extracted from espressif SDK's lwIP arch/cc.h"; \
	echo "#define LWIP_NO_STDINT_H 1"; \
	grep -e LWIP_ERR_T -e ^typedef $< \
	) > $@

glue-lwip/lwip-git-hash.h:
	makefiles/make-lwip-hash

$(LWIP_LIB_RELEASE): $(LWIP_LIB)
	@# move $(IRAM) symbols to section "move2iram"
	@# check that symbol's address is 3fff...: readelf -a sketch.elf | grep sys_check_timeouts
	@( [ ! -r $(LWIP_LIB_RELEASE) ] || [ $(LWIP_LIB) -nt $(LWIP_LIB_RELEASE) ] ) && cp $(LWIP_LIB) $(LWIP_LIB_RELEASE) && for i in $(IRAM); do \
		echo "---- moving $$i in IRAM"; \
		$(OC) --rename-section .text.$$i=.move2iram.$$i $(LWIP_LIB_RELEASE); \
		true; \
	done || true

.PHONY: $(LWIP_LIB)
$(LWIP_LIB):
	make -f makefiles/Makefile.glue-esp BUILD=$(BUILD) V=$(V)
	make -f makefiles/Makefile.glue target=$(target) BUILD=$(BUILD) TCP_MSS=$(TCP_MSS) LWIP_FEATURES=$(LWIP_FEATURES) LWIP_IPV6=$(LWIP_IPV6) V=$(V)
ifeq ($(target),arduino)
	make -C lwip2-src/src -f ../../makefiles/Makefile.lwip2 BUILD=../../$(BUILD) LWIP_LIB=../../$(LWIP_LIB) SDK=../../$(SDK) TOOLS=../../$(TOOLS) TCP_MSS=$(TCP_MSS) LWIP_FEATURES=$(LWIP_FEATURES) LWIP_IPV6=$(LWIP_IPV6) V=$(V)
else
	make -C lwip2-src/src -f ../../makefiles/Makefile.lwip2 BUILD=../../$(BUILD) LWIP_LIB=../../$(LWIP_LIB) TCP_MSS=$(TCP_MSS) LWIP_FEATURES=$(LWIP_FEATURES) LWIP_IPV6=$(LWIP_IPV6) V=$(V)
endif

section-show:
	@for i in $(IRAM); do \
		echo  $$i \\t\\t `$(OD) -t $(LWIP_LIB_RELEASE) | grep " F .* $${i}$$"`; \
	done; true

install: $(AUTO) $(LWIP_LIB_RELEASE)
	rm -rf $(LWIP_INCLUDES_RELEASE)
	mkdir -p $(LWIP_INCLUDES_RELEASE)
	cp -a lwip2-src/src/include/* $(LWIP_INCLUDES_RELEASE)
	cp -a glue/gluedebug.h glue-lwip/lwip-git-hash.h glue-lwip/lwip-err-t.h glue-lwip/arch $(LWIP_INCLUDES_RELEASE)
	echo "// this file will be overwritten upon lwip2 rebuild" > $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	echo "#ifndef __CUSTOM_EXTRA_DEFINES__" >> $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	echo "#define __CUSTOM_EXTRA_DEFINES__" >> $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	echo "$(CUSTOM_EXTRA_DEFINES)" >> $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	echo "#endif" >> $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	cat glue-lwip/$(target)/lwipopts.h >> $(LWIP_INCLUDES_RELEASE)/lwipopts.h
	echo "warning: this directory is re/over/written from lwip2 builder upon lwip2 rebuild" > $(LWIP_INCLUDES_RELEASE)/README.md
ifneq ($(target),open)
	touch $(LWIP_INCLUDES_RELEASE)/espconn.h
endif

clean:
	rm -rf $(BUILD) $(LWIP_LIB) $(AUTO)
	cd lwip2-src; rm -f src/.patched src/core/*.c.orig src/core/ipv6/*.c.orig; git checkout -- src;
