# Makefile
#
# $Id: Makefile,v 1.17 2002/11/08 03:39:48 andy Exp $
# 
# Copyright (C) 2002 Andy Goth <unununium@openverse.com>
# For more information visit http://ioioio.net/devel/xpyre/
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA  02111-1307, USA.

include global.mk

SHELL := /bin/sh
SUFFIXES :=

all: all-targets

INCPATH += $(patsubst %,-I%,$(MODULES))
MODFILES := $(patsubst %,%/module.mk,$(MODULES))
PROGRAMS :=
SHLIBS :=
TARGETS :=
CLEANTARGETS := clean

ifeq ($(MAKECMDGOALS),)
MAKECMDGOALS := all
endif

# Hide commands?
ifeq ($(HUSH),1)
override HUSH := @
override HUSH2 := > /dev/null
else
override HUSH :=
override HUSH2 :=
endif

# Include module descriptions
include.mk: Makefile global.mk
	@echo "* Generating $@..."
	$(HUSH)(echo '# This file is automatically generated by make';\
	echo '# Do not edit; changes will be lost';\
	for mod in $(MODULES); do\
	echo;\
	echo 'DIR := '$${mod};\
	echo 'include '$${mod}'/module.mk';\
	done;\
	echo;\
	echo '-include auto.mk') > $@
-include include.mk

# This must be done after including module definitions
all-programs: $(PROGRAMS)
all-shlibs: $(SHLIBS)
all-targets: all-programs all-shlibs $(TARGETS)

# Generate nifty automatic stuff
auto.mk: Makefile global.mk $(MODFILES)
	@echo "* Generating $@..."
	$(HUSH)(echo '# This file is automatically generated by make';\
	echo '# Do not edit; changes will be lost') > $@
	$(if $(PROGRAMS),$(HUSH)(for prog in $(PROGRAMS); do\
	echo;\
	echo '# Program: '$${prog};\
	echo 'OBJ-'$${prog}' := $$(foreach sfx,c s,$$(patsubst\
			%.$$(sfx),%.o,$$(filter\
			%.$$(sfx),$$(SRC-'$${prog}'))))';\
	echo 'DEP-'$${prog}' := $$(patsubst %.c,%.d,$$(filter\
			%.c,$$(SRC-'$${prog}')))';\
	echo 'SRC += $$(SRC-'$${prog}')';\
	echo 'OBJ += $$(OBJ-'$${prog}')';\
	echo 'DEP += $$(DEP-'$${prog}')';\
	echo $$prog': $$(OBJ-'$$prog')';\
	echo -e '\t@echo "* Linking $$@..."';\
	echo -e '\t$$(HUSH)$$(CC) $$(LDFLAGS) $$(LIBPATH) -o $$@'\
			'$$^ $$(LIBS-'$${prog}') $$(LIBS)';\
	done) >> $@)
	$(if $(SHLIBS),$(HUSH)(for shlib in $(SHLIBS); do\
	echo;\
	echo '# Shared library: '$${shlib};\
	echo 'OBJ-'$${shlib}' := $$(foreach sfx,c s,$$(patsubst\
			%.$$(sfx),%.o,$$(filter\
			%.$$(sfx),$$(SRC-'$${shlib}'))))';\
	echo 'DEP-'$${shlib}' := $$(patsubst %.c,%.d,$$(filter\
			%.c,$$(SRC-'$${shlib}')))';\
	echo 'SRC += $$(SRC-'$${shlib}')';\
	echo 'OBJ += $$(OBJ-'$${shlib}')';\
	echo 'DEP += $$(DEP-'$${shlib}')';\
	echo $${shlib}': $$(OBJ-'$${shlib}')';\
	echo -e '\t@echo "* Linking $$@..."';\
	echo -e '\t$$(HUSH)$$(CC) $$(LDFLAGS) -shared -fPIC $$(LIBPATH) -o $$@'\
			'$$^ $$(LIBS-'$${shlib}') $$(LIBS)';\
	done) >> $@)
	$(HUSH)(echo;\
	echo '# Include dependencies';\
	echo 'ifneq ($$(filter-out $$(CLEANTARGETS),$$(MAKECMDGOALS)),)';\
	echo '-include $$(DEP)';\
	echo 'endif';\
	echo;\
	echo '# Installation';\
	echo -n 'install:';\
	for module in $(MODULES); do\
	echo -n ' install-'$${module};\
	done;\
	echo) >> $@

# Generate dependency files from source
%.d: %.c
	@echo "* Calculating dependencies for $<..."
	$(HUSH)(dir=$(dir $@);\
	for dep in $$($(CC) $(CFLAGS) $(CPPFLAGS) $(INCPATH) -MM -MG\
			-o - $< | sed '1s@^@$@ $(dir $@)@'); do\
	[ $${dep} == '\' ] && continue;\
	[ ! -f $${dep} -a ! $${dep:0:$${#dir}} == $(dir $@) ] &&\
			echo -n '$(dir $@)';\
	echo -n $${dep}' ';\
	done;\
	echo) > $@


# Generate object files from source
%.o: %.c
	@echo "* Compiling $<..."
	$(HUSH)$(CC) $(CFLAGS) $(CPPFLAGS) $(INCPATH) -c -o $@ $<

%.o: %.s
	@echo "* Compiling $<..."
	$(HUSH)$(AS) -o $@ $<

# Remove generated files
GENERATED += $(TARGETS) $(PROGRAMS) $(SHLIBS) $(OBJ) include.mk auto.mk $(DEP)
clean:
	@echo "* Removing generated files..."
	$(HUSH)-rm -f $(GENERATED)

# Dummy target for generating dependencies
depend:

# Tell make about special (non-file) targets
.PHONY: all all-programs all-shlibs all-targets clean depend install

# EOF
