新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > [轉(zhuǎn)貼]Makefile解讀

[轉(zhuǎn)貼]Makefile解讀

作者: 時間:2007-05-23 來源:網(wǎng)絡(luò) 收藏
原文出自:http://www.linuxforum.net
作者:jkl

==========================================
初探
==========================================
的內(nèi)核配置文件有兩個,一個是隱含的.config文件,嵌入到主中;另一個是include/linux/autoconf.h,嵌入到各個c源文件中,它們由make config、make menuconfig、make xconfig這些過程創(chuàng)建。幾乎所有的源文件都會通過linux/config.h而嵌入autoconf.h,如果按照通常方法建立文件依賴關(guān)系(.depend),只要更新過autoconf.h,就會造成所有源代碼的重新編繹。

為了優(yōu)化make過程,減少不必要的重新編繹,開發(fā)了專用的mkdep工具,用它來取代gcc來生成.depend文件。mkdep在處理源文件時,忽略linux/config.h這樣的頭文件,識別源文件宏指令中具有"CONFIG_"特征的行。例如,如果有"#ifdef CONFIG_SMP"這樣的行,它就會在.depend文件中輸出$(wildcard /usr/src/linux/include/config/smp.h)。

include/config/下的文件是另一個工具split-include從autoconf.h中生成,它利用autoconf.h中的CONFIG_標(biāo)記,生成與mkdep相對應(yīng)的文件。例如,如果autoconf.h中有"#undef CONFIG_SMP"這一行,它就生成include/config/smp.h文件,內(nèi)容為"#undef CONFIG_SMP"。這些文件名只在.depend文件中出現(xiàn),內(nèi)核源文件是不會嵌入它們的。每配置一次內(nèi)核,運行split-include一次。split-include會檢查舊的子文件的內(nèi)容,確定是不是要更新它們。這樣,不管autoconf.h修改日期如何,只要其配置不變,make就不會重新編繹內(nèi)核。

如果系統(tǒng)的編繹選項發(fā)生了變化,也能進行增量編繹。為了做到這一點,make每編繹一個源文件時生成一個flags文件。例如編繹sched.c時,會在相同的目錄下生成隱含的.sched.o.flags文件。它是的一個片斷,當(dāng)make進入某個子目錄編繹時,會搜索其中的flags文件,將它們嵌入到Makefile中。這些flags代碼測試當(dāng)前的編繹選項與原來的是不是相同,如果相同,就將自已對應(yīng)的目標(biāo)文件加入FILES_FLAGS_UP_TO_DATE列表,然后,系統(tǒng)從編繹對象表中刪除它們,得到FILES_FLAGS_CHANGED列表,最后,將它們設(shè)為目標(biāo)進行更新。

下一步準(zhǔn)備逐步深入的剖析Makefile代碼。

==========================================
Makefile解讀之二: sub-make
==========================================
Linux各級內(nèi)核源代碼的子目錄下都有Makefile,大多數(shù)Makefile要嵌入主目錄下的Rule.make,Rule.make將識別各個Makefile中所定義的一些變量。變量obj-y表示需要編繹到內(nèi)核中的目標(biāo)文件名集合,定義O_TARGET表示將obj-y連接為一個O_TARGET名稱的目標(biāo)文件,定義L_TARGET表示將obj-y合并為一個L_TARGET名稱的庫文件。同樣obj-m表示需要編繹成模塊的目標(biāo)文件名集合。如果還需進行子目錄make,則需要定義subdir-y和subdir-m。在Makefile中,用"obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o"和"subdir-$(CONFIG_EXT2_FS) += ext2"這種形式自動為obj-y、obj-m、subdir-y、subdir-m添加文件名。有時,情況沒有這么單純,還需要使用條件語句個別對待。Makefile中還有其它一些變量,如mod-subdirs定義了subdir-m以外的所有模塊子目錄。

