# -----------------------------------------------------------------------------
#  (C) Altran Praxis Limited
# -----------------------------------------------------------------------------
# 
#  The SPARK toolset is free software; you can redistribute it and/or modify it
#  under terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 3, or (at your option) any later
#  version. The SPARK toolset 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 distributed with the SPARK toolset; see file
#  COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
#  the license.
# 
# =============================================================================

################################################################################
# PURPOSE
#-------------------------------------------------------------------------------
# Makefile for the entire Spark Toolset source distribution
# Build the SPARK Toolset on NT, Solaris, Linux or MacOS X
################################################################################

################################################################################
# BUILD CONFIGURATION
################################################################################

################################################################################
# DETERMINE HOST INFORMATION
################################################################################

# Determine which platform this Makefile is being run on.
TARGET:=$(shell uname -s)
GCC_TARGET:=$(shell gcc -dumpmachine)

# Canonicalize the target string.
ifeq ($(findstring CYGWIN_NT,${TARGET}),CYGWIN_NT)
   TARGET:=Windows
endif

################################################################################
# PLATFORM SPECIFIC CONFIGURATION
################################################################################

BUILD_VICTOR:=false

# Windows.
ifeq (${TARGET},Windows)
    EXEEXTN:=.exe
    ifeq (${GCC_TARGET},i686-pc-mingw32)
        BUILD_VICTOR:=true
    endif
endif

# Sun.
ifeq (${TARGET},SunOS)
    EXEEXTN:=
endif

# Linux.
ifeq (${TARGET},Linux)
    EXEEXTN:=
    BUILD_VICTOR:=true
endif

# Darwin (Mac OS X).
ifeq (${TARGET},Darwin)
    EXEEXTN:=
    BUILD_VICTOR:=true
endif

################################################################################
# TARGETS
################################################################################

# Build all of the tools, copy them to bin, and clean up.
all:
	$(MAKE) makeall
	$(MAKE) copyall 
	$(MAKE) cleanall

################################################################################
# BUILDS
################################################################################

makeall:
	$(MAKE) -C sparklalr
	$(MAKE) -C examiner
	$(MAKE) -C simplifier
	$(MAKE) -C wraputility
	$(MAKE) -C checker
	$(MAKE) -C pogs
	$(MAKE) -C sparkclean
	$(MAKE) -C sparkformat
	$(MAKE) -C sparkmake
	$(MAKE) -C sparksimp
	$(MAKE) -C victor
	$(MAKE) -C victor_wrapper

copyall:
	cp ./examiner/spark${EXEEXTN}           ./bin/
	cp ./simplifier/spadesimp${EXEEXTN}     ./bin/
	cp ./simplifier/zombiescope${EXEEXTN}   ./bin/
	cp ./wraputility/wrap_utility${EXEEXTN} ./bin/
	cp ./checker/checker${EXEEXTN}          ./bin/
	cp ./pogs/pogs${EXEEXTN}                ./bin/
	cp ./sparkclean/sparkclean${EXEEXTN}    ./bin/
	cp ./sparkformat/sparkformat${EXEEXTN}  ./bin/
	cp ./sparkmake/sparkmake${EXEEXTN}      ./bin/
	cp ./sparksimp/sparksimp${EXEEXTN}      ./bin/
	cp ./victor_wrapper/victor${EXEEXTN}    ./bin/
ifeq (${BUILD_VICTOR},true)
	cp ./victor/alt-ergo/alt-ergo.opt       ./bin/alt-ergo${EXEEXTN}
	cp ./victor/vct/bin/vct${EXEEXTN}       ./bin/
endif
	cp ./victor/vct/run/prelude.fdl         ./share/spark/
	cp ./victor/vct/run/prelude.rul         ./share/spark/
	cp ./victor/vct/run/divmod.rul          ./share/spark/
