#
# Include autoconf generated information
#
include makefile.inc


EXEC := hello1
cleanfiles :=

default: progs

# Expand dependencies one level
dependless = %.o %.a %.d %.h
expand = $($(var)) $(var) $(var).d
depend_test = $(if $(filter $(dependless),$(var)),$(var),$(expand))
depend = $(sort $(foreach var,$(1),$(depend_test)))

include $(wildcard *.d)

& = $(filter-out %.h %.d,$^)

inc_before := mak_before.mk
inc_after := mak_after.mk 
rest = $(wordlist 2,$(words $(1)),$(1))
get_dirlist = $(inc_before) $~$(firstword $(1))/makefile.inc $(wildcard $~$(firstword $(1))/*.d) $(inc_after) $(if $(rest),$(call get_dirlist,$(rest)),)
scan_subdirs = $(eval dirlist:=$(SUBDIRS) $(dirlist)) $(call get_dirlist,$(SUBDIRS))


SUBDIRS := subdir1 subdir2

include $(scan_subdirs)

~:= tilde is broken in commands

cleanfiles += $(EXEC)

create_d = $(SHELL) -ec 'gcc -MM $(CPPFLAGS) $< | sed -n "H;$$ {g;s@.*:\(.*\)@$< := \$$\(wildcard\1\)\n$*.o $@: $$\($<\)@;p}" > $@'

%.c.d: %.c
	$(DEPEND)

progs: $(EXEC)

clean:
	-rm -f $(cleanfiles)

files:
	@echo $(EXEC)



# Program(s) in the root directory

hello1: $(call depend,hello1.c)
	$(CC) $& $(CFLAGS) $(LDFLAGS) -o $@ $(LIBS)




#
# Autoconf support
#
define CONFIGURE
	@if [ -x ./config.status ] ; then \
	    ./config.status --recheck && ./config.status; \
	else \
		./configure; \
	fi
endef

##
## Hack - make sure the build system is consistant.
##
makefile: makefile.inc
makefile.inc: configure makefile.in
	$(CONFIGURE)
	
configure: configure.ac
	autoheader
	autoconf