Rules.make是如何使make進入子目錄的呢? 先來看subdir-y是如何處理的,在Rules.make中,先對subdir-y中的每一個文件名加上前綴"_subdir_"再進行排序生成subdir-list集合,再以它作為目標(biāo)集,對其中每一個目標(biāo)產(chǎn)生一個子make,同時將目標(biāo)名的前綴去掉得到子目錄名,作為子make的起始目錄參數(shù)。subdir-m與subdir-y類似,但情況稍微復(fù)雜一些。由于subdir-y中可能有模塊定義,因此利用mod-subdirs變量將subdir-y中模塊目錄提取出來,再與subdir-m合成一個大的MOD_SUB_DIRS集合。subdir-m的目標(biāo)所用的前綴是"_modsubdir_"。

一點說明,子目錄中的Makefile與Rules.make都沒有嵌入.config文件,它是通過主Makefile向下傳遞MAKEFILES變量完成的。MAKEFILES是make自已識別的一個變量,在執(zhí)行新的Makefile之前,make會首先加載MAKEFILES所指的文件。在主Makefile中它即指向.config。


==========================================
Makefile解讀之三: 模塊的版本化處理
==========================================
模塊的版本化是內(nèi)核與模塊接口之間進行嚴(yán)格類型匹配的一種方法。當(dāng)內(nèi)核配置了CONFIG_MODVERSIONS之后,make dep操作會在include/linux/modules/目錄下為各級Makefile中export-objs變量所對應(yīng)的源文件生成擴展名為.ver的文件。

例如對于kernel/ksyms.c,make用以下命令生成對應(yīng)的ksyms.ver:

gcc -E -D__KERNEL__ -D__GENKSYMS__ ksyms.c | /sbin/genksyms -k 2.4.1 >; ksyms.ver

-D__GENKSYMS__的作用是使ksyms.c中的EXPORT_SYMBOL宏不進行擴展。genksyms命令識別EXPORT_SYMBOL()中的函數(shù)名和對應(yīng)的原型,再根據(jù)其原型計算出該函數(shù)的版本號。

例如ksyms.c中有一行:
EXPORT_SYMBOL(kmalloc);
kmalloc原型是:
void *kmalloc(size_t, int);
genksyms程序?qū)?yīng)的輸出為:
#define __ver_kmalloc 93d4cfe6
#define kmalloc _set_ver(kmalloc)
在內(nèi)核符號表和模塊中,kmalloc將變成kmalloc_R93d4cfe6。

在生成完所有的.ver文件后,make將重建include/linux/modversions.h文件,它包含一系列#include指令行嵌入各個.ver文件。在編繹內(nèi)核本身export-objs中的文件時,make會增加一個"-DEXPORT_SYMTAB"編繹標(biāo)志,它使源文件嵌入modversions.h文件,將EXPORT_SYMBOL宏展開中的函數(shù)名字符串進行版本名擴展;同時,它也定義_set_ver()宏為一空操作,使代碼中的函數(shù)名不受其影響。
在編繹模塊時,make會增加"-include=linux/modversion.h -DMODVERSIONS"編繹標(biāo)志,使模塊中代碼的函數(shù)名得到相應(yīng)版本擴展。

由于生成.ver文件比較費時,make還為每個.ver創(chuàng)建了一個后綴為.stamp時戳文件。在make dep時,如果其.stamp文件比源文件舊才重新生成.ver文件,否則只是更新.stamp文件時戳。另外,在生成.ver和modversions.h文件時,make都會比較新文件和舊文件的內(nèi)容,保持它們修改時間為最舊。



==========================================
Makefile解讀之四: Rules.make的注釋
==========================================
#
# This file contains rules which are shared between multiple Makefiles.
#

#
# False targets.
#
#
.PHONY: dummy

