SHELL=/bin/sh
@SET_MAKE@

.SUFFIXES:
.SUFFIXES: .c .o .ads .adb .ali

.PHONY:
.PHONY: clean-generic mostlyclean-generic \
	clean distclean mostlyclean maintainer-clean

include Makefile.common

MAKE_ADB = make_gtk

CFLAGS = @CFLAGS@ -Wall @GTK_CFLAGS@ -I.. $(FPIC)

GNATMAKEBINDFLAGS =
GNATMAKELDFLAGS = -largs -L. -lgtkada $(GTK_LIBS)

GETTEXT_INTL=@GETTEXT_INTL@
HAVE_GETTEXT=@HAVE_GETTEXT@

LIB_DIR=lib-obj

CD = cd
MKDIR = mkdir -p
RMDIR = rmdir
MV = mv
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

LIBNAME       = libgtkada$(SO_EXT)
SONAME        = libgtkada-$(MAJOR).$(MINOR)$(SO_EXT).$(MICRO)
ARCHIVENAME   = libgtkada.a

GTKADA_CONFIG = gtkada-config
GATE_SRC      = gate.adb
GATE_EX       = gate-in.exe
GATE          = gate
GENERATE      = generate.pl
DGATE         = dgate
GDIALOG       = gdialog
GENERATE_PRJ  = ./generate_prj
PRJ           = gtkada.gpr

c_objects = $(LIB_DIR)/misc.o $(LIB_DIR)/misc_extra.o $(LIB_DIR)/misc_broken.o
c_extra_objects = gtkextra/gtk*.o

## General and public targets

all: dirs libs tools

ifeq ($(BUILD_SHARED),yes)
libs: extra $(LIBNAME) $(ARCHIVENAME) opengl glade gnome install_ali
else
libs: extra $(ARCHIVENAME) opengl glade gnome install_ali
endif

opengl: force
	$(MAKE) -C opengl

extra: force
	$(MAKE) -C gtkextra

glade: force
	$(MAKE) -C glade

gnome: force
	$(MAKE) -C gnome

tools: $(GATE_EX) $(GDIALOG)

## Private targets

dirs:: force
	-$(MKDIR) $(LIB_DIR)
	-$(CHMOD) +x $(GATE) $(GTKADA_CONFIG)

