Posts

Showing posts from October, 2023

GNU Make - Unconditionally run a target before any other targets

When writing a (GNU) Makefile, there are times when you need a particular target(s) to be run before anything else.  That can be for example to check the environment, ensure variables are set or prepare a particular directory layout.  TLDR; Define a non-phony target for a non-existing file and then include the target as if you were including a real makefile. For example: always-run-before-all : ... include always-run-before-all Usecase Ever since I've started using bmakelib to enhance my makefiles, I've always found myself writing a target ensure-variables , which I then would declare as the prerequisite of almost all other targets. include bmakelib/bmakelib.mk ####################################### .PHONY : ensure-variables ensure-variables : bmakelib.error-if-blank( REQUIRED_VAR1 REQUIRED_VAR2 ) ensure-variables : bmakelib.default-if-blank( OPTIONAL_VAR,some-value ) ######################################## my-target : ensure-va...