Linux 内核的编译系统

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 变量)

make M=drivers/misc/
 LD      drivers/misc/eeprom/built-in.o
 CC [M]  drivers/misc/eeprom/eeprom_93cx6.o
 LD      drivers/misc/built-in.o
 Building modules, stage 2.
 MODPOST 1 modules
 CC      drivers/misc/eeprom/eeprom_93cx6.mod.o
 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 工具集

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

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

伪目标

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

 make help
Cleaning targets:
  clean           - Remove most generated files but keep the config and
                    enough build support to build external modules
  mrproper        - Remove all generated files + config + various backup files
  distclean       - mrproper + remove editor backup and patch files
Configuration targets:
  config          - Update current config utilising a line-oriented program
  nconfig         - Update current config utilising a ncurses menu based program
  menuconfig      - Update current config utilising a menu based program
  xconfig         - Update current config utilising a QT based front-end
  gconfig         - Update current config utilising a GTK based front-end
  oldconfig       - Update current config utilising a provided .config as base
  localmodconfig  - Update current config disabling modules not loaded
  localyesconfig  - Update current config converting local mods to core
  silentoldconfig - Same as oldconfig, but quietly, additionally update deps
  defconfig       - New config with default from ARCH supplied defconfig
  savedefconfig   - Save current config as ./defconfig (minimal config)
  allnoconfig     - New config where all options are answered with no
  allyesconfig    - New config where all options are accepted with yes
  allmodconfig    - New config selecting modules when possible
  alldefconfig    - New config with all symbols set to default
  randconfig      - New config with random answer to all options
  listnewconfig   - List new options
  olddefconfig    - Same as silentoldconfig but sets new symbols to their default value
Other generic targets:
  all             - Build all targets marked with [*]
* vmlinux         - Build the bare kernel
* modules         - Build all modules
  modules_install - Install all modules to INSTALL_MOD_PATH (default: /)
  firmware_install- Install all firmware to INSTALL_FW_PATH
                    (default: $(INSTALL_MOD_PATH)/lib/firmware)
  dir/            - Build all files in dir and below
  dir/file.[oisS] - Build specified target only
  dir/file.lst    - Build specified mixed source/assembly target only
                    (requires a recent binutils and recent build (System.map))
  dir/file.ko     - Build module including final link
  modules_prepare - Set up for building external modules
  tags/TAGS       - Generate tags file for editors
  cscope          - Generate cscope index
  gtags           - Generate GNU GLOBAL index
  kernelrelease   - Output the release version string
  kernelversion   - Output the version stored in Makefile
  headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH
                    (default: /media/android/jiangxd/workspace/Miura/kernel/usr)
Static analysers
  checkstack      - Generate a list of stack hogs
  namespacecheck  - Name space analysis on compiled kernel
  versioncheck    - Sanity check on version.h usage
  includecheck    - Check for duplicate included header files
  export_report   - List the usages of all exported symbols
  headers_check   - Sanity check on exported headers
  headerdep       - Detect inclusion cycles in headers
  coccicheck      - Check with Coccinelle.
Kernel packaging:
  rpm-pkg             - Build both source and binary RPM kernel packages
  binrpm-pkg          - Build only the binary kernel package
  deb-pkg             - Build the kernel as a deb package
  tar-pkg             - Build the kernel as an uncompressed tarball
  targz-pkg           - Build the kernel as a gzip compressed tarball
  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball
  tarxz-pkg           - Build the kernel as a xz compressed tarball
  perf-tar-src-pkg    - Build perf-3.10.0.tar source tarball
  perf-targz-src-pkg  - Build perf-3.10.0.tar.gz source tarball
  perf-tarbz2-src-pkg - Build perf-3.10.0.tar.bz2 source tarball
  perf-tarxz-src-pkg  - Build perf-3.10.0.tar.xz source tarball
Documentation targets:
 Linux kernel internal documentation in different formats:
  htmldocs        - HTML
  pdfdocs         - PDF
  psdocs          - Postscript
  xmldocs         - XML DocBook
  mandocs         - man pages
  installmandocs  - install man pages generated by mandocs
  cleandocs       - clean all generated DocBook files
Architecture specific targets (arm):
* zImage        - Compressed kernel image (arch/arm/boot/zImage)
  Image         - Uncompressed kernel image (arch/arm/boot/Image)
* xipImage      - XIP kernel image, if configured (arch/arm/boot/xipImage)
  uImage        - U-Boot wrapped zImage
  bootpImage    - Combined zImage and initial RAM disk
                  (supply initrd image via make variable INITRD=<path>)
* dtbs          - Build device tree blobs for enabled boards
  install       - Install uncompressed kernel
  zinstall      - Install compressed kernel
  uinstall      - Install U-Boot wrapped compressed kernel
                  Install using (your) ~/bin/installkernel or
                  (distribution) /sbin/installkernel or
                  install to $(INSTALL_PATH) and run lilo

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

make menuconfig  图形化配置 config

