## This file is part of Moonlight Creator
##   Copyright (C) 1996-1998  Stephane Rehel
##
## This program 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 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
## 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
#
# April 1998: switched to libtool. ough!
#
############################################################################

ifneq (,)
This makefile requires GNU Make.
endif

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

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

.SUFFIXES: .o .lo .c .C .h .S .y .l .la

ifeq ($(LIBRARY),)
  ifeq ($(PLUGIN),)
    OBJ_DIR=obj/$(OPREFIX)
    PLUGIN_NAME=
    LIBRARY_NAME=
  else
    PLUGIN_NAME=lib$(LIB_MOON_PREFIX)$(PLUGIN)
    PLUGIN_LA_NAME=$(PLUGIN_NAME).la
    PLUGIN_LA_FILENAME=$(PLUGIN_DIR)/$(PLUGIN_LA_NAME)
    OBJ_DIR=$(PLUGIN_DIR)/$(PLUGIN_NAME)
    TARGET_LA_FILENAME=$(PLUGIN_LA_FILENAME)
    TARGET_DIR=$(PLUGIN_DIR)
  endif
else
  LIBRARY_NAME=lib$(LIB_MOON_PREFIX)$(LIBRARY)
  LIBRARY_LA_NAME=$(LIBRARY_NAME).la
  LIBRARY_LA_FILENAME=$(LIB_DIR)/$(LIBRARY_LA_NAME)
  OBJ_DIR=$(LIB_DIR)/$(LIBRARY_NAME)
  TARGET_LA_FILENAME=$(LIBRARY_LA_FILENAME)
  TARGET_DIR=$(LIB_DIR)
endif

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))
LOBJS= $(OBJS:.o=.lo)

compile-all: objects subdirs

#############################################################################

build-plugin: $(PLUGIN_LA_FILENAME)

$(PLUGIN_LA_FILENAME): $(OBJ_DIR)/stamp-objs
	@echo --
	@echo ---------- Plug-in $(PLUGIN_LA_NAME) ----------
	@echo --
	@if test ! -d $(PLUGIN_DIR) ; then mkdir -p $(PLUGIN_DIR) ; fi
	set -e ; cd $(PLUGIN_DIR) ; \
	$(LIB_LINK) --quiet $(CXX) -o $(PLUGIN_LA_NAME) \
             -export-dynamic \
             -rpath $(ml_libdir) \
             -version-info $(VERSION_MAJOR):$(VERSION_MINOR):0 \
             $(PLUGIN_NAME)/*.lo \
             $(PLUGIN_BASE_LIBS)
	set -e ; test -z "$(PLUGIN_DEP_LA_LIBS)" || \
 echo $(PLUGIN_DEP_LA_LIBS) > $(PLUGIN_LA_FILENAME).dep
	(pushd $(PLUGIN_DIR) >/dev/null ; \
  THE_PLUGIN_DIR=`pwd` ; popd > /dev/null ; cd $(AI_LIB_DIR) ; \
  $(RM) plugins ; $(LN_S) $$THE_PLUGIN_DIR plugins )
	@$(INCBUILD) $(PLUGIN)

#############################################################################

build-library: $(LIBRARY_LA_FILENAME)

$(LIBRARY_LA_FILENAME): $(OBJ_DIR)/stamp-objs
	@echo --
	@echo ---------- Library $(LIBRARY_LA_NAME) ----------
	@echo --
	@if test ! -d $(LIB_DIR) ; then mkdir -p $(LIB_DIR) ; fi
	set -e ; cd $(LIB_DIR) ; \
	$(LIB_LINK) --quiet $(CC) -o $(LIBRARY_LA_NAME) \
             -rpath $(ml_libdir) \
             -version-info $(VERSION_MAJOR):$(VERSION_MINOR):0 \
             $(LIBRARY_NAME)/*.lo \
             $(LIBRARY_BASE_LIBS)
	set -e ; test -z "$(LIBRARY_DEP_LA_LIBS)" || \
 echo $(LIBRARY_DEP_LA_LIBS) > $(LIBRARY_LA_FILENAME).dep
	touch $(LIB_DIR)/stamp-libs
	@$(INCBUILD) $(LIBRARY)

#############################################################################

ifeq ($(OBJS),)
objects:
else
objects: $(OBJ_DIR) $(DEPEND) $(OBJS)
endif

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

# if user do "make"
$(OBJ_DIR)/%.o:
	@echo ---------- $*.o ----------
	@if test ! -d $(OBJ_DIR) ; then mkdir -p $(OBJ_DIR) ; fi
ifeq ($(WINDOWS),yes)
	$(CXX) $(CXXFLAGS) $(DEFINES) $(INCLUDES) -c $*.C -o $(OBJ_DIR)/$*.lo
	cp $(OBJ_DIR)/$*.lo $(OBJ_DIR)/$*.o
else
	$(LIB_COMPILE) $(CXX) $(CXXFLAGS) $(DEFINES) $(INCLUDES) -c $*.C
	test -f $*.o || cp $*.lo $*.o ; mv -f $*.o $(OBJ_DIR)
	test -f $*.lo && mv -f $*.lo $(OBJ_DIR) || true
endif
	touch $(OBJ_DIR)/stamp-objs

# if user do "make foo.o"
.C.o:
	$(MAKE) $(OBJ_DIR)/$*.o

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

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

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

$(LIB_DIR):
	mkdir -p $(LIB_DIR)

subdirs:
ifneq ($(SUBDIRS),)
	set -e; for i in $(SUBDIRS) ; do $(MAKE) -C $$i ; done
endif

# clean up objects built from this directory and subdirs
clean:
	$(RM) '#*#' '*~' $(DEPEND) *.o *.lo
	$(RM) $(OBJS) $(LOBJS)
	$(RM) $(OBJ_DIR)/stamp-objs
ifneq ($(SUBDIRS),)
	set -e; for i in $(SUBDIRS) ; do $(MAKE) -C $$i $@ ; done
endif

# clean up objects built for this library
# also clean up the library itself
libclean:
	$(RM) -rf $(OBJ_DIR)
	$(RM) $(TARGET_LA_FILENAME) $(TARGET_LA_FILENAME).dep

# clean up any dependency file in the dir and subdirs
depclean:
	$(RM) $(DEPEND)
ifneq ($(SUBDIRS),)
	set -e; for i in $(SUBDIRS) ; do $(MAKE) -C $$i $@ ; done
endif

# build the dependency file of the current directory
$(DEPEND):
	$(RM) $(DEPEND)
ifneq ($(SRCS),)
	$(MKDEP) $(CXXFLAGS) $(DEFINES) $(INCLUDES) $(SRCS) | \
	  ( while read line ; do echo $(OBJ_DIR)/$$line >> $(DEPEND) ; done )
endif

depend: $(DEPEND)

# build the dependency file of the current directory and subdirs
dep:
	$(RM) $(DEPEND)
	$(MAKE) $(DEPEND)
ifneq ($(SUBDIRS),)
	set -e; for i in $(SUBDIRS) ; do $(MAKE) -C $$i $@ ; done
endif

# count the lines in the current directory
count:
ifneq ($(SUBDIRS),)
	gcc -E $(DEFINES) $(INCLUDES) $(SRCS) | wc
endif

# count the lines in the current directory and subdirs
lines:
	wc `find . -name \*.[Chc] -print`

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

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

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

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