【发布时间】:2015-05-09 07:12:26
【问题描述】:
我有一个生成依赖文件的简单脚本 (depends.sh),并对依赖文件进行了一些更改。
#!/bin/sh
#echo "## Got: $*"
CC="$1"
DIR="$2"
shift 2
case "$DIR" in
"" | ".")
$CC -MM -MG "$@" | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@'
;;
*)
$CC -MM -MG "$@" | sed -e "s@^\(.*\)\.o:@$DIR/\1.d $DIR/\1.o:@"
;;
esac
脚本是从 Makefile 调用的,这是它的摘录。
DEP := $(OBJ:.o=.d)
# implicit rules
%.d: %.c
./depends.sh $(CC) `dirname $*.c` $(CFLAGS) $*.c > $@
-include $(DEP)
# Actual targets
depend: $(DEP)
有趣的是make depends做了以下动作:
./depends.sh gcc `dirname src/hellomake.c` -Wall -Wno-unused-function -g -O -Isrc src/hellomake.c > src/hellomake.d
./depends.sh gcc `dirname src/hellofunc.c` -Wall -Wno-unused-function -g -O -Isrc src/hellofunc.c > src/hellofunc.d
cat depends.sh >depends
chmod a+x depends
没有depends 目标(只有depend 目标),但它执行依赖目标,甚至创建依赖脚本,并使其可执行。
这背后有什么魔力?
【问题讨论】: