##############################################################################
# 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:
#   
#   Balasko, Jeno
#   Raduly, Csaba
#
##############################################################################
.PHONY: all dep run clean distclean objects
# convert and compile are NOT phony in the subdirectories!

TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)

TTCN3_MODULES = $(GENERATED_TTCN3_MODULES) $(USER_TTCN3_MODULES)

GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.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


OBJECTS = $(GENERATED_SOURCES:.cc=.o)

OTHER_FILES = Makefile

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 -lncurses -ledit
WIN32_LIBS = -lxml2 -lncurses -ledit


all: $(TARGET) ;

objects: $(OBJECTS) ;

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

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

$(GENERATED_TTCN3_MODULES): convert
	@if [ ! -f $@ ]; then $(RM) convert; $(MAKE) convert; fi

convert: $(XML_SCHEMAS)
	$(TTCN3_DIR)/bin/xsd2ttcn -z $^
	touch $@

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

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

clean:
	-$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
	$(GENERATED_SOURCES) $(GENERATED_TTCN3_MODULES) convert compile *.log

distclean: clean
	-$(RM) *.d *.gc??

dep: $(GENERATED_SOURCES)
	makedepend $(CPPFLAGS) $^

vpath %.xsd $(ABS_SRC)

