uboot顶层config.mk分析

uboot顶层目录中的config.mk定义了确定了当前执行makefile所对应的源文件目录、目标文件目录,编译的程序编译、连接的选项,以及目标文件生成的规则等等。它被包含在顶层的makefile以及各个子目录的makefile中,可以说算是一个全局的makefile包含文件。

ifneq ($(OBJTREE),$(SRCTREE))                         //判断目标目录和源文件目录是否一样,如果不一样下边的有效

ifeq ($(CURDIR),$(SRCTREE))                             //判断当前目录是否是顶层目录
dir :=                                                                //如果是,那么dir为空
else
dir := $(subst $(SRCTREE)/,,$(CURDIR))             //如果不是,那么dir为$(CURDIR)去掉$(SRCTREE)的部分
endif

obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/)  //如果dir不为空(当前目录不是顶层目录),那么obj等于

//$(OBJTREE)/$(dir)/,否则(当前目录是顶层目录)等于$(OBJTREE)/

src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/) //如果dir不为空(当前目录不是顶层目录),那么obj等于

//$(SRCTREE)/$(dir)/,否则(当前目录是顶层目录)等于$(SRCTREE)/

$(shell mkdir -p $(obj))                                        //创建obj目录
else
obj :=
src :=
endif

//想要了解如何实现编译生成的目标文件目录与源文件目录不同,可以参考笔者的这篇

//文章实现目标文件与源码分开的makefile测试实验,附有实验源码。

# clean the slate ...                                            //先将这三个变量清零
PLATFORM_RELFLAGS = 
PLATFORM_CPPFLAGS =                                     //这个变量后边会被CPPFLAGS调用
PLATFORM_LDFLAGS =

#
# When cross-compiling on NetBSD, we have to define __PPC__ or else we
# will pick up a va_list declaration that is incompatible with the
# actual argument lists emitted by the compiler.
#
# [Tested on NetBSD/i386 1.5 + cross-powerpc-netbsd-1.3]

ifeq ($(ARCH),ppc)
ifeq ($(CROSS_COMPILE),powerpc-netbsd-)
PLATFORM_CPPFLAGS+= -D__PPC__ 
endif
ifeq ($(CROSS_COMPILE),powerpc-openbsd-)
PLATFORM_CPPFLAGS+= -D__PPC__
endif
endif

ifeq ($(ARCH),arm)
ifeq ($(CROSS_COMPILE),powerpc-netbsd-)
PLATFORM_CPPFLAGS+= -D__ARM__ 
endif
ifeq ($(CROSS_COMPILE),powerpc-openbsd-)
PLATFORM_CPPFLAGS+= -D__ARM__
endif
endif

ifeq ($(ARCH),blackfin)
PLATFORM_CPPFLAGS+= -D__BLACKFIN__ -mno-underscore
endif

//以上这部分,smdk2410定义的变量不满足上述条件,所以都无效

//以下这部分,根据make *_config生成的$(OBJTREE)/include/config.mk中相关变量的定义,选择适合开发板的config.mk

smdk2410生成的include/config.mk内容:

ARCH   = arm
CPU = arm920t
BOARD = smdk2410
SOC = s3c24x0

ifdef ARCH

sinclude $(TOPDIR)/$(ARCH)_config.mk # include architecture dependend rules

//此处包含$(TOPDIR)/$(ARCH)_config.mk;smdk2410的ARCH=ARM,所以包含源程序顶层目录中的arm_config.mk

arm_config.mk内容:

PLATFORM_CPPFLAGS += -DCONFIG_ARM -D__ARM__

endif

ifdef CPU
sinclude $(TOPDIR)/cpu/$(CPU)/config.mk # include CPU specific rules

//此处包含$(TOPDIR)/cpu/$(CPU)/config.mk;smdk2410的CPU=arm920t,所以包含源程序顶层目录中的cpu/arm920t/config.mk

cpu/arm920t/config.mk内容:

PLATFORM_RELFLAGS += -fno-strict-aliasing  -fno-common -ffixed-r8 \
-msoft-float PLATFORM_CPPFLAGS += -march=armv4
# =========================================================================
#
# Supply options according to compiler version
#
# =========================================================================
PLATFORM_CPPFLAGS +=$(call cc-option,-mapcs-,-mabi=apcs-gnu)
PLATFORM_RELFLAGS +=$(call cc-option,-mshort-load-bytes,$(call cc-option,-malignment-traps,))

endif
ifdef SOC
sinclude $(TOPDIR)/cpu/$(CPU)/$(SOC)/config.mk # include SoC specific rules

//此处包含$(TOPDIR)/cpu/$(CPU)/$(SOC)/config.mk;smdk2410的SOC=s3c24x0,所以包含源程序顶层目录中

