Chinaunix首页 | 论坛 | 博客
  • 博客访问: 868338
  • 博文数量: 190
  • 博客积分: 7021
  • 博客等级: 少将
  • 技术积分: 1752
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 19:26
文章分类

全部博文(190)

文章存档

2014年(9)

2011年(32)

2010年(149)

我的朋友

分类: LINUX

2010-07-01 14:45:05

阅读tekk的mini2440的代码,做一下笔记:

首先了解清楚各个目录的问题,在u-boot中定义了各个目录,用于存放不用的文件,方便管理:

 Makefile中定义了源码及生成的目标文件存放的目录,目标文件存放目录BUILD_DIR可以通过make O=dir 指定。如果没有指定,则设定为源码顶层目录。一般编译的时候不指定输出目录,则BUILD_DIR为空。其它目录变量定义如下:

#OBJTREE和LNDIR为存放生成文件的目录,TOPDIR与SRCTREE为源码所在目录
OBJTREE  := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR))
SRCTREE  := $(CURDIR)
TOPDIR  := $(SRCTREE)
LNDIR  := $(OBJTREE)
export TOPDIR SRCTREE OBJTREE

因为我们在编译的时候一般不指定目标目录,因此OBJTREE,SRCTREE,TOPDIR,LNDIR其实都是一个u-boot的顶层目录

u-boot的编译过程包括两个部分:
1,make mini2440_config
2, make
对于第一部分的分析如下:
mini2440_config : unconfig
@$(MKCONFIG) $(@:_config=) arm arm920t mini2440 tekkamanninja s3c24x0
首先来分析unconfig:

unconfig::
 @rm -f $(obj)include/config.h $(obj)include/config.mk \
  $(obj)board/*/config.tmp $(obj)board/*/*/config.tmp

显然,执行# make mini2440_config时,先执行unconfig目标,注意不指定输出目标时,obj,src变量均为空(通常情况下我们不指定输出目录),unconfig下面的命令清理上一次执行make *_config时生成的头文件和makefile的包含文件。主要是include/config.h 和include/config.mk文件。

然后才执行命令

 @$(MKCONFIG) $(@:_config=) arm arm920t mini2440 tekkamanninja s3c24x0
MKCONFIG 是顶层目录下的mkcofig脚本文件,后面五个是传入的参数。

对于mini2440_config而言,mkconfig主要做三件事:

在include文件夹下建立相应的文件(夹)软连接,

#如果是ARM体系将执行以下操作:
#ln -s     asm-arm        asm  

#ln -s  arch-s3c24x0    asm-arm/arch 
#ln -s   proc-armv       asm-arm/proc

生成Makefile包含文件include/config.mk,内容很简单,定义了四个变量:

ARCH   = arm
CPU    = arm920t
BOARD  = mini2440

VENDOR = tekkamanninja
SOC    = s3c24x0

生成include/config.h头文件,只有两行:

/* Automatically generated - do not edit */
#include "configs/mini2440.h"

#include"asm/config.h"

mkconfig脚本文件的执行至此结束,继续分析Makefile剩下部分。

PS:

其中需要说明的是:mini2440文件夹在tekkamanninja文件夹下,而tekkamanninja文件夹在board文件夹下

其中mini2440文件夹中包括了6个文件:

(Makefile,flash.c,mini2440.c,nand_read.c,config.mk.lowlevel_init.S)


    现在来分析config.mk文件,在u-boot中包含了很多config.mk文件,这些文件很重要,包含了一些变量的定义和规则的说明。从第一个config.mk文件说起,这个文件是由在编译u-boot的第一步中生成的文件,在include/config.mk其中就包含了5个变量的定义,是由mkconfig脚本生成的:

  1. ARCH   = arm
  2. CPU    = arm920t
  3. BOARD  = mini2440
  4. VENDOR = tekkamanninja
  5. SOC    = s3c24x0
然后就是根目录下的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))
ifeq ($(CURDIR),$(SRCTREE))
dir :=
else
dir := $(subst $(SRCTREE)/,,$(CURDIR))
endif

obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/)
src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/)

