###########################################################
# File: Makefile.xt
#
# autoxt
#
# Description
#
# This Makefile provides support for compiling Stratego 
# programs and use of some other XT tools.
#
###########################################################
#
# Variables
#

AM_CFLAGS  = $(STR_CFLAGS)
AM_LDFLAGS = $(STR_LDFLAGS)

if XT_XTC_REGISTER
  AM_CFLAGS += -DDEFAULT_XTC_REPOSITORY="\"$(REPOSITORY)\""
endif

AM_CPPFLAGS = $(STRATEGO_LIB_CFLAGS) $(STRATEGO_RUNTIME_CFLAGS) $(ATERM_CFLAGS)
LDADD = $(STRATEGO_LIB_LIBS) $(STRATEGO_RUNTIME_LIBS) $(ATERM_LIBS)

# backwards compatibilty: prefer STRATEGO_LIB_LIBS and STRATEGO_RUNTIME_LIBS in new code.
SSL_LIBS = $(ATERM_LIBS) $(STRATEGO_XTC_LIBS) $(STRATEGO_LIB_LIBS) $(STRATEGO_RUNTIME_LIBS) $(ATERM_LIBS)
STRATEGORUNLIBS = $(STRATEGO_RUNTIME_LIBS)


SUFFIXES      = .str .cr .r .rtree .def .tbl .pp .sdf .rtg .rtg-nf .dfta

PARSESTRATEGO = $(STRATEGO_FRONT)/bin/parse-stratego

pkgconfigdir  = $(libdir)/pkgconfig
sdfdatadir    = $(datadir)/sdf/$(PACKAGE)
docdatadir    = $(datadir)/doc/$(PACKAGE)

###########################################################
#
# Make rules for Stratego programs
#

.str.c : 
	$(SCOMPILE) $(STRINCLUDES) $(STRCFLAGS) $(SCFLAGS) -i $< -o $@ -c

.str.rtree :
	$(PARSESTRATEGO) $(STRINCLUDES) -i $< -o $@ 

.rtree.c : 
	$(SCOMPILE) $(STRINCLUDES) $(STRCFLAGS) $(SCFLAGS) -i $< -o $@ -c

bootclean-am: clean-am
	-test -z "$(BOOTCLEANFILES)" || rm -f $(BOOTCLEANFILES)

bootclean : bootclean-recursive

