# Based on https://make.mad-scientist.net/papers/multi-architecture-builds/

.SUFFIXES:

OBJDIR := _out_$(RUST_TARGET)-$(_link_type)-$(_profile)

# Define the rules to build in the target subdirectories.
#
MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile \
		SRCDIR=$(CURDIR) $(MAKECMDGOALS)

.PHONY: $(OBJDIR)
$(OBJDIR):
	+@[ -d "$@" ] || mkdir -p "$@"
	+@$(MAKETARGET)

# These rules keep make from trying to use the match-anything rule below to
# rebuild the makefiles--ouch!  Obviously, if you don't follow my convention
# of using a `.mk' suffix on all non-standard makefiles you'll need to change
# the pattern rule.
#
Makefile : ;
%.mk :: ;

# Anything we don't know how to build will use this rule.  The command is a
# do-nothing command, but the prerequisites ensure that the appropriate
# recursive invocations of make will occur.
#
% :: $(OBJDIR) ;

# The clean rule is best handled from the source directory: since we're
# rigorous about keeping the target directories containing only target files
# and the source directory containing only source files, `clean' is as trivial
# as removing the target directories!
#
.PHONY: clean clean-all help
clean:
	rm -rf $(OBJDIR)
clean-all:
	rm -rf _out_*

help:
	@echo "You have the choice of the following parameters:"
	@echo ""
	@echo "Variable | Description          | Default   | Values"
	@echo "---------+----------------------+-----------+---------------------------"
	@echo "LIBC     | libc to link against | 'gnu'     | 'gnu' or 'musl'"
	@echo "PROFILE  | Optimization profile | 'release' | 'debug' or 'size_optimized'"
	@echo "LINK     |                      | 'dynamic' | 'dynamic' or 'static'"
	@echo "CARGO    | cargo binary to use  | 'cargo'   | 'rustup run nightly cargo'"
	@echo "CC       | C compiler to use    | 'gcc'     | 'musl-gcc'"
	@echo "STRIP    | strip command to use | 'strip'   | -"
	@echo ""
	@echo "When building this project, each configuration will result in a separate output directory."
	@echo "The target clean only removes the output directory of the specified configuration."
	@echo "The target clean-all can be used to remove the builds of all configurations."