#CC=/path/to/your/compiler
#snapdir = /path/to/snapshot/dir
build: all
dmxctl = dmxctl.c sock.c file.c opt.c
all = dmxctl

CFLAGS=-march=armv4

#CC=/usr/local/opt/gcc4-crosstool/gcc-4.0.1-glibc-2.3.5/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-gcc
CC=/home/michael/ts/arm/75XX/tools/arm-uclibc-3.4.6/bin/arm-linux-gcc
LDFLAGS=-Wl,--rpath,/slib -Wl,-dynamic-linker,/slib/ld-uClibc.so.0
%.md5.c: %
	@echo "static char inf1[] = \"\$$md5:\" __DATE__ \" \" __TIME__ \" $(shell md5sum $<) \";" >> $@
	@echo "static char inf2[] = \"\$$md5:\" __DATE__ \" \" __TIME__ \"" $(shell $(CC) -E $(CPPFLAGS) $(CFLAGS) $< | md5sum) "$<\";" >> $@

%.d: %.c
	@set -e; rm -f $@; \
	$(CC) -MM $(CPPFLAGS) $< | \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g'  > $@;

%.de: Makefile
	@echo "$(patsubst %.de,%,$@): $(patsubst %.c,%.o,$(foreach x,$(patsubst %.de,%,$@),$($(x)))) $(patsubst %,%.md5.o,$(foreach x,$(patsubst %.de,%,$@),$($(x))))" > $@

%.ds: Makefile
ifdef snapdir
	@echo -e "$(snapdir)/$(patsubst %.ds,%,$@): $(patsubst %.ds,%,$@)\n\tcp $(patsubst %.ds,%,$@) $(snapdir)/$(patsubst %.ds,%,$@)" > $@
endif
ifdef cvsdif
	@echo -e "$(cvsdir)/$(patsubst %.ds,%,$@): $(patsubst %.ds,%,$@)\n\tcp $(patsubst %.ds,%,$@) $(cvsdir)/$(patsubst %.ds,%,$@)" >> $@
endif

clean:
	rm -f *.o $(products) *.exe *~ *.d *.de *.ds *.md5.c *.gcda *.gcno gmon.out *.gcov

ifdef cvsdir
$(cvsdir)/Makefile: $(snapdir)/Makefile
	cp $(snapdir)/Makefile $(cvsdir)/Makefile
endif

ifdef snapdir
$(snapdir)/Makefile: Makefile ../Makefile.def
	@echo "creating Makefile in snapshot directory"
	@echo "#CC=/path/to/your/compiler" > $(snapdir)/Makefile
	@echo "#snapdir = /path/to/snapshot/dir" >> $(snapdir)/Makefile
	@head --lines=-3 Makefile >> $(snapdir)/Makefile
	@cat ../Makefile.def >> $(snapdir)/Makefile >> $(snapdir)/Makefile

tarball:
	@cd $(snapdir) && \
	cd .. && \
	DIR=$(lastword $(subst /, ,$(shell pwd))) && \
	NOW=`date +%Y%m%d%H%M%S` && \
	echo "Creating tarball $$DIR-$$NOW.tar.gz" && \
	tar -czf $$DIR-$$NOW.tar.gz $$DIR/ && \
	ln -sf $$DIR-$$NOW.tar.gz $$DIR.tar.gz
endif

snap_sources = $(sort $(foreach x,$(all),$($(x))))
all_sources = $(snap_sources) $(md5_sources)
all_headers = $(wildcard *.h)
all_docs = $(wildcard *.txt)
products =  $(foreach x,$(all),$(x))
all_snap = $(snap_sources) $(all_headers) $(all_docs) $(products)
all_cvs = $(snap_sources) $(all_headers) $(all_docs)

ifdef snapdir
all:	$(products) $(foreach x,$(all_snap),$(snapdir)/$(x)) $(snapdir)/Makefile $(foreach x,$(all_cvs),$(cvsdir)/$(x)) $(cvsdir)/Makefile
else
all:	$(products)
endif

ifneq ($(MAKECMDGOALS),clean)
include $(all_sources:.c=.d)
-include $(products:=.de)
-include $(all_sources:.c=.c.ds) $(all_headers:.h=.h.ds) $(all_docs:.txt=.txt.ds) $(products:=.ds)
endif

# The purpose of this makefile:
# 1. automatically generate header file dependencies
# 2. automatically insert compilation date/time and md5sum of each source file
#    only the actual files linked to the binary are included, although there is
#    a separate md5 for each file's preprocessed source.
# 3. create snapshots of the source tree suitable for distribution.
#    this is useful because the original source contains symlinks that allow
#    library files to be used in multiple places, and a two-part Makefile
#    system to eliminate redundancy.
# 4. provide a dead simple mechanism to create a Makefile:
#
# build: all
# executable1 = source1.c source2.c ...
# executable2 = source3.c ...
# ...
# all = executable1 executable2 ...
# (blank line required here)
# snapdir = <snapshot directory> (optional)
# include ../Makefile.def
#
# the last line, as well as "build" and "all" must be verbatim.
# everything else is user-defined.
# Snapshots contain a single makefile melding the user's makefile and
# the common makefile which achieves all of the above goals.
