## This file is part of Moonlight Creator
##   Copyright (C) 1996-1998  Stephane Rehel
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Library General Public
## License as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.
##
## This library 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
## Library General Public License for more details.
##
## You should have received a copy of the GNU Library General Public
## License along with this library; if not, write to the Free
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


############################################################################
# ML
# Stephane Rehel
# Created: June 28, 1996
#
# Common Makefiles rules
#
############################################################################

ifeq ($(debug),yes)
CFLAGS+= -g
CXXFLAGS+= -g
endif

############################################################################
#
# Important Note: Makeconfig and Makenames must be included before this line
#
############################################################################

DEFINES= $(LOCAL_DEFINES)
INCLUDES= $(LOCAL_INCLUDES) -I$(TOPDIR) $(GL_INCLUDES)

.SUFFIXES: .o .c .C .h .S

ifdef LIBRARY
REAL_AR=$(AR)
LIBRARY_NAME=$(LIB_DIR)/lib$(LIBRARY).a
else
REAL_AR=true
endif

%.o: $(OBJ_DIR)/%.o
	@true

$(OBJ_DIR)/%.o: %.C
	@THE_CC="$(CXX) -c $< $(CXXFLAGS) $(DEFINES) $(INCLUDES) -o $(OBJ_DIR)/$*.o" ; \
	echo "($(LIBRARY)) $$THE_CC" ; \
	$$THE_CC
	@$(REAL_AR) $(ARFLAGS) $(LIBRARY_NAME) $(OBJ_DIR)/$*.o

$(OBJ_DIR)/%.o: %.c
	@THE_CC="$(CC) -c $< $(CFLAGS) $(DEFINES) $(INCLUDES) -o $(OBJ_DIR)/$*.o" ; \
	echo "($(LIBRARY)) $$THE_CC" ; \
	$$THE_CC
	@$(REAL_AR) $(ARFLAGS) $(LIBRARY_NAME) $(OBJ_DIR)/$*.o

$(OBJ_DIR)/%.o: %.S
	@CMD="$(CC) -c $< $(CFLAGS) $(DEFINES) -o $*.o" ; \
	echo "($(LIBRARY))" $$CMD" ; \
	$$CMD
	@$(REAL_AR) $(ARFLAGS) $(LIBRARY_NAME) $(OBJ_DIR)/$*.o

%.C: %.l
	$(FLEX) $(FLEX_FLAGS) $*.l > $*.C

%.C: %.y
	set -e ; \
	$(YACC) $(YACC_FLAGS) -o $* $*.y ; \
	mv -f $* $*.C

OBJS_C= $(SRCS:.c=.o)
OBJS_CC= $(OBJS_C:.C=.o)
OBJS_S= $(OBJS_CC:.S=.o)
OBJS_Y= $(OBJS_S:.y=.o)
OBJS_L= $(OBJS_Y:.l=.o)
OBJS= $(addprefix $(OBJ_DIR)/,$(OBJS_L))

library: $(OBJ_DIR) $(DEPEND) $(OBJS) $(LIBRARY_NAME) subdirs

$(OBJ_DIR):
	mkdir -p $(OBJ_DIR)

$(LIBRARY_NAME): $(OBJS)

subdirs:
	@if [ "$(SUBDIRS)" != "" ] ; then \
	set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i; fi ; \
	done ; \
	fi

clean:
	$(RM) core* *.o *~ *.a *.bak $(DEPEND)
	$(RM) -rf $(OBJ_DIR)/
	@set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i $@; fi ; \
	done

mrproper:
	$(RM) -rf obj/
	@set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i $@; fi ; \
	done

depclean:
	$(RM) $(DEPEND)
	@set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i $@; fi ; \
	done

config-clean:
	$(RM) aclocal.m4 config.h.in config.h config.status config.log config.cache
	$(RM) Makeconfig configure

MKDEP_CMD="$(MKDEP) $(CXXFLAGS) $(DEFINES) $(INCLUDES) $(SRCS) | \
  ( while read line ; \
      do \
       echo $(OBJ_DIR)/\$$line >> $(DEPEND) ; \
      done )"

$(DEPEND):
	@if [ "$(SRCS)" != "" ] ; then \
	  echo $(MKDEP_CMD) ; \
	  eval $(MKDEP_CMD) ; \
	fi

dep: depend

redep: $(OBJ_DIR)
	$(RM) $(DEPEND)
	$(MAKE) $(DEPEND)

depend: redep
	@set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i $@; fi ; \
	done

count:
	gcc -E $(DEFINES) $(INCLUDES) $(SRCS) | wc

lines:
	wc `find . -name \*.[Chc] -print`

libs: $(OBJS)
ifdef LIBRARY
	$(REAL_AR) $(ARFLAGS) $(LIBRARY_NAME) $(OBJS)
endif
	@set -e; for i in "" $(SUBDIRS); do \
	  if [ "$$i" != "" ] ; then $(MAKE) -C $$i $@; fi ; \
	done

ml:
	$(MAKE) -C $(TOPDIR) $@

_ml:
	$(MAKE) -C $(TOPDIR) $@

ifeq ($(DEPEND),$(wildcard $(DEPEND)))
include $(DEPEND)
endif

############################################################################
#
# Makerules end
#
############################################################################