$(LIBNAME):: $(LIB_DIR)/$(MAKE_ADB).o ${wildcard ${LIB_DIR}/*.o} $(c_objects)
ifeq ($(BUILD_SHARED),yes)
	$(CC) -shared $(FPIC) $(OS_SPECIFIC_LINK_OPTIONS) -o $(SONAME) \
	  $(SO_OPTS)$(SONAME) $(LIB_DIR)/glib*.o $(LIB_DIR)/gdk*.o \
	  $(LIB_DIR)/gtk*.o $(LIB_DIR)/pango*.o \
	  $(c_objects) $(c_extra_objects) $(GTK_LIBS)
	$(RM) $(LIBNAME)
	$(LN) $(SONAME) $(LIBNAME)
	-@if [ ! -f .devel ]; then \
	  strip $(LIBNAME); \
	  $(CHMOD) +x $(LIBNAME); \
	fi
endif

$(ARCHIVENAME):: $(LIB_DIR)/$(MAKE_ADB).o ${wildcard ${LIB_DIR}/*.o} $(c_objects)
	$(AR) $(ARFLAGS) $(ARCHIVENAME) $(LIB_DIR)/glib*.o $(LIB_DIR)/gdk*.o $(LIB_DIR)/gtk*.o $(LIB_DIR)/pango*.o $(c_objects) $(c_extra_objects)
	if [ -f /usr/bin/$(RANLIB) -o -f /bin/$(RANLIB) ]; then \
	  $(RANLIB) $(ARCHIVENAME); \
	fi

# This copies the .ali files from lib_obj/ to the current directory, and change
# the permission of the files so that dgate, gate and testgtk use the dynamic
# library. We do not do this if .devel exists, so that gnatmake will still
# rebuild the files (it does not check anything if the .ali files are read-only.
install_ali::
	test -f .devel || $(CP) $(LIB_DIR)/*.ali .
	test -f .devel || $(CHMOD) -w glib*.ali gdk*.ali gtk*.ali

$(GATE_EX):: $(GATE_SRC) $(LIBNAME) force
	@if [ -f .devel ]; then \
	  $(GNATMAKE) -aO$(LIB_DIR) $(GNATFLAGS_DEVEL) -o $(GATE_EX) $(GATE_SRC) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	else \
	  $(GNATMAKE) $(GNATFLAGS) -o $(GATE_EX) $(GATE_SRC) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	fi

$(DGATE):: $(DGATE).adb $(LIBNAME) glade
	@if [ -f .devel ]; then \
	   $(GNATMAKE) -aO$(LIB_DIR) -Iglade $(GNATFLAGS_DEVEL) $(DGATE) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	else \
	   $(GNATMAKE) -Iglade $(GNATFLAGS) $(DGATE) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	fi

$(GDIALOG):: $(GDIALOG).adb $(LIBNAME)
	@if [ -f .devel ]; then \
	   $(GNATMAKE) -aO$(LIB_DIR) $(GNATFLAGS_DEVEL) $(GDIALOG) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	else \
	   $(GNATMAKE) $(GNATFLAGS) $(GDIALOG) $(GNATMAKEBINDFLAGS) $(GNATMAKELDFLAGS); \
	fi

$(LIB_DIR)/$(MAKE_ADB).o:: gtkada-intl.adb force
	@if [ -f .devel ]; then \
	  $(CD) $(LIB_DIR); \
	  $(GNATMAKE) -c ../$(MAKE_ADB) $(GNATFLAGS_DEVEL) $(FPIC); \
	else \
	  $(CD) $(LIB_DIR); \
	  $(GNATMAKE) -c ../$(MAKE_ADB) $(GNATFLAGS) $(FPIC); \
	fi

gtkada-intl.adb: gtkada-intl.gpb Makefile
	gnatprep -DGETTEXT_INTL=$(GETTEXT_INTL) \
	  -DHAVE_GETTEXT=$(HAVE_GETTEXT) gtkada-intl.gpb gtkada-intl.adb

install: all
	@if [ "$(prefix)" != `pwd` ]; then \
	   echo Installing GtkAda in $(prefix); \
	   $(MKDIR) $(bindir); \
	   $(MKDIR) $(libdir); \
	   $(MKDIR) $(incdir); \
	   $(MKDIR) $(alidir); \
	   $(GENERATE_PRJ); \
	   $(INSTALL_DATA) $(PRJ) $(incdir); \
	   $(INSTALL_DATA) $(ARCHIVENAME) $(libdir); \
	   if [ $(BUILD_SHARED) = yes ]; then \
	     $(INSTALL_DATA) $(SONAME) $(libdir); \
	     $(RM) $(libdir)/$(LIBNAME); \
	     $(LN) $(SONAME) $(libdir)/$(LIBNAME); \
	   fi; \
	   $(CP) glib*.ads glib*.adb $(MESA_SPECS) $(incdir); \
	   $(CP) gdk*.ads gdk*.adb $(incdir); \
	   $(CP) gtk*.ads gtk*.adb $(incdir); \
	   $(CP) pango*.ads pango*.adb $(incdir); \
	   $(CP) $(LIB_DIR)/glib*.ali $(LIB_DIR)/gdk*.ali $(LIB_DIR)/gtk*.ali $(LIB_DIR)/pango*.ali $(alidir); \
	   $(CHMOD) -w $(alidir)/*.ali; \
	   $(INSTALL_SCRIPT) $(GTKADA_CONFIG) $(bindir); \
           $(INSTALL_SCRIPT) $(GATE) $(bindir); \
           $(INSTALL_SCRIPT) $(GENERATE) $(bindir); \
           $(INSTALL_PROGRAM) $(GATE_EX) $(bindir); \
           $(INSTALL_PROGRAM) $(GDIALOG) $(bindir); \
           $(MAKE) -C opengl install; \
           $(MAKE) -C glade install; \
           $(MAKE) -C gnome install; \
	fi
	@echo '------------------------------------------------------------------'
	@echo '--  GtkAda has now been installed.                              --'
	@echo '--  To be able to use the library, you may need to update your  --'
	@echo '--  LD_LIBRARY_PATH or to run ldconfig. You may also need to    --'
	@echo '--  update your PATH to include gtkada-config in it.            --'
	@echo '------------------------------------------------------------------'

clean-generic:
	-${RM} *.o *.ali *~ b_*.c b~*.ad? core
	-$(RM) $(LIB_DIR)/*
	-${RM} $(ARCHIVENAME) $(LIBNAME) $(SONAME)
	-${RM} $(GATE_EX) $(DGATE) $(GDIALOG)
	-${RM} .\#*

mostlyclean-generic: clean-generic
	-${RM} Makefile
	-${RM} $(GATE)
	-${RM} $(GTKADA_CONFIG)

clean: clean-generic
	$(MAKE) -C gtkextra clean
	$(MAKE) -C opengl clean
	$(MAKE) -C glade clean
	$(MAKE) -C gnome clean

distclean: mostlyclean-generic
	$(MAKE) -C gtkextra distclean
	$(MAKE) -C opengl distclean
	$(MAKE) -C glade distclean
	$(MAKE) -C gnome distclean
	-$(RM) Makefile.common

mostlyclean: mostlyclean-generic
	$(MAKE) -C gtkextra mostlyclean
	$(MAKE) -C opengl mostlyclean
	$(MAKE) -C glade mostlyclean
	$(MAKE) -C gnome mostlyclean
	-$(RM) Makefile.common

maintainer-clean: mostlyclean-generic
	$(MAKE) -C gtkextra maintainer-clean
	$(MAKE) -C opengl maintainer-clean
	$(MAKE) -C glade maintainer-clean
	$(MAKE) -C gnome maintainer-clean
	-$(RM) Makefile.common

## Building the C files: they all depend on the c file in the source directory
## The dependencies on the .h files have to be given explicitly

$(c_objects): $(LIB_DIR)/%.o : %.c
	$(CD) $(LIB_DIR); $(CC) -c $(CFLAGS) ../$<

force:

