Chinaunix首页 | 论坛 | 博客
  • 博客访问: 333218
  • 博文数量: 127
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 333
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-27 14:44
个人简介

兴趣是最好的学习契机!

文章分类

全部博文(127)

文章存档

2017年(1)

2016年(3)

2015年(54)

2014年(58)

2013年(11)

我的朋友

分类: LINUX

2015-03-17 18:38:44

Linux  的编译使用 GNU make 工具来检查整个系统的文件和调用 gcc 工具以及脚本完成编译源代码生成 image 等操作。要了解整个编译系统,我们首先要了解 Linux 内核的 Makefile 文件。

 

Linux 的 编译系统包括 5 个部分

Makefile         顶层的 Makefile 文件
.config 内核配置文件
arch/$(ARCH)/Makefile 平台 Makefile 文件
scripts/Makefile.*                脚本规则
kbuild Makefiles             大概 500 多个其他的 Makefile 文件


Makefile

 

查看版本

 

在内核源代码的根目录有一个 Makefile 文件,这是编译内核的入口,不管执行配置还是编译,make 命令首先读取这个文件。这个文件首先指明了内核的版本:

我们这里是 3.10

VERSION = 3
PATCHLEVEL = 10
SUBLEVEL = 0
EXTRAVERSION =
NAME = Unicycling Gorilla

 

处理参数

 

然后处理 command line ,一共有 5  个 command line

V : 设定编译时,输出信息的等级,例如你可以用 make V=1, 查看编译时执行的所有命令,包括 gcc 参数都会打印出来

C :  代码检查,使用 sparse,检查源文件。

M : 指定不在当前目录(外部模块)的编译,也可以指定当前目录的子目录,那么将只会编译子目录的内容

O :指定编译生成的目标文件位置,当设置了 O 参数,内核生成的 obj 和 builtin 文件都会按照目录结构组织到 O 参数指定的目录里面

W: 使能外部 gcc 检查

 

这几个命令参数,在特定的情况下,将会非常有用,比如我们想编译一个单独的模块就经常使用 M 参数,用 M 指定模块的路径,make 的时候将会不编译整个内核,而编译我们需要的模块:(M 参数会覆盖  KBUILD_EXTMOD 变量)

  1. make M=drivers/misc/  
  2.  LD      drivers/misc/eeprom/built-in.o  
  3.  CC [M]  drivers/misc/eeprom/eeprom_93cx6.o  
  4.  LD      drivers/misc/built-in.o  
  5.  Building modules, stage 2.  
  6.  MODPOST 1 modules  
  7.  CC      drivers/misc/eeprom/eeprom_93cx6.mod.o  
  8.  LD [M]  drivers/misc/eeprom/eeprom_93cx6.ko  


 

O 参数的指定,会改变整个编译出来的文件的结构,例如哦我们有多个平台要编译,你就需要为每个平台 clone 一份内核代码了,只需要设置不同的输出路径即可:

make O=atmel,  make O=asus  (O 参数会覆盖 KBUILD_OUTPUT 变量),相应的文件也会生成在目标路径下,例如 uImage 就在 atmel/arch/arm/boot/uImage

 

获取信息

 

接下来系统就会获取交叉编译环境和选择不同的 gcc 和 bin 工具集

  1. ARCH            ?= $(SUBARCH)  
  2. CROSS_COMPILE   ?= $(CONFIG_CROSS_COMPILE:"%"=%)  


arch 变量设置目标平台, cross compile 设置交叉编译链。

 

伪目标

 

