##############################################################################
# Copyright (c) 2000-2021 Ericsson Telecom AB
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v2.0
# which accompanies this distribution, and is available at
# https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
#
# Contributors:
#   Baji, Laszlo
#   Balasko, Jeno
#   Raduly, Csaba
#   Szabo, Bence Janos
#
##############################################################################
TOPDIR := ../..
include   ../../Makefile.regression

ORIG ?= .

CPP := cpp

# Flags for the C++ preprocessor (and makedepend as well):
#CPPFLAGS +=

# Flags for preprocessing TTCN-3 files:
#CPPFLAGS_TTCN3 +=

# Flags for the C++ compiler:
CXXFLAGS += -g -fmessage-length=0

# Flags for the linker:
LDFLAGS += -g

WIN32_LIBS += -liconv
INTERIX_LIBS += -L/usr/local/lib -liconv
FREEBSD_LIBS += -liconv

# Local flags for Titan (can be overridden from the environment or commandline)
TTCNFLAGS := -b -r -x

# Flags for the TTCN-3 and ASN.1 compiler
# (common flags already set in Makefile.regression)
COMPILER_FLAGS += $(TTCNFLAGS) -a

# $(sort ) also eliminates duplicates
COMPILER_FLAGS := $(sort $(COMPILER_FLAGS))

# Execution mode: (either ttcn3 or ttcn3-parallel)
TTCN3_LIB := ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)

##
##

# TTCN-3 modules of this project:
TTCN3_MODULES = converter.ttcn $(wildcard $(ORIG)/xtd*.ttcn)
# UTF8.ttcn

# TTCN-3 modules to preprocess:
TTCN3_PP_MODULES := $(wildcard $(ORIG)/*.ttcnpp)


# Files to include in TTCN-3 preprocessed modules:
TTCN3_INCLUDES = ../macros.ttcnin

# TTCN-3 source files generated by the C preprocessor:
PREPROCESSED_TTCN3_MODULES := $(notdir $(TTCN3_PP_MODULES:.ttcnpp=.ttcn))


# C++ source & header files generated from the TTCN-3 & ASN.1 modules of
# this project:
GENERATED_SOURCES := $(TTCN3_MODULES:.ttcn=.cc) $(PREPROCESSED_TTCN3_MODULES:.ttcn=.cc) $(ASN1_MODULES:.asn=.cc)
GENERATED_HEADERS := $(GENERATED_SOURCES:.cc=.hh)
ifdef CODE_SPLIT
GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc  _seqof.cc _setof.cc _union.cc))
else ifdef SPLIT_TO_SLICES
POSTFIXES := $(foreach file, $(SPLIT_TO_SLICES), $(addsuffix $(file), _part_))
POSTFIXES := $(foreach file, $(POSTFIXES), $(addprefix $(file), .cc))
GENERATED_SOURCES2 := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), $(POSTFIXES)))
GENERATED_SOURCES += $(GENERATED_SOURCES2)
endif

# C/C++ Source & header files of Test Ports, external functions and
# other modules:
USER_SOURCES := iconver.cc
USER_HEADERS := $(USER_SOURCES:.cc=.hh)

# Object files of this project that are needed for the executable test suite:
OBJECTS := $(GENERATED_SOURCES:.cc=.o) $(USER_SOURCES:.cc=.o)

# The name of the executable test suite:
TARGET := HN15589$(RT2_SUFFIX)$(EXESUFFIX)

#
# Rules for building the executable...
#

all:  $(TARGET)

objects: $(OBJECTS) ;

ifeq ($(findstring dynamic,$(TTCN3_LIB)),)
# not dynamic
CORELIB_BINARY := $(TTCN3_DIR)/lib/lib$(TTCN3_LIB).a
else
CORELIB_BINARY := $(TTCN3_DIR)/lib/lib$(TTCN3_LIB).so
endif

# libttcn3.a appears twice; this should be harmless
$(TARGET): $(OBJECTS) $(CORELIB_BINARY)
	$(CXX) $(LDFLAGS) -o $@ $(OBJECTS) \
	-L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \
	-L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS) \
	|| $(TTCN3_DIR)/bin/titanver $(OBJECTS)

%.o: %.cc
	@echo '(C++)' $<; $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

%.d: %.cc
	@echo '(dep)' $<; $(CXX) -MM -MP $(CPPFLAGS) $(CXXFLAGS) $< >$@

# The preprocessing step
%.ttcn: %.ttcnpp $(TTCN3_INCLUDES)
	$(CPP) -x c -nostdinc $(CPPFLAGS_TTCN3) $< $@

preprocess: $(PREPROCESSED_TTCN3_MODULES) ;

$(GENERATED_SOURCES) $(GENERATED_HEADERS): compile
	@if [ ! -f $@ ]; then $(RM) compile; $(MAKE) compile; fi

check: $(TTCN3_MODULES) $(PREPROCESSED_TTCN3_MODULES) $(ASN1_MODULES)
	$(TTCN3_COMPILER) -s $(COMPILER_FLAGS) $^

compile:: $(firstword $(TTCN3_COMPILER))
	@if [ -f $@ ]; then $(RM) compile; $(MAKE) compile; fi
#	if [ -f $(GENERATED_SOURCES) ]; then rm -f $(GENERATED_SOURCES); $(MAKE) $(GENERATED_SOURCES); fi

compile:: $(TTCN3_MODULES) $(PREPROCESSED_TTCN3_MODULES) $(ASN1_MODULES)
#	"modulepar boolean verbose" makes this unusable with old names
	$(TTCN3_COMPILER) $(COMPILER_FLAGS) $^ - $?
	touch $@

clean celan clena:
	-$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
	$(GENERATED_SOURCES) $(PREPROCESSED_TTCN3_MODULES) compile \
	tags *.log

distclean: clean
	-$(RM) $(DEPFILES)

#
# Add your rules here if necessary...
#

.SUFFIXES: .asn .ttcn .ttcnpp

RUN := $(shell which colortitan 2>/dev/null)
ifeq ($(firstword $(RUN)),no)
# stupid /bin/which on Solaris writes errors to stdout instead of stderr
RUN :=
endif

XMLLINT := $(shell which xmllint 2>/dev/null)
ifeq ($(firstword $(XMLLINT)),no)
# stupid /bin/which on Solaris writes errors to stdout instead of stderr
XMLLINT :=
endif

run: $(TARGET) config.cfg
	$(RUN) ./$^
ifneq ($(XMLLINT),) # xmllint found in path
	perl log2xml.pl $(TARGET:.exe=).log > result.xml
	xmllint --schema flipflop-check.xsd result.xml
endif
	$(RUN) ./$< xtdp.cfg
ifneq ($(XMLLINT),) # xmllint found in path
	perl log2xml.pl $(TARGET:.exe=).log > xresult.xml
	xmllint --schema xsd/XTDP-Message.xsd xresult.xml
endif

debug:
	$(MAKE) run RUN='gdb --args'

vpath %.cc   $(TOP_SRC)/iconv
vpath %.ttcn $(TOP_SRC)/iconv


dep: ;

DEPFILES := $(OBJECTS:.o=.d)

ifeq (,$(findstring n,$(MAKEFLAGS)))
ifeq (,$(filter clean distclean,$(MAKECMDGOALS)))
-include $(DEPFILES)
endif
endif

