# Replace with the location for the installation directory
PREFIX=@EXEC_PREFIX@

## The type of library we want to build
LIBRARY_TYPE=relocatable
# LIBRARY_TYPE=static

GNATMAKE=gnatmake
CC=gcc

# Compiler mode: one of "distrib", "debug", "optimize", "profile"
MODE=distrib

CHMOD=chmod
CP=cp -p -f
MKDIR=mkdir -p

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

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

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

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

.PHONY: all obj clean install install_dirs

all: obj

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

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

run_test: tests
	cd dom/test; make tests
	cd schema/test; make tests
run_test_valgrind: test
	cd dom/test; valgrind --tool=memcheck --leak-check=full ./testxml -auto

docs: force
	-${MAKE} -e -C docs all_check

# Rule for installing the modules
%_inst: force
	${CP} $*/*.ad[bs]        ${PREFIX}/include/xmlada
	-${CP} $*/lib/*.ali       ${PREFIX}/lib/xmlada/
	-${CP} $*/lib/*xmlada_* ${PREFIX}/lib/xmlada/
	${RM} ${PREFIX}/lib/*xmlada_$**
	cd ${PREFIX}/lib; ${foreach lib, ${notdir ${wildcard $*/lib/*xmlada_*}}, \
	   ${RM} ${PREFIX}/lib/${lib}; \
           ${LN} xmlada/${lib} ${PREFIX}/lib/${lib};}
	cd ${PREFIX}/lib/xmlada; ${CHMOD} ugo-w *.ali
	cd $*; ls *.ad[bs] > ${PREFIX}/lib/gnat/xmlada/xmlada_$*.lgpr

install_dirs: force
	@${MKDIR} ${PREFIX}/bin
	@${MKDIR} ${PREFIX}/lib
	@${MKDIR} ${PREFIX}/lib/xmlada
	@${MKDIR} ${PREFIX}/share/doc/xmlada
	@${MKDIR} ${PREFIX}/include/xmlada
	@${MKDIR} ${PREFIX}/lib/gnat
	@${MKDIR} ${PREFIX}/lib/gnat/xmlada
	@${MKDIR} ${PREFIX}/share/gps/plug-ins/

install: obj install_dirs ${MODULE_INSTALL}
	${CP} distrib/${LIBRARY_TYPE}/*.gpr ${PREFIX}/lib/gnat
	${CP} xmlada-config  ${PREFIX}/bin
	-${CP} docs/xml.ps   ${PREFIX}/share/doc/xmlada 2>/dev/null
	-${CP} docs/xml.pdf  ${PREFIX}/share/doc/xmlada
	-${CP} docs/xml.html ${PREFIX}/share/doc/xmlada
	-${CP} docs/xml.info ${PREFIX}/share/doc/xmlada
	${CHMOD} +x ${PREFIX}/bin/xmlada-config
	${CP} distrib/xmlada.gpr ${PREFIX}/lib/gnat/xmlada.gpr
	${CP} distrib/xmlada_gps.py ${PREFIX}/share/gps/plug-ins/

clean: force
	gnat clean -q -r -Punicode/unicode.gpr
	gnat clean -q -r -Pinput_sources/input_sources.gpr
	gnat clean -q -r -Psax/sax.gpr
	gnat clean -q -r -Pdom/dom.gpr
	gnat clean -q -r -Pschema/schema.gpr
	gnat clean -q -Punicode/test/unicode_test.gpr
	gnat clean -q -Pdom/test/dom_test.gpr
	gnat clean -q -Psax/test/sax_test.gpr
	gnat clean -q -Pschema/test/schema_test.gpr
	cd docs; ${MAKE} -e clean

distclean: clean
	${RM} dom/test/default.gpr
	${RM} Makefile config.cache config.log config.status Makefile.314

force:
