这是象棋小子根据u-boot的Makefile制作的简易Makefile模板,简单易用,本文通过分析该Makefile,帮助理解u-boot的Makefile。
首先看文件结构图:
-
.
-
├── apps
-
│ ├── main.c
-
│ └── Makefile
-
├── config.mk
-
├── Makefile
-
├── rules.mk
-
├── start_code
-
│ ├── Exception.c
-
│ ├── Exception.h
-
│ ├── LowLevelInit.S
-
│ ├── Makefile
-
│ ├── MMU.c
-
│ ├── MMU.h
-
│ ├── NAND.c
-
│ ├── NAND.h
-
│ ├── s3c2416.h
-
│ ├── s3c2416.lds
-
│ └── s3c2416.S
-
└── tree.txt
-
-
2 directories, 17 files
/目录下有config.mk Makefile rules.mk三个文件,
config.mk主要配置编译选项,该文件被所有Makefile所包含:
-
TEXT_BASE=0x30000000
-
export TEXT_BASE
-
-
CROSS_COMPILE = arm-linux-
-
export CROSS_COMPILE
-
-
DEBUG = n
-
-
AS = $(CROSS_COMPILE)as
-
LD = $(CROSS_COMPILE)ld
-
CC = $(CROSS_COMPILE)gcc
-
CPP = $(CC) -E
-
AR = $(CROSS_COMPILE)ar
-
NM = $(CROSS_COMPILE)nm
-
STRIP = $(CROSS_COMPILE)strip
-
OBJCOPY = $(CROSS_COMPILE)objcopy
-
OBJDUMP = $(CROSS_COMPILE)objdump
-
export AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP
-
-
LDSCRIPT = $(TOPDIR)/start_code/s3c2416.lds
-
LIBS := -lc
-
LDFLAGS := -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(LIBS)
-
-
AFLAGS :=
-
-
ARFLAGS := cr
-
-
CFLAGS := -Wall -Wstrict-prototypes
-
CFLAGS += -I $(TOPDIR)/start_code -I $(TOPDIR)/apps
-
ifeq ($(DEBUG), y)
-
CFLAGS += -g
-
else
-
CFLAGS += -O2
-
endif
-
-
export LDFLAGS AFLAGS CFLAGS ARFLAGS
-
-
%.o: %.S
-
@echo "assembling $<..."
-
@$(CC) $(AFLAGS) -c -o $@ $<
-
%.o: %.s
-
@echo "assembling $<..."
-
@$(CC) $(AFLAGS) -c -o $@ $<
-
%.o: %.c
-
@echo "compiling $<..."
-
@$(CC) $(CFLAGS) -c -o $@ $<
-
%.o: %.C
-
@echo "compiling $<..."
-
@$(CC) $(CFLAGS) -c -o $@ $<
再来看顶层Makefile,顶层Makefile主要是定义编译目标,包括需要编译的目录:
-
#
-
# Copyright (C) Dinghong Huang(1048272975@qq.com)
-
#
-
# See file CREDITS for list of people who contributed to this
-
# project.
-
#
-
# This program is free software; you can redistribute it and/or
-
# modify it under the terms of the GNU General Public License as
-
# published by the Free Software Foundation; either version 2 of
-
# the License, or (at your option) any later version.
-
#
-
# This program is distributed in the hope that it will be useful,
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-
# GNU General Public License for more details.
-
#
-
# You should have received a copy of the GNU General Public License
-
# along with this program; if not, write to the Free Software
-
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
-
# MA 02111-1307 USA
-
#
-
-
TOPDIR := $(shell pwd)
-
export TOPDIR
-
-
SUBDIRS = $(TOPDIR)/start_code $(TOPDIR)/apps
-
-
include config.mk
-
-
FOLDERS := $(notdir $(SUBDIRS))
-
LIBS := $(addsuffix .a, $(addprefix /lib, $(FOLDERS)))
-
__LIBS := $(join $(SUBDIRS), $(LIBS))
-
-
ALL = s3c2416.bin s3c2416.dis s3c2416.srec
-
-
.PHONY: all clean depend $(SUBDIRS) /* 声明这些目标不需要判断依赖文件的时间戳,例如当执行make all时,每次都会执行all:下面的命令,而不用判断依赖文件是否被修改(但要求依赖文件必须存在) */
-
-
all: $(ALL)
-
s3c2416.hex: s3c2416.elf
-
@echo "creating hex file $@ from $<..."
-
@$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@
-
-
s3c2416.srec: s3c2416.elf
-
@echo "creating srec file $@ from $<..."
-
@$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@
-
-
s3c2416.bin: s3c2416.elf
-
@echo "creating bin file $@ from $<..."
-
@$(OBJCOPY) ${OBJCFLAGS} -R .mmudata -S -O binary $< $@
-
-
s3c2416.dis: s3c2416.elf
-
@echo "creating dis file $@ from $<..."
-
@$(OBJDUMP) -d $< > $@
-
-
s3c2416.elf: depend $(SUBDIRS) $(LDSCRIPT)
-
@echo "Linking..."
-
@$(LD) $(LDFLAGS) --start-group $(__LIBS) --end-group -Map s3c2416.map -o s3c2416.elf
-
-
depend:
-
@for dir in $(SUBDIRS); do $(MAKE) --no-print-directory -C $$dir _depend; done
-
-
$(SUBDIRS):
-
@$(MAKE) -C $@ all
-
-
clean:
-
rm -f s3c2416.elf s3c2416.map s3c2416.srec s3c2416.bin s3c2416.dis
-
@for dir in $(SUBDIRS); do $(MAKE) -C $$dir clean; done
这里复习一下shell 中的$@等特殊变量,注意与Makefile的$@不一样
shell:
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
Makefile:
$@ 代表目标
$^ 代表所有的依赖对象
$< 代表第一个依赖对象
相对而言,rule.mk就很简单了,只是用来将.c或.s文件替换为.o目标文件:
-
.PHONY: _depend
-
_depend: .depend
-
.depend: Makefile $(TOPDIR)/config.mk $(SRCS)
-
@rm -f $@
-
@for f in $(SRCS); do \
-
g=`basename $$f | sed -e 's/\(.*\)\.\w/\1.o/'`; \
-
$(CC) -M $(CFLAGS) -MQ $$g $$f >> $@ ; \
-
done
app目录下的makefile:
-
include $(TOPDIR)/config.mk
-
-
-
OBJS = main.o
-
-
SRCS := $(OBJS:.o=.c)
-
CURDIR := $(shell pwd)
-
FOLDER := $(notdir $(CURDIR))
-
LIB := lib$(FOLDER).a
-
-
.PHONY: all clean
-
all: .depend $(LIB)
-
-
$(LIB): $(OBJS)
-
@$(AR) $(ARFLAGS) $@ $(OBJS)
-
-
clean:
-
rm -f .depend *.o $(LIB)
-
-
-
#########################################################################
-
-
# defines $(obj).depend target
-
include $(TOPDIR)/rules.mk
-
-
sinclude .depend
-
-
#########################################################################
start_code目录下的Makefile:
-
include $(TOPDIR)/config.mk
-
-
SOBJS := s3c2416.o LowLevelInit.o
-
COBJS := MMU.o NAND.o Exception.o
-
-
SRCS := $(SOBJS:.o=.S) $(COBJS:.o=.c)
-
OBJS := $(SOBJS) $(COBJS)
-
CURDIR := $(shell pwd)
-
FOLDER := $(notdir $(CURDIR))
-
LIB := lib$(FOLDER).a
-
-
.PHONY: all clean
-
all: .depend $(LIB)
-
-
$(LIB): $(OBJS)
-
@$(AR) $(ARFLAGS) $@ $(OBJS)
-
-
clean:
-
rm -f .depend *.o $(LIB)
-
-
-
#########################################################################
-
-
# defines $(obj).depend target
-
include $(TOPDIR)/rules.mk
-
-
sinclude .depend
-
-
#########################################################################
参考:
http://blog.csdn.net/huang20083200056/article/details/24305373
阅读(1450) | 评论(0) | 转发(0) |