#
# Special variables which should not be exported
#
# 取消這些變量通過環(huán)境向make子進程傳遞。
unexport EXTRA_AFLAGS # as 的開關(guān)
unexport EXTRA_CFLAGS # cc 的開關(guān)
unexport EXTRA_LDFLAGS # ld 的開關(guān)
unexport EXTRA_ARFLAGS # ar 的開關(guān)
unexport SUBDIRS #
unexport SUB_DIRS # 編繹內(nèi)核需進入的子目錄,等于subdir-y
unexport ALL_SUB_DIRS # 所有的子目錄
unexport MOD_SUB_DIRS # 編繹模塊需進入的子目錄
unexport O_TARGET # ld合并的輸出對象
unexport ALL_MOBJS # 所有的模塊名

unexport obj-y # 編繹成內(nèi)核的文件集
unexport obj-m # 編繹成模塊的文件集
unexport obj-n #
unexport obj- #
unexport export-objs # 需進行版本處理的文件集
unexport subdir-y # 編繹內(nèi)核所需進入的子目錄
unexport subdir-m # 編繹模塊所需進入的子目錄
unexport subdir-n
unexport subdir-

#
# Get things started.
#
first_rule: sub_dirs
$(MAKE) all_targets

# 在內(nèi)核編繹子目錄中過濾出可以作為模塊的子目錄。
both-m := $(filter $(mod-subdirs), $(subdir-y))
SUB_DIRS := $(subdir-y)
# 求出總模塊子目錄
MOD_SUB_DIRS := $(sort $(subdir-m) $(both-m))
# 求出總子目錄
ALL_SUB_DIRS := $(sort $(subdir-y) $(subdir-m) $(subdir-n) $(subdir-))


#
# Common rules
#
# 將c文件編繹成匯編文件的規(guī)則,$@為目標(biāo)對象。
%.s: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -S $ -o $@
# 將c文件生成預(yù)處理文件的規(guī)則。
%.i: %.c
$(CPP) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) $ >; $@
# 將c文件編繹成目標(biāo)文件的規(guī)則,$為第一個所依賴的對象;
#
在目標(biāo)文件的目錄下生成flags文件,strip刪除多余的空格,subst將逗號替換成冒號
。
%.o: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -c -o $@ $
@ (
echo 'ifeq ($(strip $(subst $(comma),:,$(CFLAGS) $(EXTRA_CFLAGS)
$(CFLAGS_$@))),$$(strip $$(subst $$(comma),:,$$(CFLAGS) $$(EXTRA_CFLAGS)
$$(CFLAGS_$@))))' ;
echo 'FILES_FLAGS_UP_TO_DATE += $@' ;
echo 'endif'
) >; $(dir $@)/.$(notdir $@).flags
# 匯編文件生成目標(biāo)文件的規(guī)則。
%.o: %.s
$(AS) $(AFLAGS) $(EXTRA_CFLAGS) -o $@ $

# Old makefiles define their own rules for compiling .S files,
# but these standard rules are available for any Makefile that
# wants to use them. Our plan is to incrementally convert all
# the Makefiles to these standard rules. -- rmk, mec

ifdef USE_STANDARD_AS_RULE
# 匯編文件生成預(yù)處理文件的標(biāo)準(zhǔn)規(guī)則。
%.s: %.S
$(CPP) $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$@) $ >; $@
# 匯編文件生成目標(biāo)文件的標(biāo)準(zhǔn)規(guī)則。
%.o: %.S
$(CC) $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$@) -c -o $@ $

endif
# c文件生成調(diào)試列表文件的規(guī)則,$*擴展為目標(biāo)的主文件名。
%.lst: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -g -c -o $*.o $
$(TOPDIR)/scripts/makelst $* $(TOPDIR) $(OBJDUMP)
#
#
#
all_targets: $(O_TARGET) $(L_TARGET)

#
# Rule to compile a set of .o files into one .o file
#
ifdef O_TARGET
$(O_TARGET): $(obj-y)
rm -f $@
# $^擴展為全部依賴對象,如果obj-y為空就生成一個同名空的庫文件。
ifneq $(strip $(obj-y))
$(LD) $(EXTRA_LDFLAGS) -r -o $@ $(filter $(obj-y), $^)
else
$(AR) rcs $@
endif
# 生成flags文件的shell語句。
@ (
echo 'ifeq ($(strip $(subst $(comma),:,$(EXTRA_LDFLAGS)
$(obj-y))),$$(strip $$(subst $$(comma),:,$$(EXTRA_LDFLAGS) $$(obj-y))))' ;