ifeq (${TARGET},Windows)
	# Copy manifests for SICStus tools.
	cp ./simplifier/spadesimp${EXEEXTN}.manifest   ./bin/
	cp ./simplifier/zombiescope${EXEEXTN}.manifest ./bin/
	cp ./checker/checker${EXEEXTN}.manifest        ./bin/
	# Copy the required DLLs into bin.
	cp ./dlls/*                                    ./bin/
endif

cleanall:
	$(MAKE) -C sparklalr      clean
	$(MAKE) -C examiner       clean
	$(MAKE) -C simplifier     clean
	$(MAKE) -C wraputility    clean
	$(MAKE) -C checker        clean
	$(MAKE) -C pogs           clean
	$(MAKE) -C sparkclean     clean
	$(MAKE) -C sparkformat    clean
	$(MAKE) -C sparkmake      clean
	$(MAKE) -C sparksimp      clean
	$(MAKE) -C victor         clean
	$(MAKE) -C victor_wrapper clean

reallycleanall:
	rm -f ./bin/*
	rm -f ./share/spark/*
	$(MAKE) -C sparklalr      reallyclean
	$(MAKE) -C examiner       reallyclean
	$(MAKE) -C simplifier     reallyclean
	$(MAKE) -C wraputility    reallyclean
	$(MAKE) -C checker        reallyclean
	$(MAKE) -C pogs           reallyclean
	$(MAKE) -C sparkclean     reallyclean
	$(MAKE) -C sparkformat    reallyclean
	$(MAKE) -C sparkmake      reallyclean
	$(MAKE) -C sparksimp      reallyclean
	$(MAKE) -C victor         clean
	$(MAKE) -C victor_wrapper reallyclean

analyseall:
	-$(MAKE) -C sparklalr      self-analysis
	-$(MAKE) -C examiner       self-analysis
	-$(MAKE) -C simplifier     spxref
	-$(MAKE) -C wraputility    self-analysis
	-$(MAKE) -C checker        spxref
	-$(MAKE) -C pogs           self-analysis
	-$(MAKE) -C sparkclean     self-analysis
	-$(MAKE) -C sparkformat    self-analysis
	-$(MAKE) -C sparkmake      self-analysis
	-$(MAKE) -C victor_wrapper self-analysis

analyseallplain:
	-$(MAKE) -C sparklalr      self-analysis
	-$(MAKE) -C examiner       self-analysis
	-$(MAKE) -C simplifier     spxrefplain
	-$(MAKE) -C wraputility    self-analysis
	-$(MAKE) -C checker        spxrefplain
	-$(MAKE) -C pogs           self-analysis
	-$(MAKE) -C sparkclean     self-analysis
	-$(MAKE) -C sparkformat    self-analysis
	-$(MAKE) -C sparkmake      self-analysis
	-$(MAKE) -C victor_wrapper self-analysis

# Regenerate the reference results.
# (May be used to capture analysis of the current code base).
generaterereference: analyseallplain
	cp ./sparklalr/vcg/sparklalr.rep      ./analyse/referenceanalysis/sparklalr.rep
	cp ./examiner/vcg/mainunits95.rep     ./analyse/referenceanalysis/examiner.rep
	cp ./simplifier/spxref_undefined.txt  ./analyse/referenceanalysis/simplifier_spxref_undefined.txt
	cp ./wraputility/vcg/wrap_utility.rep ./analyse/referenceanalysis/wrap_utility.rep
	cp ./checker/spxref_undefined.txt     ./analyse/referenceanalysis/checker_spxref_undefined.txt
	cp ./pogs/vcg/pogs.rep                ./analyse/referenceanalysis/pogs.rep
	cp ./sparkclean/vcg/sparkclean.rep    ./analyse/referenceanalysis/sparkclean.rep
	cp ./sparkformat/vcg/sparkformat.rep  ./analyse/referenceanalysis/sparkformat.rep
	cp ./sparkmake/vcg/sparkmake.rep      ./analyse/referenceanalysis/sparkmake.rep
	cp ./victor_wrapper/vcg/victor.rep    ./analyse/referenceanalysis/victor.rep

################################################################################
# END-OF-FILE
