码迷,mamicode.com
首页 > 其他好文 > 详细

kernel makefile

时间:2015-11-14 18:00:57      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

===Documentation/kbuild/makefiles.txt===
 

The Makefiles have five parts:

  1.  Makefile                               the top Makefile.
  2.  .config                                  the kernel configuration file.
  3. arch/$(ARCH)/Makefile         the arch Makefile.
  4. scripts/Makefile.*                  common rules etc. for all kbuild Makefiles.
  5. kbuild Makefiles                    there are about 500 of these.


The top Makefile reads the .config file, which comes from the kernel
configuration process.

 
The top Makefile is responsible for building two major products: vmlinux
(the resident kernel image) and modules (any module files).
顶层Makefile负责编译vmlinux和内核模块modules.
 
It builds these goals by recursively descending into the subdirectories of
the kernel source tree.
通过从内核源码树向下递归各级子目录来完成vmlinux和modues的编译。

The list of subdirectories which are visited depends upon the kernel
configuration. 
各级子目录列表依赖于内核配置。这个configuration具体是指什么暂不确定。(.config)
 
The top Makefile textually includes an arch Makefile with the name arch/$(ARCH)/Makefile. The arch Makefile supplies
architecture-specific information to the top Makefile.
顶层Makefile包含arch/$(ARCH)/Makefile。arch Makefile 向顶层Makefile提供具体的架构信息。
 

Each subdirectory has a kbuild Makefile which carries out the commands passed down from above. The kbuild Makefile uses information from the .config file to construct various file lists used by kbuild to build any built-in or modular targets. 

每个子目录有一个kbuild Makefile,kbuild Makefile 执行上一级目录传递下来的命令。kbuild Makefile 使用.config中的配置信息来生成不同的文件列表,这些列表被用来编译指明相应的对象是编译进内核还是编译成模块。
 
scripts/Makefile.* contains all the definitions/rules etc. that are used to build the kernel based on the kbuild makefiles.
scripts/Makefile.*可看成是一些编译工具。
 
 
=== 2 Who does what

People have four different relationships with the kernel Makefiles.

*Users* are people who build kernels.  These people type commands such as
"make menuconfig" or "make".  They usually do not read or edit
any kernel Makefiles (or any other source files).

*Normal developers* are people who work on features such as device
drivers
file systems, and network protocols.  These people need to
maintain the kbuild Makefiles for the subsystem they are
working on.  In order to do this effectively, they need some overall
knowledge about the kernel Makefiles, plus detailed knowledge about the
public interface for kbuild.

*Arch developers* are people who work on an entire architecture, such
as sparc or ia64.  Arch developers need to know about the arch Makefile
as well as kbuild Makefiles.

*Kbuild developers* are people who work on the kernel build system itself.
These people need to know about all aspects of the kernel Makefiles.

This document is aimed towards normal developers and arch developers.
 
=== 3 The kbuild files
Most Makefiles within the kernel are kbuild Makefiles that use the kbuild infrastructure. This chapter introduces the syntax used in the kbuild makefiles. The preferred name for the kbuild files are ‘Makefile‘ but ‘Kbuild‘ can be used and if both a ‘Makefile‘ and a ‘Kbuild‘ file exists, then the ‘Kbuild‘ file will be used.
内核中的大多数Makefile文件都是kbuild Makefiles。kbuild Makefiles有自己特有的语法。kbuild 文件的名字优先使用Makefile,Kbuild也可以使用。当Makefile和Kbuild同时存在时,Kbuild系统会使用Kbuild文件。
 
Section 3.1 "Goal definitions" is a quick intro, further chapters provide
more details, with real examples.

--- 3.1 Goal definitions

     Goal definitions are the main part (heart) of the kbuild Makefile.  全局变量是kbuild Makefile的主要部分。
     These lines define the files to be built, any special compilation     定义哪些文件将被编译,指定编译选项,向下递归的子目录
     options, and any subdirectories to be entered recursively.

     The most simple kbuild makefile contains one line:
     Example:
          obj-y += foo.o                         

     This tells kbuild that there is one object in that directory, named    obj-y告诉kbuild系统,当前目录下将编译生成一个名为foo.o          
     foo.o. foo.o will be built from foo.c or foo.S.                                   的对象 由foo.c or foo.S 编译得到,且该对象将被编译进内核  
 
     If foo.o shall be built as a module, the variable obj-m is used.          如果将该对象编译成modules,则使用obj-m变量。
     Therefore the following pattern is often used:

     Example:
          obj-$(CONFIG_FOO) += foo.o

     $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module).     $(CONFIG_FOO) 决定当前对象是编译进内核还是模块
     If CONFIG_FOO is neither y nor m, then the file will not be compiled          或是该文件不会被编译或链接。
     nor linked.
 
