From 7beb0d206e98949c56c4e2ba51bf1ed8c5426e1f Mon Sep 17 00:00:00 2001
From: Nagpal <x1080849@ti.com>
Date: Sun, 23 Jan 2022 11:04:09 +0530
Subject: [PATCH] C++ Demo

---
 board/diag/board_diag_component.mk  | 20 ++++++
 board/diag/cpp/build/makefile       | 94 +++++++++++++++++++++++++++++
 board/diag/cpp/src/cpp_test.cpp     | 19 ++++++
 build/makerules/common.mk           |  4 ++
 build/makerules/rules_a72.mk        |  5 ++
 build/makerules/rules_ti_cgt_arm.mk | 26 +++++---
 6 files changed, 158 insertions(+), 10 deletions(-)
 create mode 100644 board/diag/cpp/build/makefile
 create mode 100644 board/diag/cpp/src/cpp_test.cpp

diff --git a/board/diag/board_diag_component.mk b/board/diag/board_diag_component.mk
index 7f44b39..a7aab1a 100644
--- a/board/diag/board_diag_component.mk
+++ b/board/diag/board_diag_component.mk
@@ -630,6 +630,26 @@ export board_diag_lcd_$(SOC)_CORELIST = $(board_diag_$(SOC)_CORELIST)
 export board_diag_lcd_SBL_APPIMAGEGEN = $(board_diag_APPIMAGEGEN_CTRL)
 board_diag_EXAMPLE_LIST += board_diag_lcd
 
+# CPP Test
+board_diag_cppTest_COMP_LIST = board_diag_cppTest
+board_diag_cppTest_RELPATH = ti/board/diag/cpp/build
+board_diag_cppTest_PATH = $(PDK_BOARD_DIAG_COMP_PATH)/cpp/build
+board_diag_cppTest_CUSTOM_BINPATH = $(board_diag_LOCAL_BINPATH)
+board_diag_cppTest_MAKEFILE = -f makefile
+board_diag_cppTest_BOARD_DEPENDENCY = yes
+board_diag_cppTest_CORE_DEPENDENCY = yes
+export board_diag_cppTest_COMP_LIST
+export board_diag_cppTest_BOARD_DEPENDENCY
+export board_diag_cppTest_CORE_DEPENDENCY
+export board_diag_cppTest_MAKEFILE
+board_diag_cppTest_PKG_LIST = board_diag_cppTest
+board_diag_cppTest_INCLUDE = $(board_diag_cppTest_PATH)
+board_diag_cppTest_BOARDLIST = j721e_evm am65xx_evm am65xx_idk tpr12_evm j7200_evm am64x_evm awr294x_evm
+board_diag_cppTest_$(SOC)_CORELIST = $(board_diag_$(SOC)_CORELIST)
+export board_diag_cppTest_$(SOC)_CORELIST
+export board_diag_cppTest_SBL_APPIMAGEGEN = $(board_diag_APPIMAGEGEN_CTRL)
+board_diag_EXAMPLE_LIST += board_diag_cppTest
+
 # LED
 board_diag_led_COMP_LIST = board_diag_led
 board_diag_led_RELPATH = ti/board/diag/led/build
