help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Preconditions vs. Dependencies


From: Eric West
Subject: Re: Preconditions vs. Dependencies
Date: Mon, 21 Nov 2005 21:19:57 -0500
User-agent: KMail/1.8.2

On Monday 21 November 2005 06:24 pm, natch wrote:
  [snip snip snip]
> Consider the following makefile:
>
> DIR=outdir
> FILES=$(DIR)/a $(DIR)/b $(DIR)/c
>
> $(DIR):
>     mkdir $@
>
> $(FILES): $(DIR)
>     touch $@
> -
>
> Starting with a clean directory, if I decide to
>
>  > make outdir/a
>  > make outdir/b
>  > make outdir/a
>
> It will create the directory and make a, then make b, then make a
> again (since creating b causes the directory to have a newer
> timestamp than a).  Eventually this system will settle down and
> consider everything up to date (which I don't entirely understand). 
> If I
>
>  > rm outdir/a
>
> and ask it to
>
>  > make outdir/b
>
> it will go ahead and make b because the directory is newer than b.
>
> Does make allow for a solution to this problem?  There's an ugly
> solution involving directory creation in the target:
>
Beauty is in the eye of the beholder...

> $(FILES):
>     if [ ! -d outdir ]; then mkdir outdir; fi
>     touch $@
>
> But this adds a level of obscurity that I'd like to avoid.  It looks
> simple in this situation, but what happens with nested directories?
> Such as:
>
> DIR=outdir
> SUBDIR1=sub
> FILES=outdir/a outdir/sub/b
>
> $(SUBDIR1): $(DIR)
>     mkdir $@
>
> $(DIR):
>     mkdir $@
>
> $(FILES): $(SUBDIR1)
>
> This would cause make to try and remake subdir1 if outdir/a is
> recreated, although this again would be unnecessary.
>
> So existence of the directory is a precondition of the target, but
> not necessarily a prerequisite.  We don't care if the directory is
> newer than the target, we just want to make sure it exists.
>

So then what is the problem of using a bit of shell code?

DIR = AAA
FILES = ${DIR}/x ${DIR}/y ${DIR}/BBB/a

all: ${FILES}

${FILES} :
        @d=`dirname address@hidden ;\
        if [ ! -d $$d ]; then \
                echo "Creating $$d ..." ;\
                mkdir -p $$d            ;\
        fi
        touch $@


$ make all
Creating AAA ...
touch AAA/x
touch AAA/y
Creating AAA/BBB ...
touch AAA/BBB/a

VoilĂ !


  --Eric





reply via email to

[Prev in Thread] Current Thread [Next in Thread]