標籤:linux kernel makefile
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 helpCleaning 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 filesConfiguration 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 valueOther 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 tarballDocumentation 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 filesArchitecture 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 和 模組檔案