#------------------------------->  Makefile  <--------------------------------#
#- Copyright (C) 1994 by International Computer Science Institute            -#
#- This file is part of the GNU Sather package. It is free software; you may -#
#- redistribute  and/or modify it under the terms of the  GNU General Public -#
#- License (GPL)  as  published  by the  Free  Software  Foundation;  either -#
#- version 2 of the license, or (at your option) any later version.          -#
#- This  program  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 Doc/GPL for more details.        -#
#- The license text is also available from:  Free Software Foundation, Inc., -#
#- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     -#
#------------->  Please email comments to <bug-sather@gnu.org>  <-------------#

###############################################################################
# Be sure to read the file Doc/Installation!
# You may need to set some variables in System/Common/CONFIG.proto
# to be appropriate to your system.
# Also make sure to adjust the paths in System/Platforms/X/Platform.module
# and System/Platforms/tcltk/Platform.module
###############################################################################

export SATHER_HOME=$(shell pwd)
unexport SATHER_LIBRARY
unexport SATHER_ENV

###############################################################################
# include configuration information

include $(SATHER_HOME)/config.default

##############################################################################
# construct the needed variables

SACOMP          =bin/sacomp
SACOMP_BOOT     =bin/sacomp-boot
SACOMP_BOOT_CODE=bin/sacomp-boot.code
SACOMP_DEBUG    =bin/sacomp-debug
SACOMP_PROFILE  =bin/sacomp-profile
SABROWSE        =bin/sabrowse

CS_OPT        =-verbose -O_fast -O_no_move_while -O_no_hoist_const -O_no_cse -only_reachable -pretty
CS_OPT_DEBUG  =-verbose -chk -debug -debug_deterministic -debug_no_backtrace -debug_source -pretty
CS_OPT_PROFILE=-verbose -only_reachable -optimize -C_flag -pg

################################ DEPENDENCIES #################################

# The compiler which ends up in bin/ is the "fast" executable,
# which means that checking is turned off and optimizations are on.

# Some makes can't expand glob syntax with more than one *, so
# only specify one directory to expand instead of listing all of them.
# Watch out for this if you are modifying the compiler.

# This runs the C compiles after the Sather compiler instead of at
# the same time, to ease installation for those with small VM.

default: $(SACOMP)

$(SACOMP): $(SACOMP_BOOT)
	@echo "**** Creating C for installation compiler *****"
#	$(SACOMP_BOOT) $(CS_OPT) sacomp/sacomp.module -o $(SACOMP) -only_C
# Ugly, but the clean solution in sacomp means some work...:
	cd bin ; ../$(SACOMP_BOOT) $(CS_OPT) ../sacomp/sacomp.module -o ../$(SACOMP) -only_C
	@echo "***** Compiling C for installation compiler *****"
	$(MAKE) -C $(SACOMP).code
# CFLAGS="-I. -g"

boot: $(SACOMP_BOOT)

optional: browser tcltk f77

browser: $(SACOMP_BOOT)
	@echo "***** Creating C for Browser *****"
	$(SACOMP_BOOT) $(CS_OPT) sabrowse/sabrowse.module -o $(SABROWSE) -only_C
	@echo "***** Compiling C for Browser *****"
	$(MAKE) -C $(SABROWSE).code

tcltk: $(SACOMP)
	$(MAKE) -C platforms/X
	$(MAKE) -C platforms/tcltk
	$(MAKE) -C platforms/gui
	$(MAKE) -C platforms/dualgui

f77: $(SACOMP)
	$(MAKE) -C platforms/f77

debug: $(SACOMP_BOOT)
	cd bin ; ../$(SACOMP_BOOT) $(CS_OPT_DEBUG) ../sacomp/sacomp.module -o ../$(SACOMP_DEBUG) -only_C
	$(MAKE) -C $(SACOMP_DEBUG).code

profile: $(SACOMP_BOOT)
	cd bin ; ../$(SACOMP) $(CS_OPT_PROFILE) ../sacomp/sacomp.module -o ../$(SACOMP_PROFILE) -only_C
	$(MAKE) -C $(SACOMP_PROFILE).code

$(SACOMP_BOOT): system
	@echo; echo "Making boot compiler..." ; echo
	ln -sf ../$(BOOT_PLATFORM)/header.h platforms/BOOT/header.h
	$(MAKE) -C $(SACOMP_BOOT_CODE) $(MAKE_OPT_BOOT)

# The rule for System/Common/CONFIG also makes the serial libraries,
# as well as making any platforms.

system: platforms resources
	$(MAKE) -C system

# The following is only meant for real platforms. Special platforms (gui,
# tcltk, etc.) are made by the "optional" target
platforms:
	for i in `echo "BOOT",$(PLATFORMS) | tr , \ `; do		\
	  echo "Making platform $$i..." ;				\
	  $(MAKE) -C platforms/$$i ;					\
	done

resources:
	$(LN) $(LIBCHARS_VERSION) $(LIBCHARS)

info:
	cd emacs && makeinfo sather.texinfo
	cd emacs && makeinfo sather-tutorial.texinfo
	cd emacs && makeinfo sather-mode.texinfo

.PHONY: full boot optional tcltk f77 debug profile system platforms info \
test testfull clean-unneeded clean

################################### TESTING ###################################

# There are two tests.  The first is the test classes in the library,
# which is pretty straightforward.  The second is a full
# double compilation of the compiler, which can take quite a while.

test:
	$(MAKE) -C test test

# The full test does the following:
#
#    1. Compile using Bin/sacomp, with full checking on.
#    2. Compile using result.

testfull:
	$(MAKE) -C test testfull


################################### CLEANING ##################################

clean:
	$(MAKE) -C system clean
	$(MAKE) -C test clean

	for i in platforms/* ; do					\
	  if [ -f $$i/Makefile ] ; then $(MAKE) -C $$i clean ; fi	\
	done;

	$(RM) -r $(SACOMP).code $(SABROWSE).code
	$(RM) -r $(SACOMP_PROFILE).code $(SACOMP_DEBUG).code
	$(RM) $(SACOMP_BOOT).code/*.o
	$(RM) $(SACOMP) $(SACOMP_BOOT) $(SABROWSE)
	$(RM) $(SACOMP_PROFILE) $(SACOMP_DEBUG)

	$(RM) config.platform

	$(RM) emacs/*.elc
	$(RM) emacs/*.info emacs/*.info??

	debian/rules clean-safe
