## The type of library we want to build. Possible values:
##   relocatable
##   static
LIBRARY_TYPE=static

# Replace with the location for the installation directory
INSTALL=@INSTALL@
PACKAGE_TARNAME=@PACKAGE_TARNAME@
VERSION=@PACKAGE_VERSION@
BUILDS_SHARED=@GNAT_BUILDS_SHARED@
prefix=@prefix@
exec_prefix=@exec_prefix@
datadir=@datadir@
datarootdir=@datarootdir@
bindir=@bindir@
libdir=@libdir@
xmllib=${libdir}/xmlada/${LIBRARY_TYPE}/
docdir=@docdir@
includedir=@includedir@

# Set a default value (ignored if set in the environment)
PROCESSORS?=1
GNATMAKE=@gnatmake@ -j${PROCESSORS}

# Compiler mode: one of "distrib", "Debug", "Production", "profile", "coverage"
MODE=@BUILD_TYPE@

LIBEXT=@SO_EXT@

# Commands for installation procedure
CHMOD=chmod
LN_S=@LN_S@
MKDIR=@MKDIR@

# In some cases, we use cp instead of install for doing the installation,
# since the latter does not support installing multiple files at once.
CP=cp -p

#########################################################################
## Nothing to modify below this point
#########################################################################

MODULES=unicode input_sources sax dom schema
MODULE_INSTALL=${MODULES:%=%_inst}

MAKE_ARGS=-XBUILD=${MODE} -XLIBRARY_TYPE=${LIBRARY_TYPE}

.PHONY: all obj clean install install_dirs build_library_type install_library_type

ifeq (${BUILDS_SHARED},yes)
all: static relocatable
install: install_static install_relocatable
else
all: static
install: install_static
endif

build_library_type: obj
static:
	${MAKE} LIBRARY_TYPE=static build_library_type
shared relocatable:
	${MAKE} LIBRARY_TYPE=relocatable build_library_type

obj: force
	${GNATMAKE} -m -p -Punicode/unicode.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Pinput_sources/input_sources.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Psax/sax.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Pdom/dom.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Pschema/schema.gpr ${MAKE_ARGS}

test: force
	${GNATMAKE} -m -p -Punicode/test/unicode_test.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Psax/test/sax_test.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Pdom/test/dom_test.gpr ${MAKE_ARGS}
	${GNATMAKE} -m -p -Pschema/test/schema_test.gpr ${MAKE_ARGS}

run_test: force
	@make --no-print-directory -C unicode/test tests
	@make --no-print-directory -C sax/test tests
	@make --no-print-directory -C dom/test tests
	@make --no-print-directory -C schema/test tests
run_test_valgrind: test
	cd dom/test; valgrind --tool=memcheck --leak-check=full ./testxml -auto

docs: force
	-${MAKE} -e -C docs html latexpdf

# Rule for installing the modules
%_inst: force
	${CP} $*/*.ad[bs]        ${includedir}/xmlada
	cd $*; ls *.ad[bs] > ${prefix}/lib/gnat/xmlada/xmlada_$*.lgpr
	-cd ${xmllib}; ${CHMOD} ugo+w *.ali
	-${CP} $*/lib/${LIBRARY_TYPE}/*.ali   ${xmllib}
	${RM} ${libdir}/libxmlada_$**
	${RM} ${xmllib}libxmlada_$**
ifeq (${LIBRARY_TYPE},static)
	${INSTALL} $*/lib/${LIBRARY_TYPE}/libxmlada_$*.a ${xmllib}
else
ifeq (${LIBEXT},.dll)
	${INSTALL} $*/lib/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT} ${xmllib}
	cd ${bindir}; ${CP} ${libdir}/xmlada/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT} libxmlada_$*${LIBEXT}
else
	${INSTALL} $*/lib/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT}.${VERSION} ${xmllib}

	@# prefix/lib/xmlada/relocatable/libxmlada.so -> libxmlada.so.4.4.0w
	cd ${xmllib}; ${LN_S} libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}

	@# prefix/lib/xmlada/relocatable/libxmlada.so.4.4 -> libxmlada.so.4.4.0w
	-cd ${xmllib}; ${LN_S} libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}.${basename ${VERSION}}

	@# prefix/lib/xmlada/relocatable/libxmlada.so.4 -> libxmlada.so.4.4.0w
ifneq (${basename ${basename ${VERSION}}},${basename ${VERSION}})
	cd ${xmllib}; ${LN_S} libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}.${basename ${basename ${VERSION}}}

	@# prefix/lib/libxmlada.so.4 -> prefix/lib/xmlada/relocatable/libxmlada.so.4.4.0w
	cd ${libdir}; ${LN_S} xmlada/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}.${basename ${basename ${VERSION}}}
endif

	@# prefix/lib/libxmlada.so.4.4 -> prefix/lib/xmlada/relocatable/libxmlada.so.4.4.0w
	-cd ${libdir}; ${LN_S} xmlada/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}.${basename ${VERSION}}

	@# prefix/lib/libxmlada.so.4.4.0w -> prefix/lib/xmlada/relocatable/libxmlada.so.4.4.0w
	-cd ${libdir}; ${LN_S} xmlada/${LIBRARY_TYPE}/libxmlada_$*${LIBEXT}.${VERSION} libxmlada_$*${LIBEXT}.${VERSION}
endif
endif
	cd ${xmllib}; ${CHMOD} ugo-w *.ali

install_dirs: force
	@${MKDIR} ${bindir}
	@${MKDIR} ${xmllib}
	@${MKDIR} ${prefix}/lib/gnat/xmlada
	@${MKDIR} ${libdir}
	@${MKDIR} ${datadir}/doc/xmlada
	@${MKDIR} ${includedir}/xmlada
	@${MKDIR} ${datadir}/gps/plug-ins/

## Do the installation for either shared or static, depending on
## $LIBRARY_TYPE
install_library_type: install_dirs ${MODULE_INSTALL}
	${INSTALL} distrib/xmlada_shared.gpr         ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada.gpr                ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada_dom.gpr            ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada_sax.gpr            ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada_input.gpr          ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada_schema.gpr         ${prefix}/lib/gnat/
	${INSTALL} distrib/xmlada_unicode.gpr        ${prefix}/lib/gnat/
	${INSTALL} xmlada-config  ${bindir}
	-$(CP) -r docs/_build/html/* $(datadir)/doc/xmlada/ 2>/dev/null
	-$(CP) -r docs/_build/latex/XMLAda.pdf $(datadir)/doc/xmlada/ 2>/dev/null
	${CHMOD} +x ${bindir}/xmlada-config
	${INSTALL} distrib/xmlada_gps.py ${datadir}/gps/plug-ins/

## Do the installation for an explicit target
install_static: 
	${MAKE} LIBRARY_TYPE=static install_library_type
install_shared install_relocatable:
	${MAKE} LIBRARY_TYPE=relocatable install_library_type

clean_%: force
	-gnat clean -XLIBRARY_TYPE=$* -q -r -Punicode/unicode.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -r -Pinput_sources/input_sources.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -r -Psax/sax.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -r -Pdom/dom.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -r -Pschema/schema.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -Punicode/test/unicode_test.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -Pdom/test/dom_test.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -Psax/test/sax_test.gpr
	-gnat clean -XLIBRARY_TYPE=$* -q -Pschema/test/schema_test.gpr

clean: clean_relocatable clean_static
	cd docs; ${MAKE} -e clean

distclean: clean
	${RM} dom/test/default.gpr
	${RM} Makefile config.cache config.log config.status Makefile.314 xmlada-config shared.gpr distrib/xmlada_shared.gpr

force:
