#########################################################################
##                                                                     ##
##                         Applied Type System                         ##
##                                                                     ##
##                              Hongwei Xi                             ##
##                                                                     ##
#########################################################################

##
## ATS/Anairiats - Unleashing the Potential of Types!
##
## Copyright (C) 2002-2008 Hongwei Xi.
##
## ATS is  free software;  you can redistribute it and/or modify it under
## the  terms of the  GNU General Public License as published by the Free
## Software Foundation; either version 2.1, or (at your option) any later
## version.
##
## ATS is distributed in the hope that it will be useful, but WITHOUT ANY
## WARRANTY; without  even  the  implied  warranty  of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
## for more details.
##
## You  should  have  received  a  copy of the GNU General Public License
## along  with  ATS;  see  the  file  COPYING.  If not, write to the Free
## Software Foundation, 51  Franklin  Street,  Fifth  Floor,  Boston,  MA
## 02110-1301, USA.
##

######

##
## Author: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
## Author: Likai Liu (liulk AT cs DOT bu DOT edu)
## Author: Yuri D'Elia (wavexx AT thregr DOT org)
##

.SUFFIXES:
all:: Makefile

######

# integration with autoconf.

SRC_CONFIG_FILES := config.mk.in config.h.in
BUILT_CONFIG_FILES := $(SRC_CONFIG_FILES:%.in=%)

Makefile: config.mk

config.status: configure
	./configure
	touch $@

$(BUILT_CONFIG_FILES): %: config.status $(filter-out %,$(SRC_CONFIG_FILES))
	./config.status
	touch $(BUILT_CONFIG_FILES)

config.h.in: configure.ac
	autoheader $<
	touch $@

configure: configure.ac $(SRC_CONFIG_FILES)
	aclocal
	automake --add-missing --foreign || true
	autoconf
	touch $(BUILT_CONFIG_FILES)

-include config.mk

######

DESTDIR :=

export ATSHOME
export ATSHOMERELOC
export ATSHOMEQ

######

