forked from thunderace/Esp8266-Arduino-Makefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266Arduino.mk
234 lines (179 loc) · 10 KB
/
esp8266Arduino.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
TARGET = $(notdir $(realpath .))
ARCH = $(shell uname)
ifeq ($(ARCH), Linux)
ROOT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
else
# The extensa tools cannot use cygwin paths, so convert /cygdrive/c/abc/... to c:/cygwin64/abc/...
ROOT_DIR_RAW := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
ROOT_DIR := $(shell cygpath -m $(ROOT_DIR_RAW))
EXEC_EXT = ".exe"
endif
SERIAL_PORT ?= /dev/tty.nodemcu
ARDUINO_HOME ?= $(ROOT_DIR)/esp8266
ARDUINO_ARCH = esp8266
ARDUINO_BOARD ?= ESP8266_ESP12
ARDUINO_VARIANT ?= nodemcu
ARDUINO_VERSION ?= 10605
#ESPTOOL_VERBOSE ?= -vv
BOARDS_TXT = $(ARDUINO_HOME)/boards.txt
PARSE_BOARD = $(ROOT_DIR)/bin/ard-parse-boards
PARSE_BOARD_OPTS = --boards_txt=$(BOARDS_TXT)
PARSE_BOARD_CMD = $(PARSE_BOARD) $(PARSE_BOARD_OPTS)
VARIANT = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.variant)
MCU = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.mcu)
SERIAL_BAUD = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) upload.speed)
F_CPU = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.f_cpu)
FLASH_SIZE ?= $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.flash_size)
FLASH_MODE ?= $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.flash_mode)
FLASH_FREQ ?= $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) build.flash_freq)
UPLOAD_RESETMETHOD = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) upload.resetmethod)
UPLOAD_SPEED = $(shell $(PARSE_BOARD_CMD) $(ARDUINO_VARIANT) upload.speed)
LOCAL_USER_LIBDIR ?= ./libraries
GLOBAL_USER_LIBDIR ?= $(ROOT_DIR)/libraries
XTENSA_TOOLCHAIN ?= $(ROOT_DIR)/xtensa-lx106-elf/bin/
ESPRESSIF_SDK = $(ARDUINO_HOME)/tools/sdk
ESPTOOL ?= $(ROOT_DIR)/bin/esptool$(EXEC_EXT)
ESPOTA ?= $(ARDUINO_HOME)/tools/espota.py
TAG := $(shell date --iso=seconds)
BUILD_OUT = ./build.$(ARDUINO_VARIANT)
CORE_SSRC = $(wildcard $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/*.S)
CORE_SRC = $(wildcard $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/*.c)
CORE_SRC += $(wildcard $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/*/*.c)
CORE_CXXSRC = $(wildcard $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/*.cpp)
CORE_OBJS = $(addprefix $(BUILD_OUT)/core/, \
$(notdir $(CORE_SSRC:.S=.S.o) $(CORE_CXXSRC:.cpp=.cpp.o))) \
$(addprefix $(BUILD_OUT)/core/, $(patsubst $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/%.c,%.c.o,$(CORE_SRC)))
CORE_DIRS = $(sort $(dir $(CORE_OBJS)))
#autodetect arduino libs and user libs
LOCAL_SRCS = $(USER_SRC) $(USER_CXXSRC) $(LIB_INOSRC) $(USER_HSRC) $(USER_HPPSRC)
ifndef ARDUINO_LIBS
# automatically determine included libraries
ARDUINO_LIBS = $(sort $(filter $(notdir $(wildcard $(ARDUINO_HOME)/libraries/*)), \
$(shell $(SED) -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))))
endif
ifndef USER_LIBS
# automatically determine included user libraries
USER_LIBS = $(sort $(filter $(notdir $(wildcard $(LOCAL_USER_LIBDIR)/*)), \
$(shell $(SED) -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))))
USER_LIBS += $(sort $(filter $(notdir $(wildcard $(GLOBAL_USER_LIBDIR)/*)), \
$(shell $(SED) -ne 's/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p' $(LOCAL_SRCS))))
endif
# arduino libraries
ALIBDIRS = $(sort $(dir $(wildcard \
$(ARDUINO_LIBS:%=$(ARDUINO_HOME)/libraries/%/*.c) \
$(ARDUINO_LIBS:%=$(ARDUINO_HOME)/libraries/%/*.cpp) \
$(ARDUINO_LIBS:%=$(ARDUINO_HOME)/libraries/%/src/*.c) \
$(ARDUINO_LIBS:%=$(ARDUINO_HOME)/libraries/%/src/*.cpp))))
# user libraries and sketch code
ULIBDIRS = $(sort $(dir $(wildcard \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/*.c) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*.c) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*/*.c) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*/*/*.c) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/*.cpp) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*.cpp) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*/*.cpp) \
$(USER_LIBS:%=$(LOCAL_USER_LIBDIR)/%/src/*/*/*.cpp) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/*.c) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*.c) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*/*.c) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*/*/*.c) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/*.cpp) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*.cpp) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*/*.cpp) \
$(USER_LIBS:%=$(GLOBAL_USER_LIBDIR)/%/src/*/*/*.cpp))))
USRCDIRS = .
# all sources
LIB_SRC = $(wildcard $(addsuffix /*.c,$(ULIBDIRS))) \
$(wildcard $(addsuffix /*.c,$(ALIBDIRS)))
LIB_CXXSRC = $(wildcard $(addsuffix /*.cpp,$(ULIBDIRS))) \
$(wildcard $(addsuffix /*.cpp,$(ALIBDIRS)))
USER_SRC = $(wildcard $(addsuffix /*.c,$(USRCDIRS)))
USER_CXXSRC = $(wildcard $(addsuffix /*.cpp,$(USRCDIRS))) \
USER_HSRC = $(wildcard $(addsuffix /*.h,$(USRCDIRS)))
USER_HPPSRC = $(wildcard $(addsuffix /*.hpp,$(USRCDIRS)))
LIB_INOSRC = $(wildcard $(addsuffix /*.ino,$(USRCDIRS)))
# object files
OBJ_FILES = $(addprefix $(BUILD_OUT)/,$(notdir $(LIB_SRC:.c=.c.o) $(LIB_CXXSRC:.cpp=.cpp.o) $(LIB_INOSRC:.ino=.ino.o) $(USER_SRC:.c=.c.o) $(USER_CXXSRC:.cpp=.cpp.o)))
#compiler.cpreprocessor.flags=-D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ "-I{compiler.sdk.path}/include" "-I{compiler.sdk.path}/lwip/include"
CPREPROCESSOR_FLAGS = -D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ -I$(ESPRESSIF_SDK)/include -I$(ESPRESSIF_SDK)/lwip/include
DEFINES = $(USER_DEFINE) \
$(CPREPROCESSOR_FLAGS) \
-DF_CPU=$(F_CPU) -DARDUINO=$(ARDUINO_VERSION) \
-DARDUINO_$(ARDUINO_BOARD) -DESP8266 \
-DARDUINO_ARCH_$(shell echo "$(ARDUINO_ARCH)" | tr '[:lower:]' '[:upper:]') \
CORE_INC = $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH) \
$(ARDUINO_HOME)/variants/$(VARIANT)
INCLUDES = $(CORE_INC:%=-I%) $(ALIBDIRS:%=-I%) $(ULIBDIRS:%=-I%)
VPATH = . $(CORE_INC) $(ALIBDIRS) $(ULIBDIRS)
#compiler.S.flags=-c -g -x assembler-with-cpp -MMD -mlongcalls
ASFLAGS = -c -g -x assembler-with-cpp -MMD -mlongcalls $(DEFINES)
# from 2.2.0 compiler.c.flags=-c {compiler.warning_flags} -Os -g -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections
CFLAGS = -c -Os -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL \
-fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals \
-falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections
# from 2.2.0 compiler.cpp.flags=-c {compiler.warning_flags} -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -MMD -ffunction-sections -fdata-sections
CXXFLAGS = -c -Os -mlongcalls -mtext-section-literals -fno-exceptions \
-fno-rtti -falign-functions=4 -std=c++11 -MMD -ffunction-sections -fdata-sections
#LDFLAGS = -nostdlib -Wl,--gc-sections -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy
# from 2.2.0 compiler.c.elf.libs=-lm -lgcc -lhal -lphy -lpp -lnet80211 -lwpa -lcrypto -lmain -lwps -laxtls -lsmartconfig -lmesh -lwpa2 {build.lwip_lib}
ELFLIBS = -lm -lgcc -lhal -lphy -lpp -lnet80211 -lwpa -lcrypto -lmain -lwps -laxtls -lsmartconfig -lmesh -lwpa2 -llwip
# from 2.2.0 compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy
ELFFLAGS = -g -w -Os -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static \
-L$(ESPRESSIF_SDK)/lib -L$(ESPRESSIF_SDK)/ld \
-T$(ESPRESSIF_SDK)/ld/eagle.flash.4m.ld \
-Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy
CC := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-gcc
CXX := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-g++
AR := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-ar
LD := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-gcc
OBJDUMP := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-objdump
SIZE := $(XTENSA_TOOLCHAIN)xtensa-lx106-elf-size
CAT = cat$(EXEC_EXT)
SED = sed$(EXEC_EXT)
.PHONY: all arduino dirs clean upload
all: show_variables dirs core libs bin size
show_variables:
$(info [ARDUINO_LIBS] : $(ARDUINO_LIBS))
$(info [USER_LIBS] : $(USER_LIBS))
dirs:
@mkdir -p $(CORE_DIRS)
clean:
rm -rf $(BUILD_OUT)
core: dirs $(BUILD_OUT)/core/core.a
libs: dirs $(OBJ_FILES)
bin: $(BUILD_OUT)/$(TARGET).bin
$(BUILD_OUT)/core/%.S.o: $(ARDUINO_HOME)/cores/$(ARDUINO_ARCH)/%.S
$(CC) $(ASFLAGS) -o $@ $<
$(BUILD_OUT)/core/core.a: $(CORE_OBJS)
$(AR) cru $@ $(CORE_OBJS)
$(BUILD_OUT)/core/%.c.o: %.c
$(CC) $(DEFINES) $(CORE_INC:%=-I%) $(CFLAGS) -o $@ $<
$(BUILD_OUT)/core/%.cpp.o: %.cpp
$(CXX) $(DEFINES) $(CORE_INC:%=-I%) $(CXXFLAGS) $< -o $@
$(BUILD_OUT)/%.c.o: %.c
$(CC) -D_TAG_=\"$(TAG)\" $(DEFINES) $(CFLAGS) $(INCLUDES) -o $@ $<
$(BUILD_OUT)/%.ino.o: %.ino
$(CXX) -x c++ -D_TAG_=\"$(TAG)\" $(DEFINES) $(CXXFLAGS) $(INCLUDES) $< -o $@
$(BUILD_OUT)/%.cpp.o: %.cpp
$(CXX) -D_TAG_=\"$(TAG)\" $(DEFINES) $(CXXFLAGS) $(INCLUDES) $< -o $@
#recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{build.path}/arduino.ar" {compiler.c.elf.libs} -Wl,--end-group "-L{build.path}"
$(BUILD_OUT)/$(TARGET).elf: core libs
$(LD) $(ELFFLAGS) -o $@ \
-Wl,--start-group $(OBJ_FILES) $(BUILD_OUT)/core/core.a \
$(ELFLIBS) \
-Wl,--end-group -L$(BUILD_OUT)
size : $(BUILD_OUT)/$(TARGET).elf
$(SIZE) -A $(BUILD_OUT)/$(TARGET).elf | grep -E '^(?:\.text|\.data|\.rodata|\.irom0\.text|)\s+([0-9]+).*'
$(BUILD_OUT)/$(TARGET).bin: $(BUILD_OUT)/$(TARGET).elf
$(ESPTOOL) -eo $(ARDUINO_HOME)/bootloaders/eboot/eboot.elf -bo $(BUILD_OUT)/$(TARGET).bin \
-bm $(FLASH_MODE) -bf $(FLASH_FREQ) -bz $(FLASH_SIZE) \
-bs .text -bp 4096 -ec -eo $(BUILD_OUT)/$(TARGET).elf -bs .irom0.text -bs .text -bs .data -bs .rodata -bc -ec
upload: $(BUILD_OUT)/$(TARGET).bin size
$(ESPTOOL) $(ESPTOOL_VERBOSE) -cd $(UPLOAD_RESETMETHOD) -cb $(UPLOAD_SPEED) -cp $(SERIAL_PORT) -ca 0x00000 -cf $(BUILD_OUT)/$(TARGET).bin
ota: $(BUILD_OUT)/$(TARGET).bin
$(ESPOTA) -i $(OTA_IP) -p $(OTA_PORT) -a $(OTA_AUTH) -f $(BUILD_OUT)/$(TARGET).bin
term:
minicom -D $(SERIAL_PORT) -b $(UPLOAD_SPEED)
print-%: ; @echo $* = $($*)
-include $(OBJ_FILES:.o=.d)