0

I'm writing a makefile for working with a microcontroller.

Among various PHONY targets, are:

  • build - builds the project
  • flash - flashes the MCU
  • serial - opens Minicom serial interface

None of these strictly depends on the other - e.g. I can open serial without rebuilding or reflashing.

BUT, if the other targets are also specified (e.g. make flash serial), then a certain ordering needs to be respected (build before flashing, flash before opening serial).

Make is running with concurrency enabled (-j) and I don't want to change this.

How do I define optional-dependencies, to ensure ordering of targets, without requiring those dependencies to be checked/updated each time unless requested?

1 Answer 1

1

This is written for GNU Make, I'm not aware of whether MAKECMDGOALS is GNU-only or POSIX.

The targets Make is explicitly asked to build are in the MAKECMDGOALS variable.

We can filter this to define optional-dependencies, for ordering purposes:

after = $(filter $(strip $1), $(MAKECMDGOALS)) post_build = $(call after, build) post_flash = $(call after, flash) $(post_build) ... .PHONY: build flash serial debug ... flash: | $(post_build) st-flash ... serial: | $(post_flash) minicom ... debug: | $(post_flash) $(GDB) ... 

That way:

  • make build will build the project
  • make flash will flash the MCU with the current build
  • make serial will open the serial interface
  • make flash serial (or vice-versa) will flash the MCU, then open the serial interface.
  • make build flash will build the project then flash the MCU.
  • make build flash serial (in any order) will first build, then flash, then open the serial interface.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.