Eduardo Reis
Eduardo Reis

Reputation: 1971

Issue with Makefile target rule and PHONY

I know this could be done in different ways, but I am actually looking for learning more about makefiles.

I want to be able to call make somefile.txt and run a given script.

In my current folder I have:

makefile

%.txt:
    # perl -pe "s/#.*//g; s/^\n//g; s/\n/\t/g;" .txt
    echo "Hi"

When I call make, I am getting

make: `somefile.txt' is up to date.

I know I would need to use .PHONY, but I am having trouble trying to use it with %.txt.

I already tried things such as

files = *.txt
.PHONY = $(files)
%.txt:
    # perl -pe "s/#.*//g; s/^\n//g; s/\n/\t/g;" .txt
    echo "Hi"

But that didn't actually work.

Upvotes: 0

Views: 416

Answers (2)

MadScientist
MadScientist

Reputation: 100856

If you don't want to list your targets to be built in your makefile but instead just give them on the command line, you can use this:

.PHONY: $(MAKECMDGOALS)

The variable MAKECMDGOALS will be set to the set of goals you gave make on the command line.

It's important to understand that a makefile is not the shell, and so just sticking *.txt anywhere in your makefile won't always expand it. Only particular areas of the makefile work with shell globs directly.

If you want to always expand it, you can use make's wildcard function like this:

files = $(wildcard *.txt)

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180351

The (a) conventional way to do this is to make your real target have a phony one as a prerequisite. Phony targets are always considered initially out of date, so anything that depends on a phony target will also always be considered out of date. "force" is a conventional name for a target used for this purpose.

For example,

.PHONY: force

force:

%.txt: force
        echo "Hi at $$(date)" > $@

As demonstrated in the example, this does not require the phony target to have a recipe.

Upvotes: 1

Related Questions