主要内容
本章我们来分析SDK中的Makefile,同时学习如何创建自己的源码文件夹。
预备知识
一份代码工程编译成二进制可执行文件,需要经过四个步骤:预处理、编译、汇编和链接。
进行编译需要有编译器,Linux环境下使用的是gcc,而对应的,ESP8266使用的编译器是xtensa-lx106-elf-gcc。在后面分析Makefile中,我们就会看到该编译器。
下面是其他一些工具的含义:
- ar:用于建立或修改备存文件,或是从备存文件中抽取文件
- nm:用来列出目标文件的符号清单
- objcopy:将目标文件的一部分或者全部内容拷贝到另外一个目标文件中,或者实现目标文件的格式转换
- objdump:查看目标文件或者可执行的目标文件的构成
Makefile分析
首先我们来看看ESP8266 SDK的目录结构以及Makefile:
│ License
│ Makefile # 主目录下的Makefile
├─app/
│ │ Makefile
│ ├─ driver
│ └─ user
│ └─ Makefile
├─bin
├─documents
├─driver_lib
├─examples
├─lib
├─include
├─ld
└─tools
由此可见,我们至少要分析三个Makefile:
- 主目录下的Makefile
- app目录下的Makefile
-
app/user/
的Makefile
主目录下的Makefile
下面我们来hack一下主目录下的Makefile,详细解释请看注释:
# copyright (c) 2010 Espressif System
#
ifndef PDIR
endif
############################################
# 编译工具配置
# ESP8266 主要使用的编译器是 xtensa-lx106-elf-gcc
ifeq ($(COMPILE), gcc)
AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc
NM = xtensa-lx106-elf-nm
CPP = xtensa-lx106-elf-cpp
OBJCOPY = xtensa-lx106-elf-objcopy
OBJDUMP = xtensa-lx106-elf-objdump
else
AR = xt-ar
CC = xt-xcc
NM = xt-nm
CPP = xt-cpp
OBJCOPY = xt-objcopy
OBJDUMP = xt-objdump
endif
############################################
# 参数默认配置
# =none - 不使用boot
# =old - 使用老版本的boot_v1.1+
# =new - 使用新版本的boot_v1.2+
BOOT?=none
# =0 - 不使用远程升级FOTA
# =1 - 使用FOTA,生成user1.<flash_map>.<BOOT>.bin
# =2 - 使用FOTA,生成user2.<flash_map>.<BOOT>.bin
APP?=0
# SPI速率和模式,一般不用改动
SPI_SPEED?=40
SPI_MODE?=QIO
# SPI_SIZE_MAP flash映射方式
# 4MB Flash使用 SPI_SIZE_MAP?=4
SPI_SIZE_MAP?=4
############################################
ifeq ($(BOOT), new)
boot = new
else
ifeq ($(BOOT), old)
boot = old
else
boot = none
endif
endif
ifeq ($(APP), 1)
app = 1
else
ifeq ($(APP), 2)
app = 2
else
app = 0
endif
endif
ifeq ($(SPI_SPEED), 26.7)
freqdiv = 1
else
ifeq ($(SPI_SPEED), 20)
freqdiv = 2
else
ifeq ($(SPI_SPEED), 80)
freqdiv = 15
else
freqdiv = 0
endif
endif
endif
ifeq ($(SPI_MODE), QOUT)
mode = 1
else
ifeq ($(SPI_MODE), DIO)
mode = 2
else
ifeq ($(SPI_MODE), DOUT)
mode = 3
else
mode = 0
endif
endif
endif
addr = 0x01000
ifeq ($(SPI_SIZE_MAP), 1)
size_map = 1
flash = 256
else
ifeq ($(SPI_SIZE_MAP), 2)
size_map = 2
flash = 1024
ifeq ($(app), 2)
addr = 0x81000
endif
else
ifeq ($(SPI_SIZE_MAP), 3)
size_map = 3
flash = 2048
ifeq ($(app), 2)
addr = 0x81000
endif
else
ifeq ($(SPI_SIZE_MAP), 4)
size_map = 4
flash = 4096
ifeq ($(app), 2)
addr = 0x81000
endif
else
ifeq ($(SPI_SIZE_MAP), 5)
size_map = 5
flash = 2048
ifeq ($(app), 2)
addr = 0x101000
endif
else
ifeq ($(SPI_SIZE_MAP), 6)
size_map = 6
flash = 4096
ifeq ($(app), 2)
addr = 0x101000
endif
else
size_map = 0
flash = 512
ifeq ($(app), 2)
addr = 0x41000
endif
endif
endif
endif
endif
endif
endif
############################################
# 选择链接工具
# 这里选择了主目录下的 ld/eagle.app.v6.ld
LD_FILE = $(LDDIR)/eagle.app.v6.ld
# 如果boot!=none,才进入这里
ifneq ($(boot), none)
ifneq ($(app),0)
ifeq ($(size_map), 6)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).2048.ld
else
ifeq ($(size_map), 5)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).2048.ld
else
ifeq ($(size_map), 4)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).1024.app$(app).ld
else
ifeq ($(size_map), 3)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).1024.app$(app).ld
else
ifeq ($(size_map), 2)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).1024.app$(app).ld
else
ifeq ($(size_map), 0)
LD_FILE = $(LDDIR)/eagle.app.v6.$(boot).512.app$(app).ld
endif
endif
endif
endif
endif
endif
BIN_NAME = user$(app).$(flash).$(boot).$(size_map)
endif
else
app = 0
endif
############################################
CSRCS ?= $(wildcard *.c)
ASRCs ?= $(wildcard *.s)
ASRCS ?= $(wildcard *.S)
SUBDIRS ?= $(patsubst %/,%,$(dir $(wildcard */Makefile)))
ODIR := .output
OBJODIR := $(ODIR)/$(TARGET)/$(FLAVOR)/obj
# 设置 OBJS 变量
OBJS := $(CSRCS:%.c=$(OBJODIR)/%.o) \
$(ASRCs:%.s=$(OBJODIR)/%.o) \
$(ASRCS:%.S=$(OBJODIR)/%.o)
# 设置 DEPS 变量
DEPS := $(CSRCS:%.c=$(OBJODIR)/%.d) \
$(ASRCs:%.s=$(OBJODIR)/%.d) \
$(ASRCS:%.S=$(OBJODIR)/%.d)
# 设置 LIBODIR 库文件夹
LIBODIR := $(ODIR)/$(TARGET)/$(FLAVOR)/lib
OLIBS := $(GEN_LIBS:%=$(LIBODIR)/%)
IMAGEODIR := $(ODIR)/$(TARGET)/$(FLAVOR)/image
OIMAGES := $(GEN_IMAGES:%=$(IMAGEODIR)/%)
# 设置 BINODIR 二进制文件夹
BINODIR := $(ODIR)/$(TARGET)/$(FLAVOR)/bin
OBINS := $(GEN_BINS:%=$(BINODIR)/%)
#############################################################
CCFLAGS += \
-g \
-Wpointer-arith \
-Wundef \
-Werror \
-Wl,-EL \
-fno-inline-functions \
-nostdlib \
-mlongcalls \
-mtext-section-literals \
-ffunction-sections \
-fdata-sections \
-fno-builtin-printf
# -Wall
CFLAGS = $(CCFLAGS) $(DEFINES) $(EXTRA_CCFLAGS) $(INCLUDES)
DFLAGS = $(CCFLAGS) $(DDEFINES) $(EXTRA_CCFLAGS) $(INCLUDES)
#############################################################
# Functions
#
define ShortcutRule
$(1): .subdirs $(2)/$(1)
endef
define MakeLibrary
DEP_LIBS_$(1) = $$(foreach lib,$$(filter %.a,$$(COMPONENTS_$(1))),$$(dir $$(lib))$$(LIBODIR)/$$(notdir $$(lib)))
DEP_OBJS_$(1) = $$(foreach obj,$$(filter %.o,$$(COMPONENTS_$(1))),$$(dir $$(obj))$$(OBJODIR)/$$(notdir $$(obj)))
$$(LIBODIR)/$(1).a: $$(OBJS) $$(DEP_OBJS_$(1)) $$(DEP_LIBS_$(1)) $$(DEPENDS_$(1))
@mkdir -p $$(LIBODIR)
$$(if $$(filter %.a,$$?),mkdir -p $$(EXTRACT_DIR)_$(1))
$$(if $$(filter %.a,$$?),cd $$(EXTRACT_DIR)_$(1); $$(foreach lib,$$(filter %.a,$$?),$$(AR) xo $$(UP_EXTRACT_DIR)/$$(lib);))
$$(AR) ru $$@ $$(filter %.o,$$?) $$(if $$(filter %.a,$$?),$$(EXTRACT_DIR)_$(1)/*.o)
$$(if $$(filter %.a,$$?),$$(RM) -r $$(EXTRACT_DIR)_$(1))
endef
define MakeImage
DEP_LIBS_$(1) = $$(foreach lib,$$(filter %.a,$$(COMPONENTS_$(1))),$$(dir $$(lib))$$(LIBODIR)/$$(notdir $$(lib)))
DEP_OBJS_$(1) = $$(foreach obj,$$(filter %.o,$$(COMPONENTS_$(1))),$$(dir $$(obj))$$(OBJODIR)/$$(notdir $$(obj)))
$$(IMAGEODIR)/$(1).out: $$(OBJS) $$(DEP_OBJS_$(1)) $$(DEP_LIBS_$(1)) $$(DEPENDS_$(1))
@mkdir -p $$(IMAGEODIR)
$$(CC) $$(LDFLAGS) $$(if $$(LINKFLAGS_$(1)),$$(LINKFLAGS_$(1)),$$(LINKFLAGS_DEFAULT) $$(OBJS) $$(DEP_OBJS_$(1)) $$(DEP_LIBS_$(1))) -o $$@
endef
$(BINODIR)/%.bin: $(IMAGEODIR)/%.out
@mkdir -p $(BINODIR)
ifeq ($(APP), 0)
@$(RM) -r ../bin/eagle.S ../bin/eagle.dump
@$(OBJDUMP) -x -s $< > ../bin/eagle.dump
@$(OBJDUMP) -S $< > ../bin/eagle.S
else
mkdir -p ../bin/upgrade
@$(RM) -r ../bin/upgrade/$(BIN_NAME).S ../bin/upgrade/$(BIN_NAME).dump
@$(OBJDUMP) -x -s $< > ../bin/upgrade/$(BIN_NAME).dump
@$(OBJDUMP) -S $< > ../bin/upgrade/$(BIN_NAME).S
endif
@$(OBJCOPY) --only-section .text -O binary $< eagle.app.v6.text.bin
@$(OBJCOPY) --only-section .data -O binary $< eagle.app.v6.data.bin
@$(OBJCOPY) --only-section .rodata -O binary $< eagle.app.v6.rodata.bin
@$(OBJCOPY) --only-section .irom0.text -O binary $< eagle.app.v6.irom0text.bin
@echo ""
@echo "!!!"
# 默认是 app==0
# 下面是输出打印信息
ifeq ($(app), 0)
@python ../tools/gen_appbin.py $< 0 $(mode) $(freqdiv) $(size_map) $(app)
@mv eagle.app.flash.bin ../bin/eagle.flash.bin
@mv eagle.app.v6.irom0text.bin ../bin/eagle.irom0text.bin
@rm eagle.app.v6.*
@echo "No boot needed."
@echo "Generate eagle.flash.bin and eagle.irom0text.bin successully in folder bin."
@echo "eagle.flash.bin-------->0x00000"
@echo "eagle.irom0text.bin---->0x10000"
else
ifneq ($(boot), new)
@python ../tools/gen_appbin.py $< 1 $(mode) $(freqdiv) $(size_map) $(app)
@echo "Support boot_v1.1 and +"
else
@python ../tools/gen_appbin.py $< 2 $(mode) $(freqdiv) $(size_map) $(app)
ifeq ($(size_map), 6)
@echo "Support boot_v1.4 and +"
else
ifeq ($(size_map), 5)
@echo "Support boot_v1.4 and +"
else
@echo "Support boot_v1.2 and +"
endif
endif
endif
@mv eagle.app.flash.bin ../bin/upgrade/$(BIN_NAME).bin
@rm eagle.app.v6.*
@echo "Generate $(BIN_NAME).bin successully in folder bin/upgrade."
@echo "boot.bin------------>0x00000"
@echo "$(BIN_NAME).bin--->$(addr)"
endif
@echo "!!!"
#############################################################
# Rules base
# Should be done in top-level makefile only
#
############################################
# make all执行的方法
all: .subdirs $(OBJS) $(OLIBS) $(OIMAGES) $(OBINS) $(SPECIAL_MKTARGETS)
############################################
# make clean执行的方法
clean:
$(foreach d, $(SUBDIRS), $(MAKE) -C $(d) clean;)
$(RM) -r $(ODIR)/$(TARGET)/$(FLAVOR)
clobber: $(SPECIAL_CLOBBER)
$(foreach d, $(SUBDIRS), $(MAKE) -C $(d) clobber;)
$(RM) -r $(ODIR)
.subdirs:
@set -e; $(foreach d, $(SUBDIRS), $(MAKE) -C $(d);)
#.subdirs:
# $(foreach d, $(SUBDIRS), $(MAKE) -C $(d))
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),clobber)
ifdef DEPS
sinclude $(DEPS)
endif
endif
endif
# “$<”表示所有的依赖目标集(所有.c后缀文件),“$@”表示目标集(所有.o后缀文件)
$(OBJODIR)/%.o: %.c
@mkdir -p $(OBJODIR);
$(CC) $(if $(findstring $<,$(DSRCS)),$(DFLAGS),$(CFLAGS)) $(COPTS_$(*F)) -o $@ -c $<
$(OBJODIR)/%.d: %.c
@mkdir -p $(OBJODIR);
@echo DEPEND: $(CC) -M $(CFLAGS) $<
@set -e; rm -f $@; \
$(CC) -M $(CFLAGS) $< > $@.$$$$; \
sed 's,\($*\.o\)[ :]*,$(OBJODIR)/\1 $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
$(OBJODIR)/%.o: %.s
@mkdir -p $(OBJODIR);
$(CC) $(CFLAGS) -o $@ -c $<
$(OBJODIR)/%.d: %.s
@mkdir -p $(OBJODIR); \
set -e; rm -f $@; \
$(CC) -M $(CFLAGS) $< > $@.$$$$; \
sed 's,\($*\.o\)[ :]*,$(OBJODIR)/\1 $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
$(OBJODIR)/%.o: %.S
@mkdir -p $(OBJODIR);
$(CC) $(CFLAGS) -D__ASSEMBLER__ -o $@ -c $<
$(OBJODIR)/%.d: %.S
@mkdir -p $(OBJODIR); \
set -e; rm -f $@; \
$(CC) -M $(CFLAGS) $< > $@.$$$$; \
sed 's,\($*\.o\)[ :]*,$(OBJODIR)/\1 $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
$(foreach lib,$(GEN_LIBS),$(eval $(call ShortcutRule,$(lib),$(LIBODIR))))
$(foreach image,$(GEN_IMAGES),$(eval $(call ShortcutRule,$(image),$(IMAGEODIR))))
$(foreach bin,$(GEN_BINS),$(eval $(call ShortcutRule,$(bin),$(BINODIR))))
$(foreach lib,$(GEN_LIBS),$(eval $(call MakeLibrary,$(basename $(lib)))))
$(foreach image,$(GEN_IMAGES),$(eval $(call MakeImage,$(basename $(image)))))
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
############################################
# 设置头文件路径
# 把主目录下的 include头文件包含进来
INCLUDES := $(INCLUDES) -I $(PDIR)include -I $(PDIR)include/$(TARGET) -I $(PDIR)driver_lib/include
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
app下的Makefile
下面我们就来hack一下app/Makefile
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of object file images to be generated ()
# GEN_BINS - list of binaries to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
#############################################################
TARGET = eagle
#############################################################
# 选择debug工程还是release
#FLAVOR = release
FLAVOR = debug
#EXTRA_CCFLAGS += -u
ifndef PDIR # {
GEN_IMAGES= eagle.app.v6.out
GEN_BINS= eagle.app.v6.bin
SPECIAL_MKTARGETS=$(APP_MKTARGETS)
#############################################################
# 子目录,如果你在app里新建了一个源文件目录,需要添加到这里
# 否则不会编译未添加的目录的源文件!
SUBDIRS= \
user \
driver
# 比如添加了一个 network 文件夹,则:
#SUBDIRS= \
# user \
# driver \
# network
endif # } PDIR
APPDIR = .
LDDIR = ../ld
CCFLAGS += -Os
TARGET_LDFLAGS = \
-nostdlib \
-Wl,-EL \
--longcalls \
--text-section-literals
#############################################################
# 根据 FLAVOR 变量选择优化等级
# -O0不优化,-O2中级优化
ifeq ($(FLAVOR),debug)
TARGET_LDFLAGS += -g -O2
endif
ifeq ($(FLAVOR),release)
TARGET_LDFLAGS += -g -O0
endif
#############################################################
# 添加静态链接库,对应前面 SUBDIRS 变量设置的目录
# 每一个文件夹里的源代码先编译成对应的静态库,然后在进行链接
# 所以如果添加新的源码文件夹,也需要在这里添加对应的静态库!
COMPONENTS_eagle.app.v6 = \
user/libuser.a \
driver/libdriver.a
# 比如添加network:
#COMPONENTS_eagle.app.v6 = \
# user/libuser.a \
# driver/libdriver.a \
# network/libnetwork.a
#############################################################
# 链接参数
# 参考lib文件夹里的静态链接库
# 比如,如果需要使用smartconfig接口
# 只需要添加 -lsmartconfig 即可。
LINKFLAGS_eagle.app.v6 = \
-L../lib \
-nostdlib \
-T$(LD_FILE) \
-Wl,--no-check-sections \
-Wl,--gc-sections \
-u call_user_start \
-Wl,-static \
-Wl,--start-group \
-lc \
-lgcc \
-lhal \
-lphy \
-lpp \
-lnet80211 \
-llwip \ # lwip静态库,可见ESP8266使用的是lwip协议栈
-lwpa \
-lcrypto \
-lmain \ # main静态库
-ljson \ # JSON静态库
-lupgrade \ # OTP在线升级相关库
-lssl \ # 安全链接SSL静态库
-lpwm \ # 有关PWM的静态库
-lsmartconfig \ # 快速连接smartconfig
$(DEP_LIBS_eagle.app.v6)\
-Wl,--end-group
DEPENDS_eagle.app.v6 = \
$(LD_FILE) \
$(LDDIR)/eagle.rom.addr.v6.ld
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#UNIVERSAL_TARGET_DEFINES = \
# Other potential configuration flags include:
# -DTXRX_TXBUF_DEBUG
# -DTXRX_RXBUF_DEBUG
# -DWLAN_CONFIG_CCX
CONFIGURATION_DEFINES = -DICACHE_FLASH
DEFINES += \
$(UNIVERSAL_TARGET_DEFINES) \
$(CONFIGURATION_DEFINES)
DDEFINES += \
$(UNIVERSAL_TARGET_DEFINES) \
$(CONFIGURATION_DEFINES)
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
#############################################################
# 把当前目录下的 include 文件夹包含进来
INCLUDES := $(INCLUDES) -I $(PDIR)include
# 把子目录包含进来
PDIR := ../$(PDIR)
# 把子目录下的Makefile加进来
# sinclude:以忽略错误的方式执行
sinclude $(PDIR)Makefile
.PHONY: FORCE
FORCE:
app/user/
下的Makefile
由app/Makefile
可知,Makefile最后会把子目录下的Makefile加进来,而且好像还添加了user/libuser.a
和driver/libdriver.a
,但是这些.a
文件怎么生成我们还不知道。
所以我们来看下app/user/
下的Makefile做了一些什么事情。
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
# 仔细阅读上面英文即可知道是什么意思了
# 下面语句即是生成 libuser.a
ifndef PDIR
GEN_LIBS = libuser.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
# 查找头文件时,进入当前文件夹的 include 文件夹进行查找(如果有)
INCLUDES := $(INCLUDES) -I $(PDIR)include
# 在当前文件夹的进行查找
INCLUDES += -I ./
# 在主目录下的 include/ets/ 文件夹进行查找
# (实际上本人都没有找到 include/ets/ 文件夹)
INCLUDES += -I ../../include/ets
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
如何新添源码文件夹
- 在
app/
目录下新建一个文件夹,比如叫network
; - 然后把
app/user/Makefile
文件拷贝过去,修改下面语句:
ifndef PDIR
GEN_LIBS = libuser.a
endif
改为:
ifndef PDIR
GEN_LIBS = libnetwork.a
endif
- 然后编辑
app/Makefile
,给SUBDIRS
变量和COMPONENTS_eagle.app.v6
添加对应的文件名称即可(app/Makefile
代码分析小节已经有说明);
# ...
SUBDIRS= \
user \
driver
# ...
COMPONENTS_eagle.app.v6 = \
user/libuser.a \
driver/libdriver.a
添加network:
# ...
SUBDIRS= \
user \
driver \
network
# ...
COMPONENTS_eagle.app.v6 = \
user/libuser.a \
driver/libdriver.a \
network/libnetwork.a
- 最后编译的时候,
network
文件夹下的源码即会被编译进来了。