echo 'FILES_FLAGS_UP_TO_DATE += $@' ;
echo 'endif'
) >; $(dir $@)/.$(notdir $@).flags
endif # O_TARGET

#
# Rule to compile a set of .o files into one .a file
#
# 將obj-y組合成庫L_TARGET的方法。
ifdef L_TARGET
$(L_TARGET): $(obj-y)
rm -f $@
$(AR) $(EXTRA_ARFLAGS) rcs $@ $(obj-y)
@ (
echo 'ifeq ($(strip $(subst $(comma),:,$(EXTRA_ARFLAGS)
$(obj-y))),$$(strip $$(subst $$(comma),:,$$(EXTRA_ARFLAGS) $$(obj-y))))' ;

echo 'FILES_FLAGS_UP_TO_DATE += $@' ;
echo 'endif'
) >; $(dir $@)/.$(notdir $@).flags
endif


#
# This make dependencies quickly
#
# wildcard為查找目錄中的文件名的宏。
fastdep: dummy
$(TOPDIR)/scripts/mkdep $(wildcard *.[chS] local.h.master) >; .depend
ifdef ALL_SUB_DIRS
#
將ALL_SUB_DIRS中的目錄名加上前綴_sfdep_作為目標(biāo)運行子make,并將ALL_SUB_DIRS
通過
# 變量_FASTDEP_ALL_SUB_DIRS傳遞給子make。
$(MAKE) $(patsubst %,_sfdep_%,$(ALL_SUB_DIRS))
_FASTDEP_ALL_SUB_DIRS=$(ALL_SUB_DIRS)
endif

ifdef _FASTDEP_ALL_SUB_DIRS
#
與上一段相對應(yīng),定義子目錄目標(biāo),并將目標(biāo)名還原為目錄名,進入該子目錄make。
$(patsubst %,_sfdep_%,$(_FASTDEP_ALL_SUB_DIRS)):
$(MAKE) -C $(patsubst _sfdep_%,%,$@) fastdep
endif


#
# A rule to make subdirectories
#
# 下面2段完成內(nèi)核編繹子目錄中的make。
subdir-list = $(sort $(patsubst %,_subdir_%,$(SUB_DIRS)))
sub_dirs: dummy $(subdir-list)

ifdef SUB_DIRS
$(subdir-list) : dummy
$(MAKE) -C $(patsubst _subdir_%,%,$@)
endif

#
# A rule to make modules
#
# 求出有效的模塊文件表。
ALL_MOBJS = $(filter-out $(obj-y), $(obj-m))
ifneq $(strip $(ALL_MOBJS))
# 取主目錄TOPDIR到當(dāng)前目錄的路徑。
PDWN=$(shell $(CONFIG_SHELL) $(TOPDIR)/scripts/pathdown.sh)
endif

unexport MOD_DIRS
MOD_DIRS := $(MOD_SUB_DIRS) $(MOD_IN_SUB_DIRS)
# 編繹模塊時,進入模塊子目錄的方法。
ifneq $(strip $(MOD_DIRS))
.PHONY: $(patsubst %,_modsubdir_%,$(MOD_DIRS))
$(patsubst %,_modsubdir_%,$(MOD_DIRS)) : dummy
$(MAKE) -C $(patsubst _modsubdir_%,%,$@) modules
# 安裝模塊時,進入模塊子目錄的方法。
.PHONY: $(patsubst %,_modinst_%,$(MOD_DIRS))
$(patsubst %,_modinst_%,$(MOD_DIRS)) : dummy
$(MAKE) -C $(patsubst _modinst_%,%,$@) modules_install
endif

