Linux从用户层到内核层系列 - TCP/IP协议栈部分系列7: 基础知识之 - Linux内核源码目录与内核编译选项

题记:本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码,byhankswang和你一起玩转linux开发


轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswang@gmail.com微笑

欢迎加入到CHLK - Linux开发交流群 QQ:327084515 讨论Linux开发相关问题

 

Linux内核源码目录与内核编译选项

1.Linux内核源码目录说明

从kernel.org上下载一份内核代码,ls一下可以看到有很多文件和目录:COPYING 、Documentation 、Kconfig 、 Makefile、REPORTING-BUGS、block 、drivers、 fs 、 init 、 kernel、mm  、 samples、security  、tools 、virt、CREDITS、Kbuild、MAINTAINERS、README、arch、crypto、firmware、include、ipc 、lib、net 、scripts、sound和usr。下面我们逐个分析各个目录和文件的用处。

Arch

目录包括了所有和体系结构相关的核心代码。它下面的每一个子目录都代表一种Linux支持的体系结构,例如X86就是Intel CPU及与之相兼容体系结构的子目录。Arch目录下的子目录包括:/arch/kernel、/arch/mm、/arch/include、/arch/boot、/arch/configs。其中/arch/kernel目录包含了与CPU相关的中断和SMP等信息,/arch/boot包含了系统启动的相关信息和make之后生成的启动镜像文件bzImage。

Copying

目录下是GPL版权申明。对具有GPL版权的源代码改动而形成的程序,或使用GPL工具产生的程序,具有使用GPL发表的义务,如公开源代码。

Credits

目录下是光荣榜。对Linux做出过很大贡献的一些人的信息。

Documentation

目录下是一些文档,linux-3.0.4版本该目录下有217个文件,文件00-INDEX对该目录下的所有文件进行了简要说明,其他文件分别对linux操作系统的各个部分进行了说明。IRQ.txt描述了什么是中断及中断在系统中的枚举定义和头文件的位置;Changes描述了当前版本的系统对各个软件版本的需求,linux 3.0.4要求make的版本为3.8.0,grub的版本为0.93。

Drivers

目录中是系统中所有的设备驱动程序。它又进一步划分成几类设备驱动,每一种有对应的子目录,如声卡的驱动对应于drivers/sound; block 下为块设备驱动程序,比如ide(ide.c)。如果你希望查看所有可能包含文件系统的设备是如何初始化的,你可以看drivers/block/genhd.c中的device_setup()。它不仅初始化硬盘,也初始化,因为安装nfs文件系统的时候需要网络其他: 如, Lib放置核心的库代码; Net,核心与网络相关的代码; Ipc,这个目录包含核心的进程间通讯的代码; Fs,所有的文件系统代码和各种类型的文件操作代码,它的每一个子目录支持一个文件系统,例如fat和ext2。

Fs

目录存放Linux支持的文件系统代码和各种类型的文件操作代码。每一个子目录支持一个文件系统,包括我们常用的文件系统类型:Ext2、Ext3、Ext4、 Proc、Fat和Nfs。其中Ext3文件系统对应的就是/fs/ext3目录。

Include

目录包括编译核心所需要的大部分头文件,例如与平台无关的头文件在include/linux子目录下,与 intel cpu相关的头文件在include/asm-i386子目录下,而include/scsi目录则是有关scsi设备的头文件目录。

Init

目录包含核心的初始化代码(不是系统的引导代码),有main.c和Version.c两个文件。这是研究核心如何工作的好起点。

Ipc

目录包含了核心进程间的通信代码。

Kernel

内核管理的核心代码,此目录下的文件实现了大多数linux系统的内核函数,其中最重要的文件当属sched.c;同时与处理器结构相关代码都放在arch/*/kernel目录下。

Lib

目录包含了核心的库代码,不过与处理器结构相关的库代码被放在arch/*/lib/目录下。

Maintainers

目录存放了维护人员列表,对当前版本的内核各部分都有谁负责。

Makefile

目录第一个Makefile文件。用来组织内核的各模块,记录了个模块间的相互这间的联系和依托关系,编译时使用;仔细阅读各子目录下的Makefile文件对弄清各个文件这间的联系和依托关系很有帮助。

Mm