diff --git a/board/diag/cpp/build/makefile b/board/diag/cpp/build/makefile
new file mode 100644
index 0000000..e1fde96
--- /dev/null
+++ b/board/diag/cpp/build/makefile
@@ -0,0 +1,94 @@
+# Copyright (C) 2019-2021 Texas Instruments Incorporated - http://www.ti.com/
+#
+#
+#  Redistribution and use in source and binary forms, with or without
+#  modification, are permitted provided that the following conditions
+#  are met:
+#
+#    Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+#
+#    Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the
+#    distribution.
+#
+#    Neither the name of Texas Instruments Incorporated nor the names of
+#    its contributors may be used to endorse or promote products derived
+#    from this software without specific prior written permission.
+#
+#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#
+# Macro definitions referenced below
+#
+
+#
+# This file is the makefile for building CSL uart app.
+#
+include $(PDK_INSTALL_PATH)/ti/build/Rules.make
+
+# Board diag parameters which can be controlled outside this makefile
+DIAGNAME ?= cppTest
+PROFILE ?= release
+TESTMODE ?= FUNCTIONAL_TEST
+MODENAME ?=
+BOARD_DIAG_CFLAGS ?=
+APP_NAME ?= board_diag_$(DIAGNAME)
+
+BUILD_PROFILE_$(CORE) = $(PROFILE)
+
+BUILD_OS_TYPE = baremetal
+LNKFLAGS_LOCAL_mpu1_0  += --entry Entry
+EXTERNAL_INTERFACES =
+XDC_CFG_FILE_mpu1_0 =
+XDC_CFG_FILE_mcu1_0 =
+
+ifeq ($(MODENAME), )
+ifeq ($(TESTMODE), STRESS_TEST)
+MODENAME = Stress
+endif
+endif
+
+SRCDIR = ../src ../../common/$(SOC) $(BOARD)
+INCDIR = ../../../../board ../src ../../../src/$(BOARD)/include ../../../src/$(BOARD) ../../../src/flash ../../common/$(SOC) ../../../src/evmKeystone3/include
+
+# List all the external components/interfaces, whose interface header files
+# need to be included for this component
+INCLUDE_EXTERNAL_INTERFACES = pdk
+
+# List all the components required by the application
+COMP_LIST_COMMON = $(PDK_COMMON_BAREMETAL_COMP)
+
+# Common source files and CFLAGS across all platforms and cores
+PACKAGE_SRCS_COMMON = ../src makefile
+PACKAGE_SRCS_COMMON += ../../common/$(SOC)
+PACKAGE_SRCS_COMMON += ../../board_diag_component.mk
+PACKAGE_SRCS_COMMON += ../../create_sd.bat ../../create_sd.sh
+
+SRCS_COMMON_CPP += cpp_test.cpp
+
+CFLAGS_LOCAL_COMMON = $(PDK_CFLAGS) -DDIAG_$(TESTMODE) $(BOARD_DIAG_CFLAGS)
+
+# Include common make files
+ifeq ($(MAKERULEDIR), )
+#Makerule path not defined, define this and assume relative path from ROOTDIR
+  MAKERULEDIR := $(ROOTDIR)/ti/build/makerules
+  export MAKERULEDIR
+endif
+include $(MAKERULEDIR)/common.mk
+
+# OBJs and libraries are built by using rule defined in rules_<target>.mk
+#     and need not be explicitly specified here
+
+# Nothing beyond this point
diff --git a/board/diag/cpp/src/cpp_test.cpp b/board/diag/cpp/src/cpp_test.cpp
new file mode 100644
index 0000000..28c277b
--- /dev/null
+++ b/board/diag/cpp/src/cpp_test.cpp
@@ -0,0 +1,19 @@
+// Your First C++ Program
+
+#include <ti/drv/uart/UART_stdio.h>
+#include "board.h"
+
+int main() {
+    Board_initCfg boardCfg;
+
+#ifdef PDK_RAW_BOOT
+    boardCfg = BOARD_INIT_PINMUX_CONFIG |
+               BOARD_INIT_UART_STDIO;
+#else
+    boardCfg = BOARD_INIT_UART_STDIO;
+#endif
+
+    Board_init(boardCfg);
+    UART_printf("Test log for C++ build\n");
+    return 0;
+}
diff --git a/build/makerules/common.mk b/build/makerules/common.mk
index 7265095..3748d7a 100644
--- a/build/makerules/common.mk
+++ b/build/makerules/common.mk
@@ -209,11 +209,13 @@ $(PACKAGEDIR) :
 
 # Assemble list of source file names
 SRCS = $(SRCS_COMMON) $(SRCS_$(CORE)) $(SRCS_$(SOC))
+SRCS_CPP = $(SRCS_COMMON_CPP) $(SRCS_$(CORE)) $(SRCS_$(SOC))
 SRCS_ASM = $(SRCS_ASM_COMMON) $(SRCS_ASM_$(CORE)) $(SRCS_ASM_$(SOC))
 SRCS_S = $(SRCS_S_COMMON) $(SRCS_S_$(CORE)) $(SRCS_S_$(SOC))
 
 ifneq ($(SOC),$(BOARD))
 SRCS += $(SRCS_$(BOARD))
+SRCS_CPP += $(SRCS_CPP_$(BOARD))
 SRCS_ASM += $(SRCS_ASM_$(BOARD))
 SRCS_S += $(SRCS_S_$(BOARD))
 endif #ifneq ($(SOC,$(BOARD))
@@ -241,6 +243,7 @@ ifndef MODULE_NAME
   # of the file assumed is <MOD>_cfg.c under the top-level directory - i.e.
   # specified by <MOD>_PATH variable
   SRCS += $(SRCS_APPSTG_FILES)
+	SRCS_CPP += $(SRCS_APPSTG_FILES)
   PKG_PATHS = $(foreach PKG,$(PKG_LIST),$($(PKG)_PATH))
   VPATH += $(PKG_PATHS)
 
