Gnu Make Tips

Enable Javascript for TOC.

Auto-Completion and Forcing Recipes

With the $(wildcard ...) command we can save a list of files in a variable of the Makefile. This variable can be used as a target:
DEFCONFIGS = $(wildcard projects/*/configs/*_defconfig)

$(DEFCONFIGS): FORCE | $(BR_FOLDER)
	$(MAKE) BR2_EXTERNAL=$(BR_EXTERNAL) -C $(BR_FOLDER) defconfig BR2_DEFCONFIG="../$@"

FORCE: ;
Without the FORCE target, the $(DEFCONFIGS) target would only print:
$ make projects/92106-DEMO/configs/92106-DEMO_defconfig
make: 'projects/92106-DEMO/configs/92106-DEMO_defconfig' is up to date.

Newline in Makefile

First a variable n has to be defined:
# new-line variable
define n


endef
This can be used in error messages:
BUSYBOX_CONFIG=$(wildcard $(BUILDROOT_FOLDER)/output/build/busybox-*/.config)
busybox-menuconfig: $(BUILDROOT_CONFIG)
ifeq ($(words $(BUSYBOX_CONFIG)),0)
	$(error "$n$ERROR: No busybox build-folder found!$n$n")
endif
ifneq ($(words $(BUSYBOX_CONFIG)),1)
	$(error "$n$nERROR: Cannot call busybox-menuconfig with more than one busybox build folder!$n$n")
endif
With the new-line the error message is better to notice:
$ make busybox-menuconfig 
make -C system busybox-menuconfig
make[1]: Entering directory '/home/kkr/Projects/aseries-axos-x86_64/system'
Makefile:330: *** "

ERROR: Cannot call busybox-menuconfig with more than one busybox build folder!

".  Stop.
make[1]: Leaving directory '/home/kkr/Projects/aseries-axos-x86_64/system'
Makefile:71: recipe for target 'busybox-menuconfig' failed
make: *** [busybox-menuconfig] Error 2

Private Targets

If you want to split targets (e.g. to reuse them) but they shall not be executed/visible to the user, you can use this hack:
--parcial-clean: --FORCE
	-rm project/some

clean: --parcial-clean
	-rm other
Source: Stackoverflow.com

Temporary Files/Directories

BOOT.bin: $(FPGA) $(ELF)
	$(eval $@_TMP.bif := $(shell mktemp "$@_${USER}_XXXX.bif"))
	$(eval $@_TMP.tcl := $(shell mktemp "$@_${USER}_XXXX.tcl"))
	echo "some bif text" >> $(@_TMP.bif)
	echo "some bif text" >> $(@_TMP.bif)
	echo "some bif text" >> $(@_TMP.bif)
	echo "some tcl text" >> $(@_TMP.tcl)
	echo "some tcl text" >> $(@_TMP.tcl)
	echo "some tcl text" >> $(@_TMP.tcl)
	source Makefile_env && \
		cd PS && \
		xsct $(@_TMP.tcl)
	rm -f $(@_TMP.tcl) $(@_TMP.bif)
Source: Stackoverflow.com