--- 3.2 Built-in object goals - obj-y

     The kbuild Makefile specifies object files for vmlinux          vmlinux中的目标文件包含在$(obj-y) lists 中,
     in the $(obj-y) lists.  These lists depend on the kernel         $(obj-?) lists 中的目标是否编译进内核取决于.config。
     configuration.

     Kbuild compiles all the $(obj-y) files.  It then calls               Kbuild会编译所有的 $(obj-y) files ,并用"$(LD) -r" 将各级子目录
     "$(LD) -r" to merge these files into one built-in.o file.          下的built-in.o 连接成顶层目录下的一个built-in.o ,
     built-in.o is later linked into vmlinux by the parent Makefile. 上一级目录的Makefile将负责打包子目录的各个built-in.o,依此类推 

     The order of files in $(obj-y) is significant.  Duplicates in     $(obj-y) 中的文件是有一定次序的。列表中允许重复的目标,
     the lists are allowed: the first instance will be linked into          最先遇到的目标将被链接进built-in.o,之后的重复目标将被忽略。
     built-in.o and succeeding instances will be ignored.

     Link order is significant, because certain functions               链接也是有序的。在boot过程中,有些函数将根据其出现的次序被
     (module_init() / __initcall) will be called during boot in the     依次调用。 (如module_init() / __initcall) 
     order they appear. So keep in mind that changing the link     改变链接次序可能导致SCSI控制器的次序,将会使磁盘
     order may e.g. change the order in which your SCSI               被重新编号。
     controllers are detected, and thus your disks are renumbered.

     Example:
          #drivers/isdn/i4l/Makefile
          # Makefile for the kernel ISDN subsystem and device drivers.
          # Each configuration option enables a list of files.
          obj-$(CONFIG_ISDN_I4L)         += isdn.o
          obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
 
--- 3.3 Loadable module goals - obj-m

     $(obj-m) specify object files which are built as loadable                $(obj-m) 指定的目标文件将被编译成可加载的内核模块
     kernel modules.

     A module may be built from one source file or several source          仅有一个源文件的内核模块的简单Makefile的写法
     files. In the case of one source file, the kbuild makefile
     simply adds the file to $(obj-m).

     Example:
          #drivers/isdn/i4l/Makefile
          obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o

     Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to ‘m‘

     If a kernel module is built from several source files, you specify               当一个内核模块是由多个目标文件构成时,需要通过
     that you want to build a module in the same way as above; however,      $(<module_name>-y)变量设置需要编译的目标文件。 
     kbuild needs to know which object files you want to build your
     module from, so you have to tell it by setting a $(<module_name>-y)
     variable.

     Example:
          #drivers/isdn/i4l/Makefile
          obj-$(CONFIG_ISDN_I4L) += isdn.o
          isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o

     In this example, the module name will be isdn.o. Kbuild will                    在这个例子中,模块名是isdn.o,Kbuild将编译
     compile the objects listed in $(isdn-y) and then run                         $(isdn-y)中的对象,然后执行"$(LD) -r"将这些目标文件 
     "$(LD) -r" on the list of these files to generate isdn.o.                         链接成isdn.o。

     Due to kbuild recognizing $(<module_name>-y) for composite objects,
     you can use the value of a CONFIG_ symbol to optionally include an
     object file as part of a composite object.

     Example:
          #fs/ext2/Makefile
             obj-$(CONFIG_EXT2_FS) += ext2.o
          ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \
                 namei.o super.o symlink.o
             ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \
                              xattr_trusted.o

     In this example, xattr.o, xattr_user.o and xattr_trusted.o are only
     part of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR)
     evaluates to ‘y‘.

     Note: Of course, when you are building objects into the kernel,
     the syntax above will also work. So, if you have CONFIG_EXT2_FS=y,
     kbuild will build an ext2.o file for you out of the individual
     parts and then link this into built-in.o, as you would expect.
 
--- 3.4 Objects which export symbols

     No special notation is required in the makefiles for
     modules exporting symbols.
 
--- 3.5 Library file goals - lib-y

     Objects listed with obj-* are used for modules, or
     combined in a built-in.o for that specific directory.
     There is also the possibility to list objects that will
     be included in a library, lib.a.
     All objects listed with lib-y are combined in a single          lib-y中的目标将被组合成一个lib.a库,放在当前目录下
     library for that directory.
     Objects that are listed in obj-y and additionally listed in     既在obj-y 列表中又在lib-y 列表中的目标将不会被包含在库中
     lib-y will not be included in the library, since they will          因为obj-y中的目标总能访问到。
     be accessible anyway.
     For consistency, objects listed in lib-m will be included in lib.a.     lib-m 中的目标会包含在lib.a中。

     Note that the same kbuild makefile may list files to be built-in
     and to be part of a library. Therefore the same directory
     may contain both a built-in.o and a lib.a file.

     Example:
          #arch/x86/lib/Makefile
          lib-y    := delay.o

     This will create a library lib.a based on delay.o. For kbuild to
     actually recognize that there is a lib.a being built, the directory
     shall be listed in libs-y
