4

I'm trying to have a makefile variable for the content of a directory after that directory has been updated by a recipe.

Why does this not work:

A_FILE = $(wildcard subdir/*)

all: a
        @echo $(A_FILE)

a:
        @mkdir ./subdir
        @touch subdir/b
        @touch a
$ rm -rf ./subdir && make

$

...whereas this does:

A_FILE = $(wildcard subdir/*)

all: a
        @echo $(A_FILE)

a: subdir/b
        @touch a

subdir/b:
        @mkdir ./subdir
        @touch subdir/b
$ rm -rf ./subdir && make
subdir/b
$

?

I thought lazy-evaluation meant the variable was not evaluated until actually used. In both versions, $(A_FILE) is used in the same recipe, and after a prereq has been evaluated. In fact, I'd struggle to articulate a meaningful difference between the two rules, other than the superficial: the first is a chain of two rules/prereqs, and the second is a chain of three.

StoneThrow
  • 5,314
  • 4
  • 44
  • 86

1 Answers1

2

You need to also delete a:

$ rm -rf ./subdir a && make

Since you've deleted subdir but not a, the a: rule isn't triggered. Only this rule runs:

all: a
        @echo $(A_FILE)

And since subdir wasn't created, the $(wildcard subdir/*) expansion is empty.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578