//的cpu/arm920t/s3c24x0/config.mk,但这个目录中不存在config.mk文件,就忽略

endif
ifdef VENDOR                                    //smdk2410的include/config.mk没有这个变量的定义
BOARDDIR = $(VENDOR)/$(BOARD)
else                                                  //所以else分支有效
BOARDDIR = $(BOARD)                    //BOARDDIR = $(BOARD) ;对于smdk2410,BOARDDIR = smdk2410
endif
ifdef BOARD 
sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk # include board specific rules

//此处包含$(TOPDIR)/board/$(BOARDDIR)/config.mk;smdk2410的SOC=s3c24x0,所以

//包含源程序顶层目录中的board/smdk2410/config.mk

board/smdk2410/config.mk内容:

TEXT_BASE = 0x33F80000

endif

#########################################################################

CONFIG_SHELL := $(shell if [ -x "$ $BASH" ]; then echo $ $BASH; \

//[ -x "$file" ] 判断$file是否存在且有可执行权限 

else if [ -x /bin/bash ]; then echo /bin/bash; \
else echo sh; fi ; fi)

//定义CONFIG_SHELL变量,笔者用linux,此变量等于/bin/bash

ifeq ($(HOSTOS)-$(HOSTARCH),darwin-ppc)

//笔者用的笔记本cpu是酷睿双核,在虚拟机上搭建的linux操作系统,所以$(HOSTOS)-$(HOSTARCH)=linux-i386
HOSTCC = cc
else
HOSTCC = gcc

//else分支有效,HOSTCC = gcc
endif
HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer 
HOSTSTRIP = strip

//定义了两个变量HOSTCFLAGS、HOSTSTRIP

#########################################################################
#
# Option checker (courtesy linux kernel) to ensure
# only supported compiler options are used
#
cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

//英文注释的意思好像是说检查编译选项对于当前编译器是否存在,确保仅仅只有支持的编译器选项被使用

#
# Include the make variables (CC, etc...)
#
AS = $(CROSS_COMPILE)as     //定义arm-linux-as
LD = $(CROSS_COMPILE)ld     //定义arm-linux-ld
CC = $(CROSS_COMPILE)gcc   //定义编译所用的编译器,CROSS_COMPILE在顶层Makefile中已经定义,

//对于smdk2410开发板,这里是arm-linux-gcc 
CPP = $(CC) -E                      //预处理后就停止,不进行编译

AR = $(CROSS_COMPILE)ar                          //定义arm-linux-ar
NM = $(CROSS_COMPILE)nm                        //定义arm-liux-nm
STRIP = $(CROSS_COMPILE)strip                  //定义arm-linux-strip
OBJCOPY = $(CROSS_COMPILE)objcopy        //定义arm-linu-objcopy
OBJDUMP = $(CROSS_COMPILE)objdump      //定义amr-linux-objdump
RANLIB = $(CROSS_COMPILE)RANLIB           //定义arm-linux-RANLIB

ifneq (,$(findstring s,$(MAKEFLAGS)))           //MAKEFLAGS是否包含s
ARFLAGS = cr                                             //是
else
ARFLAGS = crv                                            //不是,就在变量ARFLAGS上加v选项
endif
RELFLAGS= $(PLATFORM_RELFLAGS)            //定义RELFLAGS 
DBGFLAGS= -g # -DDEBUG                         //定义DBGFLAGS,即编译选项
OPTFLAGS= -Os #-fomit-frame-pointer         //定义OPTFLAGS

//以下这部分是确定连接脚本

ifndef LDSCRIPT                                           //没有定义变量 LDSCRIPT
#LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds.debug
ifeq ($(CONFIG_NAND_U_BOOT),y)                //也没有定义CONFIG_NAND_U_BOOT
LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot-nand.lds
else                                                            //所以分支有效
LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds

//连接脚本是$(TOPDIR)/board/$(BOARDDIR)/u-boot.lds ;对于smdk2410,连接脚本是board/smdk2410/u-boot.lds
endif
endif
OBJCFLAGS += --gap-fill=0xff                                           //定义变量OBJCFLAGS

gccincdir := $(shell $(CC) -print-file-name=include)              //定义变量gccincdir

CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS) \   //定义变量CPPFLAGS
-D__KERNEL__ -DTEXT_BASE=$(TEXT_BASE) \

ifneq ($(OBJTREE),$(SRCTREE))                                       //判断目标目录与源文件目录是否相同
CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include

//不相同CPPFLAGS要加选项 -I$(OBJTREE)/include2 -I$(OBJTREE)/include 
endif

CPPFLAGS += -I$(TOPDIR)/include                                    //相同CPPFLAGS要加选项 -I$(TOPDIR)/include 
CPPFLAGS += -fno-builtin -ffreestanding -nostdinc \  
-isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS)           //CPPFLAGS还要再加一些选项

