#
# This file contains definitions for RTEMS builds
#
# Author: W. Eric Norum
#         University of Saskatchewan
#         eric.norum@usask.ca
#
#  Contains definitions common to all RTEMS targets
#
#  This file is maintained by the build community.
#  Sites may override definitions in os/CONFIG_SITE.Common.RTEMS
#-------------------------------------------------------
#

#-------------------------------------------------------
# RTEMS tools are similar to UNIX tools
-include $(CONFIG)/os/CONFIG.Common.UnixCommon

GNU_TARGET_INCLUDE_DIR =
unexport GCC_EXEC_PREFIX

#--------------------------------------------------
# Get RTEMS_BASE definition
-include $(CONFIG)/os/CONFIG_SITE.Common.RTEMS

ifneq ($(CONFIG),$(TOP)/configure)
-include $(TOP)/configure/CONFIG_SITE.Common.RTEMS
endif

#--------------------------------------------------
# Set RTEMS_BSP and GNU_TARGET if not already done
RTEMS_BSP ?= $(subst RTEMS-,,$(T_A))
GNU_TARGET ?= $(RTEMS_TARGET_CPU)-rtems

#-------------------------------------------------------
# Pick up the RTEMS tool/path definitions from the RTEMS BSP directory.
include $(RTEMS_BASE)/$(GNU_TARGET)$(RTEMS_VERSION)/$(RTEMS_BSP)/Makefile.inc
include $(RTEMS_CUSTOM)
include $(CONFIG.CC)

#-------------------------------------------------------
# RTEMS cross-development tools
CC = $(RTEMS_TOOLS)/bin/$(CC_FOR_TARGET) $(GCCSPECS) -fasm
CCC = $(RTEMS_TOOLS)/bin/$(CXX)
CPP = $(RTEMS_TOOLS)/bin/$(CC_FOR_TARGET) -x c -E $(GCCSPECS)
AR = $(RTEMS_TOOLS)/bin/$(AR_FOR_TARGET)
LD = $(RTEMS_TOOLS)/bin/$(LD_FOR_TARGET) -r

RANLIB := $(RTEMS_TOOLS)/bin/$(RANLIB)

#-------------------------------------------------------
# Build types
VALID_BUILDS = Ioc

#--------------------------------------------------
# The RTEMS Makefiles redefine several macros, so we have to
# reset them to the proper EPICS values, from CONFIG_COMMON
CFLAGS = $($(BUILD_CLASS)_CFLAGS) $(POSIX_CFLAGS) $(OPT_CFLAGS)\
 $(DEBUG_CFLAGS) $(PIPE_CFLAGS) $(WARN_CFLAGS) $(TARGET_CFLAGS)\
 $(USR_CFLAGS) $(CMD_CFLAGS) $(ARCH_DEP_CFLAGS) $(CODE_CFLAGS)\
 $(STATIC_CFLAGS) $(OP_SYS_CFLAGS) $(LIBRARY_SRC_CFLAGS)

CXXFLAGS = $($(BUILD_CLASS)_CXXFLAGS) $(POSIX_CXXFLAGS) $(OPT_CXXFLAGS)\
 $(DEBUG_CXXFLAGS) $(PIPE_CFLAGS) $(WARN_CXXFLAGS) $(TARGET_CXXFLAGS)\
 $(USR_CXXFLAGS) $(CMD_CXXFLAGS) $(ARCH_DEP_CXXFLAGS) $(CODE_CXXFLAGS)\
 $(STATIC_CXXFLAGS) $(OP_SYS_CXXFLAGS) $(LIBRARY_SRC_CFLAGS)

LDFLAGS = $(OPT_LDFLAGS) $(TARGET_LDFLAGS) $(USR_LDFLAGS) $(CMD_LDFLAGS)\
 $(POSIX_LDFLAGS) $(ARCH_DEP_LDFLAGS) $(DEBUG_LDFLAGS) $(OP_SYS_LDFLAGS)\
 $($(BUILD_CLASS)_LDFLAGS) $(RUNTIME_LDFLAGS) $(CODE_LDFLAGS)

LDLIBS = $(POSIX_LDLIBS) $(ARCH_DEP_LDLIBS) $(DEBUG_LDLIBS) $(OP_SYS_LDLIBS)\
 $(GNU_LDLIBS_$(GNU))

CPPFLAGS += $($(BUILD_CLASS)_CPPFLAGS) $(POSIX_CPPFLAGS) $(OPT_CPPFLAGS)\
 $(DEBUG_CPPFLAGS) $(WARN_CPPFLAGS) $(BASE_CPPFLAGS) $(TARGET_CPPFLAGS)\
 $(USR_CPPFLAGS) $(CMD_CPPFLAGS) $(ARCH_DEP_CPPFLAGS) $(OP_SYS_CPPFLAGS)\
 $(OP_SYS_INCLUDE_CPPFLAGS) $(CODE_CPPFLAGS)

ECHO = @$(if $(make-s),$(NOP),echo)

# Originally set in os/CONFIG.UnixCommon.Common
MKDIR = mkdir -p

#--------------------------------------------------
# Although RTEMS uses gcc, it wants to use gcc its own way
CROSS_CPPFLAGS =
CROSS_LDFLAGS =
SHRLIB_CFLAGS =
OPT_CFLAGS_YES   = $(CFLAGS_OPTIMIZE_V)
OPT_CXXFLAGS_YES = $(CFLAGS_OPTIMIZE_V)
OPT_CFLAGS_NO    = $(CFLAGS_DEBUG_V)
OPT_CXXFLAGS_NO  = $(CFLAGS_DEBUG_V)

