###############################################################################
# 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
#   Baranyi, Botond
#   Beres, Szabolcs
#   Delic, Adam
#   Feher, Csaba
#   Forstner, Matyas
#   Kovacs, Ferenc
#   Kremer, Peter
#   Lovassy, Arpad
#   Ormandi, Matyas
#   Pandi, Krisztian
#   Raduly, Csaba
#   Szabados, Kristof
#   Szabo, Janos Zoltan – initial implementation
#   Szalai, Endre
#   Zalanyi, Balazs Andor
#
###############################################################################
ifndef TOP
$(error please define TOP)
endif

# Configurations for the Free Open Source Software version, if available
ifneq ($(wildcard $(TOP)/MakefileFOSS.cfg), )
  include $(TOP)/MakefileFOSS.cfg
else
  # Set whether licensing is enabled: (yes or no)
  LICENSING := yes

  # Set whether to send usage statistics: (yes or no)
  USAGE_STATS := yes
endif

# Define SRCDIR to the root of Titan sources to enable out-of-tree build
#SRCDIR := ${HOME}/workspace/TTCNv3

# Set these variables to fit your system:

# Set it to 'yes' to display git commit id beside the version
GIT_COMMIT_ID := yes

# The target directory where the Test Executor will be installed to:
TTCN3_DIR := ${TOP}/Install

# New installed files are run from make install, so they are added to the PATH
PATH := ${PATH}:${TTCN3_DIR}/bin

# Set it to 'yes' for developing or 'no' for release.
DEBUG := no

# Set it to 'yes' to display debug information for parsers (very spammy)
PARSER_DEBUG := no

# Set it to 'yes' to generate coverage data (requires DEBUG=yes)
COVERAGE := no

# Set it to 'yes' to enable extra features for the debugger UI in single mode
# (this requires an extra library when linking the generated code).
# Set it to 'no' to use a simplified debugger UI.
ADVANCED_DEBUGGER_UI := no

# Should be set to 'yes' if the version of the 'editline' library is 0:53:0 or older
# (i.e. the first line of 'editline/readline.h' contains 'v1.34' or lower).
OLD_LIBEDIT := no

# Your platform. Allowed values: SOLARIS, SOLARIS8, LINUX, FREEBSD,
# WIN32. Decided automagically if not defined (recommended).
# PLATFORM :=

# Uncomment the following for "native" Win32
#MINGW := -DMINGW -mno-cygwin

# Utility for building lexical analyzers (tokenizers):
FLEX := flex

# Utility for building parsers:
BISON := bison

# The C compiler used for building:
CC := gcc

# The C++ compiler used for building:
CXX := g++

# generate userguide at install ('make release' always generates it)
GEN_PDF := no

# Set whether to build the CLI for MC: (yes or no)
CLI := yes

# set whether to build the JNI native library: (yes or no)
JNI := yes

# Build the LTTng-UST logger plugin
LTTNGUSTLOGGER := no

# Path of the Java Developement Kit installation
# (must be at least version  1.5.0_10)
ifndef JDKDIR
JDKDIR := /usr/lib/jvm/default-java
endif

# Path of OpenSSL installation: (always required)
OPENSSL_DIR := default

# Location of libxml2
XMLDIR := default

# Flags for the C(++) preprocessor:
# Prevent a warning about yyinput being defined but not used
CPPFLAGS += -DYY_NO_INPUT

# Flags shared between C and C++
COMPILERFLAGS := -Wall

# 'Hardcore' settings
# Unlikely to work for GCC below 4.x
#COMPILERFLAGS += -Werror -pedantic -W -Wno-unused-parameter -O1
#COMPILERFLAGS += -Wpedantic -Wextra -Wconversion -Wfloat-conversion -Wsign-conversion -Wint-conversion
#COMPILERFLAGS += -Wfloat-equal -Wcast-qual -Wparentheses -Wlogical-op

# Flags for the C compiler.
# std=gnu9x shuts up warnings about long long and variadic macros
CCFLAGS += $(COMPILERFLAGS) -std=gnu9x

# Flags for the C++ compiler:
CXXFLAGS += $(COMPILERFLAGS) -Wno-long-long

# The command for maintaining static libraries:
AR := ar

# The command for building the shared libraries:
# It shall be set to either "$(CXX) -G" or "$(CXX) -shared" depending
# on the linker you use.
LD = $(CXX) -shared

# Flags for linking binary executables (e.g. for profiling):
LDFLAGS += $(MINGW)

# The command for removing symbol table from the executables:
STRIP := strip

# Flags for flex:
FLEXFLAGS := -B -s -Cr

# Flags for bison:
BISONFLAGS := -d

# Override common settings with personal preferences if needed
-include $(TOP)/Makefile.personal

## ## ## ## ## Variables below are automatically set ## ## ## ## ##

ifeq ($(DEBUG), yes)
  CPPFLAGS += -DMEMORY_DEBUG -DFATAL_DEBUG
else
  CPPFLAGS += -DNDEBUG
endif

ifeq ($(PARSER_DEBUG), yes)
  CPPFLAGS += -DPARSER_DEBUG
endif

# MingW flags need to be passed to the preprocessor during ctags configure
CPPFLAGS += $(MINGW)

ifeq ($(DEBUG), yes)
  CCFLAGS += -g
  CCFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
    -Wstrict-prototypes
else
ifeq ($(COVERAGE), yes)
  CCFLAGS += -O0
else
  CCFLAGS += -O2
endif
endif

# Only the default Cygwin compiler can be used for Mingw.
# Its version is 3.4.4, too low.
ifndef MINGW
# Only GCC 4.x understands -Wno-variadic-macros, comment out for lower versions
#CXXFLAGS += -Wno-variadic-macros
endif

ifeq ($(DEBUG), yes)
  CXXFLAGS += -g
  CXXFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align
else
ifeq ($(COVERAGE), yes)
  CXXFLAGS += -O0
else
  CXXFLAGS += -O2
endif
endif

# Flags for the C/C++ compilers to generate dependency list (-M or -MM
# for gcc or clang, -xM or -xM1 for Sun C compiler).

ifneq ($(filter gcc% clang%, $(notdir $(CC))),)
CCDEPFLAG := -MM
CXXDEPFLAG := -MM
else
# Let's hope it's the Sun compiler
CCDEPFLAG := -xM1
CXXDEPFLAG := -xM1
endif

ifeq ($(DEBUG), yes)
  LDFLAGS += -g
endif


ifeq ($(DEBUG), yes)
  FLEXFLAGS += -b -p
#  FLEXFLAGS += -d
else
# Comment out the line below if flex version 2.5.4[a] is used.
# This version does not support optimized tables together with yylineno.
  FLEXFLAGS += -Cfe
endif


ifeq ($(DEBUG), yes)
  BISONFLAGS += -t -v
endif

ifeq ($(PARSER_DEBUG), yes)
  BISONFLAGS += --debug --verbose
endif

ifeq ($(COVERAGE), yes)
  CPPFLAGS += -DCOVERAGE_BUILD
  COMPILERFLAGS += -fprofile-arcs -ftest-coverage
  LDFLAGS += -fprofile-arcs -ftest-coverage -lgcov
endif

ifeq ($(ADVANCED_DEBUGGER_UI), yes)
  CPPFLAGS += -DADVANCED_DEBUGGER_UI
endif

ifeq ($(OLD_LIBEDIT), yes)
  CPPFLAGS += -DOLD_LIBEDIT
endif

# Directory which contains the code for POSIX regular expression handling.
# It is needed on platforms where the system's libc does not support POSIX
# regexps. This is the case for Mingw.
ifdef MINGW
# If your platform isn't Mingw but lacks POSIX regular expressions,
# comment out the ifdef/endif
REGEX_DIR := /mnt/TTCN/Tools/CPP/regex-glibc-2.5.1
endif

# Warning! Never set the above directories to /usr.
# If your OpenSSL, Qt is installed under /usr you'd better set
# these variables to a fake (non-existent) directory. If this is the case,
# gcc will find the required headers and libs anyway, but -I/usr/include
# might confuse it.

# This is especially true on Windows with Mingw installed as a Cygwin package.
# -mno-cygwin alters the include serach path to find the Mingw headers.
# Passing -I/usr/include would cause the Mingw compiler to read the Cygwin
# headers, and the compilation will fail.

# The path name where the LaTeX binaries can be found. (optional)
# If this variable is not defined the User Documentation will not be built
# or installed.
TEXPATH := /usr/local/teTeX2.0.2/bin/sparc-sun-solaris2.8


###########################################################################
# You don't have to alter anything below this line.

# Setting PLATFORM automagically if it is not defined.
ifndef PLATFORM
PLATFORM1 := $(shell uname -s)
PLATFORM2 := $(shell uname -r)
PLATFORM3 := $(shell uname -m)
ifeq ($(PLATFORM1), SunOS)
  ifneq ($(PLATFORM2), 5.6)
    PLATFORM := SOLARIS8
    PLATSUFFIX = -sol8
  else
    PLATFORM := SOLARIS
    PLATSUFFIX = -sol6
  endif # 5.8
endif # SunOS
ifeq ($(PLATFORM1), Linux)
  PLATFORM := LINUX
  PLATSUFFIX = -linux
endif # Linux
ifeq ($(PLATFORM1), FreeBSD)
  PLATFORM := FREEBSD
endif # FreeBSD
ifeq ($(findstring CYGWIN, $(PLATFORM1)), CYGWIN)
  PLATFORM := WIN32
  PLATSUFFIX = -cygwin
endif # CYGWIN
endif # ifndef PLATFORM
ifndef PLATFORM
	PLATFORM := $(error PLEASE CHECK THE PLATFORM SETTINGS)
endif # ifndef PLATFORM

# Derived variables, which you should never change.

ifdef MINGW

# Target directory for binaries:
BINDIR := $(TTCN3_DIR)/programs
# Target directory for manual pages:
MANDIR := $(TTCN3_DIR)/manuals
# Target directory for other files (nedit/xemacs additions, etc.)
ETCDIR := $(TTCN3_DIR)

else

# Target directory for binaries:
BINDIR := $(TTCN3_DIR)/bin
# Target directory for manual pages:
MANDIR := $(TTCN3_DIR)/man
# Target directory for other files (nedit/xemacs additions, etc.)
ETCDIR := $(TTCN3_DIR)/etc

endif

# Target directory for header files:
INCDIR := $(TTCN3_DIR)/include
# Target directory for libraries:
LIBDIR := $(TTCN3_DIR)/lib
# Target directory for html help pages:
HELPDIR := $(TTCN3_DIR)/help
# Target directory for the documentation:
DOCDIR := $(TTCN3_DIR)/doc
# Target directory for the demo "Hello World!"
DEMODIR := $(TTCN3_DIR)/demo

CPPFLAGS += -D$(PLATFORM)

ifeq ($(LICENSING), yes)
    CPPFLAGS += -DLICENSE
    LICENSE_LIBS = -L$(OPENSSL_DIR)/lib -lcrypto
else
    LICENSE_LIBS :=
endif

ifeq ($(USAGE_STATS), yes)
    CPPFLAGS += -DUSAGE_STATS
endif

# Windows/Cygwin specific settings
ifeq ($(PLATFORM), WIN32)
    EXESUFFIX := .exe
    LDFLAGS += -Wl,--enable-auto-import,--enable-runtime-pseudo-reloc

    ifdef MINGW
    MINGW_LIBS := -lgdi32
    endif
else
    EXESUFFIX :=

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

    CCFLAGS  += -fPIC
    CXXFLAGS += -fPIC
    LDFLAGS  += -fPIC
endif

# Try to use -isystem for Qt headers (this is a GCC feature).
# This avoids the myriads of warnings issued for the Qt headers.
ifneq (,$(findstring SOLARIS,$(PLATFORM)))
# Avoid using it on Solaris, because
# -isystem puts an implicit "extern C" around every system header,
# which causes compilation of Qt headers to fail miserably :-(
# (this could be fixed by recompiling GCC)
    INCLUDEQT := -I
else
# Not Solaris
    INCLUDEQT := -isystem
endif

# Setting EPOLL usage - It is Linux specific
ifeq ($(PLATFORM), LINUX)
    CPPFLAGS += -DUSE_EPOLL
endif

ifdef ALPINE_LINUX
    CPPFLAGS += -DALPINE_LINUX
endif

#not useful in compiler2/asn1, for example
#REL_DIR := $(notdir $(CURDIR))

export ABS_TOP := $(abspath $(TOP))/
ifeq "$(ABS_TOP)" "/"
# abspath only from GNU make 3.81, here's a replacement
ABS_TOP := $(shell cd $(TOP); pwd)/
endif

ifdef SRCDIR

REL_DIR := $(subst $(ABS_TOP),,$(CURDIR))

ABS_SRC := $(SRCDIR)/$(REL_DIR)

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

SRC_TOP := $(SRCDIR)

else

ABS_SRC := $(abspath $(CURDIR))
ifeq "$(ABS_SRC)" ""
ABS_SRC := $(shell cd $(CURDIR); pwd)
endif

SRC_TOP := $(TOP)

endif


