Chinaunix首页 | 论坛 | 博客
  • 博客访问: 479518
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-21 09:30
文章分类

全部博文(100)

文章存档

2017年(1)

2016年(16)

2015年(83)

我的朋友

分类: 嵌入式

2015-10-12 11:45:33

这是象棋小子根据u-boot的Makefile制作的简易Makefile模板,简单易用,本文通过分析该Makefile,帮助理解u-boot的Makefile。

首先看文件结构图:
  1. .
  2. ├── apps
  3. │   ├── main.c
  4. │   └── Makefile
  5. ├── config.mk
  6. ├── Makefile
  7. ├── rules.mk
  8. ├── start_code
  9. │   ├── Exception.c
  10. │   ├── Exception.h
  11. │   ├── LowLevelInit.S
  12. │   ├── Makefile
  13. │   ├── MMU.c
  14. │   ├── MMU.h
  15. │   ├── NAND.c
  16. │   ├── NAND.h
  17. │   ├── s3c2416.h
  18. │   ├── s3c2416.lds
  19. │   └── s3c2416.S
  20. └── tree.txt

  21. 2 directories, 17 files
/目录下有config.mk Makefile rules.mk三个文件,

config.mk主要配置编译选项,该文件被所有Makefile所包含:

  1. TEXT_BASE=0x30000000
  2. export TEXT_BASE

  3. CROSS_COMPILE = arm-linux-
  4. export CROSS_COMPILE

  5. DEBUG = n

  6. AS = $(CROSS_COMPILE)as
  7. LD = $(CROSS_COMPILE)ld
  8. CC = $(CROSS_COMPILE)gcc
  9. CPP = $(CC) -E
  10. AR = $(CROSS_COMPILE)ar
  11. NM = $(CROSS_COMPILE)nm
  12. STRIP = $(CROSS_COMPILE)strip
  13. OBJCOPY = $(CROSS_COMPILE)objcopy
  14. OBJDUMP = $(CROSS_COMPILE)objdump
  15. export    AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP

  16. LDSCRIPT = $(TOPDIR)/start_code/s3c2416.lds
  17. LIBS     := -lc
  18. LDFLAGS := -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(LIBS)

  19. AFLAGS :=

  20. ARFLAGS := cr

  21. CFLAGS := -Wall -Wstrict-prototypes
  22. CFLAGS += -I $(TOPDIR)/start_code -I $(TOPDIR)/apps
  23. ifeq ($(DEBUG), y)
  24. CFLAGS += -g
  25. else
  26. CFLAGS += -O2
  27. endif

  28. export LDFLAGS AFLAGS CFLAGS ARFLAGS

  29. %.o: %.S
  30.     @echo "assembling $<..."
  31.     @$(CC) $(AFLAGS) -c -o $@ $<
  32. %.o: %.s
  33.     @echo "assembling $<..."
  34.     @$(CC) $(AFLAGS) -c -o $@ $<
  35. %.o: %.c
  36.     @echo "compiling $<..."
  37.     @$(CC) $(CFLAGS) -c -o $@ $<
  38. %.o: %.C
  39.     @echo "compiling $<..."
  40.     @$(CC) $(CFLAGS) -c -o $@ $<
