DEV Community

Discussion on: How To Make A Makefile

Collapse
 
gypsydave5 profile image
David Wickes • Edited

Great article!

I use make in really crappy ways - very unsophisticated. Each makefile is more of a dumping ground for useful scripts to run on a project (build, run, test, clean ... stuff like that). This is a bit more advanced.

Could you talk me through two things?

help:
    @echo "Usage: make {deps|help}" 1>&2 && false

What's going on with the redirect and the false at the end? (I know what the @ does).

and

Why do you need to add .PHONY at the top to the tasks with no output? I don't do this, it works OK... am I quietly breaking stuff?

Collapse
 
deciduously profile image
Ben Lovy • Edited

Thanks!

@echo "Usage: make {deps|help}" 1>&2 && false

Thanks so much for pointing this out - I completely forgot to cover it!

I'll mention it here in case anyone doesn't know about @ - it suppresses printing this line to stdout when make runs.

The redirect just sends the echo to stderr instead of stdout, which in practice doesn't really make a difference when invoked at the command line. Returning false signals a failed recipe. If any line in a recipe in a makefile returns false, the target stops building and make considers the target failed. Here, it's just a way to ensure that make exits immediately after displaying this help line, and as no target was built, a non-zero error code was appropriate.

None of it's strictly necessary. It'll work fine without it. One way it could be used is to make help your default rule. This will prevent anything from happening, and prompt the user to choose what they want to do.

Why do you need to add .PHONY at the top to the tasks with no output?

You don't need to. You're not breaking anything. It can prevent name collisions with actual targets if you run into that problem, and when evaluating these rules the implicit rule search is skipped. There's also a whole use case around recursive subcalls of make, but I've never run into that sort of thing. Basically it skips some useless work, which is a performance bump but chances are you're not running in to much of that!

So, you can probably continue to omit them without worrying too, but I don't think it's a terrible habit either. It also serves as documentation to some minimal degree.

Collapse
 
gypsydave5 profile image
David Wickes

Thanks Ben :D