make uImage          编译生成 uImage

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

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

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

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

Linux 内核的编译系统

时间: 2024-08-01 10:45:03

Linux 内核的编译系统的相关文章

Linux内核Makefile文件(翻译自内核手册)

转载自:http://www.cnblogs.com/jason-lu/p/3728198.html --译自Linux3.9.5 Kernel Makefiles(内核目录documention/kbuild/makefiles.txt) kbuild(kernel build) 内核编译器 This document describes the Linux kernel Makefiles 本文当介绍了Linux内核的Makefile === Table of Contents=== 目录

《linux内核设计与实现》读书笔记第三章

第3章 进程管理 3.1 进程 1.进程 进程就是处于执行期的程序. 进程包括: 可执行程序代码 打开的文件 挂起的信号 内核内部数据 处理器状态 一个或多个具有内存映射的内存地址空间 一个或多个执行线程 用来存放全局变量的数据段 …… 实际上,进程就是正在执行的程序代码的实时结果 2.执行线程 简称线程,是在进程中活动的对象. 每个线程都拥有一个独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程,而不是进程. 进程提供两种虚拟机制: 虚拟处理器和虚拟内存. 在线程之间可以共享虚拟

Linux内核分析——期中总结

期中总结 一.MOOC课程 (一)计算机是如何工作的 1.冯诺依曼体系结构的核心思想是存储程序计算机. 2.CPU在实际取指令时根据cs:eip来准确定位一个指令. 3.寄存器模式,以%开头的寄存器标示符. 立即数是以$开头的数值. 直接寻址:直接访问一个指定的内存地址的数据. 间接寻址:将寄存器的值作为一个内存地址来访问内存. 变址寻址:在间接寻址之时改变寄存器的数值. 4.eip寄存器不能被直接修改,只能通过特殊指令间接修改. 5.在32位x86 CPU中,我们使用push和pop指令实现入

Linux内核project导论——linux学习和职业曲线(刚開始学习的人,中级,高级都可參考)

Linux世界介绍 给自己定级 门外汉: 不会安装操作系统 不会用虚拟机(安装和使用) 入门级: 熟悉常见的发行版,甚至装过而且能用一些特殊发行版(比如kali)做过一些简单的图形界面的使用. 会一些最基础的命令(比如cd.ps.top.ls.ifconfig等这个级别的) 基础级: 能够使用一些常见的命令(touch.tail.date.find.du.fdisk.less.pidof等这个级别的命令) 图形界面操作的比較熟练.而且能够相应一部分的后台命令. 知道一些经常使用的配置文件的作用(

linux内核编译相关

参考:http://www.arm.linux.org.uk/docs/kerncomp.php 一. 内核编译1) linux 2.4make clean/make mrpropermake depmake zImagemake modules //编译模块2) linux 2.6make clean/make mrpropermake // 编译内核同时编译了模块3) 模块安装上述步骤完成后,模块分布在各个内核目录下,通过下面命令安装到具体目录下:make modules_install I

Linux内核编译详解

学习了网上的一些资料,自己试着摸索了一下,整理出这篇文章. 不当之处,还请大家批评指正.谢谢. 重要的参考资料有: http://raspberrypi.stackexchange.com/questions/192/how-do-i-cross-compile-the-kernel-on-a-ubuntu-host http://blog.csdn.net/xdw1985829/article/details/6833319 好了,下面进入正题. 一.准备工作 准备工作如何做,这里就不详说了.

Linux 内核 Makefile 体系简单分析 (转)

转载自mz_linux的ChinaUnix博客 :http://blog.chinaunix.net/uid-26806098-id-3141136.html 众所周知,Linux内核是使用make命令来配置并编译的,那必然少不了Makefile.在内核目录树中我们可以看到内核编译系统的顶层Makefile文件.但是如此复杂.庞大的内核源码绝不可能使用一个或几个Makefile文件来完成配置编译,而是需要一套同样复杂.庞大,且为Linux内核定制的Makefile系统.她可以说是内核的一个子系统

自定义配置编译linux内核

1 编译linux内核原因一般情况下,我们是不需要重新去编译linux内核的,但如果你发现你需要修改内核的某个部分或者说你需要的某个模块并没有编译进内核,那里你可以通过重新编译内核来满足你的需求,比如当我们需要用bcache时,但默认bcache是没有编译进内核的,我们可以通过修改编译配置文件,将bcache编译进内核,以下的编译操作均是在Centos7.3平台上进行的演示. 2 编译前准备工作2.1 编译用户身份选择官方是强调编译linux内核是强烈不建议以root身份来进行编译的,因为这样有

Linux内核源码分析--内核启动之zImage自解压过程

copy from:https://www.cnblogs.com/pengdonglin137/p/3838245.html 阅读目录(Content) zImage来历 piggy.gz压缩文件的特点 vmlinux.lds arch/arm/boot/compressed/head.S arch/arm/boot/compressed/misc.c arch/arm/boot/compressed/decompressed.c lib/decompress_inflate.c 参考: ht