@@ -249,6 +252,7 @@ endif
 
 # Change the extension from C to $(OBJEXT) and also add path
 OBJ_PATHS = $(patsubst %.c, $(OBJDIR)/%.$(OBJEXT), $(SRCS))
+OBJ_PATHS_CPP = $(patsubst %.cpp, $(OBJDIR)/%.$(OBJEXT), $(SRCS_CPP))
 
 # Change the extension from ASM to $(OBJEXT) and also add path
 OBJ_PATHS_ASM = $(patsubst %.asm, $(OBJDIR)/%.$(OBJEXT), $(SRCS_ASM))
diff --git a/build/makerules/rules_a72.mk b/build/makerules/rules_a72.mk
index 4fb562a..c692328 100644
--- a/build/makerules/rules_a72.mk
+++ b/build/makerules/rules_a72.mk
@@ -62,7 +62,12 @@ ifeq ($(FORMAT),ELF)
 endif
 
 # Internal CFLAGS - normally doesn't change
+ifeq ($(CPLUSPLUS_BUILD), yes)
+CFLAGS_INTERNAL = -Wall -Wunused -Wunknown-pragmas -ffunction-sections -fdata-sections -c -mcpu=cortex-a72+fp+simd -g -mabi=lp64 -mcmodel=large -mstrict-align -fms-extensions
+else
 CFLAGS_INTERNAL = -Wimplicit -Wall -Wunused -Wunknown-pragmas -ffunction-sections -fdata-sections -c -mcpu=cortex-a72+fp+simd -g -mabi=lp64 -mcmodel=large -mstrict-align -std=gnu99 -fms-extensions
+endif
+
 CFLAGS_INTERNAL += -mfix-cortex-a53-835769 -mfix-cortex-a53-843419
 CFLAGS_INTERNAL += -Wno-address-of-packed-member
 CFLAGS_INTERNAL += -Wno-stringop-truncation
diff --git a/build/makerules/rules_ti_cgt_arm.mk b/build/makerules/rules_ti_cgt_arm.mk
index d1d64eb..c1046ea 100644
--- a/build/makerules/rules_ti_cgt_arm.mk
+++ b/build/makerules/rules_ti_cgt_arm.mk
@@ -75,7 +75,7 @@ endif
 
 # OPTIMIZATION can take two values i.e. CODE and PERFORMANCE
 # This is parameter is only valid for release mode and not for debug mode
-# CODE is recommended for optimizing code size 
+# CODE is recommended for optimizing code size
 # PERFORMANCE is recommended for optimizing performance, but it is likely to increase compiler generated code size
 ifeq ($(OPTIMIZATION),PERFORMANCE)
   OPT_LEVEL = -O3
@@ -229,7 +229,12 @@ $(OBJ_PATHS): $(OBJDIR)/%.$(OBJEXT): %.c $(GEN_FILE) | $(OBJDIR) $(DEPDIR)
 	$(MKDIR) -p $(dir $@)
 	$(CC) -MMD $(_CFLAGS) $(INCLUDES) -c $(COMPILEMODE) $< -o $@ -MF $(DEPFILE).d
 	$(CC) $(_CFLAGS) $(INCLUDES) -c $(COMPILEMODE) $< -o $@
-   
+
+$(OBJ_PATHS_CPP): $(OBJDIR)/%.$(OBJEXT): %.cpp $(GEN_FILE) | $(OBJDIR) $(DEPDIR)
+	$(ECHO) \# Compiling $(PRINT_MESSAGE):$<
+	$(MKDIR) -p $(dir $@)
+	$(CC) -MMD $(_CFLAGS) $(INCLUDES) -c $(COMPILEMODE) $< -o $@ -MF $(DEPFILE).d
+	$(CC) $(_CFLAGS) $(INCLUDES) -c $(COMPILEMODE) $< -o $@
 
 #TODO: Check ASMFLAGS if really required
 ASMFLAGS = -me -g -mthumb --diag_warning=225
@@ -255,11 +260,11 @@ $(PACKAGE_PATHS): $(PACKAGEDIR)/%: %
 ARFLAGS = rc
 
 # Archive/library file creation
-$(LIBDIR)/$(LIBNAME).$(LIBEXT) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(GEN_FILE) | $(LIBDIR)
+$(LIBDIR)/$(LIBNAME).$(LIBEXT) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP) $(GEN_FILE) | $(LIBDIR)
 	$(ECHO) \#
 	$(ECHO) \# Archiving $(PRINT_MESSAGE) into $@ ...
 	$(ECHO) \#