install:: config.h
	# recursively install all files in the list except .svn control files.
	for d in ccomp/runtime contrib doc libats libc prelude; do \
	  cd $(abs_top_srcdir) && \
	  $(INSTALL) -d $(DESTDIR)$(ATSNEWHOME)/"$$d" && \
	  find "$$d" -name .svn -prune -o -type f \
            -exec $(INSTALL) -m 644 -D \{} $(DESTDIR)$(ATSNEWHOME)/\{} \; \
	    -print; \
	done

	# install specific files in the list as regular files.
	for f in \
	    COPYING INSTALL *.txt ccomp/lib/*.a ccomp/lib64/*.a config.h; \
	do \
	  [ -f "$$f" ] || continue; \
	  cd $(abs_top_srcdir) && \
	  $(INSTALL) -m 644 -D "$$f" $(DESTDIR)$(ATSNEWHOME)/"$$f" && \
	  echo "$$f"; \
	done

	# install specific files in the list as executables.
	for f in bin/*; do \
	  cd $(abs_top_srcdir) && \
	  $(INSTALL) -m 755 -D "$$f" $(DESTDIR)$(ATSNEWHOME)/"$$f" && \
	  echo "$$f"; \
	done

	# install multiple copies of wrapper script, for each binary.
	for f in bin/*; do \
	  b=`basename "$$f"`; \
	  cd $(abs_top_srcdir) && \
	  $(INSTALL) -m 755 -D ats_env.sh $(DESTDIR)$(bindir)/"$$b" && \
	  echo install ats_env.sh to $(bindir)/"$$b"; \
	done

######

all:: \
  atsopt1 \
  bin/atscc \
  bin/atslib \
  libfiles libfiles_mt \
  bin/atspack \
  bin/atslex \
  ccomp/runtime/GCATS/gc.o \
  ccomp/runtime/GCATS/gc_mt.o \
  atsopt1_gc \
  contrib
	@echo "ATS/Anairiats has been built up successfully!"
	@echo "The value of ATSHOME for this build is \"$(ATSHOME)\"."
	@echo "The value of ATSHOMERELOC for this build is \"$(ATSHOMERELOC)\"."

###### w/o GC ######

atsopt1::
	$(MAKE) -C bootstrap1 -f ../Makefile_bootstrap BOOTSTRAP1=1 atsopt
	cp bootstrap1/atsopt $(ATSHOMEQ)/bin/atsopt

###### with GC ######

atsopt1_gc::
	$(MAKE) -C bootstrap1 -f ../Makefile_bootstrap BOOTSTRAP1=1 atsopt_gc
	cp bootstrap1/atsopt_gc $(ATSHOMEQ)/bin/atsopt

###### contrib libraries ######

contrib::
ifeq ($(HAVE_LIBGLIB20),1)
	cd contrib/glib; make atsctrb_glib.o; make clean
endif
ifeq ($(HAVE_LIBGTK20),1)
	cd contrib/cairo; make atsctrb_cairo.o; make clean
	cd contrib/pango; make atsctrb_pango.o; make clean
	cd contrib/GTK; make atsctrb_GTK.o; make clean
endif
ifeq ($(HAVE_LIBSDL10),1)
	cd contrib/SDL; make atsctrb_SDL.o; make clean
endif

###### some toplevel commands ######

bin/atscc bin/atslib:
	cd utils/scripts; $(MAKE) atscc; cp atscc $(ATSHOMEQ)/bin
	cd utils/scripts; $(MAKE) atslib; cp atslib $(ATSHOMEQ)/bin
	cd utils/scripts; $(MAKE) clean

bin/atspack:
	cd utils/scripts; $(MAKE) atspack; cp atspack $(ATSHOMEQ)/bin

###### library ######

ATS_PROOFCHECK=
#
# ATS_PROOFCHECK=-D_ATS_PROOFCHECK # it should be turned on from time to time
#
# [CC -E] for preprocessing
#

ATSLIB=$(ATSHOMEQ)/bin/atslib

.libfiles_local: .libfiles ; $(CC) -E -P -x c -o $@ $<
libfiles: .libfiles_local
	$(ATSLIB) $(ATS_PROOFCHECK) -D_XOPEN_SOURCE -O2 --libats
	$(ATSLIB) $(ATS_PROOFCHECK) -O2 --libats_lex
	$(ATSLIB) $(ATS_PROOFCHECK) -O2 --libats_smlbas

lib32files: .libfiles_local
	$(ATSLIB) $(ATS_PROOFCHECK) -m32 -D_XOPEN_SOURCE -O2 --libats
	$(ATSLIB) $(ATS_PROOFCHECK) -m32 -O2 --libats_lex
	$(ATSLIB) $(ATS_PROOFCHECK) -m32 -O2 --libats_smlbas

lib64files: .libfiles_local
	$(ATSLIB) $(ATS_PROOFCHECK) -m64 -D_XOPEN_SOURCE -O2 --libats
	$(ATSLIB) $(ATS_PROOFCHECK) -m64 -O2 --libats_lex
	$(ATSLIB) $(ATS_PROOFCHECK) -m64 -O2 --libats_smlbas

.libfiles_mt_local: .libfiles_mt ; $(CC) -E -P -x c -o $@ $<
libfiles_mt: .libfiles_mt_local
	$(ATSLIB) $(ATS_PROOFCHECK) -D_ATS_MULTITHREAD -O2 --libats_mt

###### a lexer for ATS ######

bin/atslex:
	cd utils/atslex; $(MAKE) atslex; cp atslex $(ATSHOMEQ)/bin
	cd utils/atslex; $(MAKE) clean

###### GC runtime ######

ccomp/runtime/GCATS/gc.o:
	cd ccomp/runtime/GCATS; $(MAKE) gc.o; $(MAKE) clean

ccomp/runtime/GCATS/gc_mt.o:
	cd ccomp/runtime/GCATS; $(MAKE) gc_mt.o; $(MAKE) clean

######

clean::
	rm -f bootstrap1/*.o
	cd utils/scripts; $(MAKE) clean
	cd utils/atslex; $(MAKE) clean
	cd ccomp/runtime/GCATS; $(MAKE) clean

cleanall:: clean
	rm -f $(BUILT_CONFIG_FILES)
	rm -f .libfiles_local
	rm -f .libfiles_mt_local
	rm -f bin/atsopt bin/atscc bin/atslib bin/atslex bin/atspack
	rm -f ccomp/lib/libats.a
	rm -f ccomp/lib/libats_mt.a
	rm -f ccomp/lib/libats_lex.a
	rm -f ccomp/lib/libats_smlbas.a
	rm -f ccomp/lib/output/*
	rm -f ccomp/lib64/libats.a
	rm -f ccomp/lib64/libats_mt.a
	rm -f ccomp/lib64/libats_lex.a
	rm -f ccomp/lib64/libats_smlbas.a
	rm -f ccomp/lib64/output/*
	cd ccomp/runtime/GCATS; $(MAKE) cleanall
	rm -f contrib/glib/atsctrb_glib.o
	rm -f contrib/cairo/atsctrb_cairo.o
	rm -f contrib/pango/atsctrb_pango.o
	rm -f contrib/X11/atsctrb_X11.o
	rm -f contrib/GTK/atsctrb_GTK.o
	rm -f contrib/GL/atsctrb_GL.o
	rm -f contrib/SDL/atsctrb_SDL.o
	find . -name .svn -prune -o -name \*~ -exec rm \{} \;

######
#
# end of [Makefile_dist]
#
######