$(shell mkdir -p $(obj))
else
obj :=
src :=
endif

# clean the slate ...

PLATFORM_RELFLAGS =
PLATFORM_CPPFLAGS =
PLATFORM_LDFLAGS =

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


ifeq ($(HOSTOS),darwin)
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

#

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
LD    = $(CROSS_COMPILE)ld
CC    = $(CROSS_COMPILE)gcc
CPP    = $(CC) -E
AR    = $(CROSS_COMPILE)ar
NM    = $(CROSS_COMPILE)nm
LDR    = $(CROSS_COMPILE)ldr
STRIP    = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
RANLIB    = $(CROSS_COMPILE)RANLIB

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


# Load generated board configuration

sinclude $(OBJTREE)/include/autoconf.mk

ifdef    ARCH
sinclude $(TOPDIR)/lib_$(ARCH)/config.mk    # include architecture dependend rules

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

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

endif
ifdef    VENDOR
BOARDDIR = $(VENDOR)/$(BOARD)
else
BOARDDIR = $(BOARD)
endif
ifdef    BOARD
sinclude $(TOPDIR)/board/$(BOARDDIR)/config.mk    # include board specific rules

endif

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


ifneq (,$(findstring s,$(MAKEFLAGS)))
ARFLAGS = cr
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 := $(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__
ifneq ($(TEXT_BASE),)
CPPFLAGS += -DTEXT_BASE=$(TEXT_BASE)
endif

ifneq ($(RESET_VECTOR_ADDRESS),)
CPPFLAGS += -DRESET_VECTOR_ADDRESS=$(RESET_VECTOR_ADDRESS)
endif

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

CFLAGS += $(call cc-option,-fno-stack-protector)

# 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

# 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 $(obj)u-boot.lds $(PLATFORM_LDFLAGS)
ifneq ($(TEXT_BASE),)
LDFLAGS += -Ttext $(TEXT_BASE)
endif

#定义AR选项ARFLAGS,调试选项DBGFLAGS,优化选项OPTFLAGS

#预处理选项CPPFLAGS,C编译器选项CFLAGS,连接选项LDFLAGS

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

关于连接的具体细节可以去查看:

http://blog.chinaunix.net/u3/114831/showart_2241591.html

# 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),darwin)
BFD_ROOT_DIR =        /usr/local/tools
else
ifeq ($(HOSTARCH),$(ARCH))
# 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
endif
endif

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


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

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


# Allow boards to use custom optimize flags on a per dir/file basis

BCURDIR := $(notdir $(CURDIR))
$(obj)%.s:    %.S
    $(CPP) $(AFLAGS) $(AFLAGS_$(@F)) $(AFLAGS_$(BCURDIR)) -o $@ $<
$(obj)%.o:    %.S
    $(CC) $(AFLAGS) $(AFLAGS_$(@F)) $(AFLAGS_$(BCURDIR)) -o $@ $< -c
$(obj)%.o:    %.c
    $(CC) $(CFLAGS) $(CFLAGS_$(@F)) $(CFLAGS_$(BCURDIR)) -o $@ $< -c
$(obj)%.i:    %.c
    $(CPP) $(CFLAGS) $(CFLAGS_$(@F)) $(CFLAGS_$(BCURDIR)) -o $@ $< -c
$(obj)%.s:    %.c
    $(CC) $(CFLAGS) $(CFLAGS_$(@F)) $(CFLAGS_$(BCURDIR)) -o $@ $< -c -S

在这个config.mk文件中我们看到了定义了很多变量(包括编译工具和编译选项)已经又包含了很多子config.mk文件(这些子config.mk文件定义了TEXT_BASE PLATFORM_CPPFLAGS PLATFORM_RELFLAGS等等的信息),还有就是定义了Makefile的规则(在最后一部分)
最好的办法是从根目录下的config.mk一级一级的往下查看,里面定义了我们需要用到的所有的变量和工具还还有规则(可能有时候会觉得定义这个多config.mk不合适,在一个config.mk里面全部定义完成就可以,其实分开定义是为了数据的管理,达到分门别类的目的)


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