.
     See also "6.4 List directories to visit when descending".

     Use of lib-y is normally restricted to lib/ and arch/*/lib.
 
--- 3.6 Descending down in directories

     A Makefile is only responsible for building objects in its own
     directory. Files in subdirectories should be taken care of by
     Makefiles in these subdirs. The build system will automatically
     invoke make recursively in subdirectories, provided you let it know of
     them.

     To do so, obj-y and obj-m are used.
     ext2 lives in a separate directory, and the Makefile present in fs/
     tells kbuild to descend down using the following assignment.                            fs
                                                                                                                        |-- Makefile
     Example:                                                                                                      `-- ext2
          #fs/Makefile                                                                                                 |-- Makefile
          obj-$(CONFIG_EXT2_FS) += ext2/                                                                 |-- *

     If CONFIG_EXT2_FS is set to either ‘y‘ (built-in) or ‘m‘ (modular)
     the corresponding obj- variable will be set, and kbuild will descend
     down in the ext2 directory.
     Kbuild only uses this information to decide that it needs to visit                   Kbuild只是根据该信息判断要访问子目录,
     the directory, it is the Makefile in the subdirectory that                                至于在子目录下做什么,有子目录的Makefile决定。
     specifies what is modules and what is built-in.

     It is good practice to use a CONFIG_ variable when assigning directory
     names. This allows kbuild to totally skip the directory if the
     corresponding CONFIG_ option is neither ‘y‘ nor ‘m‘.
 
--- 3.7 Compilation flags

    ccflags-yasflags-y and ldflags-y
     These three flags apply only to the kbuild makefile in which they               ccflags-yasflags-y and ldflags-y 只在当前被指定
     are assigned. They are used for all the normal cc, as and ld                         Makefile中有效。当前Makefile的所有目标的cc,as,ld
     invocations happening during a recursive build.                                          均有效。
     Note: Flags with the same behaviour were previously named:
     EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
     They are still supported but their usage is deprecated
.

     ccflags-y specifies options for compiling with $(CC).

     Example:
          # drivers/acpi/Makefile
          ccflags-y := -Os
          ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT

     This variable is necessary because the top Makefile owns the
     variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
     entire tree.

     asflags-y specifies options for assembling with $(AS).

     Example:
          #arch/sparc/kernel/Makefile
          asflags-y := -ansi

     ldflags-y specifies options for linking with $(LD).

     Example:
          #arch/cris/boot/compressed/Makefile
          ldflags-y += -T $(srctree)/$(src)/decompress_$(arch-y).lds

    subdir-ccflags-ysubdir-asflags-y
     The two flags listed above are similar to ccflags-y and asflags-y.
     The difference is that the subdir- variants have effect for the kbuild     对当前目录和所有子目录均有效
     file where they are present and all subdirectories
.
     Options specified using subdir-* are added to the commandline before
     the options specified using the non-subdir variants.


     Example:
          subdir-ccflags-y := -Werror

    CFLAGS_$@, AFLAGS_$@

     CFLAGS_$@ and AFLAGS_$@ only apply to commands in current     仅对当前Makefile有效,并只对指定的目标有效
     kbuild makefile.

     $(CFLAGS_$@) specifies per-file options for $(CC).  The $@
     part has a literal value which specifies the file that it is for.

     Example:
          # drivers/scsi/Makefile
          CFLAGS_aha152x.o =   -DAHA152X_STAT -DAUTOCONF
          CFLAGS_gdth.o    = # -DDEBUG_GDTH=2 -D__SERIAL__ -D__COM2__ \
                         -DGDTH_STATISTICS

     These two lines specify compilation flags for aha152x.o and gdth.o.

     $(AFLAGS_$@) is a similar feature for source files in assembly
     languages.

     Example:
          # arch/arm/kernel/Makefile
          AFLAGS_head.o        := -DTEXT_OFFSET=$(TEXT_OFFSET)
          AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
          AFLAGS_iwmmxt.o      := -Wa,-mcpu=iwmmxt
 
作用范围:
     subdir-ccflags-ysubdir-asflags-y  >  ccflags-yasflags-y and ldflags-y > CFLAGS_$@, AFLAGS_$@
 

kernel makefile

标签:

原文地址:http://www.cnblogs.com/black-mamba/p/4964726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!