-	$(AR) $(ARFLAGS) $@ $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS)
+	$(AR) $(ARFLAGS) $@ $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP)
 
 $(LIBDIR)/$(LIBNAME).$(LIBEXT)_size: $(LIBDIR)/$(LIBNAME).$(LIBEXT)
 	$(ECHO) \#
@@ -272,7 +277,7 @@ $(LIBDIR)/$(LIBNAME).$(LIBEXT)_size: $(LIBDIR)/$(LIBNAME).$(LIBEXT)
 # Linker options and rules
 LNKFLAGS_INTERNAL_COMMON += -Xlinker -q -Xlinker -u -Xlinker _c_int00 -Xlinker --display_error_number -Xlinker --use_memcpy=fast -Xlinker --use_memset=fast
 # Supress warning for "entry-point symbol other than "_c_int00" specified"
-LNKFLAGS_INTERNAL_COMMON += -Xlinker --diag_suppress=10063-D 
+LNKFLAGS_INTERNAL_COMMON += -Xlinker --diag_suppress=10063-D
 # Supress warning for "no matching section"
 LNKFLAGS_INTERNAL_COMMON += -Xlinker --diag_suppress=10068-D
 # Supress warning for " LOAD placement ignored for "":  object is uninitialized"
@@ -336,10 +341,10 @@ endif
 #endif
 
 ifneq ($(XDC_CFG_FILE_$(CORE)),)
-$(EXE_NAME) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(LIB_PATHS) $(LNKCMD_FILE) $(OBJDIR)/$(CFG_COBJ_XDC) $(BUILD_LIB_ONCE)
+$(EXE_NAME) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP) $(LIB_PATHS) $(LNKCMD_FILE) $(OBJDIR)/$(CFG_COBJ_XDC) $(BUILD_LIB_ONCE)
 	$(CP) $(CONFIGURO_DIR)/package/cfg/*.rov.xs $(BINDIR)
 else
-$(EXE_NAME) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(LIB_PATHS) $(LNKCMD_FILE) $(BUILD_LIB_ONCE)
+$(EXE_NAME) : $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP) $(LIB_PATHS) $(LNKCMD_FILE) $(BUILD_LIB_ONCE)
 ifeq ($(BUILD_OS_TYPE), freertos)
 	$(CP) $(pdk_PATH)/ti/kernel/freertos/rov/syscfg_c.rov.xs $(BINDIR)
 endif
@@ -349,12 +354,12 @@ endif
 ifneq ($(XDC_CFG_FILE_$(CORE)),)
 	$(CP) $(OBJDIR)/$(CFG_COBJ_XDC) $(CONFIGURO_DIR)/package/cfg
   ifeq ($(BUILD_PROFILE_$(CORE)),whole_program_debug)
-	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJDIR)/$(CFG_COBJ_XDC) $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -o $@ -Xlinker -map_file=$@.map $(LNK_LIBS) $(RTSLIB_PATH)
+	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP)  $(OBJDIR)/$(CFG_COBJ_XDC) $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -o $@ -Xlinker -map_file=$@.map $(LNK_LIBS) $(RTSLIB_PATH)
   else
-	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -o $@ -m $@.map $(LNK_LIBS) $(RTSLIB_PATH)
+	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP) $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -o $@ -m $@.map $(LNK_LIBS) $(RTSLIB_PATH)
   endif
 else
-	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) -Xlinker $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -Xlinker --map_file=$@.map -Xlinker --output_file=$@ $(LNK_LIBS) $(RTSLIB_PATH)
+	$(LNK) $(_LNKFLAGS) $(OBJ_PATHS_ASM) $(OBJ_PATHS_S) $(OBJ_PATHS) $(OBJ_PATHS_CPP) -Xlinker $(LNKCMD_FILE) $(EXTERNAL_LNKCMD_FILE) $(APPEND_LNKCMD_FILE) -Xlinker --map_file=$@.map -Xlinker --output_file=$@ $(LNK_LIBS) $(RTSLIB_PATH)
 endif
 	$(ECHO) \#
 	$(ECHO) \# $@ created.
@@ -421,5 +426,6 @@ endif
 
 # Include dependency make files that were generated by $(CC)
 -include $(SRCS:%.c=$(DEPDIR)/%.d)
+-include $(SRCS_CPP:%.cpp=$(DEPDIR)/%.d)
 
 # Nothing beyond this point
-- 
2.27.0.windows.1