# make modules 的入口。
.PHONY: modules
modules: $(ALL_MOBJS) dummy
$(patsubst %,_modsubdir_%,$(MOD_DIRS))

.PHONY: _modinst__
# 拷貝模塊的過程。
_modinst__: dummy
ifneq $(strip $(ALL_MOBJS))
mkdir -p $(MODLIB)/kernel/$(PDWN)
cp $(ALL_MOBJS) $(MODLIB)/kernel/$(PDWN)
endif

# make modules_install 的入口,進入子目錄安裝。
.PHONY: modules_install
modules_install: _modinst__
$(patsubst %,_modinst_%,$(MOD_DIRS))

#
# A rule to do nothing
#
dummy:

#
# This is useful for testing
#
script:
$(SCRIPT)

#
# This sets version suffixes on exported symbols
# Separate the object into normal objects and exporting objects
# Exporting objects are: all objects that define symbol tables
#
ifdef CONFIG_MODULES
# list-multi列出那些由多個文件復(fù)合而成的模塊;
# 從編繹文件表和模塊文件表中過濾出復(fù)合模塊名。
multi-used := $(filter $(list-multi), $(obj-y) $(obj-m))
# 取復(fù)合模塊的構(gòu)成表。
multi-objs := $(foreach m, $(multi-used), $($(basename $(m))-objs))
# 求出需進行編譯的總模塊表。
active-objs := $(sort $(multi-objs) $(obj-y) $(obj-m))

ifdef CONFIG_MODVERSIONS
ifneq $(strip $(export-objs))
# 如果有需要進行版本化的文件。
MODINCL = $(TOPDIR)/include/linux/modules

# The -w option (enable warnings) for genksyms will return here in 2.1
# So where has it gone?
#
# Added the SMP separator to stop module accidents between uniprocessor
# and SMP Intel boxes - AC - from bits by Michael Chastain
#

ifdef CONFIG_SMP
genksyms_smp_prefix := -p smp_
else
genksyms_smp_prefix :=
endif
# 從源文件計算版本文件的規(guī)則。
$(MODINCL)/%.ver: %.c
@if [ ! -r $(MODINCL)/$*.stamp -o $(MODINCL)/$*.stamp -ot $ ]; then
echo '$(CC) $(CFLAGS) -E -D__GENKSYMS__ $';
echo '| $(GENKSYMS) $(genksyms_smp_prefix) -k
$(VERSION).$(PATCHLEVEL).$(SUBLEVEL) >; $@.tmp';
$(CC) $(CFLAGS) -E -D__GENKSYMS__ $
| $(GENKSYMS) $(genksyms_smp_prefix) -k
$(VERSION).$(PATCHLEVEL).$(SUBLEVEL) >; $@.tmp;
if [ -r $@ ] cmp -s $@ $@.tmp; then echo $@ is unchanged; rm -f
$@.tmp;
else echo mv $@.tmp $@; mv -f $@.tmp $@; fi;
fi; touch $(MODINCL)/$*.stamp
#
將版本處理源文件的擴展名改為.ver,并加上完整的路徑名,它們依賴于autoconf.h?br>;?br>;$(addprefix $(MODINCL)/,$(export-objs:.o=.ver)):
$(TOPDIR)/include/linux/autoconf.h

# updates .ver files but not modversions.h
# 通過fastdep,逐個生成export-objs對應(yīng)的版本文件。
fastdep: $(addprefix $(MODINCL)/,$(export-objs:.o=.ver))

# updates .ver files and modversions.h like before (is this needed?)
# make dep過程的入口
dep: fastdep update-modverfile

endif # export-objs