当系统信息获取成功,就可以执行编译命令了,每一个伪目标都可以作为一个编译命令:(大概有 40 个左右的伪目标),但是我们会使用到的并没有这么多,可以用 make help 查看我们使用的编译命令:

  1.  make help  
  2. Cleaning targets:  
  3.   clean           - Remove most generated files but keep the config and  
  4.                     enough build support to build external modules  
  5.   mrproper        - Remove all generated files + config + various backup files  
  6.   distclean       - mrproper + remove editor backup and patch files  
  7. Configuration targets:  
  8.   config          - Update current config utilising a line-oriented program  
  9.   nconfig         - Update current config utilising a ncurses menu based program  
  10.   menuconfig      - Update current config utilising a menu based program  
  11.   xconfig         - Update current config utilising a QT based front-end  
  12.   gconfig         - Update current config utilising a GTK based front-end  
  13.   oldconfig       - Update current config utilising a provided .config as base  
  14.   localmodconfig  - Update current config disabling modules not loaded  
  15.   localyesconfig  - Update current config converting local mods to core  
  16.   silentoldconfig - Same as oldconfig, but quietly, additionally update deps  
  17.   defconfig       - New config with default from ARCH supplied defconfig  
  18.   savedefconfig   - Save current config as ./defconfig (minimal config)  
  19.   allnoconfig     - New config where all options are answered with no  
  20.   allyesconfig    - New config where all options are accepted with yes  
  21.   allmodconfig    - New config selecting modules when possible  
  22.   alldefconfig    - New config with all symbols set to default  
  23.   randconfig      - New config with random answer to all options  
  24.   listnewconfig   - List new options  
  25.   olddefconfig    - Same as silentoldconfig but sets new symbols to their default value  
  26. Other generic targets:  
  27.   all             - Build all targets marked with [*]  
  28. * vmlinux         - Build the bare kernel  
  29. * modules         - Build all modules  
  30.   modules_install - Install all modules to INSTALL_MOD_PATH (default: /)  
  31.   firmware_install- Install all firmware to INSTALL_FW_PATH  
  32.                     (default: $(INSTALL_MOD_PATH)/lib/firmware)  
  33.   dir/            - Build all files in dir and below  
  34.   dir/file.[oisS] - Build specified target only  
  35.   dir/file.lst    - Build specified mixed source/assembly target only  
  36.                     (requires a recent binutils and recent build (System.map))  
  37.   dir/file.ko     - Build module including final link  
  38.   modules_prepare - Set up for building external modules  
  39.   tags/TAGS       - Generate tags file for editors  
  40.   cscope          - Generate cscope index  
  41.   gtags           - Generate GNU GLOBAL index  
  42.   kernelrelease   - Output the release version string  
  43.   kernelversion   - Output the version stored in Makefile  
  44.   headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH  
  45.                     (default: /media/android/jiangxd/workspace/Miura/kernel/usr)  
  46. Static analysers  
  47.   checkstack      - Generate a list of stack hogs  
  48.   namespacecheck  - Name space analysis on compiled kernel  
  49.   versioncheck    - Sanity check on version.h usage  
  50.   includecheck    - Check for duplicate included header files  
  51.   export_report   - List the usages of all exported symbols  
  52.   headers_check   - Sanity check on exported headers  
  53.   headerdep       - Detect inclusion cycles in headers  
  54.   coccicheck      - Check with Coccinelle.  
  55. Kernel packaging:  
  56.   rpm-pkg             - Build both source and binary RPM kernel packages  
  57.   binrpm-pkg          - Build only the binary kernel package  
  58.   deb-pkg             - Build the kernel as a deb package  
  59.   tar-pkg             - Build the kernel as an uncompressed tarball  
  60.   targz-pkg           - Build the kernel as a gzip compressed tarball  
  61.   tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball  
  62.   tarxz-pkg           - Build the kernel as a xz compressed tarball  
  63.   perf-tar-src-pkg    - Build perf-3.10.0.tar source tarball  
  64.   perf-targz-src-pkg  - Build perf-3.10.0.tar.gz source tarball  
  65.   perf-tarbz2-src-pkg - Build perf-3.10.0.tar.bz2 source tarball  
  66.   perf-tarxz-src-pkg  - Build perf-3.10.0.tar.xz source tarball  
  67. Documentation targets:  
  68.  Linux kernel internal documentation in different formats:  
  69.   htmldocs        - HTML  
  70.   pdfdocs         - PDF  
  71.   psdocs          - Postscript  
  72.   xmldocs         - XML DocBook  
  73.   mandocs         - man pages  
  74.   installmandocs  - install man pages generated by mandocs  
  75.   cleandocs       - clean all generated DocBook files  
  76. Architecture specific targets (arm):  
  77. * zImage        - Compressed kernel image (arch/arm/boot/zImage)  
  78.   Image         - Uncompressed kernel image (arch/arm/boot/Image)  
  79. * xipImage      - XIP kernel image, if configured (arch/arm/boot/xipImage)  
  80.   uImage        - U-Boot wrapped zImage  
  81.   bootpImage    - Combined zImage and initial RAM disk  
  82.                   (supply initrd image via make variable INITRD=)  
  83. * dtbs          - Build device tree blobs for enabled boards  
  84.   install       - Install uncompressed kernel  
  85.   zinstall      - Install compressed kernel  
  86.   uinstall      - Install U-Boot wrapped compressed kernel  
  87.                   Install using (your) ~/bin/installkernel or  
  88.                   (distribution) /sbin/installkernel or  
  89.                   install to $(INSTALL_PATH) and run lilo  

内容非常之多。这里只介绍几个常用的:

make menuconfig  图形化配置 config

make uImage          编译生成 uImage

make clean              删除大部分生成的文件,但是保留配置,以便可以编译模块

make distclean       删除所有生成的文件,补丁和配置,以及一些备份文件

make mrproper       删除所有生成的文件,补丁和配置


总的来说,顶层 Makefile 文件读取 config 文件生成 Linux 的两大目标文件 vmlinux 和 模块文件。

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