config.mk 文件详细分析

Posted linu-later

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了config.mk 文件详细分析相关的知识,希望对你有一定的参考价值。

config.mk 文件详细分析

网友有这么一种说法

“u-boot根目录下自带一个config.mk文件,应 该说这才是真正的Makefile,以上介绍的两个脚本Makefile和mkconfig完成了环境配置之后,在该文件中才定义具体的编译规则,所以你会发现在各个子模块(board、 cpu、lib_xxx、net、disk...)目录中的Makefile第一句就是:include $(TOPDIR)/config.mk。”

#
# (C) Copyright 2000-2006
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# 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
#
#########################################################################

ifneq ($(OBJTREE),$(SRCTREE))     #如果目标存放目录不等于源码目录(UBOOT顶层目录)

ifeq ($(CURDIR),$(SRCTREE))          #如果当前目录等于源码目录(UBOOT顶层目录) 
    dir :=                                   #那么我们让dir为空 
else                                   #否则 
    dir := $(subst $(SRCTREE)/,,$(CURDIR))     #把$(CURDIR)中的$(SRCTREE)/替换称空,然后赋给dir 
                                                 #例如:当前目录如果为/u-boot-1.1.6/include/(源码目录为/u-boot-1.1.6/), 
                                                 #则把/u-boot-1.1.6/include/中的/u-boot-1.1.6/替换为空,成为/include/ 
                                                 #例如:当前目录如果为/u-boot-1.1.6/include/(源码目录为/u-boot-1.1.6/), 
                                                 #则把/u-boot-1.1.6/include/中的/u-boot-1.1.6/替换为空,成为/include/ 
endif 
obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/)      #如果dir不为空,则把$(OBJTREE)/$(dir)/赋给obj,否则把直接把$(OBJTREE)/赋给obj 
src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/)      #如果src不为空,则把$(SRCTREE)/$(dir)/赋给src,否则把$(SRCTREE)/赋给src 
$(shell mkdir -p $(obj))     # 通过一个shell函数创建obj路径

else          # 注意,这里的else对应上面的ifneq,这个else意思是目标存放目录就等于uboot顶层目录的时候,执行下面两行

obj :=          #obj为空 
src :=

endif

# clean the slate ...     #清除三个不同的标记,下面会用到
PLATFORM_RELFLAGS =
PLATFORM_CPPFLAGS =
PLATFORM_LDFLAGS =

#
# When cross-compiling on NetBSD, we have to define __PPC__ or else we  #当我们在NetBSD上进行交叉编译的时候,我们得定义__PPC__
# will pick up a va_list declaration that is incompatible with the                     #否则我们得做一个 va_list 声明,这个声明是和实际的参数
# 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

ifdef     ARCH     #指定预编译体系结构选项
    sinclude $(TOPDIR)/$(ARCH)_config.mk     # include architecture dependend rules
# 包含体系结构依赖规则,$(TOPDIR)/$(ARCH)_config.mk 即/u-boot-1.1.6/arm920t_config.mk
endif

#如果您希望对不存在且不能重新创建的makefile文件进行忽略,并且不产生错误信息,则使用-include指令代替include指令,格式如下:
#-include filenames...
#这种指令的作用就是对于任何不存在的makefile文件都不会产生错误(即使警告信息也不会产生)。如果希望保持和其它版本的make兼容,
#使用sinclude指令代替-include指令。


ifdef     CPU          #定义编译时对齐,浮点等选项
    sinclude $(TOPDIR)/cpu/$(CPU)/config.mk     # include  CPU     specific rules    
                                  # 包含特定的CPU的依赖规则, 即/u-boot-1.1.6/cpu/arm920t/config.mk
endif


ifdef     SOC          #$(TOPDIR)/cpu/$(CPU)/$(SOC)/config.mk 这个文件确实不存在,$(TOPDIR)/cpu/$(CPU)/目录下倒是有一个config.mk
    sinclude $(TOPDIR)/cpu/$(CPU)/$(SOC)/config.mk     # include  SoC     specific rules     #包含特定的片上系统的依赖规则
endif


ifdef     VENDOR
    BOARDDIR = $(VENDOR)/$(BOARD)
else
    BOARDDIR = $(BOARD)
endif


ifdef     BOARD
    #对各个目标文件进行链接时的内存基地址(定义在/u-boot-1.1.6/board/smdk2410/config.mk)TEXT_BASE = 0x33F80000,相当重要!!!!
    #这里可以参考一篇文章,这篇文章对uboot内存分配做了比较详细的解释。点击连接:
    #http://zqwt.012.blog.163.com/blog/static/120446842010323113546103/
    sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk     # include board specific rules
endif

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

CONFIG_SHELL     := $(shell if [ -x "

BASH"];thenechoBASH"];thenecho
BASH;
             else if [ -x /bin/bash ]; then echo /bin/bash;
             else echo sh; fi ; fi)