# update modversions.h, but only if it would change
# 刷新版本文件的過程。
update-modverfile:
@(echo #ifndef _LINUX_MODVERSIONS_H;
echo #define _LINUX_MODVERSIONS_H;
echo #include linux/modsetver.h>;;
cd $(TOPDIR)/include/linux/modules;
for f in *.ver; do
if [ -f $$f ]; then echo #include linux/modules/$${f}>;; fi;
done;
echo #endif;
) >; $(TOPDIR)/include/linux/modversions.h.tmp
@if [ -r $(TOPDIR)/include/linux/modversions.h ] cmp -s
$(TOPDIR)/include/linux/modversions.h
$(TOPDIR)/include/linux/modversions.h.tmp; then
echo $(TOPDIR)/include/linux/modversions.h was not updated;
rm -f $(TOPDIR)/include/linux/modversions.h.tmp;
else
echo $(TOPDIR)/include/linux/modversions.h was updated;
mv -f $(TOPDIR)/include/linux/modversions.h.tmp
$(TOPDIR)/include/linux/modversions.h;
fi
$(active-objs): $(TOPDIR)/include/linux/modversions.h

else
# 如果沒有配置版本化,modversions.h的內(nèi)容。
$(TOPDIR)/include/linux/modversions.h:
@echo #include linux/modsetver.h>; >; $@

endif # CONFIG_MODVERSIONS

ifneq $(strip $(export-objs))
# 版本化目標(biāo)文件的編繹方法。
$(export-objs): $(export-objs:.o=.c) $(TOPDIR)/include/linux/modversions.h
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -DEXPORT_SYMTAB -c $(@:.o=.c)
@ (
echo 'ifeq ($(strip $(subst $(comma),:,$(CFLAGS) $(EXTRA_CFLAGS)
$(CFLAGS_$@) -DEXPORT_SYMTAB)),$$(strip $$(subst $$(comma),:,$$(CFLAGS)
$$(EXTRA_CFLAGS) $$(CFLAGS_$@) -DEXPORT_SYMTAB)))' ;
echo 'FILES_FLAGS_UP_TO_DATE += $@' ;
echo 'endif'
) >; $(dir $@)/.$(notdir $@).flags
endif

endif # CONFIG_MODULES


#
# include dependency files if they exist
#
# 嵌入源文件之間的依賴關(guān)系。
ifneq ($(wildcard .depend),)
include .depend
endif
# 嵌入頭文件之間的依賴關(guān)系。
ifneq ($(wildcard $(TOPDIR)/.hdepend),)
include $(TOPDIR)/.hdepend
endif

#
# Find files whose flags have changed and force recompilation.
# For safety, this works in the converse direction:
# every file is forced, except those whose flags are positively
up-to-date.
#
# 已經(jīng)更新過的文件列表。
FILES_FLAGS_UP_TO_DATE :=

# For use in expunging commas from flags, which mung our checking.
comma = ,
# 將當(dāng)前目錄下所有flags文件嵌入。
FILES_FLAGS_EXIST := $(wildcard .*.flags)
ifneq ($(FILES_FLAGS_EXIST),)
include $(FILES_FLAGS_EXIST)
endif
# 將無需更新的文件從總的對象中刪除。
FILES_FLAGS_CHANGED := $(strip
$(filter-out $(FILES_FLAGS_UP_TO_DATE),
$(O_TARGET) $(L_TARGET) $(active-objs)
))

# A kludge: .S files don't get flag dependencies (yet),
# because that will involve changing a lot of Makefiles. Also
# suppress object files explicitly listed in $(IGNORE_FLAGS_OBJS).
# This allows handling of assembly files that get translated into
# multiple object files (see arch/ia64/lib/idiv.S, for example).
#
# 將由匯編文件生成的目件文件從FILES_FLAGS_CHANGED刪除。
FILES_FLAGS_CHANGED := $(strip
$(filter-out $(patsubst %.S, %.o, $(wildcard *.S)
$(IGNORE_FLAGS_OBJS)),
$(FILES_FLAGS_CHANGED)))
# 將FILES_FLAGS_CHANGED設(shè)為目標(biāo)。
ifneq ($(FILES_FLAGS_CHANGED),)
$(FILES_FLAGS_CHANGED): dummy
endif
/pre>;
linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)


關(guān)鍵詞: Makefile Linux

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