DEV Community

Romain Lespinasse
Romain Lespinasse

Posted on • Updated on

Create a wrapper task in Makefile

In makefile, you can define tasks and run them individually (e.g. make run-task1 run-task2 run-task3)

run-task1:
    @echo "run task 1"

run-task2:
    @echo "run task 2"

run-task3:
    @echo "run task 3"
Enter fullscreen mode Exit fullscreen mode

When you want to run tasks from another tasks (e.g. make run-all-tasks), you can

run-all-tasks: run-task1 run-task2 run-task3
Enter fullscreen mode Exit fullscreen mode
  • use sub-command line with $(MAKE)
run-all-tasks:
    @$(MAKE) run-task1
    @$(MAKE) run-task2
    @$(MAKE) run-task3
Enter fullscreen mode Exit fullscreen mode

You can also create a wrapper task to run all tasks with a specific naming.

This will enable you to don't known all task names.
Useful when using include mechanism in your makefile.

include makefile-with-run-tasks.mk

run-all-tasks:
    @grep -E '^[\.a-zA-Z0-9_%-]+:.*$$' $(MAKEFILE_LIST) \
        | cut -d ":" -f2 | grep "^run-task" | sort -u \
        | xargs $(MAKE)
Enter fullscreen mode Exit fullscreen mode

So when you run it, the run-all-tasks task will run all run-task tasks.

$ make run-all-tasks
run task 1
run task 2
run task 3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)