makefilesimplyanalyze(簡(jiǎn)單地分析文件)_第1頁(yè)
makefilesimplyanalyze(簡(jiǎn)單地分析文件)_第2頁(yè)
makefilesimplyanalyze(簡(jiǎn)單地分析文件)_第3頁(yè)
makefilesimplyanalyze(簡(jiǎn)單地分析文件)_第4頁(yè)
makefilesimplyanalyze(簡(jiǎn)單地分析文件)_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、make file simply analyze (簡(jiǎn)單地分析文件)學(xué)習(xí)linux 2. 6內(nèi)核makefile分析2010 - 04 01 12: 47來(lái)源 與非網(wǎng) 博客.關(guān)鍵字makefile linux配置.摘要由于linux的獨(dú)特優(yōu)勢(shì),使越來(lái)越多的企業(yè)和科研機(jī)構(gòu)把目 光轉(zhuǎn)向linux的開(kāi)發(fā)和研究上.目前l(fā)inux最新的穩(wěn)定內(nèi)核版本為 2. 6. 17,但是當(dāng)今絕大部分對(duì)于linux makefile的介紹文章都是基 于2. 4內(nèi)核的,可以說(shuō)關(guān)于2. 6內(nèi)核makefile相關(guān)的文章鳳毛麟角, 筆者抽時(shí)間完成了這篇分析文章,讓讀者迅速熟悉linux最新 makefile體系,從而加深對(duì)內(nèi)

2、核的理解,同時(shí)也希望能對(duì)linux在 公司的推廣起到一定的推動(dòng)作用,算是拋磚引玉吧.1 makefile組織層次linux的make體系由如下幾部分組成.?頂層 makefile頂層makefile通過(guò)讀取配置文件,遞歸編譯內(nèi)核代碼樹(shù)的相關(guān)目錄, 從而產(chǎn)生兩個(gè)重要的目標(biāo)文件:vnilirmx和模塊.?內(nèi)核相關(guān)makefile位于arch / $ (arch)目錄下,為頂層makefile提供與具體硬件體 協(xié)結(jié)構(gòu)相關(guān)的信息.?公共編譯規(guī)則定義文件. 包括 makefile .build 、 makefile clea n、 makefile .1 ib、 makefile, host等文件組成這

3、些文件位于scripts目錄中,定義了 編譯需要的公共的規(guī)則和定義.?內(nèi)核配置文件.config通過(guò)調(diào)用make menuconfig或者make xconfig命令,用戶(hù)可以選擇 需要的配置來(lái)生成期望的目標(biāo)文件.?其他 makefile主要為整個(gè)makefile體系提供各自模塊的目標(biāo)文件定義,上層 makefile根據(jù)它所定義的冃標(biāo)來(lái)完成各自模塊的編譯.2 makefile的使用在編譯內(nèi)核之前,用戶(hù)必須首先完成必要的配置.linux內(nèi)核提供了 數(shù)不勝數(shù)的功能,支持眾多的硬件體系結(jié)構(gòu),這就需要用戶(hù)對(duì)將要 生成的內(nèi)核進(jìn)行裁減.內(nèi)核提供了多種不同的工具來(lái)簡(jiǎn)化內(nèi)核的配置, 最簡(jiǎn)單的一種是字符界面下命

4、令行工具.make config這個(gè)工具會(huì)依次遍歷內(nèi)核所有的配置項(xiàng),要求用戶(hù)進(jìn)行逐項(xiàng)的選擇 配置這個(gè)工具會(huì)耗費(fèi)用戶(hù)太多時(shí)間,除非萬(wàn)不得以(你的編譯主機(jī) 不支持其他配置工具)一般不建議使用.用戶(hù)還可以使用利用ncurse庫(kù)編制的圖形界面工具,這就是大名鼎 鼎的.make menuconfig 相信以前對(duì)2. 4內(nèi)核比較熟悉的用戶(hù)一定不會(huì)陌生當(dāng)然在2. 6內(nèi)核 中提供了更漂亮和方便的基于xll的圖形配置工具.make xconfig當(dāng)用戶(hù)使用這個(gè)工具對(duì)limix內(nèi)核進(jìn)行配置時(shí),界面下方會(huì)出現(xiàn)這 個(gè)配置項(xiàng)相關(guān)的幫助信息和簡(jiǎn)單描述,當(dāng)你對(duì)內(nèi)核配置選項(xiàng)不太熟 悉時(shí),建議你使用這個(gè)工具來(lái)進(jìn)行內(nèi)核配置.wh

5、en the user completes the configuration, the configuration tool automatically generates the .config file, which is stored in the root directory of the kernel code tree users can easily find it and, of course, users can make simple changes to the file directly. however, after you have modified the co

