Sprite
Sprite

Reputation: 406

Makefile wildcard target expansion

I'm a fairly new user of GNU Make. I want to get a list of Golang files and build each one of them using Make.

I want to create a target that will receive the apt Go file as a param. In the snippet I've written the control of the program never reaches the %.go target.

Here is a snippet of a file.

EXECUTABLES := $(wildcard cmd/*/*/*.go)

%.go:
    echo "Build the go file"

build: $(EXECUTABLES)
    echo $<

Output:

echo cmd/abc/handler/main.go
cmd/abc/handler/main.go

I modified the script to this but I'm facing the same issue. Also tried replacing %.go with *.go and with cmd/abc/handler/main.go

Here is one of the variants mentioned above.

%.go:
    echo "Hello world"

build: $(wildcard cmd/*/*/*.go)
    echo $<

Anything I might be missing here?

Upvotes: 0

Views: 277

Answers (1)

MadScientist
MadScientist

Reputation: 100836

You have a rule that tells make how to build a .go file. But, you already HAVE a .go file. Make doesn't have to build it, it's a source file that you wrote. That rule has no prerequisites, so as far as make is concerned if the file exists, it's up to date (it doesn't depend on any other file).

So, when you ask make to build that file make looks and says "this file exists already, and it doesn't depend on anything, so the thing you asked me to do is already done and I don't need to run any commands".

When you write a makefile you have to think of it from the end first back to the beginning. What is the thing you want to end up with? That is the first target you write. Then what files are used as inputs to that final thing? Those are the prerequisites of that target. And what commands are needed to create the target from the prerequisites? That is the recipe.

Then once you have that, you think about those prerequisites, considering them as targets. What prerequisites do they have? And what commands do you need to turn those prerequisites into that target?

And you keep going backwards like that until you come to a place where all the prerequisites are original source files that can't be built from anything, then you're done.

Upvotes: 1

Related Questions