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

# WARNING! This Makefile can be used with GNU make only.
# Other versions of make may report syntax errors in it.

#
# Do NOT touch this line...
#
.PHONY: all archive check clean distclean dep objects preprocess

.SUFFIXES: .d

#
# Set these variables...
#

# C preprocessor used for TTCN-3 files:
CPP = cpp

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

# Flags for dependency generation
CXXDEPFLAGS = -MM

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

# Flags for the C++ compiler:
#CXXFLAGS = -Wall

# Flags for the linker:
LDFLAGS +=

ifeq ($(PLATFORM), WIN32)
# Silence linker warnings.
LDFLAGS += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc
endif

# Flags for the TTCN-3 and ASN.1 compiler:
COMPILER_FLAGS += -brx
COMPILER_FLAGS := $(sort $(COMPILER_FLAGS))
# $(sort ) removes duplicates

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

#
# You may change these variables. Add your files if necessary...
#

# TTCN-3 modules of this project:
TTCN3_MODULES := UsefulTtcn3Types.ttcn XSD.ttcn \
www_example_com_POQ.ttcn www_example_com_POU.ttcn \
www_example_com_POQAQ.ttcn www_example_com_POUAQ.ttcn \
converter.ttcn www_example_org_all.ttcn

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

# TTCN-3 modules to preprocess:
TTCN3_PP_MODULES = POtest.ttcnpp

# Files to include in TTCN-3 preprocessed modules:
TTCN3_INCLUDES = $(TOP_SRC)/XML/macros.ttcnin

# ASN.1 modules of this project:
ASN1_MODULES =

# 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) $(TTCN3_PP_MODULES:.ttcnpp=.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)

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

# Other files of the project (Makefile, configuration files, etc.)
# that will be added to the archived source files:
OTHER_FILES = po_qual_default.xml po_qual_explicit.xml po_unqual.xml \
po_qual.xsd po_qual_atr.xsd  po_qual_both.xsd po_unqual.xsd \
config.cfg Makefile

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

#
# Do not modify these unless you know what you are doing...
# Platform specific additional libraries:
#
SOLARIS_LIBS  = -lsocket -lnsl -lxml2 -lresolv -lcurses -ledit
SOLARIS8_LIBS = -lsocket -lnsl -lxml2 -lresolv -lcurses -ledit
LINUX_LIBS = -lxml2 -lpthread -lrt -lncurses -ledit
FREEBSD_LIBS = -lxml2 -liconv -lncurses -ledit
WIN32_LIBS = -lxml2 -liconv -lncurses -ledit

#
# Rules for building the executable...
#

all: $(TARGET) ;

shared_objects: $(SHARED_OBJECTS) ;

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

$(TARGET): $(OBJECTS) $(CORELIB_BINARY)
	$(CXX) $(LDFLAGS) -o $@ $(OBJECTS) \
	-L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \
	-L$(OPENSSL_DIR)/lib -lcrypto \
	-L$(XMLDIR)/lib $($(PLATFORM)_LIBS) \
	|| $(TTCN3_DIR)/bin/titanver $(OBJECTS)

.cc.o .c.o:
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o $@ $<

.cc.d .c.d:
	@echo Creating dependency file for '$<'; set -e; \
	$(CXX) $(CXXDEPFLAGS) $(CPPFLAGS) $(CXXFLAGS) $< \
	| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
	[ -s $@ ] || rm -f $@

%.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

compile:: $(TTCN3_MODULES) $(PREPROCESSED_TTCN3_MODULES) $(ASN1_MODULES)
	$(TTCN3_COMPILER) $(COMPILER_FLAGS) $^ - $?
	touch $@

clean distclean:
	-$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
	$(GENERATED_SOURCES) $(PREPROCESSED_TTCN3_MODULES) compile \
	tags *.log $(DEPFILES) \
	www_example*.ttcn UsefulTtcn3Types.ttcn XSD.ttcn

dep: $(GENERATED_SOURCES) $(USER_SOURCES) $(DEPFILES);

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


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

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

run: $(TARGET) config.cfg
	$(RUN) ./$^


# # # # # # # # # # # # # # #
#
# xsd2ttcn *.xsd renames the various PurchaseOrder types
# to prevent name clashes. To avoid this, compile each xsd separately.
#
UsefulTtcn3Types.ttcn XSD.ttcn www_example_com_POUAQ.ttcn:: po_qual_atr.xsd
	$(TTCN3_DIR)/bin/xsd2ttcn -z $<

UsefulTtcn3Types.ttcn XSD.ttcn www_example_com_POQAQ.ttcn:: po_qual_both.xsd
	$(TTCN3_DIR)/bin/xsd2ttcn -z $<

UsefulTtcn3Types.ttcn XSD.ttcn www_example_com_POU.ttcn:: po_unqual.xsd
	$(TTCN3_DIR)/bin/xsd2ttcn -z $<

UsefulTtcn3Types.ttcn XSD.ttcn www_example_com_POQ.ttcn:: po_qual.xsd
	$(TTCN3_DIR)/bin/xsd2ttcn -z $<

UsefulTtcn3Types.ttcn XSD.ttcn www_example_org_all.ttcn:: globall.xsd
	$(TTCN3_DIR)/bin/xsd2ttcn -z $<

