
include ${TRICK_HOME}/share/trick/makefiles/Makefile.common

ifdef TRICK_VERBOSE_BUILD
PRINT_CP =
PRINT_ICG =
PRINT_CONVERT_SWIG =
PRINT_MAKEFILE_SRC =
PRINT_MAKEFILE_SWIG =
ECHO_CMD =
else
PRINT_CP = @echo "[34mRunning configuration_processor[0m"
PRINT_ICG = @echo "[34mRunning ICG[0m"
PRINT_CONVERT_SWIG = @echo "[34mRunning convert_swig[0m"
PRINT_MAKEFILE_SRC = @echo "[34mCreating/updating source Makefile[0m"
PRINT_MAKEFILE_SWIG = @echo "[34mCreating/updating swig Makefile[0m"
ECHO_CMD = @
endif

.NOTPARALLEL: all test

export TRICK_ICG_EXCLUDE

# Use /bin/bash as the shell so we can use PIPESTATUS
SHELL = /bin/bash

all test : ${TRICK_LIB_DIR}/libtrick.a S_source.hh \
 $(CURDIR)/build/Makefile_io_src \
 $(CURDIR)/build/Makefile_src \
 $(CURDIR)/build/Makefile_swig \
 $(CURDIR)/build/convert_swig_last_run \
 $(CURDIR)/build/MAKE_out
	@$(MAKE) --no-print-directory -f build/Makefile_src $@ 2>&1 | tee -a build/MAKE_out ; exit $${PIPESTATUS[0]}

build:
	mkdir $@

debug : all
debug : TRICK_CPFLAGS += --debug

${TRICK_LIB_DIR}/libtrick.a:
	@echo "Cannot find $@.  Please build Trick for this platfrom"
	@exit -1

$(CURDIR)/build/MAKE_out : | build
	@/bin/cp ${TRICK_HOME}/share/trick/MAKE_out_header.txt $@

# CP creates S_source.hh required for ICG and SWIG processing
S_source.hh : S_define | build
	$(PRINT_CP)
	$(ECHO_CMD)${TRICK_HOME}/$(LIBEXEC)/trick/configuration_processor $(TRICK_CPFLAGS)

# Automatic and manual ICG rules
$(CURDIR)/build/Makefile_io_src : | S_source.hh
	$(PRINT_ICG)
	$(ECHO_CMD)${TRICK_HOME}/bin/trick-ICG -m ${TRICK_CXXFLAGS} ${TRICK_SYSTEM_CXXFLAGS} S_source.hh

ICG:
	$(PRINT_ICG)
	$(ECHO_CMD)${TRICK_HOME}/bin/trick-ICG -m ${TRICK_CXXFLAGS} ${TRICK_SYSTEM_CXXFLAGS} S_source.hh

force_ICG:
	$(PRINT_ICG)
	$(ECHO_CMD)${TRICK_HOME}/bin/trick-ICG -f -m ${TRICK_CXXFLAGS} ${TRICK_SYSTEM_CXXFLAGS} S_source.hh

# Create makefile for source code
$(CURDIR)/build/Makefile_src: $(CURDIR)/build/Makefile_io_src
	$(PRINT_MAKEFILE_SRC)
	$(ECHO_CMD)${TRICK_HOME}/$(LIBEXEC)/trick/make_makefile_src $?

# Create makefile for SWIG code
$(CURDIR)/build/Makefile_swig : | $(CURDIR)/build/Makefile_io_src
	$(PRINT_MAKEFILE_SWIG)
	$(ECHO_CMD)${TRICK_HOME}/$(LIBEXEC)/trick/make_makefile_swig $?

# Automatic and manual convert_swig rules
$(CURDIR)/build/convert_swig_last_run : | $(CURDIR)/build/Makefile_swig
	$(PRINT_CONVERT_SWIG)
	$(ECHO_CMD)${TRICK_HOME}/$(LIBEXEC)/trick/convert_swig ${TRICK_CONVERT_SWIG_FLAGS}
	@ touch $@

convert_swig:
	$(PRINT_CONVERT_SWIG)
	$(ECHO_CMD)${TRICK_HOME}/$(LIBEXEC)/trick/convert_swig ${TRICK_CONVERT_SWIG_FLAGS}
	@ touch $(CURDIR)/build/convert_swig_last_run

S_define_exp:
	$(TRICK_CC) -E -C -xc++ ${TRICK_SFLAGS} $(TRICK_SYSTEM_SFLAGS) S_define > $@

# Pass these options
sie:
	@if [ -f build/Makefile_src ] ; then $(MAKE) --no-print-directory -f build/Makefile_src $@ ; else echo "No build/Makefile_src found" ; fi

help:
	@ echo -e "\
Simulation make options:\n\
    make [debug] [TRICK_VERBOSE_BUILD=1] - Makes everything: S_main and S_sie.resource\n\
    make sie                     - Builds the S_sie.resource file.\n\
    make clean                   - Removes all object files in simulation directory\n\
    make spotless                - Performs a clean\n\
    make apocalypse              - Performs a clean"

tidy:
	-rm -f S_source.hh S_sie.resource
	-rm -f S_main* T_main*
	-rm -f build/Makefile_*
	-rm -rf S_default.dat

clean: tidy
	-rm -f DP_Product/DP_rt_frame DP_Product/DP_rt_itimer
	-rm -f DP_Product/DP_rt_jobs DP_Product/DP_rt_timeline DP_Product/DP_mem_stats
	-rm -rf build trick
	@ echo "Removed build directory"

spotless: clean

apocalypse: clean
	@echo "[31mI love the smell of napalm in the morning[0m"


# Dependencies for the above rules generated by configuration_process, ICG, make_makefile and make_makefile_swig
-include build/S_define.deps
-include build/Makefile_ICG
-include build/Makefile_convert_swig
-include build/Makefile_src_deps

