makefilemakepp

Makefile: depend on every file of a directory


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

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.

An example:

./dirA/x
./dirA/y
./dirB/e
./dirB/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.


Solution

  • 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