bootclean-recursive :
	@dot_seen=no; \
	target=bootclean; \
	list='$(BOOTCLEAN_SUBDIRS) .'; for subdir in $$list; do \
	  echo "Making $$target in $$subdir"; \
	  if test "$$subdir" = "."; then \
	    dot_seen=yes; \
	    local_target="$$target-am"; \
	  else \
	    local_target="$$target"; \
	  fi; \
	  (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \
	   || case "$$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; \
	done; \
	if test "$$dot_seen" = "no"; then \
	  $(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \
	fi; test -z "$$fail"


###########################################################
#
# Makerules for SDF definitions
#

.sdf.def : 
	$(SDF_FRONT)/bin/pack-sdf $(SDFINCLUDES) -i $< -o $@ --dep $*.dep

.def.tbl :
	$(PGEN)/bin/sdf2table $(PGEN_FLAGS) -i $< -o $@ 

###########################################################
#
# Makerules for GPP
#

%.pp.af : %.pp
	$(GPP)/bin/parse-pp-table -i $< -o $@

## Generation of pretty-print tables.
## pptable-diff is used to check consistency of an existing
## pretty-print table and a newly created one. The build is
## terminated when inconsistencies are detected.
.def.pp :
	@if [ ! -f ${srcdir}/$@ ]; then \
           $(GPP)/bin/ppgen -i $< -o $@ ;\
        else \
           $(GPP)/bin/parse-pp-table -i ${srcdir}/$@ -o $@.af ;\
           $(GPP)/bin/ppgen -t -i $< \
           | $(GPP)/bin/pptable-diff --old $@.af 2>&1 || \
                     eval "echo '## pptable-diff error: $@ is not consistent with syntax definition' >&2; exit 1";\
           touch $@ ;\
           $(RM) $@.af ;\
        fi

###########################################################
#
# Makerules for Stratego Regular
#

.def.rtg :
	$(STRATEGO_REGULAR)/bin/sdf2rtg $(SDF2RTG_FLAGS) -i $< -o $@

.rtg.str :
	$(STRATEGO_REGULAR)/bin/rtg2sig --module `basename "$*"` -i $< -o $@

.rtg.rtg-nf :
	$(STRATEGO_REGULAR)/bin/parse-rtg -i $< | \
	$(STRATEGO_REGULAR)/libexec/rtg-reduce | \
	$(STRATEGO_REGULAR)/libexec/rtg-group -o $@

.rtg.dfta :
	$(STRATEGO_REGULAR)/bin/rtg2dfta -i $< -o $@

###########################################################
#
# Makerules for XTC
#

XTCFLAGS = -I $(XTC)/share/xtc 

relname:
	echo -n $(distdir) > relname

DREPOSITORY = $(DESTDIR)$(REPOSITORY)

if XT_XTC_REGISTER

install-data-local::
	$(mkinstalldirs) `dirname $(DREPOSITORY)`
	@for file in fordummy $(XTC_IMPORT) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) import $$file ; \
	  echo $(XTC_PROG) -r $(DREPOSITORY) import $$file ; \
	done
	@for file in fordummy $(pkgdata_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(pkgdatadir) -V $(VERSION) -t `basename $$file` ; \
	done
	@for file in fordummy $(nobase_pkgdata_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(pkgdatadir) -V $(VERSION) -t $$file ; \
	done
	@for file in fordummy $(sdfdata_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(sdfdatadir) -V $(VERSION) -t `basename $$file` ; \
	done
	@for file in fordummy $(nobase_sdfdata_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(sdfdatadir) -V $(VERSION) -t $$file ; \
	done
	@for file in fordummy $(data_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(datadir) -V $(VERSION) -t `basename $$file` ; \
	done
	@for file in fordummy $(nobase_data_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(datadir) -V $(VERSION) -t $$file ; \
	done
	@echo "Data registered in repository $(DREPOSITORY): $(data_DATA) $(nobase_data_DATA) $(pkgdata_DATA) $(nobase_pkgdata_DATA) $(sdfdata_DATA) $(nobase_sdfdata_DATA)"

install-exec-local::
	$(mkinstalldirs) `dirname $(DREPOSITORY)`
	@for file in fordummy $(bin_PROGRAMS) $(bin_SCRIPTS) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(bindir) -V $(VERSION) -t `basename $$file $(EXEEXT)` ; \
	done
	@for file in fordummy $(libexec_PROGRAMS) $(libexec_SCRIPTS) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(DREPOSITORY) register -l $(libexecdir) -V $(VERSION) -t `basename $$file $(EXEEXT)` ; \
	done
	@echo "Tools registered in repository $(DREPOSITORY): $(bin_PROGRAMS) $(bin_SCRIPTS) $(libexec_PROGRAMS) $(libexec_SCRIPTS)"

all-local::
	@for file in fordummy $(XTC_IMPORT) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  $(XTC_PROG) -r $(BUILD_REPOSITORY) import $$file ; \
	  echo $(XTC_PROG) -r $(BUILD_REPOSITORY) import $$file ; \
	done
	@for file in fordummy $(data_DATA) $(pkgdata_DATA) $(sdfdata_DATA) $(bin_SCRIPTS) $(libexec_SCRIPTS); do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  if test -f "$$file"; then d="`pwd`"; else d="@abs_srcdir@"; fi; \
	  $(XTC_PROG) -r $(BUILD_REPOSITORY) register -l "$$d" -V $(VERSION) -t `basename $$file` ; \
	done
	@for file in fordummy $(nobase_data_DATA) $(nobase_pkgdata_DATA) $(nobase_sdfdata_DATA) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  if test -f "$$file"; then d="`pwd`"; else d="@abs_srcdir@"; fi; \
	  $(XTC_PROG) -r $(BUILD_REPOSITORY) register -l "$$d" -V $(VERSION) -t $$file ; \
	done
	@for file in fordummy $(bin_PROGRAMS) $(libexec_PROGRAMS) ; do \
	  if [ "$$file" = "fordummy" ]; then continue; fi; \
	  if test -f "$$file"; then d="`pwd`"; else d="@abs_srcdir@"; fi; \
	  $(XTC_PROG) -r $(BUILD_REPOSITORY) register -l "$$d" -V $(VERSION) -t `basename $$file $(EXEEXT)` ; \
	done
	@echo "Tools registered in repository $(BUILD_REPOSITORY): $(bin_PROGRAMS) $(bin_SCRIPTS) $(libexec_PROGRAMS) $(libexec_SCRIPTS)"
	@echo "Data registered in repository $(BUILD_REPOSITORY): $(data_DATA) $(nobase_data_DATA) $(pkgdata_DATA) $(nobase_pkgdata_DATA) $(sdfdata_DATA) $(nobase_sdfdata_DATA)"

endif

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