MODEXT=.obj

#--------------------------------------------------
# operating system class (include/os/<os_class>)
OS_CLASS = RTEMS

#--------------------------------------------------
# Operating system compile & link flags
OP_SYS_CFLAGS += -D__LINUX_ERRNO_EXTENSIONS__

OP_SYS_CFLAGS_NET_yes = -DRTEMS_LEGACY_STACK
OP_SYS_CFLAGS += $(OP_SYS_CFLAGS_NET_$(RTEMS_HAS_NETWORKING))

ifeq ($(RTEMS_HAS_POSIX_API),yes)
POSIX_CPPFLAGS = -D_GNU_SOURCE -D_DEFAULT_SOURCE
endif

OP_SYS_LDLIBS_posix_NET_yes = -ltftpfs -lnfs -lz -ltelnetd
OP_SYS_LDLIBS_posix_NET_no = -ltftpfs -lbsd -lz
OP_SYS_LDLIBS_score_NET_yes = -lnfs
OP_SYS_LDLIBS_score_NET_no = -lnfs
OP_SYS_LDLIBS += -lrtemsCom -lCom
OP_SYS_LDLIBS += $(OP_SYS_LDLIBS_$(OS_API)_NET_$(RTEMS_HAS_NETWORKING))
OP_SYS_LDLIBS += -lrtemscpu -lc -lm

OP_SYS_LDFLAGS_posix = -u POSIX_Init
OP_SYS_LDFLAGS_score = -u Init \
                 $(PROJECT_RELEASE)/lib/no-dpmem.rel \
                 $(PROJECT_RELEASE)/lib/no-mp.rel \
                 $(PROJECT_RELEASE)/lib/no-part.rel \
                 $(PROJECT_RELEASE)/lib/no-signal.rel \
                 $(PROJECT_RELEASE)/lib/no-rtmon.rel
OP_SYS_LDFLAGS += $(CPU_CFLAGS) $(OP_SYS_LDFLAGS_$(OS_API))

# Settings for GeSys
MOD_SYS_LDFLAGS += $(CPU_CFLAGS) -Wl,-r -nostdlib

# Do not link against libraries which are part of the Generic Image
GESYS_LIBS += -lgcc
GESYS_LIBS += -lc -lm -lrtemscpu -lrtemsbsp -lrtems++
GESYS_LIBS += -lcexp -ltecla_r -lspencer_regexp -lpmelf -lpmbfd
GESYS_LIBS += -lnfs -ltelnetd -lrtems-gdb-stub

# While not part of the Generic Image it provides symbols which
# would conflict.
GESYS_LIBS += -lrtemsCom

#--------------------------------------------------
# Options for building GeSys loadable objects

MODNAME_YES = $(PRODNAME:%$(EXE)=%$(MODEXT))
MODNAME += $(MODNAME_$(USE_GESYS))
PRODTARGETS += $(MODNAME)
BIN_INSTALLS += $(MODNAME)

# changes to LDFLAGS in CONFIG_COMMON and LINK.cpp in CONFIG.Common.UnixCommon
# should be reflected here with the following exceptions
#
# replace OP_SYS_LDFLAGS with MOD_SYS_LDFLAGS
# replace PROD_LDLIBS with MOD_LDLIBS
# remove STATIC_LDFLAGS

MOD_LDLIBS = $(filter-out $(GESYS_LIBS),$(PROD_LDLIBS))

MOD_LDFLAGS = $(OPT_LDFLAGS) $(TARGET_LDFLAGS) $(USR_LDFLAGS) $(POSIX_LDFLAGS) \
 $(ARCH_DEP_LDFLAGS) $(DEBUG_LDFLAGS) $(MOD_SYS_LDFLAGS) $($(BUILD_CLASS)_LDFLAGS)\
 $(RUNTIME_LDFLAGS) $(CODE_LDFLAGS)

LINK.mod = $(CCC) -o $@ $(PRODDIR_LDFLAGS) $(MOD_LDFLAGS)
LINK.mod += $(PROD_LDFLAGS) $(PROD_LD_OBJS) $(PROD_LD_RESS) $(MOD_LDLIBS)

#--------------------------------------------------
# Here munching means creating a bootable object binary
ifdef MUNCH_SUFFIX
  MUNCHNAME = $(PRODNAME:%$(EXE)=%$(MUNCH_SUFFIX))
  TESTMUNCHNAME = $(TESTPRODNAME:%$(EXE)=%$(MUNCH_SUFFIX))
endif

#--------------------------------------------------
# RTEMS has neither shared libraries nor dynamic loading
STATIC_BUILD=YES
SHARED_LIBRARIES=NO
CODE_CFLAGS =
CODE_CXXFLAGS =

#--------------------------------------------------
# Override the usual RTEMS verbosity from ar
ARFLAGS = rc

#--------------------------------------------------
# Command-line input support
LDLIBS_LIBTECLA = -ltecla_r -lncurses
LDLIBS_READLINE = -lreadline -lncurses

#--------------------------------------------------
# Allow site overrides
-include $(CONFIG)/os/CONFIG_SITE.$(EPICS_HOST_ARCH).RTEMS
