fixdep.c 分析

使用gcc -MD 生成目标时,同时会生成依赖文件
例如,新建一个main.c

#include 
int main()
{
	printf("Hello World !");
	return 0;
}

执行gcc -MD main.c -o main.o,生成两个文件main.o,main.d,main.d就是依赖文件的输出
内容如下

main.o: main.c /usr/include/stdc-predef.h /usr/include/stdio.h \
 /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
 /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \
 /usr/include/x86_64-linux-gnu/bits/wordsize.h \
 /usr/include/x86_64-linux-gnu/bits/long-double.h \
 /usr/include/x86_64-linux-gnu/gnu/stubs.h \
 /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
 /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \
 /usr/include/x86_64-linux-gnu/bits/types.h \
 /usr/include/x86_64-linux-gnu/bits/typesizes.h \
 /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \
 /usr/include/x86_64-linux-gnu/bits/types/FILE.h \
 /usr/include/x86_64-linux-gnu/bits/libio.h \
 /usr/include/x86_64-linux-gnu/bits/_G_config.h \
 /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \
 /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \
 /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
 /usr/include/x86_64-linux-gnu/bits/sys_errlist.h

可以看出来,main.o 依赖与main.c和一些库文件
使用这一功能,可以辅助make工具,判断一个目标是否需要重新生成,但是在内核中,几乎所有文件都会依赖一配置时生成的一些头文件,例如autoconf.h,这意味这我们每次重新配置编译配置文件,就需要重新编译所有文件,即使只改动了一行,十分的不友好,fixdep工具,主要目的是为了避免重编所有文件,并且会记录下编译目标的文件和指令
fixdep使用方法

Usage: fixdep   
		depfile : gcc -MD 生成的依赖文件
		target  : 生成的目标文件
		cmdline : 生成目标文件 

对于main.c 我们可以执行如下命令

./fixdep main.d main.o  "gcc -MD main.c -o main.o"

输出

cmd_main.o := gcc -MD main.c -o main.o

source_main.o := main.c

deps_main.o := \
  /usr/include/stdc-predef.h \
  /usr/include/stdio.h \
  /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
  /usr/include/features.h \
  /usr/include/x86_64-linux-gnu/sys/cdefs.h \
  /usr/include/x86_64-linux-gnu/bits/wordsize.h \
  /usr/include/x86_64-linux-gnu/bits/long-double.h \
  /usr/include/x86_64-linux-gnu/gnu/stubs.h \
  /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
  /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \
  /usr/include/x86_64-linux-gnu/bits/types.h \
  /usr/include/x86_64-linux-gnu/bits/typesizes.h \
  /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \
  /usr/include/x86_64-linux-gnu/bits/types/FILE.h \
  /usr/include/x86_64-linux-gnu/bits/libio.h \
  /usr/include/x86_64-linux-gnu/bits/_G_config.h \

  /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \
  /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \
  /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
  /usr/include/x86_64-linux-gnu/bits/sys_errlist.h \

main.o: $(deps_main.o)

$(deps_main.o):

可以看出,记录了生成目标的指令,依赖,还有makefile的依赖格式

值得注意的是,依赖文件中,多了行

    $(wildcard include/config/h.h) \

这就是fixdep工作的关键,fixdep会将依赖中的

			if (strrcmp(s, "include/generated/autoconf.h") &&
			    strrcmp(s, "arch/um/include/uml-config.h") &&
			    strrcmp(s, "include/linux/kconfig.h") &&
			    strrcmp(s, ".ver")) {

这写文件过滤掉,并且会将源文件中CONFIG_XXX,转换成include/config/XXX.h
修改main.c为

#include 
int main()
{
#ifdef CONFIG_XPC
#endif
	printf("Hello World !");
	return 0;
}

执行

gcc -MD main.c -o main.o
./fixdep main.d main.o  "gcc -MD main.c -o main.o"

输出为

cmd_main.o := gcc -MD main.c -o main.o

source_main.o := main.c

deps_main.o := \
    $(wildcard include/config/xpc.h) \
  /usr/include/stdc-predef.h \
  /usr/include/stdio.h \
  /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
  /usr/include/features.h \
  /usr/include/x86_64-linux-gnu/sys/cdefs.h \
  /usr/include/x86_64-linux-gnu/bits/wordsize.h \
  /usr/include/x86_64-linux-gnu/bits/long-double.h \
  /usr/include/x86_64-linux-gnu/gnu/stubs.h \
  /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
  /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \
  /usr/include/x86_64-linux-gnu/bits/types.h \
  /usr/include/x86_64-linux-gnu/bits/typesizes.h \
  /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \
  /usr/include/x86_64-linux-gnu/bits/types/FILE.h \
  /usr/include/x86_64-linux-gnu/bits/libio.h \
  /usr/include/x86_64-linux-gnu/bits/_G_config.h \
    $(wildcard include/config/h.h) \
  /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \
  /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \
  /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
  /usr/include/x86_64-linux-gnu/bits/sys_errlist.h \

main.o: $(deps_main.o)

可以看到,多了一行
$(wildcard include/config/xpc.h)
,如果依赖中有配置文件,会被过滤掉,而去根据源文件中的CONFIG_开头的宏,依赖include/config下的头文件,这些头文件都是根据配置文件生成的空文件,或文件夹,当配置文件中对应宏改变是,这些文件会在配置时更新,这样就不必编译所有的文件了,在配置改变时

你可能感兴趣的:(u-boot)