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

#-------------------------------------------------------
# Pick up the RTEMS tool/path definitions from the RTEMS BSP directory.
include $(RTEMS_BASE)/$(RTEMS_TARGET_CPU)-rtems$(RTEMS_VERSION)/$(subst RTEMS-,,$(T_A))/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
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) $(HDEPENDS_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_CXXCFLAGS) $(OP_SYS_CXXFLAGS) $(LIBRARY_SRC_CFLAGS) $(HDEPENDS_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 $(findstring s,$(patsubst T_A=%,,$(MAKEFLAGS))),$(NOP),echo)

#--------------------------------------------------
# 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 flags
OP_SYS_LDLIBS += -lrtemsCom -lc -lrtemscpu -lCom -lnfs -lm
OP_SYS_LDFLAGS += $(CPU_CFLAGS) -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

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++ -lbspExt
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)

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