日期:2014-05-16  浏览次数:20826 次

Linux下makefile和kconfig简单解析

  想要研究linux下的驱动开发,makefile和kconfig是我们必须关注的,kbuild系统更是我们必须了解的重要部分,那我们就先看看kbuild的英文文档吧!

MostMakefiles within the kernel are kbuild Makefiles that use the kbuildinfrastructure. This chapter introduces the syntax used in the kbuildmakefiles.
The preferred name for the kbuild files are 'Makefile' but 'Kbuild' can beused and if both a 'Makefile' and a 'Kbuild' file exists, then the 'Kbuild'
file will be used.

Section3.1 "Goal definitions" is a quick intro, further chaptersprovide more details, with real examples.

---3.1 Goal definitions

 Goaldefinitions are the main part (heart) of the kbuild Makefile.These lines definethe files to be built, any special compilation options, and anysubdirectories to be entered recursively.

 Themost simple kbuild makefile contains one line:

 Example:
 obj-y += foo.o

 Thistells kbuild that there is one object in that directory,named  foo.o. foo.o will be built from foo.c or foo.S.

 Iffoo.o shall be built as a module, the variable obj-m is used.
 Therefore the following pattern is often used:

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

 $(CONFIG_FOO)evaluates to either y (forbuilt-in) or m (for module). If CONFIG_FOO isneither y nor m, then the file will not be compiled nor linked.

---3.2 Built-in object goals - obj-y

 Thekbuild Makefile specifies object files for vmlinux in the $(obj-y) lists. These lists depend on the kernel  configuration.

 Kbuildcompiles all the $(obj-y) files.  It then calls  "$(LD) -r" tomerge these files into one built-in.o file.  built-in.ois later linked into vmlinux bythe parent Makefile.

Theorder of files in $(obj-y) is significant.  Duplicates in the listsare allowed: the first instance will be linked into built-in.o andsucceeding instances will be ignored.

Linkorder is significant, because certain functions (module_init() / __initcall)will be called during boot in the order they appear. So keep in mind thatchanging the link order may e.g. change the order in which your SCSIcontrollers are detected, and thus your disks are renumbered.

---3.3 Loadable module goals - obj-m

$(obj-m) specifyobject files which are built as loadable kernel modules.A module may bebuilt from one source file or several source files. In the case of one sourcefile, the kbuild makefile simply adds the file to $(obj-m).