编译kernel:编译

韦东山Linux视频第1期_裸板_UBoot_文件系统_驱动初步第10课第3节 内核启动流程分析之Makefile.WMV

 1. 编译内核分三步:

  make xxx_defconfig [linux/arch/arm/configs/s3c2410_defconfig]

  make menuconfig 或者使用厂商配置好的ok_config即可, cp ok_config linux/.config

    执行该命令时,从Kconfig里面读取菜单选项,形成可视化菜单。 执行后,根据用户自己配置,改写 .config文件。如果厂商已经配置好ok_config,可以不经过这一步选配,直接拷贝过来即可,cp ok_config linux/.config.

    到此时, 生成了几个.cmd文件和.c以及.config,从而指导后面的编译选择.【各级子Makefile一般根据.config 里面的宏定义选项,选择是否编译以及是否编译为模块 。】

      试下make distclean{

        CLEAN scripts/basic
        CLEAN scripts/kconfig
        CLEAN include/config
        CLEAN .config

      }

make

  主Makefile以及各级Makefile 引用.config 里面的宏定义选项(同时还要引用各级写好的Kbuild/kconfig),生成宏定义头文件(  /include/linux/autoconf.h :#define CONFIG_ARM 1 )并编译内核。

==================================================

2. 根据Makefile, vmlinux.lds文件,分析编译文件是哪些:

/* linux-2.6.22.6archarmMakefile */
zImage Image xipImage bootpImage uImage: vmlinux 

/* linux-2.6.22.6Makefile ; include $(srctree)/arch/$(ARCH)/Makefile*/
all: vmlinux 
vmlinux: $(vmlinux-lds) $(vmlinux-init) $(vmlinux-main) $(kallsyms.o) FORCE

>>>
	vmlinux-lds  := arch/$(ARCH)/kernel/vmlinux.lds
	{
		lds和代码分析示例:
		
		in lds:
			.text.head : {
				_stext = .;
				_sinittext = .;
				*(.text.head)
			}
			
		in head.S:
			.section ".text.head", "ax"
			.type	stext, %function
			code...
	}

	vmlinux-init := $(head-y) $(init-y)
	{
		head-y		:= arch/arm/kernel/head.o arch/arm/kernel/init_task.o /* linux-2.6.22.6archarmMakefile */
		init-y		:= init/built-in.o ;  /*init/文件夹下面的所有代码被编译成init/built-in.o */
	}

	vmlinux-main := $(core-y) $(libs-y) $(drivers-y) $(net-y)
	{
		core-y		:= usr/built-in.o kernel/built-in.o mm/built-in.o fs/built-in.o ipc/built-in.o security/built-in.o crypto/built-in.o block/built-in.o
		libs-y		:= $(libs-y1) $(libs-y2)
		libs-y1		:= lib/lib.a
		libs-y2		:= lib/built-in.o
		drivers-y	:= drivers/built-in.o sound/built-in.o
		net-y		:= net/built-in.o 
	}

 

 ==================================================

3. 关于编译选项的格式:

.如果一个驱动直接编译进内核,直接按照以下方式书写:
obj-y += generic_serial.o vme_scc.o
obj-y += a.o

.如果一个驱动要编译成模块,一个模块由1个源文件编译而成:

obj-m += a.o

 .如果一个驱动要编译成模块,且一个模块由多个源文件编译而成, 其编译过程为:

obj-m += ab.o
ab-objs := a.o b.o
编译过程:
a.c -> a.o
b.c -> b.o
a.o b.o -> ab.o -> ab.ko

参考:linux-2.6.22.6Documentationkbuild

    Example:
        #drivers/isdn/i4l/Makefile
        obj-$(CONFIG_ISDN) += isdn.o
        isdn-objs := isdn_net_lib.o isdn_v110.o isdn_common.o
        
        
    $(obj-m) specify object files which are built as loadable
    kernel modules.

    A module may be built from one source file or several source
    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.

    Kbuild needs to know which the parts that you want to build your
    module from, so you have to tell it by setting an
    $(<module_name>-objs) variable.

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

    In this example, the module name will be isdn.o. Kbuild will
    compile the objects listed in $(isdn-objs) and then run
    "$(LD) -r" on the list of these files to generate isdn.o.
原文地址:https://www.cnblogs.com/mylinux/p/5055428.html