##################################################################################################################
# 这里把test命令好好整一下:
# 用途:评估条件表达式。
# 语法:test Expression 或 [ Expression ]
# 描述:test 命令用于评估 Expression 参数, 如果表达式值为 True,返回一个零(true)退出值。
#       否则, test 命令返回一个非零(false)退出值。 如果没有参数,test 命令也返回一个非零退出值。
#注:    1、在命令的第二种格式中,[ ](方括号)必须被空白包围。
#          2、必须在 C shell 中对文件名进行显式测试。文件名替换(全局)会导致 shell 脚本退出。
# Expression 参数指的是一个需要被检查为 true 或 false 的条件的语句。下列函数可用来构造此参数:
# -x FileName 意思是如果所指定的FileName存在且可执行,则返回一个 true 退出值。
# 如果所指定的文件存在且是一个目录,true 退出值表示当前进程被允许在目录中搜索。
##################################################################################################################
ifeq ($(HOSTOS)-$(HOSTARCH),darwin-ppc)
    HOSTCC          = cc
else
    HOSTCC          = gcc
endif

 


HOSTCFLAGS     = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
HOSTSTRIP     = strip

#########################################################################
#
# Option checker (courtesy linux kernel) to ensure
# only supported compiler options are used
#
# 这是一个选项检查器,以确保我们只使用所支持的编译器
#
# 这一句实际上使用了一个shell函数,本函数使用了if-then语句,检测条件分别为"-S, -o, -xc",也用到了重定向,具体含义可以自己分析
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...)
# 这里$(CROSS_COMPILE)即你的交叉编译器安装目录
#
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
RANLIB     = $(CROSS_COMPILE)RANLIB

# 这是这个脚本所做的第二件事:定义AR、调试、优化、预处理、C编译器、链接选项
ifneq (,$(findstring s,$(MAKEFLAGS)))     #如果函数$(findstring s,$(MAKEFLAGS))不为空,也就是说能从$(MAKEFLAGS)中找到字符s
    ARFLAGS = cr     #则定义AR选项
else
    ARFLAGS = crv
endif


RELFLAGS= $(PLATFORM_RELFLAGS)
DBGFLAGS= -g # -DDEBUG     #定义调试选项
OPTFLAGS= -Os #-fomit-frame-pointer          #定义优化选项


ifndef LDSCRIPT
#LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds.debug

 

ifeq ($(CONFIG_NAND_U_BOOT),y)

    LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot-nand.lds

else

#依赖目标$(LDSCRIPT),即/u-boot-1.1.6/board/smdk2410/u-boot.lds,这个文件定义了链接时,各个目标文件是如何组织的

#可以参阅:http://zqwt.012.blog.163.com/blog/static/120446842010320101137932/

    LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds

endif

endif


OBJCFLAGS += --gap-fill=0xff

gccincdir := $(shell $(CC) -print-file-name=include)

CPPFLAGS := $(DBGFLAGS) $(OPTFLAGS) $(RELFLAGS)          
    -D__KERNEL__ -DTEXT_BASE=$(TEXT_BASE)          

ifneq ($(OBJTREE),$(SRCTREE))
    CPPFLAGS += -I$(OBJTREE)/include2 -I$(OBJTREE)/include
endif

CPPFLAGS += -I$(TOPDIR)/include
CPPFLAGS += -fno-builtin -ffreestanding -nostdinc      
    -isystem $(gccincdir) -pipe $(PLATFORM_CPPFLAGS)

ifdef BUILD_TAG
    CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes
    -DBUILD_TAG=‘"$(BUILD_TAG)"‘
else
    CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes
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)
    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 :=

# turn jbsr into jsr for m68k
ifeq ($(ARCH),m68k)
    ifeq ($(findstring 3.4,$(shell $(CC) --version)),3.4)
         AFLAGS_DEBUG := -Wa,-gstabs,-S
    endif
endif

AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS)

LDFLAGS += -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)     #定义链接选项
#指定了起始地址TEXT_BASE

# Location of a usable BFD library, where we define "usable" as          #BFD库的定位器,在此我们定义所谓的
# "built for ${HOST}, supports ${TARGET}".  Sensible values are          #"usable"为"built for ${HOST}, supports ${TARGET}".敏感的值
# - When cross-compiling: the root of the cross-environment            #是当交叉编译的时候:交叉环境变量的root
# - 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.                         #至今,这仅仅被tools/gdb/Makefile使用

ifeq ($(HOSTOS)-$(HOSTARCH),darwin-ppc)                                        #如果主机操作系统和硬件体系结构为darwin-ppc
    BFD_ROOT_DIR =          /usr/local/tools                                        #那么.............
else
    ifeq ($(HOSTARCH),$(ARCH))                                                       #如果主机体系结构为arm920t
    # native
         BFD_ROOT_DIR =          /usr                                                       #那么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
    endif
endif

ifeq ($(PCI_CLOCK),PCI_66M)
    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

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

else

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

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

以上是关于config.mk 文件详细分析的主要内容,如果未能解决你的问题,请参考以下文章

主Makefile配置文件config.mk的分析

嵌入式开发(S5PV210)——u-boot的顶层config.mk分析

u-boot顶层目录config.mk分析

子目录Makefile分析

mosquitto---config.mk

uboot主Makefile之11——源码目录下mkconfig和config.mk文件的区别