转载地址:https://blog.csdn.net/cdaozhi/article/details/6135032?utm_source=blogxgwz0
在linux kernel与Uboot中,rules.mk 被包含到没有子目录的makefile文件中,用于产生文件依赖关系文件.depend文件,下面我们看一下uboot的rules.mk文件:
源文件:
#
# (C) Copyright 2006
# Wolfgang Denk, DENX Software Engineering, [email protected].
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
#########################################################################
_depend: $(obj).depend
$(obj).depend: $(src)Makefile $(TOPDIR)/config.mk $(SRCS) $(HOSTSRCS)
@rm -f $@
@touch $@
@for f in $(SRCS); do /
g=`basename $$f | sed -e 's//(.*/)/./w//1.o/'`; /
$(CC) -M $(CPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; /
done
@for f in $(HOSTSRCS); do /
g=`basename $$f | sed -e 's//(.*/)/./w//1.o/'`; /
$(HOSTCC) -M $(HOSTCPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; /
done
$(HOSTOBJS): $(obj)%.o: %.c
$(HOSTCC) $(HOSTCFLAGS) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c
$(NOPEDOBJS): $(obj)%.o: %.c
$(HOSTCC) $(HOSTCFLAGS_NOPED) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c
解析:
_depend: $(obj).depend
$(obj).depend: $(src)Makefile $(TOPDIR)/config.mk $(SRCS) $(HOSTSRCS) //描述.depend 的依赖关系
@rm -f $@ //强制删掉所有的目标文件,即*.depend文件
@touch $@ //强制更新目标文件属性,以便使文件过期
@for f in $(SRCS); do / //for循环读取子目录下的文件
g=`basename $$f | sed -e 's//(.*/)/./w//1.o/'`; /
$(HOSTCC) -M $(HOSTCPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; /
done
//这一段比较难懂,我们把它分开来看,“basename $$f” 代表去变量$f的文件名,去掉路径等信息。“$f”为SRCS目录下的具体文 件。“|”为管道描述,“sed -e 's//(.*/)/./w//1.o/'`”为一个sed命令的文本处理部分,”-e“ 命令行,"s/.../.."为替换命令,“/(.*/)/./w”代表匹配任意除换行符开始的任意长度的字符接着是”.“字符最后是一个匹配字母或数字或下划线或汉字,"//1.O/" /1为前面的.*/匹配字符串 ,".O"加上.o。简单的说,此句实现的功能为将一个源文件替换为一个.o的OBJ文件
@for f in $(HOSTSRCS); do /
g=`basename
f|sed−e′s//(.∗/)/./w//1.o/′‘;/$(HOSTCC)−M$(HOSTCPPFLAGS)−MQ$(obj)
f|sed−e′s//(.∗/)/./w//1.o/′‘;/$(HOSTCC)−M$(HOSTCPPFLAGS)−MQ$(obj)
g $$f >> $@ ; /
done
//下面为.c编译规则
$(HOSTOBJS): $(obj)%.o: %.c
$(HOSTCC) $(HOSTCFLAGS) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c
$(NOPEDOBJS): $(obj)%.o: %.c
$(HOSTCC) $(HOSTCFLAGS_NOPED) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c