# -----------------------------------------------------------------------------
#  (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 Examiner
# For Linux, Windows, Solaris or Mac OS X (aka Darwin).
################################################################################

.SUFFIXES:

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

# Name of the output program.
OUTPUT_NAME:=spark

# Location of root.
ROOT:=..

# Location of SPARKLALR parser generator binary
SPARKLALR:=${ROOT}/sparklalr/sparklalr

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

################################################################################
# DYNAMIC DEPENDENCIES
################################################################################

# EXAMINERSIZE
# ------------
# Large (Build to handle large programs)
# Mega  (Build to handle mega programs)
EXAMINERSIZE:=Large

# Every *.ad? file is required, except those excluded below.
# Note that some of these excluded files may not be present due to a make
# clean before building the source distribution. However, they are still
# referenced here to make their exclusion explicit.

# Do not distribute files that must be prepped.
SPARK_PREPED:=examinerconstants.ads filesystem.adb

# Do not distribute files that are generated by the parser.
SPARK_PARSER:=sp_symbols.ads sp_parser_goto.adb sp_parser_goto.ads sp_relations.adb \
	sp_relations.ads sp_parser_actions.adb sp_parser_actions.ads \
	sp_productions.ads sp_expected_symbols.adb sp_expected_symbols.ads SPARK.PAR

FILES_GRAMMAR:=SPARK.LLA

################################################################################
# PLATFORM INDEPENDENT CONFIGURATION
################################################################################

# Location of customer lib
CUSTOMER_LIB:=${ROOT}/lib/spark/current

# Location of common.
COMMON:=${ROOT}/common

# Options to submit to gnatprep.
# DEBUG: The Examiner raises a fatal error during self-analysis with
#        Source_Reference pragmas in place. Thus, the handy "-r" option
#        is removed until the error is fixed.
PREP_OPTS:=-c

# Files containing platform specific code that is handled by gnatprep
PREP_TARGETS:=${SPARK_PREPED} \
	indexmanager-cache.adb indexmanager-cache.shb \
	sli-xref.adb sli-xref.shb

# Debug info file produced by sparklalr (so they can be cleaned up)
PARSER_DEBUG:=SPARK.DGN SPARK.DSC SPARK.EKO SPARK.SYM SPARK.ACT

# Common compiler options.
COMMON_OPTS:=-g -gnatwae -gnat05 -gnatwl -gnaty3abefhiklnprt -I${CUSTOMER_LIB} -I${COMMON}/versioning -gnatf -k
GNATMAKE_OPTS:=${COMMON_OPTS} -O1
GCC_OPTS:=-fstack-check
BIND_OPTS:=-E

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

# Windows.
ifeq (${TARGET},Windows)
    EXE_EXTN:=.exe
    LINK_ARGS:=-Xlinker --stack=0x10000000,0x100000
    PREP_TARGET:=Intel_WinNT
    ifeq (${GCC_TARGET},i686-pc-mingw32)
        ADDRESS_SIZE:=32
    else
        ADDRESS_SIZE:=64
    endif
endif

# Solaris.
ifeq (${TARGET},SunOS)
    EXE_EXTN:=
    LINK_ARGS:=
    PREP_TARGET:=SPARC_Solaris
    ADDRESS_SIZE:=32
endif

# Linux.
ifeq (${TARGET},Linux)
    EXE_EXTN:=
    LINK_ARGS:=
    PREP_TARGET:=Intel_Linux
    ifeq (${GCC_TARGET},x86_64-pc-linux-gnu)
        ADDRESS_SIZE:=64
    else
        ADDRESS_SIZE:=32
    endif
endif

# Darwin (Mac OS X 10.5 or 10.6, 64-bit).
ifeq (${TARGET},Darwin)
    EXE_EXTN:=
    LINK_ARGS:=
    PREP_TARGET:=Darwin
    ADDRESS_SIZE:=64

    # The full-blown implementation of symbolic tracebacks is not
    # working on GNAT Pro at the moment, so
    # force local compilation of dummy GNAT.Traceback.Symbolic in
    # examiner/g-trasym.adb
    GNATMAKE_OPTS:=-a ${GNATMAKE_OPTS}
endif

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

all: ${OUTPUT_NAME}${EXE_EXTN}

${OUTPUT_NAME}${EXE_EXTN}: prep parser
	gnatmake ${GNATMAKE_OPTS} examiner -o ${OUTPUT_NAME}${EXE_EXTN} -cargs ${GCC_OPTS} -bargs ${BIND_OPTS} -largs ${LINK_ARGS}

examine: prep parser
	-spark -report=mainunits95.rep -plain      @${OUTPUT_NAME}.smf

examinexml: prep parser
	-spark -report=mainunits95.xml -plain -xml @${OUTPUT_NAME}.smf

# Building parser
# ===============

parser: ${SPARK_PARSER}

${SPARK_PARSER}: ${FILES_GRAMMAR}
	# -p = Operate as parser
	# -s = Perform self-pack
	# -m = Accept multi comp
	# -v = Verbose
	# -du = Activate debug feature: dump memory
	${SPARKLALR} SPARK -p -s -m -v -du
	gnatchop -w SPARK.PAR

# Platform specific prepping
# ==========================

prep: ${PREP_TARGETS}

%.ads: %.aps
	gnatprep ${PREP_OPTS} -DExaminerSize=${EXAMINERSIZE} -DTarget=${PREP_TARGET} -DAddress_Size=${ADDRESS_SIZE} $< $@

%.adb: %.apb
	gnatprep ${PREP_OPTS} -DExaminerSize=${EXAMINERSIZE} -DTarget=${PREP_TARGET} -DAddress_Size=${ADDRESS_SIZE} $< $@

indexmanager-cache.adb: indexmanager-cache.SHADOW.adb
	gnatprep ${PREP_OPTS} -r -DSPARK=False $< $@

indexmanager-cache.shb: indexmanager-cache.SHADOW.adb
	gnatprep ${PREP_OPTS} -DSPARK=True $< $@

sli-xref.adb: sli-xref.SHADOW.adb
	gnatprep ${PREP_OPTS} -r -DSPARK=False $< $@

sli-xref.shb: sli-xref.SHADOW.adb
	gnatprep ${PREP_OPTS} -DSPARK=True $< $@

# Cleaning code base
# ==================

clean: residueclean standardclean

reallyclean: clean targetclean parserclean prepclean vcclean

residueclean:
	rm -f ${PARSER_DEBUG}

standardclean:
	rm -f *.o *.ali

targetclean:
	rm -f ${OUTPUT_NAME}${EXE_EXTN}
	rm -f b~*.ad[sb]

parserclean:
	rm -f ${SPARK_PARSER}

prepclean:
	rm -f ${PREP_TARGETS}

vcclean:
	-find vcg -type f -name "*.vcg" -delete
	-find vcg -type f -name "*.fdl" -delete
	-find vcg -type f -name "*.rls" -delete
	-find vcg -type f -name "*.siv" -delete
	-find vcg -type f -name "*.slg" -delete
	-find vcg -type f -name "*.sli" -delete
	-find vcg -type f -name "*.lsb" -delete
	-find vcg -type f -name "*.log" -delete
	-find vcg -type f -name "*.vct" -delete
	-find vcg -type f -name "*.vlg" -delete
	-find vcg -type f -name "*.vsm" -delete
	rm -f vcg/*.rep vcg/mainunits95.xml
	-find vcg -depth -mindepth 1 -type d | grep -v -e '/\.svn' | xargs rmdir --ignore-fail-on-non-empty

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