SHELL=/bin/sh
CC = @CC@
FPIC = @FPIC@
PWD = $(shell pwd)

GNATMAKE = gnatmake
OS_SPECIFIC_LINK_OPTIONS = @OS_SPECIFIC_LINK_OPTIONS@
BUILD_SHARED = @BUILD_SHARED@
SO_EXT   = @SO_EXT@
SO_OPTS  = @SO_OPTS@
MAJOR    = @GTKADA_MAJOR_VERSION@
MINOR    = @GTKADA_MINOR_VERSION@
MICRO    = @GTKADA_MICRO_VERSION@
GTK_LIBS = @GTK_LIBS@
HAVE_LIBGLADE = @HAVE_LIBGLADE@
HAVE_OPENGL   = @HAVE_OPENGL@
HAVE_GNOME    = @HAVE_GNOME@
BUILD_TYPE      = @BUILD_TYPE@
DEFAULT_LIBRARY_TYPE=@DEFAULT_LIBRARY_TYPE@

GPR_BUILD=-XBUILD=${BUILD_TYPE} -XOPENGL=${HAVE_OPENGL} -XGNOME=${HAVE_GNOME} -XGLADE=${HAVE_LIBGLADE}

CP = cp -p -f
ifeq ($(OS),Windows_NT)
   LN = $(CP)
else
   LN = ln -f -s
endif

CHMOD = chmod
ARFLAGS = cr
CD = cd
MKDIR = mkdir -p
RMDIR = rmdir
MV = mv
RM = rm -f

RANLIB = @RANLIB@

prefix        = @prefix@
exec_prefix   = $(prefix)
bindir        = $(prefix)/bin
libdir        = $(prefix)/lib
incdir        = $(prefix)/include/gtkada
alidir        = $(prefix)/lib/gtkada

#################################################################
## Generic targets
## This code is reused by all modules, but is put in a common place
## to avoid code duplication.
## Object directory for the modules must be defined in OBJ_DIR.
## Library to generate is defined in SONAME, ARCHIVENAME and LIBNAME
## List of source files basenames in SRC_FILES
## Additional external libs in EXTERNAL_LIBS
## Module's project file in GPRFILE
#################################################################

.PHONY: clean distclean module_ada module_install_common module_libname \
   module_archivename force
.SUFFIXES: .c .o .ads .adb .ali

all:

ifeq ($(BUILD_SHARED),yes)
module_all:     module_ada ${OBJ_DIR}/${ARCHIVENAME} ${OBJ_DIR}/${SONAME}
module_install: module_install_common module_install_shared
else
module_all:     module_ada ${OBJ_DIR}/${ARCHIVENAME}
module_install: module_install_common
endif

module_ada: force
	${GNATMAKE} -P${GPRFILE} -u ${FPIC} ${GPR_BUILD}

ifneq (${SONAME},)
module_install_common: force
	$(INSTALL_DATA) ${OBJ_DIR}/$(ARCHIVENAME) $(alidir)/static
	$(RM) $(libdir)/$(ARCHIVENAME)
	$(CP) *.ads *.adb $(incdir)
	${CP} ${OBJ_DIR}/*.ali ${alidir}/static

module_install_shared: force
	$(INSTALL_DATA) ${OBJ_DIR}/$(SONAME) $(alidir)/relocatable
	$(RM) $(libdir)/$(LIBNAME)
	$(LN) $(SONAME) $(alidir)/relocatable/$(LIBNAME)
	$(LN) gtkada/relocatable/$(SONAME) $(libdir)/$(LIBNAME)
	$(LN) gtkada/relocatable/$(SONAME) $(libdir)/$(SONAME)
	${CP} ${OBJ_DIR}/*.ali ${alidir}/relocatable

${OBJ_DIR}/${SONAME}: ${wildcard ${SRC_FILES:%=${OBJ_DIR}/%}}
	@echo Building ${SONAME}
	@cd ${OBJ_DIR}; $(CC) -shared $(FPIC) $(OS_SPECIFIC_LINK_OPTIONS) \
	  -o $(SONAME) $(SO_OPTS)$(SONAME) ${SRC_FILES} \
          $(GTK_LIBS) $(EXTERNAL_LIBS)
	@cd ${OBJ_DIR}; $(RM) $(LIBNAME)
	@cd ${OBJ_DIR}; $(LN) $(SONAME) $(LIBNAME)
ifneq (${BUILD_TYPE},Debug)
ifneq (${OS},Windows_NT)
	@cd ${OBJ_DIR}; strip $(LIBNAME)
endif
endif
	@cd ${OBJ_DIR}; ${CHMOD} +x ${LIBNAME}

${OBJ_DIR}/${ARCHIVENAME}: ${wildcard ${SRC_FILES:%=${OBJ_DIR}/%}}
	@echo Building ${ARCHIVENAME}
	@cd ${OBJ_DIR}; $(AR) $(ARFLAGS) $(ARCHIVENAME) ${SRC_FILES}
	@if [ -f /usr/bin/$(RANLIB) -o -f /bin/$(RANLIB) ]; then \
	   cd ${OBJ_DIR}; $(RANLIB) $(ARCHIVENAME); \
	fi
endif

module_clean: force
	-${RM} *~ core .\#*
	-${RM} ${OBJ_DIR}/*.ali
	-${RM} ${OBJ_DIR}/*.o
	-${RM} ${OBJ_DIR}/lib*

module_distclean: clean
	-${RM} Makefile

force:
