##############################################################################
# Copyright (c) 2000-2025 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
#   Baranyi, Botond
#   Beres, Szabolcs
#   Delic, Adam
#   Kovacs, Ferenc
#   Ormandi, Matyas
#   Raduly, Csaba
#   Szabados, Kristof
#   Szabo, Bence Janos
#
##############################################################################
#
# Common settings for all regression tests (formerly passed through
# the environment).
#
# Warning! This makefile is included at the beginning of other makefiles.
# Do not put any targets here (they would override the default target).
#

ifndef TOPDIR
$(error Please define TOPDIR in the including makefile)
endif

# hardcode a path for Jenkinks to workaround XML/XmlWorkflow/bin/prj2mk.pl
ifneq ($(origin TTCN3_DIR), file)
# The directory that the Test Executor is installed in:
TTCN3_DIR := $(shell cd $(TOPDIR)/..; pwd)/Install
export TTCN3_DIR
# The following would be nicer, but abspath requires make 3.81 (rhea has only 3.79.1)
#TTCN3_DIR := $(abspath $(TOPDIR)/../Install)
endif

export PATH := $(TTCN3_DIR)/bin:$(PATH)

# Command for compiling TTCN-3 and ASN.1 modules
TTCN3_COMPILER := $(TTCN3_DIR)/bin/compiler

# The standalone ASN.1 encoder tests need a compiler with no extra command line options.
ASN1_COMPILER := $(TTCN3_COMPILER)

# Many tests ignore COMPILER_FLAGS. Append any common options to the compiler name.
TTCN3_COMPILER += -g -L

# Use the alternative runtime when running the tests
#RT2 := true

# Enable tests for the LTTng UST Logger plug-in
LTTNGUSTLOGGER := no

# Use code splitting when running the tests
# DO NOT enable both CODE_SPLIT and SPLIT_TO_SLICES
#CODE_SPLIT := true
# Use code splitting into slices when running the tests
#SPLIT_TO_SLICES := 30

# Use legacy codec handling when running the tests
#LEGACY_CODEC_HANDLING := yes

ifdef RT2
# add command line option to generate code for alternative runtime
RT2_FLAG := -R
TTCN3_COMPILER += $(RT2_FLAG)
RT2_SUFFIX := -rt2
endif

ifdef DYN
DYNAMIC_SUFFIX = -dynamic
ifeq ($(PLATFORM), WIN32)
  export PATH:=$(PATH):$(TTCN3_DIR)/lib:
else
  export LD_LIBRARY_PATH:=$(LD_LIBRARY_PATH):$(TTCN3_DIR)/lib:
endif 
endif

ifdef CODE_SPLIT
SPLIT_FLAG = -Utype
# No space between -U and type !
TTCN3_COMPILER += $(SPLIT_FLAG)
export SPLIT_FLAG
else ifdef SPLIT_TO_SLICES
SPLIT_FLAG := -U$(SPLIT_TO_SLICES)
TTCN3_COMPILER += $(SPLIT_FLAG)
SPLIT_TO_SLICES := $(shell echo $(SPLIT_TO_SLICES) - 1 | bc)
SPLIT_TO_SLICES := $(shell seq 1 ${SPLIT_TO_SLICES})
export SPLIT_FLAG
endif

# Your platform. Allowed values: SOLARIS, SOLARIS8, LINUX, FREEBSD, WIN32
ifndef PLATFORM
PLATFORM1 := $(shell uname -s)
PLATFORM2 := $(shell uname -r)
PLATFORM3 := $(shell uname -m)

ifeq ($(PLATFORM1), SunOS)
  ifeq ($(PLATFORM2), 5.6)
    PLATFORM := SOLARIS
  else
    PLATFORM := SOLARIS8
  endif # 5.6
endif # SunOS
ifeq ($(PLATFORM1), Linux)
  PLATFORM := LINUX
endif # Linux
ifeq ($(PLATFORM1), FreeBSD)
  PLATFORM := FREEBSD
endif # FreeBSD
ifeq ($(findstring CYGWIN, $(PLATFORM1)), CYGWIN)
  PLATFORM := WIN32
endif # CYGWIN
ifeq ($(PLATFORM1), Interix)
  PLATFORM := INTERIX
endif # Interix
endif # ifndef PLATFORM
ifndef PLATFORM
	PLEASE CHECK THE ABOVE PLATFORM SETTINGS!!!
endif # ifndef PLATFORM

# Flags for the C++ preprocessor (and makedepend):
# This must be recursively expanded (CPPFLAGS=), not simply expanded (CPPFLAGS:=)
CPPFLAGS = -D$(PLATFORM) -I$(TTCN3_DIR)/include
ifdef RT2
CPPFLAGS += -DTITAN_RUNTIME_2
endif

# # # # Old naming and enum hack (uncomment to test) # # # #
# TTCN3_COMPILER += -Nold -E
# CPPFLAGS += -DOLD_NAMES


# The C++ compiler used for building:
CXX := g++
CC   = $(CXX)

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

# Flags for the linker:
LDFLAGS =

ifeq ($(PLATFORM), WIN32)
    EXESUFFIX := .exe
    LDFLAGS += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc
else
    EXESUFFIX :=

    ifdef MINGW
    $(error MingW not supported on $(PLATFORM))
    endif
endif

ifeq ($(PLATFORM3), x86_64)
    CCFLAGS  += -fPIC
    CXXFLAGS += -fPIC
endif

ifeq ($(PLATFORM),INTERIX)
# Pick up the right iconv
CPPFLAGS += -I/usr/local/include
endif

# Path of OpenSSL installation:
OPENSSL_DIR = /mnt/TTCN/Tools/openssl-0.9.8k

LCOV_DIR := /mnt/TTCN/Tools/lcov-1.7/usr

#LCOV=1

ifdef LCOV
  CPPFLAGS += -fprofile-arcs -ftest-coverage -g
  CXXFLAGS += -fprofile-arcs -ftest-coverage -g
  LDFLAGS += -fprofile-arcs -ftest-coverage -g -lgcov
endif


# Utility for finding patterns:
ifeq ($(PLATFORM1), SunOS)
  GREP := /usr/xpg4/bin/grep
else
  GREP := grep
endif # SunOS
export GREP

# Suffix of executables
ifeq ($(PLATFORM), WIN32)
  EXESUFFIX := .exe
else
  EXESUFFIX :=
endif # WIN32

SOLARIS_LIBS = -lxnet -L${XMLDIR}/lib -lxml2 -lresolv -lcurses -ledit
SOLARIS8_LIBS = $(SOLARIS_LIBS) -lresolv -lcurses -ledit
LINUX_LIBS = -L${XMLDIR}/lib -lxml2 -lpthread -lrt -lncurses -ledit
FREEBSD_LIBS = -lncurses -ledit
WIN32_LIBS = -L${XMLDIR}/lib -lxml2 -lncurses -ledit
INTERIX_LIBS = -L${XMLDIR}/lib -lxml2 -liconv -lncurses -ledit


#
# Include local preferences
#
-include $(TOPDIR)/Makefile.personal

ifdef LEGACY_CODEC_HANDLING
TTCN3_COMPILER += -e
endif

ifeq (${XMLDIR}, default)
CPPFLAGS += -I/usr/include/libxml2
else
CPPFLAGS += -I${XMLDIR}/include/libxml2
endif

ifeq ($(DEBUG), yes)
CPPFLAGS += -DMEMORY_DEBUG
CXXFLAGS += -g
endif


# Quiet down a bit
CXXFLAGS += -Wno-deprecated-declarations

# At least ASN1/errorMessages needs ASN1_COMPILER in the environment
export ASN1_COMPILER TTCN3_COMPILER

print-%:
	@echo $* = \"$($*)\" from $(origin $*)

#
# Define SRCDIR for OOBE. It should point to the original regression_test dir.
#
ifdef SRCDIR
TOP_SRC := $(SRCDIR)

# absolute path to "regression_test" in the build dir
ABS_TOP := $(abspath $(TOPDIR))/

# relative path from "regression_test" to current dir
REL_DIR := $(subst $(ABS_TOP),,$(CURDIR))

# absolute path to the equivalent of current dir
ABS_SRC := $(SRCDIR)/$(REL_DIR)/

vpath %.ttcnpp $(ABS_SRC)
vpath %.inc  $(ABS_SRC)
vpath %.ttcn $(ABS_SRC)
vpath %.asn  $(ABS_SRC)
vpath %.ttcnin $(ABS_TOP)/XML

$(foreach src, $(USER_SOURCES), $(eval vpath $(src) $(ABS_SRC)))

CPPFLAGS += -I. -I$(ABS_SRC)

else
TOP_SRC := $(TOPDIR)

ABS_SRC := $(abspath .)
ifeq "${ABS_SRC}" ""
ABS_SRC :=$(shell pwd)
endif

endif

export ABS_SRC

vpath %.cfg $(ABS_SRC)
