#########################################################################
##                                                                     ##
##                         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.
##

######

GCC=gcc
RANLIB=ranlib

PWD=$(shell pwd)
ifdef ATSHOME
  ATSHOMEDEF=1
else
  ATSHOMEDEF=0
  export ATSHOME=$(PWD)
endif

######

all: \
  atscheck \
  config.h \
  atsopt1 \
  bin/atscc \
  bin/atslib \
  libfiles libfiles_mt \
  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)\"."

###### system configuration ######

atscheck::
	echo "$(ATSHOME)" > .ATSHOME
ifeq ($(ATSHOMEDEF),1)
	/bin/bash -r ./ATSHOME_check.sh
endif
ifdef ATSHOMERELOC
	echo "$(ATSHOMERELOC)" > .ATSHOMERELOC
endif

config.h: configure ; ./configure

###### bootstrap/Makefile ######

bootstrap1/Makefile:
	$(GCC) -D_BOOTSTRAP1 -E -x c .bootstrap_header \
      | cat - .bootstrap_makefile > bootstrap1/Makefile

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

atsopt1:: bootstrap1/Makefile; cd bootstrap1; make atsopt; mv atsopt "$(ATSHOME)"/bin

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

atsopt1_gc:: bootstrap1/Makefile; cd bootstrap1; make atsopt_gc; mv atsopt "$(ATSHOME)"/bin

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

contrib::
ifeq ($(LIBGLIB20),1)
	cd contrib/glib; make atsctrb_glib.o; make clean
endif
ifeq ($(LIBGTK20),1)
	cd contrib/GTK; make atsctrb_GTK.o; make clean
endif

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

bin/atscc bin/atslib:
	cd utils/scripts; make atscc; mv atscc "$(ATSHOME)"/bin
	cd utils/scripts; make atslib; mv atslib "$(ATSHOME)"/bin
	cd utils/scripts; make clean

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

# [gcc -E] for preprocessing
.libfiles_local: ; $(GCC) -E -P -x c .libfiles -o .libfiles_local
libfiles:: .libfiles_local
	"$(ATSHOME)"/bin/atslib $(ATS_TERMINATION_CHECK) -O2 --libats
	"$(ATSHOME)"/bin/atslib $(ATS_TERMINATION_CHECK) -O2 --libats_smlbas

.libfiles_mt_local: ; $(GCC) -E -P -x c .libfiles_mt -o .libfiles_mt_local
libfiles_mt:: .libfiles_mt_local
	"$(ATSHOME)"/bin/atslib $(ATS_TERMINATION_CHECK) -D_ATS_MULTITHREAD -O2 --libats_mt

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

bin/atslex:
	cd utils/atslex; make atslex; mv atslex "$(ATSHOME)"/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 bootstrap0/*.o
	rm -f bootstrap1/*.o
	cd utils/scripts; make clean
	cd utils/atslex; make clean
	cd ccomp/runtime/GCATS; make clean

cleanall:: clean
	rm -f config.h
	rm -f .libfiles_local
	rm -f .libfiles_mt_local
	rm -f bin/atsopt bin/atscc bin/atslib bin/atslex
	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/*
	rm -f .*~ *~ */*~ */*/*~ */*/*/*~ */*/*/*/*~
	rm -f bootstrap1/Makefile bootstrap1/*.o
	cd utils/scripts; make clean
	cd utils/atslex; make clean
	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

######
#
# end of [Makefile_dist_main]
#
######