目录包含了所有独立于 cpu 体系结构的内存管理代码,如页式存储管理内存的分配和释放等。与具体硬件体系结构相关的内存管理代码位于arch/*/mm目录下,例如arch/i386/mm/Fault.c 。

Net

目录里是核心的网络部分代码,其每个子目录对应于网络的一个方面。

ReadMe

文件提供内核的各种编译方法;生成文件的查看方法,如 nm vmlinux | sort | less

Reporting-bugs

目录里是有关报告Bug 的一些内容

Rules.make

目录里是各种Makefilemake所使用的一些共同规则

Scripts

目录包含用于配置核心的脚本文件等。

一般在每个目录下都有一个.depend文件和一个Makefile文件。这两个文件都是编译时使用的辅助文件。仔细阅读这两个文件对弄清各个文件之间的联系和依托关系很有帮助。另外有的目录下还有Readme文件,它是对该目录下文件的一些说明,同样有利于对内核源码的理解。

 

隐藏文件:

.Config

Make oldconfig 和make defconfig会把生成的默认的configuration放到文件.config中。执行Make之后会根据Makefile上下层级依赖关系编译整个系统,.config作为编译各个模块的依据。

 

2.内核编译的Make选项

Make提供的伪目标

Cleaning targets:

  clean                 - Remove most generated files but keep theconfig and enough build support to build external modules

  mrproper          - Remove all generated files + config +various backup files

  distclean           - mrproper + remove editor backup and patchfiles

 

Configuration targets:

  config                 -Update current config utilising a line-oriented program

  nconfig          - Update current config utilising a ncursesmenu 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 notloaded

  localyesconfig  - Update current config converting local modsto core

  silentoldconfig - Same as oldconfig, butquietly, additionally update deps

  defconfig           - New config with default from ARCH supplieddefconfig

  savedefconfig   - Save current config as ./defconfig(minimal config)

  allnoconfig        - New config where all options are answeredwith no

  allyesconfig      - New config where all options are acceptedwith yes

  allmodconfig    - New config selecting modules when possible

  alldefconfig    - New config with all symbols set todefault

  randconfig        - New config with random answer to alloptions

  listnewconfig   - List new options

  oldnoconfig     - Same as silentoldconfig but set newsymbols to n (unset)

 

Other generic targets:

  all                      - Build all targets marked with [*]

*vmlinux              - Build the bare kernel

*modules             - Build all modules

  modules_install - Install all modules toINSTALL_MOD_PATH (default: /)

  firmware_install- Install all firmware toINSTALL_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/assemblytarget only

                    (requires a recent binutilsand recent build (System.map))

  dir/file.ko     - Build module including final link

  modules_prepare - Set up for buildingexternal 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 kernelheaders to INSTALL_HDR_PATH

                    (default:/home/hanks/linux-3.0.4/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 RPMkernel packages

  binrpm-pkg          - Build only the binary kernelpackage

  deb-pkg             - Build the kernel as an debpackage

  tar-pkg            - Build the kernel as anuncompressed tarball

  targz-pkg           - Build the kernel as a gzipcompressed tarball

  tarbz2-pkg          - Build the kernel as a bzip2compressed tarball

  tarxz-pkg           - Build the kernel as a xzcompressed tarball

  perf-tar-src-pkg    - Build perf-3.0.4.tar source tarball

  perf-targz-src-pkg  - Build perf-3.0.4.tar.gz source tarball

  perf-tarbz2-src-pkg - Buildperf-3.0.4.tar.bz2 source tarball

  perf-tarxz-src-pkg  - Build perf-3.0.4.tar.xz source tarball

 

Documentation targets:

 Linux kernel internal documentation indifferent 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 specifictargets (x86):

*bzImage      - Compressed kernel image(arch/x86/boot/bzImage)

  install     - Install kernel using

                  (your) ~/bin/installkernel or

                  (distribution)/sbin/installkernel or

                  install to $(INSTALL_PATH)and run lilo

  fdimage     - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)

  fdimage144  - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)

  fdimage288  - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)

  isoimage    - Create a boot CD-ROM image (arch/x86/boot/image.iso)

                  bzdisk/fdimage*/isoimage alsoaccept:

                  FDARGS="..."  arguments for the booted kernel

                  FDINITRD=file initrd for thebooted kernel

 

  i386_defconfig           - Build for i386

  x86_64_defconfig         - Build for x86_64

 

  make V=0|1  [targets] 0 => quiet build (default), 1=> verbose build

  make V=2   [targets] 2 => give reason forrebuild of target

  make O=dir  [targets] Locate all output files in"dir", including .config

  make C=1   [targets] Check all c source with$CHECK (sparse by default)

  make C=2  [targets] Force check of all c source with $CHECK

  make W=n  [targets] Enable extra gcc checks, n=1,2,3 where

                            1: warnings whichmay be relevant and do not occur too often

                            2: warnings whichoccur quite often but may still be relevant

                            3: more obscurewarnings, can most likely be ignored

                            Multiple levels canbe combined with W=12 or W=123

  make RECORDMCOUNT_WARN=1 [targets] Warn aboutignored mcount sections

 

Execute"make" or "make all" to build all targets marked with [*]

Forfurther info see the ./README file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值