我想做一个使用gnumake或makepp运行的Makefile,它包含给定directiories下的所有文件:
DIRS:=$(shell find . -mindepth 2 -maxdepth 2 -not -name mp3 -not -name ".*" -type d)
PACKAGES = $(DIRS:%=%.npk)
all: packages
packages: $(PACKAGES)
%.npk: %/*
npack c $@ @^
.PHONY: all packages
Run Code Online (Sandbox Code Playgroud)
问题是依赖项中没有%/*这样的东西.我需要目标(X.npk)依赖于目录X中的每个文件,但是当我编写Makefile时我不知道文件是什么,因为它们是稍后生成的.
一个例子:
./dirA/x
./dirA/y
./dirB/e
./dirB/f
Run Code Online (Sandbox Code Playgroud)
我想创建./dirA.npk(取决于x,y),./dirB.npk(e,f)除了在第1行中使用的查找之外,我没有提前知道dirs或文件发现所有的目录.
这是我找到的解决方案:它基于makedepend想法,并带有一些“元”脚本。不太好,但是有效。
PACKAGES :=
all: packages
-include Makefile.depend
packages: Makefile.depend $(PACKAGES)
depend: clean Makefile.depend
Makefile.depend:
@(PACKAGES= ; \
for DIR in `find . -mindepth 2 -maxdepth 2 -not -name mp3 -not -name ".*" -type d` ; \
do \
PACKAGE=`basename $${DIR}.npk` ; \
PACKAGES="$${PACKAGES} $${PACKAGE}" ; \
DEPS=`find $${DIR} -not -type d | sed -e 's#\([: ]\)#\\\\\1#' -e 's#^\./\(.*\)# \1#' | tr -d "\n"` ; \
SUBDIR=`echo $${DIR} | sed -e 's#^\./\([^/]\+\)/.*#\1#'` ; \
FILES=`echo \ $${DEPS} | sed -e "s# $${SUBDIR}/# #g"` ; \
echo "$${PACKAGE}:$${DEPS}" ; \
echo " @cd $${SUBDIR} ; \\" ; \
echo " npack c ../\$$@ $${FILES} ; \\" ; \
echo ; \
done ; \
echo "PACKAGES = $${PACKAGES}" \
)>> Makefile.depend ; \
cleanall: clean
rm -f *.npk
clean:
@rm -f Makefile.depend
.PHONY: all packages depend clean
Run Code Online (Sandbox Code Playgroud)