?
?
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 和 模塊文件
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
