########################################################-*-mode:Makefile-*-
# Copyright (c) 2000 Carnegie Mellon University.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The names "Sphinx" and "Carnegie Mellon" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. To obtain permission, contact 
#    sphinx@cs.cmu.edu.
#
# 4. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by Carnegie
#    Mellon University (http://www.speech.cs.cmu.edu/)."
#
# THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
# ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
# NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ====================================================================
#                                                                       
#    Common make rules included in all Makefiles                        
#                                                                       
########################################################################

# System type
include $(TOP)/config/system.mak

ifeq ($(SYSTEM_LOADED),)
	MACHINETYPE=unknown
	OSTYPE=unknown
	OSREV=
endif

#  Include project specific rules
-include $(TOP)/config/project.mak


## User defined configuration options
include $(TOP)/config/config

## Compile-time directories 
BINDIR=$(shell test -d $(TOP)/bin.$(PLATFORM) || mkdir $(TOP)/bin.$(PLATFORM); echo $(TOP)/bin.$(PLATFORM);)
SCRIPTDIR=$(BINDIR)
LIBDIR=$(shell test -d $(TOP)/lib.$(PLATFORM) || mkdir $(TOP)/lib.$(PLATFORM); echo $(TOP)/lib.$(PLATFORM);)

ifdef SRCS
OBJDIR=$(shell test -d obj.$(PLATFORM) || mkdir obj.$(PLATFORM); echo obj.$(PLATFORM);)
OBJS=$(shell echo $(SRCS:.c=.o) | awk '{for (i=1; i<=NF; i++) printf("'$(OBJDIR)'/%s\n",$$i)}';)
endif

INCLUDES = -I$(TOP)/include
CPPFLAGS += $(LOCAL_INCLUDES) $(INCLUDES)
EXTRA_CC_FLAGS += 
#-DNEXT_UTT_STATES_VERBOSE
CFLAGS += $(EXTRA_CC_FLAGS) $(OPT) $(DBG)
LDFLAGS += -L$(LIBDIR) $(EXTRA_LD_FLAGS)
STLIBS = $(LIBDIR)/libcommon.a \
	 $(LIBDIR)/libmllr.a \
	 $(LIBDIR)/libio.a \
	 $(LIBDIR)/libs2io.a \
	 $(LIBDIR)/libcep_feat.a \
	 $(LIBDIR)/libmodinv.a \
	 $(LIBDIR)/libclust.a
LIBS := $(STLIBS) $(LIBS)

$(OBJDIR)/%.o : %.c
	$(CC) $(DEFS) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

all: .make_build_dirs $(ALL) nothing

nothing:
	@ echo > /dev/null

.make_build_dirs:
	@ echo making in $(DIRNAME) ...
ifneq (,$(BUILD_DIRS))
	@ for i in $(BUILD_DIRS) ; \
	do \
	   $(MAKE) --print-directory -C $$i || exit ; \
	done
endif


# Library build rules
.build_lib: $(OBJS) 
	$(AR) cruv $(LIBDIR)/lib$(LIBNAME).a $(OBJS)
	$(RANLIB) $(LIBDIR)/lib$(LIBNAME).a
	touch .build_lib

# Program build rules
$(BINDIR)/$(TARGET): $(OBJS) $(STLIBS) $(DEP_LIBS)
	$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS) $(LIBS)

# Festival Script build rules
$(SCRIPTDIR)/%: %.festival
	@ echo "#!/bin/sh" > $@
	@ echo "\"true\"; exec "$(FESTIVAL)' --script $$0 $$*' >>$@
	@ cat $< >>$@
	@ chmod +x $@

# csh Script build rules
$(SCRIPTDIR)/%: %.csh
	@ echo "#!/bin/csh" > $@
	@ cat $< >>$@
	@ chmod +x $@

# Perl Script build rules
$(SCRIPTDIR)/$(DIRNAME)/%.pl: %.pl
	@ if [ ! -d $(SCRIPTDIR)/$(DIRNAME) ] ;\
	  then  \
	     mkdir -p $(SCRIPTDIR)/$(DIRNAME); \
	  fi
	@ echo "#!"$(PERL) > $@
	@ sed '1d' $< >>$@
	@ chmod +x $@

clean:
	@ echo make clean in $(DIRNAME) ...
	@(test "$(LIBNAME)" && $(RM) $(LIBDIR)/lib$(LIBNAME).a) || true
	@(test "$(TARGET)" && $(RM) $(BINDIR)/$(TARGET)) || true
	@$(RM) *.o *.a *~ $(LOCAL_CLEAN)
	@$(RM) -r $(OBJDIR)
ifneq (,$(ALL_DIRS))
	@ for i in $(ALL_DIRS) ; \
	do \
	   $(MAKE) -C $$i --no-print-directory clean; \
	done
endif

DEPEND=make.depend
MAKE_DEPEND=$(CC) -MM $(CFLAGS) $(CPPFLAGS)

depend:
	@ echo make depend in $(DIRNAME) ...
	@ rm -f $(DEPEND)
	@ $(MAKE) nothing
ifdef ALL_DIRS
	@ for i in $(ALL_DIRS) ; \
	do \
	   $(MAKE) -C $$i --no-print-directory depend ; \
	done
endif

$(DEPEND): $(SRCS)
	@ rm -f $(DEPEND)
	@ for i in $(SRCS) ; \
	  do \
            echo "# $$i" ; \
	    $(MAKE_DEPEND) $$i | sed -e 's,^\([^:]*.o\):,$(OBJDIR)/\1:,' ; \
	    echo ; \
          done > $(DEPEND)
file-list:
	@ echo making file-list in $(DIRNAME) ...
	@ for f in $(FILES) ; \
	do \
	   echo $(DIRNAME)/$$f >>$(TOP)/FileList ; \
	done 
ifneq (,$(ALL_DIRS))
	@ for i in $(ALL_DIRS) ; \
	do \
	   $(MAKE) -C $$i --no-print-directory file-list; \
	done
endif

info:
	@echo Project Name = $(PROJECT_NAME)
	@echo Project Prefix = $(PROJECT_PREFIX)
	@echo Project Version = $(PROJECT_VERSION)
	@echo Project Date = $(PROJECT_DATE)
	@echo Project State = $(PROJECT_STATE)
	@echo
	@echo Configuration Variables
	@echo none at present

# If there are sources in this directory, load in the dependencies
ifdef SRCS
-include $(DEPEND)
endif