6、nfiguration file, you must validate and update the configuration by following the command:make oldconfigthe difference with version 2.4 is that the user does not need to display the call make dep command to generate dependency files, and the kernel automatically maintains dependencies between the co

7、dewhen everything is done, the user simply needs to type make, and all the rest of the work will be done automatically for you by makefile3 makefile compilation f1owwhen the user compiles the kernel version with liniix's makefile, the makefile compilation process follows:use the command line or

8、graphical interface configuration tool to cut the kernel and generate the .config configuration filesave the kernel version information to include/1 inux/vers io n. hproduces symbolic link include/asm, pointing to the actual directory include/asm-$(arch)make necessary preparations for the final goal

9、 file generationrecursively enters directories such as /init, /core, /drivers, /net, /lib, and subdirectories to compile and generate all of the target filesthe target file generated by linking the above process generates vmlinux, and the vmlinux is stored in the root directory of the kernel code tr

10、eefinally, the final image boot image is created based on the later compiled processing rules defined by the arch/$(arch) /makefile file, including creation of boot records, preparation of initrd mappings, and associated processing4 makefile key rules and definitions are described1) goal definitiont

11、he target is defined as the core part of the makefile file, define the target according to the special need to notify the makefile compiler options link object files which generate the target file, at the same time, to control which is compiled into subdirectories recursively.this example, the makef

12、ile file, is located in the /fs/ext2 directory:#makefile for the linux ext2-filesystem routines #obj-$(c0nfig_ext2_fs)二 ext2.oext2一y:二 balloco, bitmapo, dir.o, fileo, fsynco, ialloco, inode o ioct 1. o namei o super.o symlink.oext2-$(c0nfig_ext2_fs_xattr)二 xattr.o xattr_user. o xattr_trusted oext2-$

13、(c0nfig_ext2_fs_p0six_acl)二 acl. oext2-$(c0nfig_ext2_fs_security)二 xattr_security. oext2-$(c0nfig_ext2_fs_xip)二 xip. othis representation is associated with the ext2 object file consists of the definition of ext2-y file list, including ext2-$(*) is decided by the configuration of. config kernel conf

14、iguration file, makefile will eventually generate a unified ext2. o file in this directory (by obj-$ (c0nfig_ext2_fs) decision) where obj-y represents the set of target files required to generate the vmlinux file,the specific file depends on the kernel configuration.makefile compiles all the files d

15、efined in $(obj-y), and then calls the linker to link these files to the built-in. o file finally, the built-in.o file is linked to the vmlinux through the top-level makefile it is important to note that the order of $ (obj-y) files is important the list file can be repeated the file will be linked

16、to built-in. o for the first time, and the later name file will be ignored. the order of documents determines the order in which they are invoked, which requires special attention.the reader may find the lib-y definition in some makefile, and all the target files included in the lib-y definition wil

17、l be compiled into a unified library file in that directory. it is worth noting that the lib-y definition is generally limited to the lib and arch/$ (arch) /lib directoriesthe system makefile file and the top-level makefile file define the rules for how to build vmlinux files$(head-y) enumerates the

18、 object files that are first linked to vmlinux.$ (libs-y) lists directories that can find lib. a filesthe rest of the variables list directories that can find embedded object filesobjects listed after $ (init-y) are located after $ (head-y) objectsthen the following order of position:$ (core-y), $ (

19、libs-y), $ (drivers-y) and $ (net-y)the top-level makefile defines all generic directories, and the arch/$ (arch) /makefile file simply adds system related directoriesfor example: #arch/i386/makefilelibs-y 二 arch/1386/1ib/core-y 二 arch/i386/kernel/arch/i386/min/ arch/i386/$(mcore-y) / arch/i386/cryp

20、to/drivers-$ (config_math_emulation)二 arch/i386/math-emu/drivers-$(config_pci)二 arch/i386/pci/2) directory recursionthe makefile file is only responsible for the current directory object files, subdirectories in the file directory by makefile is responsible for the compilation, the compiler system u

21、sing obj-y and obj-m to automatically compile recursive sub files in the directory.for fs/makefile:obj-$(c0nfig_ext2_fs)二 ext2/if config_ext2_fs is set to y or m in the kernel configuration file config, the kernel makefile automatically enters the ext2 directory to compile the kernel makefile uses o

22、nly this information to determine whether the directory needs to be compiled, and the makefile in the subdirectory specifies which files are compiled into modules and which files are compiled into the kerne1.3) dependencylinux makefile defines the dependent dependencies by the file name0. cmd genera

