News for hackers. Stuff that matters.
Re:I Like Makefiles by matheusmoreira on Saturday September 21, 2024 @05:36AM (#41608762)
Every makefile recipe should produce exactly one output: $@. The makefile as a whole produces an arbitrary number of outputs since rules can depend on other rules.

This leads us to a neat rule of thumb for phony targets: any recipe that does not touch $@ and only $@ should have $@ marked as phony.

I find that keeping track of phony targets with a list makes things much easier.

  phonies :=

  phonies += something
  something:
          ./do-something

  phonies += something-else
  something-else: something
          ./do-something-else

  # touches $@ and thus does not need to be phony
  create-file:
          ./generate-some-output > $@

  .PHONY: $(phonies)
Re:I Like Makefiles by Brian_K_White on Saturday September 21, 2024 @05:50AM (#41608817)
I don't know if it's actually saner than just normal phonys but man I like it.

What does it get you other than the ability to print the list of all phonys?

Re:I Like Makefiles by matheusmoreira on Saturday September 21, 2024 @06:01AM (#41608861)
It's mostly so I can immediately see which targets are phonies. Every phony has a line directly above it adding it to the list of phonies. When a makefile gets complex enough we need all the help we can get.

I use phony targets so much I wrote a shell script to parse the makefile database dump into some sort of help text. It doesn't depend on that variable at all.

https://github.com/matheusmoreira/.files/blob/master/~/.loca...

Prints output like:

  phony1
  phony2
          dependency1
          dependency2
Re:I Like Makefiles by 153957 on Saturday September 21, 2024 @09:11AM (#41609750)
It is also possible to define `.PHONY` multiple times, so you can simplify this to:

  .PHONY: something
  something:
      ./do-something

  .PHONY: something-else
  something-else: something
      ./do-something-else

  create-file:
      ./generate-some-output > $@
Re:I Like Makefiles by matheusmoreira on Saturday September 21, 2024 @10:57PM (#41614310)
Good tip! Never realized this could be done.