Skip to content

Instantly share code, notes, and snippets.

@rkrug
Created August 20, 2020 06:53
Show Gist options
  • Save rkrug/496572989e80e640b4cb2ffc74f01c04 to your computer and use it in GitHub Desktop.
Save rkrug/496572989e80e640b4cb2ffc74f01c04 to your computer and use it in GitHub Desktop.
Makefile to recursively run all makefiles in subfolders with specified targets
## See https://stackoverflow.com/a/11206700/632423
## adjust when different
SUBDIRS := $(wildcard */.) # e.g. "foo/. bar/."
TARGETS := all build build-cran check clean clean_check clean_readme deps docs drat files # whatever else, but must not contain '/'
##
# foo/.all bar/.all foo/.clean bar/.clean
SUBDIRS_TARGETS := \
$(foreach t,$(TARGETS),$(addsuffix $t,$(SUBDIRS)))
.PHONY : $(TARGETS) $(SUBDIRS_TARGETS)
# static pattern rule, expands into:
# all clean : % : foo/.% bar/.%
$(TARGETS) : % : $(addsuffix %,$(SUBDIRS))
@echo 'Done "$*" target'
# here, for foo/.all:
# $(@D) is foo
# $(@F) is .all, with leading period
# $(@F:.%=%) is just all
$(SUBDIRS_TARGETS) :
@echo
@echo "#######" make $(@D) $(@F:.%=%)
$(MAKE) -C $(@D) $(@F:.%=%)
@echo "#######" Done
@echo
############# Help targets #############
list_variables:
@echo
@echo "#############################################"
@echo "## Variables ################################"
make -pn | grep -A1 "^# makefile"| grep -v "^#\|^--" | sort | uniq
@echo "#############################################"
@echo ""( input, output )
## from https://stackoverflow.com/a/26339924/632423
list_targets:
@echo
@echo "#############################################"
@echo "## Targets ###############################"
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
@echo "#############################################"
@echo
list: list_variables list_targets
#############
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment