1# Makefile to compile the flasher stub program
2#
3# Note that YOU DO NOT NEED TO COMPILE THIS IN ORDER TO JUST USE
4# esptool.py - a precompiled version is included in esptool.py,
5# so if you don't want to modify the stub code then you are good to go.
6#
7# See the comments in the top of the Makefile for parameters that
8# you probably want to override.
9#
10# Copyright (c) 2016 Cesanta Software Limited & Angus Gratton
11# All rights reserved
12#
13#
14# This program is free software; you can redistribute it and/or modify it under
15# the terms of the GNU General Public License as published by the Free Software
16# Foundation; either version 2 of the License, or (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful, but WITHOUT
19# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License along with
23# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
24# Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26# Adapted from Cesanta's original Makefile at
27# https://github.com/cesanta/fnc/tree/master/common/platforms/esp8266/stubs
28
29# Override these variables on the command line
30# or set them in a local.mk
31-include local.mk
32
33# Prefix for each cross compiler (can include a directory path)
34# These can be overridden via environment variables or on the make command line
35CROSS_8266 ?= xtensa-lx106-elf-
36CROSS_32 ?= xtensa-esp32-elf-
37CROSS_32S2 ?= xtensa-esp32s2-elf-
38CROSS_32S3 ?= xtensa-esp32s3-elf-
39CROSS_ESPRISCV32 ?= riscv32-esp-elf-
40
41# extra CFLAGS to be passed on the compiler, in addition to the stock ones
42EXTRA_CFLAGS ?=
43EXTRA_CFLAGS_ESPRISCV32 ?=
44
45# Python command to invoke wrap_stub.py
46WRAP_STUB ?= ./wrap_stub.py
47
48# Pass V=1 to see the commands being executed by make
49ifneq ("$(V)","1")
50Q = @
51endif
52
53STUB = stub_flasher
54SRCS = stub_flasher.c slip.c stub_commands.c stub_write_flash.c stub_io.c
55SRCS_8266 = miniz.c
56
57BUILD_DIR = build
58ESPTOOL_STUBS_DIR = ../esptool/targets/stub_flasher
59
60STUB_ELF_8266 = $(BUILD_DIR)/$(STUB)_8266.elf
61STUB_ELF_32 = $(BUILD_DIR)/$(STUB)_32.elf
62STUB_ELF_32S2 = $(BUILD_DIR)/$(STUB)_32s2.elf
63STUB_ELF_32S3_BETA_2 = $(BUILD_DIR)/$(STUB)_32s3beta2.elf
64STUB_ELF_32S3 = $(BUILD_DIR)/$(STUB)_32s3.elf
65STUB_ELF_32C3 = $(BUILD_DIR)/$(STUB)_32c3.elf
66STUB_ELF_32C6BETA = $(BUILD_DIR)/$(STUB)_32c6beta.elf
67STUB_ELF_32H2_BETA_1 = $(BUILD_DIR)/$(STUB)_32h2beta1.elf
68STUB_ELF_32H2_BETA_2 = $(BUILD_DIR)/$(STUB)_32h2beta2.elf
69STUB_ELF_32C2 = $(BUILD_DIR)/$(STUB)_32c2.elf
70STUB_ELF_32C6 = $(BUILD_DIR)/$(STUB)_32c6.elf
71STUB_ELF_32C5_BETA_3 = $(BUILD_DIR)/$(STUB)_32c5beta3.elf
72STUB_ELF_32H2 = $(BUILD_DIR)/$(STUB)_32h2.elf
73STUB_ELF_32P4 = $(BUILD_DIR)/$(STUB)_32p4.elf
74
75STUBS_ELF =
76ifneq ($(WITHOUT_ESP8266),1)
77STUBS_ELF += \
78  $(STUB_ELF_8266)
79endif
80
81ifneq ($(WITHOUT_ESP32_XTENSA),1)
82STUBS_ELF += \
83  $(STUB_ELF_32) \
84  $(STUB_ELF_32S2) \
85  $(STUB_ELF_32S3_BETA_2) \
86  $(STUB_ELF_32S3)
87endif
88
89ifneq ($(WITHOUT_ESP32_RISCV32),1)
90STUBS_ELF += \
91  $(STUB_ELF_32C3) \
92  $(STUB_ELF_32C6BETA) \
93  $(STUB_ELF_32H2_BETA_1) \
94  $(STUB_ELF_32H2_BETA_2) \
95  $(STUB_ELF_32C2) \
96  $(STUB_ELF_32C6) \
97  $(STUB_ELF_32C5_BETA_3) \
98  $(STUB_ELF_32H2) \
99  $(STUB_ELF_32P4)
100endif
101
102.PHONY: all clean install
103
104all: $(STUBS_ELF:.elf=.json)
105
106install: $(patsubst $(BUILD_DIR)/%.elf,$(ESPTOOL_STUBS_DIR)/%.json,$(STUBS_ELF))
107
108$(BUILD_DIR):
109	$(Q) mkdir $@
110
111$(BUILD_DIR)/%.json: $(BUILD_DIR)/%.elf
112	@echo "  WRAP $^ -> $(BUILD_DIR)"
113	$(Q) $(WRAP_STUB) $^
114
115$(ESPTOOL_STUBS_DIR)/%.json: $(BUILD_DIR)/%.json
116	@echo "  INSTALL $^ -> $@"
117	$(Q) cp $^ $@
118
119CFLAGS = -std=c99 -Wall -Werror -Os \
120         -mtext-section-literals -mlongcalls -nostdlib -fno-builtin -flto \
121         -Wl,-static -g -ffunction-sections -Wl,--gc-sections -Iinclude -Lld \
122         $(EXTRA_CFLAGS)
123CFLAGS_ESPRISCV32 = -std=c99 -Wall -Werror -Os \
124		 -march=rv32imc -mabi=ilp32 -msmall-data-limit=0 \
125         -nostdlib -fno-builtin -flto \
126         -Wl,-static -g -ffunction-sections -Wl,--gc-sections -Iinclude -Lld \
127         $(EXTRA_CFLAGS_ESPRISCV32)
128LDLIBS = -lgcc
129
130$(STUB_ELF_8266): $(SRCS) $(SRCS_8266) $(BUILD_DIR) ld/stub_8266.ld | Makefile
131	@echo "  CC(8266)   $^ -> $@"
132	$(Q) $(CROSS_8266)gcc $(CFLAGS) -DESP8266=1 -Tstub_8266.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
133
134$(STUB_ELF_32): $(SRCS) $(BUILD_DIR) ld/stub_32.ld | Makefile
135	@echo "  CC(32)   $^ -> $@"
136	$(Q) $(CROSS_32)gcc $(CFLAGS) -DESP32=1 -Tstub_32.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
137
138$(STUB_ELF_32S2): $(SRCS) $(BUILD_DIR) ld/stub_32s2.ld
139	@echo "  CC(32S2)   $^ -> $@"
140	$(Q) $(CROSS_32S2)gcc $(CFLAGS) -DESP32S2=1 -Tstub_32s2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
141
142$(STUB_ELF_32S3_BETA_2): $(SRCS) $(BUILD_DIR) ld/stub_32s3_beta_2.ld
143	@echo "  CC(32S3)   $^ -> $@"
144	$(Q) $(CROSS_32S3)gcc $(CFLAGS) -DESP32S3BETA2=1 -Tstub_32s3_beta_2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
145
146$(STUB_ELF_32S3): $(SRCS) $(BUILD_DIR) ld/stub_32s3.ld
147	@echo "  CC(32S3)   $^ -> $@"
148	$(Q) $(CROSS_32S3)gcc $(CFLAGS) -DESP32S3=1 -Tstub_32s3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
149
150$(STUB_ELF_32C3): $(SRCS) $(BUILD_DIR) ld/stub_32c3.ld
151	@echo "  CC(32C3)   $^ -> $@"
152	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C3=1 -Tstub_32c3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
153
154$(STUB_ELF_32C6BETA): $(SRCS) $(BUILD_DIR) ld/stub_32c6_beta.ld
155	@echo "  CC(32C6BETA)   $^ -> $@"
156	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C6BETA=1 -Tstub_32c6_beta.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
157
158$(STUB_ELF_32H2_BETA_1): $(SRCS) $(BUILD_DIR) ld/stub_32h2_beta_1.ld
159	@echo "  CC(32H2BETA1)   $^ -> $@"
160	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2BETA1=1 -Tstub_32h2_beta_1.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
161
162$(STUB_ELF_32H2_BETA_2): $(SRCS) $(BUILD_DIR) ld/stub_32h2_beta_2.ld
163	@echo "  CC(32H2BETA2)   $^ -> $@"
164	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2BETA2=1 -Tstub_32h2_beta_2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
165
166$(STUB_ELF_32C2): $(SRCS) $(BUILD_DIR) ld/stub_32c2.ld
167	@echo "  CC(32C2)   $^ -> $@"
168	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C2=1 -Tstub_32c2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
169
170$(STUB_ELF_32C6): $(SRCS) $(BUILD_DIR) ld/stub_32c6.ld
171	@echo "  CC(32C6)   $^ -> $@"
172	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C6=1 -Tstub_32c6.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
173
174$(STUB_ELF_32C5_BETA_3): $(SRCS) $(BUILD_DIR) ld/stub_32c5_beta_3.ld
175	@echo "  CC(32C5BETA3)   $^ -> $@"
176	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32C5BETA3=1 -Tstub_32c5_beta_3.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
177
178$(STUB_ELF_32H2): $(SRCS) $(BUILD_DIR) ld/stub_32h2.ld
179	@echo "  CC(32H2)   $^ -> $@"
180	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32H2=1 -Tstub_32h2.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
181
182$(STUB_ELF_32P4): $(SRCS) $(BUILD_DIR) ld/stub_32p4.ld
183	@echo "  CC(32P4)   $^ -> $@"
184	$(Q) $(CROSS_ESPRISCV32)gcc $(CFLAGS_ESPRISCV32) -DESP32P4=1 -Tstub_32p4.ld -Wl,-Map=$(@:.elf=.map) -o $@ $(filter %.c, $^) $(LDLIBS)
185
186clean:
187	$(Q) rm -rf $(BUILD_DIR)
188