# custom LUFA library build options are reflected in the compiled
# code.
-all:
- $(MAKE) -C MouseHostDevice clean
- $(MAKE) -C MouseHostDevice all
+PROJECT_DIRECTORIES = $(shell ls -d */)
-%:
- $(MAKE) -C MouseHostDevice $@
+# This makefile is potentially infinitely recursive if something really bad
+# happens when determining the set of project directories - hard-abort if
+# more than 10 levels deep to avoid angry emails.
+ifeq ($(MAKELEVEL), 10)
+ $(error EMERGENCY ABORT: INFINITE RECURSION DETECTED)
+endif
+# If building without a per-project object directory, we can't build in parallel
+ifeq ($(OBJDIR),)
+ .NOTPARALLEL:
+
+ # Ensure projects are pre-cleaned if the target is the default or "all"
+ ifeq ($(MAKECMDGOALS),)
+ MAKECMDGOALS := clean all
+ endif
+ ifneq ($(findstring all, $(MAKECMDGOALS)),)
+ MAKECMDGOALS := clean $(MAKECMDGOALS)
+ endif
+endif
+
+%: $(PROJECT_DIRECTORIES)
+ @echo . > /dev/null
+
+$(PROJECT_DIRECTORIES):
+ @$(MAKE) -C $@ $(MAKECMDGOALS)
+
+.PHONY: $(PROJECT_DIRECTORIES)