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

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

# Name of the output program.
OUTPUT_NAME:=spark

# Location of root.
ROOT:=..

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

# 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

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

include ${COMMON}/Makefile.inc

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

# 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

GCC_OPTS:=-fstack-check

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

# Windows.
ifeq (${TARGET},Windows)
    # The --stack option is Windows specific, and supports setting the
    # 'StackCommitSize' and 'StackReserveSize'.  The interpretation of
    # these values is not treated consistently across different
    # Windows versions. Experience and testing on various Windows
    # versions has led to the current stack size being set at:
    # 0x10000000,0x100000 Do not change this value without considering
    # its full ramifications.
    LINK_ARGS:=-Xlinker --stack=0x10000000,0x100000
endif

# Solaris.
ifeq (${TARGET},SunOS)
    LINK_ARGS:=
endif

# Linux.
ifeq (${TARGET},Linux)
    LINK_ARGS:=
endif

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

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

all: ${OUTPUT_NAME}${EXE_EXTN}

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

self-analysis: prep parser
	-spark -report=mainunits95.rep -plain -dictionary_file=examiner @${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} -DTarget=${PREP_TARGET} -DAddress_Size=${ADDRESS_SIZE} $< $@

%.adb: %.apb
	gnatprep ${PREP_OPTS} -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}

parserclean:
	rm -f ${SPARK_PARSER}

prepclean:
	rm -f ${PREP_TARGETS}

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