ifdef BUILD_TAG                                                              //如果定义了BUILD_TAG ,smdk2410没有定义
CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes \
-DBUILD_TAG='"$(BUILD_TAG)"'
else                                                                                //所以分支有效
CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes            //定义变量CFLAGS
endif

# avoid trigraph warnings while parsing pci.h (produced by NIOS gcc-2.9)
# this option have to be placed behind -Wall -- that's why it is here
ifeq ($(ARCH),nios)                                                          //cpu架构不是nios,此部分无效
ifeq ($(findstring 2.9,$(shell $(CC) --version)),2.9)
CFLAGS := $(CPPFLAGS) -Wall -Wno-trigraphs
endif
endif

# $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format>
# option to the assembler.
AFLAGS_DEBUG :=                                                        //定义变量AFLAGS_DEBUG,好像是汇编编译调试选项 

# turn jbsr into jsr for m68k
ifeq ($(ARCH),m68k)                                                       //cpu架构不是m68k,此部分无效
ifeq ($(findstring 3.4,$(shell $(CC) --version)),3.4)
AFLAGS_DEBUG := -Wa,-gstabs,-S
endif
endif

AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS)                               //定义变量AFLAGS

LDFLAGS += -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS) //定义连接选项LDFLAGS 

# Location of a usable BFD library, where we define "usable" as
# "built for ${HOST}, supports ${TARGET}". Sensible values are
# - When cross-compiling: the root of the cross-environment
# - Linux/ppc (native): /usr
# - NetBSD/ppc (native): you lose ... (must extract these from the
# binutils build directory, plus the native and U-Boot include
# files don't like each other)
#
# So far, this is used only by tools/gdb/Makefile.

ifeq ($(HOSTOS)-$(HOSTARCH),darwin-ppc) //$(HOSTOS)-$(HOSTARCH)=linux-i386,此部分无效
BFD_ROOT_DIR = /usr/local/tools
else
ifeq ($(HOSTARCH),$(ARCH))                      //如果主机架构与目标板cpu架构相同;但是显然不一样,此部分无效
# native
BFD_ROOT_DIR = /usr
else
#BFD_ROOT_DIR = /LinuxPPC/CDK # Linux/i386
#BFD_ROOT_DIR = /usr/pkg/cross # NetBSD/i386
BFD_ROOT_DIR = /opt/powerpc                  //分支有效,定义变量BFD_ROOT_DIR = /opt/powerpc
endif
endif

ifeq ($(PCI_CLOCK),PCI_66M)                     //没有定义变量PCI_CLOCK,此部分无效
CFLAGS := $(CFLAGS) -DPCI_66M
endif

#########################################################################

export CONFIG_SHELL HPATH HOSTCC HOSTCFLAGS CROSS_COMPILE \
AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP \
MAKE
export TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS CPPFLAGS CFLAGS AFLAGS

//将一些列编译、连接、生成库等等的选项导出

#########################################################################

ifndef REMOTE_BUILD                                  //看是否目标目录与源码目录不同,REMOTE_BUILD在顶层目录Makefile中定义

//目标目录与源码目录相同

%.s: %.S                   
$(CPP) $(AFLAGS) -o $@ $<
%.o: %.S
$(CC) $(AFLAGS) -c -o $@ $<
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

//定义目标文件(*.o)以及中间文件(*.s)的生成规则

else

//目标目录与源码目录不相同

$(obj)%.s: %.S
$(CPP) $(AFLAGS) -o $@ $<                                                                                            
$(obj)%.o: %.S
$(CC) $(AFLAGS) -c -o $@ $<
$(obj)%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
endif

//定义目标文件(*.o)以及中间文件(*.s)的生成规则

#########################################################################

最后,附上几个比较重要变量的打印值

AFLAGS:
-D__ASSEMBLY__ -g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float -malignment-traps -D__KERNEL__ -DTEXT_BASE=0x33F80000 -I/work/u-boot-1.1./include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/crosstools_3..5_softfloat/gcc-3.4.-glibc-2.3./arm-linux/lib/gcc/arm-linux/3.4./include -pipe -DCONFIG_ARM -D__ARM__ -march=armv4 -mapcs-

 

CFLAGS:
-g -Os -fno-strict-aliasing -fno-common -ffixed-r8 -msoft-float -malignment-traps -D__KERNEL__ -DTEXT_BASE=0x33F80000 -I/work/u-boot-1.1./include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/crosstools_3..5_softfloat/gcc-3.4.-glibc-2.3./arm-linux/lib/gcc/arm-linux/3.4./include -pipe -DCONFIG_ARM -D__ARM__ -march=armv4 -mapcs- -Wall -Wstrict-prototypes

  

 

上一篇:springMVC: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config


下一篇:CSS ::before 和 ::after 伪元素另类用法