hgiesel
hgiesel

Reputation: 5648

Makefile Automatic Target Generation

I have the following directory tree

moving_files/
        Makefile
        source/
                a
                b
                c
        target/

With my Makefile I want to cp every file in source/ to target/. The catch: I want to be able to move other files to source/ afterwards without having to edit the Makefile. For this purpose I have written this:

FILES = $(filter-out Makefile, $(wildcard source/*) ) 
all: $(subst source,target,$(FILES))
$(subst source,target,$(FILES)): $(FILES)
    cat $< >| $@

And it works fine.

However, when I execute touch source/d afterwards and make once again, in addition to d, a, b and c get cated, as well. What do I have to do in order to change this behavior.

Upvotes: 1

Views: 172

Answers (1)

user657267
user657267

Reputation: 21000

$(subst source,target,$(FILES)): $(FILES)

expands to

target/a target/b target/c: source/a source/b source/c

which means that each single target depends on all files in source, probably not what you intended. Either a static or an implicit rule can fix this, static rules are generally better as they are more specific:

$(subst source,target,$(FILES)): target/%: source/%

Upvotes: 1

Related Questions