23、ted in the compilation process (for example, for the main.c file, which corresponds to the dependency file name main. o. cmd).the dependencies of general files are made up of the following:all previous dependency files (including all relevant *c and *h)all files associated with the c0nfig_ optioncom

24、pile the command line used by the destination filethe dependency filemain, o cmd of the main. c file located in the init directory reads as follows, and the reader can combine to understand the three components of the above file dependencies:cmd_init/main. o:二 gcc -m32 -wp, -md init/ main. o. d, -no

25、stdinc -isystem /usr/1ib/gcc-1ib/i386-redhat-linux/3 2. 2/include -d_kernel_ -iinclude -iinclude2-i/home/linux/linux-2.6. 17. 11/include -include include/linux/autoconf. h -i/home/linux/ linux一2 6. 17. 11/init -iinit -wall -wundef -wstrict-prototypes -wno-trigraphs -fno-strict-aliasing -fno-common -

26、os 一fomit-franie-pointer -pipe -msoft-float -mpreferred-stack-boundary=2 -march二i686 -mcpu=pentium4 -mregparm=3 -ffreestanding -i/home/linux/ linux-2. 6. 17. ll/include/asni-i386/niach-default -iinclude/asm-i386/mach-default d kbuildstr (s) =#s -d kbuild_basename=kbuild_str (main) -dkbuild_modname-k

27、build_str (main) -c -o init/. tmp_main. o /home/linux/linux-2. 6. 17. 11/init/ main. cdeps_init/main.o:二 /home/linux/linux-2. 6. 17. 11/init/main. c $(wildcard, include/config/x86/local/apic. h) $(wildcard, include/config/acpi. h) # due to the length of the relationship, omitting some definitionsinc

28、lude2/asm/mpspec_def. h /home/linux/linux-2. 6 17. il/include/asni-i386/niach-default/ni ach_mpspec. h include2/asm/io_apich include2/asm/apich init/main. o: $(deps_init/main o)$ (deps_init/main. o):4) special rulesspecial rules are used when kernel compilation requires rule definitions without the

29、corresponding definition. a typical example, such as the generation rules for compile time header files other examples are special rules for system makefile compiling boot images special rules are written in accordance with common makefile rulesthe compiler cannot execute the directory in which the

30、makefile is located, so all the special rules need to provide the relative path between the previous file and the target filewhen defining a special rule, two variables are used:$ (src):$ (src) is the relative path to the makefile file directory, when using the code file in the treeuse this variable

31、 $ (src)$ (obj) : $ (obj) is a relative path to the target file directory.the generated file uses the $ (obj) variablefor example: #drivers/scsi/makefile$ (obj) /53c8xx_d. h: $ (src) /53c7,8xx.scr $ (src) /script_asm pl$ (cpp) -dchip二810 - < $< | (src) /script_asm. pl $.this is the special com

32、pilation rule using plain syntaxthe destination file relies on two prerequisite filesthe prefix of the destination file is $ (obj), and the prefix of the prerequisite file is $ (src) (because they are not generated files)5) boot imagethe system makefile file defines the target object that compiles t

33、he vmlinux file, compresses them and encapsulates them into the boot code and copies it to the right location. this includes various installation commands. in linux, makefile does not provide standardized methods for all architectures, so makefile is often required to provide additional processing r

34、ules under specific hardware architecturesadditional processing is often in the boot/ directory under arch/$ (arch) / below.the kernel compilation system cannot provide a convenient way to create a target system file in the boot/ directory. therefore, arch/$ (arch) /makefile calls the make command t

35、o set up the target system file in the boot/ directory. the recommended method is to set the call in arch/$(arch) /makefile, and use the full path referenee arch/$ (arch) /boot/makefilefor example: #arch/i386/makefileboot:二 arch/i386/bootbzlmage: vmlinux$(q) $ (make) $ (build)二 $(boot) $ (boot) /$it

36、 is recommended to call the make command in the subdirectory using the $(q) $ (make) $ (build) =<dir>.when the make command without arguments is executed, the first target object is compiled first in the top-level makefile, the first target object is all.an architecture needs to define a defau

37、lt bootable imageadding new prerequisite files to all targets can set default target objects different from those of vmlinuxfor example: #arch/i386/makefileall: bzimagewhen you execute the make command without arguments, the bzimage file will be compiled6) compile commands frequentlyif_changedif nec

38、essary, execute the passed commandusage:$(bu訂tin-target): $ (obj-y) force$ (call, if changed, link_o_target)when this rule is used, it checks which files need to be updated, or the command line is changed this situation will force laterrecompile the compiled options to change the executable file the