再来看顶层Makefile,顶层Makefile主要是定义编译目标,包括需要编译的目录:


  1. #
  2. # Copyright (C) Dinghong Huang(1048272975@qq.com)
  3. #
  4. # See file CREDITS for list of people who contributed to this
  5. # project.
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 2 of
  10. # the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. # MA 02111-1307 USA
  21. #

  22. TOPDIR        := $(shell pwd)
  23. export TOPDIR

  24. SUBDIRS = $(TOPDIR)/start_code $(TOPDIR)/apps

  25. include config.mk

  26. FOLDERS    := $(notdir $(SUBDIRS))
  27. LIBS    := $(addsuffix .a, $(addprefix /lib, $(FOLDERS)))
  28. __LIBS    := $(join $(SUBDIRS), $(LIBS))

  29. ALL = s3c2416.bin s3c2416.dis s3c2416.srec

  30. .PHONY: all clean depend $(SUBDIRS)          /* 声明这些目标不需要判断依赖文件的时间戳,例如当执行make all时,每次都会执行all:下面的命令,而不用判断依赖文件是否被修改(但要求依赖文件必须存在) */

  31. all: $(ALL)
  32. s3c2416.hex: s3c2416.elf
  33.     @echo "creating hex file $@ from $<..."
  34.     @$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@

  35. s3c2416.srec: s3c2416.elf
  36.     @echo "creating srec file $@ from $<..."
  37.     @$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@

  38. s3c2416.bin: s3c2416.elf
  39.     @echo "creating bin file $@ from $<..."
  40.     @$(OBJCOPY) ${OBJCFLAGS} -R .mmudata -S -O binary $< $@    

  41. s3c2416.dis: s3c2416.elf
  42.     @echo "creating dis file $@ from $<..."
  43.     @$(OBJDUMP) -d $< > $@

  44. s3c2416.elf: depend $(SUBDIRS) $(LDSCRIPT)
  45.     @echo "Linking..."
  46.     @$(LD) $(LDFLAGS) --start-group $(__LIBS) --end-group -Map s3c2416.map -o s3c2416.elf

  47. depend:
  48.     @for dir in $(SUBDIRS); do $(MAKE) --no-print-directory -C $$dir _depend; done

  49. $(SUBDIRS):
  50.     @$(MAKE) -C $@ all

  51. clean:
  52.     rm -f s3c2416.elf s3c2416.map s3c2416.srec s3c2416.bin s3c2416.dis
  53.     @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目标文件:

  1. .PHONY: _depend
  2. _depend: .depend
  3. .depend: Makefile $(TOPDIR)/config.mk $(SRCS)
  4.     @rm -f $@
  5.     @for f in $(SRCS); do \
  6.         g=`basename $$f | sed -e 's/\(.*\)\.\w/\1.o/'`; \
  7.         $(CC) -M $(CFLAGS) -MQ $$g $$f >> $@ ; \
  8.     done
app目录下的makefile:

  1. include $(TOPDIR)/config.mk


  2. OBJS    = main.o

  3. SRCS    := $(OBJS:.o=.c)
  4. CURDIR    := $(shell pwd)
  5. FOLDER    := $(notdir $(CURDIR))
  6. LIB    := lib$(FOLDER).a

  7. .PHONY: all clean
  8. all: .depend $(LIB)

  9. $(LIB): $(OBJS)
  10.     @$(AR) $(ARFLAGS) $@ $(OBJS)

  11. clean:
  12.     rm -f .depend *.o $(LIB)


  13. #########################################################################

  14. # defines $(obj).depend target
  15. include $(TOPDIR)/rules.mk

  16. sinclude .depend

  17. #########################################################################

start_code目录下的Makefile:

  1. include $(TOPDIR)/config.mk

  2. SOBJS    := s3c2416.o LowLevelInit.o
  3. COBJS    := MMU.o NAND.o Exception.o

  4. SRCS    := $(SOBJS:.o=.S) $(COBJS:.o=.c)
  5. OBJS    := $(SOBJS) $(COBJS)
  6. CURDIR    := $(shell pwd)
  7. FOLDER    := $(notdir $(CURDIR))
  8. LIB    := lib$(FOLDER).a

  9. .PHONY: all clean
  10. all: .depend $(LIB)

  11. $(LIB): $(OBJS)
  12.     @$(AR) $(ARFLAGS) $@ $(OBJS)

  13. clean:
  14.     rm -f .depend *.o $(LIB)


  15. #########################################################################

  16. # defines $(obj).depend target
  17. include $(TOPDIR)/rules.mk

  18. sinclude .depend

  19. #########################################################################



参考:
http://blog.csdn.net/huang20083200056/article/details/24305373



阅读(1481) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~