Makefile:取决于目录中的每个文件 [英] Makefile: depend on every file of a directory

查看:75
本文介绍了Makefile:取决于目录中的每个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个可以用gnumake或makepp运行的Makefile,该文件可以在给定的目录下打包所有文件:

I'd like to do a Makefile that runs either with gnumake or makepp that packs all the files under given 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

问题是依赖项中没有%/*之类的东西. 我需要目标(X.npk)依赖于目录X中的每个文件,但是在编写Makefile时我不知道这些文件是什么,因为它们是稍后生成的.

the problem is that there's no such thing as %/* in the dependencies. I need the targets (X.npk) to depend on every file in directory X, but I don't know what the files are when I write the Makefile, 'cause they're generated later.

一个例子:

./dirA/x
./dirA/y
./dirB/e
./dirB/f

我想创建./dirA.npk(取决于x,y),./dirB.npk(e,f) 除了第一行中使用的查找可以找到所有目录外,我对目录或文件的事先了解不多.

I'd like to create ./dirA.npk (depending on x,y), ./dirB.npk (e,f) There's nothing I know about the dirs or the files in advance except that the find used in the 1st line finds all the dirs.

推荐答案

这是我发现的解决方案: 它基于 makedepend 的思想,带有一些元"脚本.不是很好,但是可以.

This is the solution I found: it is based on the makedepend idea, with some "meta" scripting. Not very nice, but works.

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

这篇关于Makefile:取决于目录中的每个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