Ant6n
Ant6n

Reputation: 2007

Makefile: immedate assignment evaluated lazily?

I'm trying to have *.s files as inputs, and files with the extension stripped as output files. I thought I could use an immediate binding with ($basename ...), but this does not work. My minimal example:

INPUT := *.s
OUTPUT := $(basename $(INPUT))

all:
    echo 'input:' $(INPUT)
    echo 'output:' $(OUTPUT)

when running, produces

echo 'input:' *.s
input: hello.s int3.s
echo 'output:' *
output: Makefile int3 hello hello.o hello.s int3.s

I thought the := would force immediate evaluation, but it seems to actually do a lazy binding, and the basename strips the ".s" from the "*". Thus this will result in all files in the directory. How to I get this to just give me the .s files with the extension removed?

Upvotes: 0

Views: 216

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61550

You need to replace:

INPUT := *.s

with:

INPUT := $(wildcard *.s)

The first causes immediate assignment of *.s to INPUT, which you don't want. The second causes immediate assignment of the filename-list resulting from expansion of $(wildcard *.s), which you do want.

See 4.4.1 Wildcard Examples and note:

Wildcard expansion does not happen when you define a variable. Thus, if you write this:

objects = *.o

then the value of the variable objects is the actual string ‘*.o’. [...] To set objects to the expansion, instead use:

objects := $(wildcard *.o)

Upvotes: 1

Related Questions