39、 target object that uses if changed must be listed in $(builtin-target), otherwise the command line check will fail and the target will compile all the timeif_changed_depif necessary, execute the passed command and update the dependency fileusage:%. o: %. s force$(cal1, if_changed_dep, as_o_s)when t

40、his rule is used, it checks which files need to be updated, or the command line is changed. at the same time, it will review dependency changes and generate new dependencies this is the difference from the if_changed command7) custom commandswhen the normal execution of a compile command occurs, bri

41、ef information is displayed (to add the v 二 1 to the comma nd line to display the detailed command) to enable a custom command to do this, you need to set two variables:quiet_cmd_<command> 一 what will be displayedcmd_<coniniand> 一 the command being executedfor example: #quiet_cmd_image 二

42、 $ buildcmd_image 二 $ (obj) /工具/美元建造(buildflags) $ (obj) / vmlinux. bin > $ 冃標(biāo)+二先$ (obj) /先:$ (obj) / vmlinux. bin $ (obj) /工具/建設(shè)力(電話if_changed美元,圖像)內(nèi)核執(zhí)行使命令編譯$ (obj) /先冃標(biāo)時(shí)將顯示:建立弓/ 1386 /啟動(dòng)/先8) 預(yù)處理鏈接腳本當(dāng)編譯vmlinux映像時(shí)將使用弓/ $ (拱)/核/ vmlinux lds鏈接腳 本。相同目錄下的vmlinuxo lds的文件是這個(gè)腳木的預(yù)處理的變體lds。 內(nèi)核編譯系統(tǒng)知曉。文件。并使

43、用規(guī)則* lds的-* ldso例如:#弓/ i386 /核/ makefile總:二 vmlinux ids# makefile岀口 cppf lags_vmlinux. ids + 二 pcu$(拱)$ (總是)賦值語(yǔ)句告訴編譯系統(tǒng)編譯冃標(biāo)是vmlinuxo ldso$ (cppflags_vmlinuxo ld)賦值語(yǔ)句告訴編譯系統(tǒng)編譯vmlinux lds冃標(biāo)的編譯選項(xiàng)。編譯lds時(shí)將使用到下面這些變量*:cppflags: 定義在頂層 makefileextra_cppflags:可以設(shè)置在編譯的makefile文件中cppflags. $ (a):目標(biāo)編譯選項(xiàng)。注意要使用文件全名。

44、9)主機(jī)輔助程序的編譯內(nèi)核編譯系統(tǒng)支持在編譯階段編譯主機(jī)可執(zhí)行程序。為了使用主機(jī)程 序需要兩個(gè)步驟:第一個(gè)步驟使用hostprogs-y變量告訴內(nèi)核編譯系 統(tǒng)有主機(jī)程序可用。第二步給主機(jī)程序添加潛在的依賴(lài)關(guān)系。有兩種 方法美元,在規(guī)則中增加依賴(lài)關(guān)系或使用(總是)變量。這一部分的 內(nèi)容相對(duì)于其他內(nèi)核文件的編譯要簡(jiǎn)單的多,感興趣的讀者可以參考 腳木/建立中的相關(guān)內(nèi)容makefileo10)清潔機(jī)制清潔命令清除在編譯內(nèi)核生成的大部分文件,例如主機(jī)程序,列舉在 $ (hostprogs-y)、$ (hostprogs-m)、(總是)、美元美元美元(超 y) 和(冃標(biāo))中冃標(biāo)文件都將被刪除。代碼冃錄數(shù)中

45、的”*。組 織“、” *°k0”文件和一些由編譯系統(tǒng)產(chǎn)生的附加文件也將被刪除。附加文件可以使用$ (干凈的文件)進(jìn)行定義。例如:#司機(jī)/ pci / makefile清理文件:二devlisto h h列表。當(dāng)執(zhí)行“干凈”的命令時(shí),"devlist列表。ho h”兩個(gè)文件將被刪 除。內(nèi)核編譯系統(tǒng)默認(rèn)這些文件與makefile具有相同的相對(duì)路徑, 否則需要設(shè)置以/開(kāi)頭的絕對(duì)路徑。刪除整個(gè)冃錄使用以下方式:例如:#腳本/包/文件干凈的目錄:二(objtree美元)/ debian /這樣就將刪除包括子目錄在內(nèi)的整個(gè)debian目錄。如果不使用以/ 開(kāi)頭的絕對(duì)路徑內(nèi)核編譯系統(tǒng)見(jiàn)默認(rèn)使用相對(duì)路徑。typically, the kernel compilation system enters the subdirectory according to obj-*:二 dir/,/, but in the system makefile, explicit use of the following methods is required:for example: #arch/i386/boot/makefilesubdir-:二 compressed/the above assignment statement indicates

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論