Skip to content

Commit

Permalink
Added C++ support for GCC (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Anestis Athanasiadis <[email protected]>
Co-authored-by: Juan Sapriza <[email protected]>
  • Loading branch information
3 people authored Jul 19, 2024
1 parent 322f466 commit 66c3084
Show file tree
Hide file tree
Showing 36 changed files with 502 additions and 156 deletions.
30 changes: 24 additions & 6 deletions .github/workflows/build-apps-job/build-apps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,39 @@ LONG_R="${RED}==================================================================
LONG_W="${WHITE}================================================================================${RESET}"

# Error vars are not defined if there is problem!
APPS=$(\ls sw/applications/) &&\
APPS_GCC=$(\ls sw/applications/) &&\

# Applications that should only be compiled with GCC
ONLY_GCC="example_cpp"

# Initialize APPS_CLANG with the same content as APPS_GCC
APPS_CLANG="$APPS_GCC"

# Loop through ONLY_GCC to filter out the specified applications from APPS_CLANG
for app in $ONLY_GCC; do
# Remove the app from APPS_CLANG
APPS_CLANG=${APPS_CLANG//$app/}
done

declare -i FAILURES=0 &&\
FAILED='' &&\

echo -e ${LONG_W}
echo -e "Will try building the following apps:${RESET}"
echo -e $APPS | tr " " "\n"
echo -e "----> GCC"
echo -e $APPS_GCC | tr " " "\n"
echo -e "----> CLANG"
echo -e $APPS_CLANG | tr " " "\n"
echo -e ${LONG_W}

if [ -z "$APPS" ]; then
if [ -z "$APPS_GCC" ]; then
echo -e ${LONG_R}
echo -e "${RED}No apps found${RESET}"
echo -e ${LONG_R}
exit 2
fi

for APP in $APPS
for APP in $APPS_GCC
do

# Build the app with GCC
Expand All @@ -44,7 +60,10 @@ do
FAILURES=$(( FAILURES + 1 ))
FAILED="$FAILED(gcc)\t$APP "
fi

done

for APP in $APPS_CLANG
do
# Build the app with Clang
make app-clean
if make app PROJECT=$APP COMPILER=clang ; then
Expand All @@ -58,7 +77,6 @@ do
FAILURES=$(( FAILURES + 1 ))
FAILED="$FAILED(clang)\t$APP "
fi

done


Expand Down
72 changes: 54 additions & 18 deletions sw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if(NOT WIN32)
endif()

# set the project name
project(${PROJECT} ASM C)
project(${PROJECT} ASM C CXX)

# set the required CMake standard
set(CMAKE_CXX_STANDARD 14)
Expand Down Expand Up @@ -89,17 +89,18 @@ FOREACH(file_path ${new_list})
# Add it to the string format list
SET(dir_list_str ${dir_list_str} "-I ${dir_path} \
")
string(REPLACE ";" "" dir_list_str ${dir_list_str})


# Add it to the header list
SET(h_dir_list_ ${h_dir_list_} "${dir_path}")
endif()

ENDFOREACH()

LIST(REMOVE_DUPLICATES dir_list_str)
LIST(REMOVE_DUPLICATES dir_list_str)
string(REPLACE ";" "" dir_list_str ${dir_list_str}) #This has been moved here to avoid multiple includes of the same path, see: https://github.com/esl-epfl/x-heep/issues/470
LIST(REMOVE_DUPLICATES h_dir_list_)


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Add list to the directories to be included

Expand All @@ -108,8 +109,7 @@ SET(INCLUDE_FOLDERS "-I ${RISCV}/${COMPILER_PREFIX}elf/include \
-I ${RISCV}/${COMPILER_PREFIX}elf/include/ \
-I ${ROOT_PROJECT} \
-I ${SOURCE_PATH} \
${dir_list_str}")

${dir_list_str}")

#######################################################################
# FIND SOURCE FILES TO BE INCLUDED
Expand All @@ -124,7 +124,9 @@ FILE(GLOB_RECURSE c_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.c)
FILE(GLOB_RECURSE s_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.s)
# Make a list of the .S source files that need to be linked
FILE(GLOB_RECURSE S_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.S)
SET(new_list ${c_files} ${s_files} ${S_files})
# Make a list of the .cpp source files that need to be linked
FILE(GLOB_RECURSE cpp_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.cpp)
SET(new_list ${c_files} ${s_files} ${S_files} ${cpp_files})

SET( c_dir_list "" )
SET( app_found 0 )
Expand Down Expand Up @@ -162,7 +164,9 @@ if( app_found EQUAL 0 )
FILE(GLOB_RECURSE s_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.s)
# Make a list of the .S source files that need to be linked
FILE(GLOB_RECURSE S_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.S)
SET(new_list ${c_files} ${s_files} ${S_files})
# Make a list of the .cpp source files that need to be linked
FILE(GLOB_RECURSE cpp_files FOLLOW_SYMLINKS ${SOURCE_PATH}*.cpp)
SET(new_list ${c_files} ${s_files} ${S_files} ${cpp_files})

SET(c_dir_list "")
FOREACH(file_path IN LISTS new_list)
Expand All @@ -182,10 +186,11 @@ if( app_found EQUAL 0 )
endif()

ENDFOREACH()
endif()

endif()
LIST(REMOVE_DUPLICATES c_dir_list)


#######################################################################
# DETERMINE IF APP IS EXTERNAL
#######################################################################
Expand Down Expand Up @@ -282,7 +287,7 @@ message( "${Magenta}Target: ${TARGET}${ColourReset}")
SET(LINKED_FILES "${LIB_CRT_P} \
${LIB_VCTR_P} \
${LIB_CRT_EXT_P} \
${c_dir_list}")
${c_dir_list}")

message( "${Magenta}Linked files: ${LINKED_FILES}${ColourReset}")

Expand Down Expand Up @@ -341,6 +346,7 @@ else()
")
endif()
set(CMAKE_C_FLAGS ${COMPILER_LINKER_FLAGS})
set(CMAKE_CXX_FLAGS ${COMPILER_LINKER_FLAGS})

if (${COMPILER} MATCHES "clang")
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --target=riscv32 \
Expand All @@ -362,8 +368,18 @@ endif()
#######################################################################
# SET TARGETS
#######################################################################

set(SOURCES ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.c)
if (${COMPILER} MATCHES "gcc")
set(SOURCES ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.c)
if(NOT EXISTS ${SOURCES})
# If main.c does not exist, set the source file to main.cpp
set(SOURCES ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.cpp)
message("${Red}cpp SOURCES set")
endif()
elseif(${COMPILER} MATCHES "clang")
set(SOURCES ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.c)
elseif(${COMPILER} MATCHES "g\\+\\+.*$")
set(SOURCES ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.cpp)
endif()

# add the executable
add_executable(${MAINFILE}.elf ${SOURCES})
Expand Down Expand Up @@ -393,6 +409,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-T ${LINKER_SCRIPT} \
-Wl,-Map=${MAINFILE}.map \
-L ${RISCV}/${COMPILER_PREFIX}elf/lib \
-lc -lm -lgcc -flto \
-fpermissive -fno-rtti -fno-exceptions -fno-threadsafe-statics \
-ffunction-sections -fdata-sections -specs=nano.specs")
message( "${Magenta}Lib Folder RISCV-GCC: ${RISCV}/${COMPILER_PREFIX}elf/lib${ColourReset}")

Expand Down Expand Up @@ -448,12 +465,31 @@ add_custom_command(TARGET ${MAINFILE}.elf POST_BUILD
COMMENT "Invoking: Hexdump")

# Pre-processing command to create disassembly for each source file
foreach (SRC_MODULE ${MAINFILE} )
if ((${COMPILER} MATCHES "gcc") OR (${COMPILER} MATCHES "clang"))
if(EXISTS ${SOURCE_PATH}applications/${PROJECT}/${MAINFILE}.c)
foreach (SRC_MODULE ${MAINFILE} )
add_custom_command(TARGET ${MAINFILE}.elf
PRE_LINK
COMMAND ${CMAKE_OBJDUMP} -S ${ROOT_PROJECT}build/CMakeFiles/${MAINFILE}.elf.dir/${OBJ_PATH}applications/${PROJECT}/${SRC_MODULE}.c.obj > ${SRC_MODULE}.s
COMMENT "Invoking: GCC C Disassemble ( CMakeFiles/${MAINFILE}.dir/${SRC_MODULE}.c.obj)")
endforeach()
else() #main.cpp targets
foreach (SRC_MODULE ${MAINFILE} )
add_custom_command(TARGET ${MAINFILE}.elf
PRE_LINK
COMMAND ${CMAKE_OBJDUMP} -S ${ROOT_PROJECT}build/CMakeFiles/${MAINFILE}.elf.dir/${OBJ_PATH}applications/${PROJECT}/${SRC_MODULE}.cpp.obj > ${SRC_MODULE}.s
COMMENT "Invoking: GCC CPP Disassemble ( CMakeFiles/${MAINFILE}.dir/${SRC_MODULE}.cpp.obj)")
endforeach()
endif()
elseif(${COMPILER} MATCHES "g\\+\\+.*$")
foreach (SRC_MODULE ${MAINFILE} )
add_custom_command(TARGET ${MAINFILE}.elf
PRE_LINK
COMMAND ${CMAKE_OBJDUMP} -S ${ROOT_PROJECT}build/CMakeFiles/${MAINFILE}.elf.dir/${OBJ_PATH}applications/${PROJECT}/${SRC_MODULE}.c.obj > ${SRC_MODULE}.s
COMMENT "Invoking: Disassemble ( CMakeFiles/${MAINFILE}.dir/${SRC_MODULE}.c.obj)")
endforeach()
COMMAND ${CMAKE_OBJDUMP} -S ${ROOT_PROJECT}build/CMakeFiles/${MAINFILE}.elf.dir/${OBJ_PATH}applications/${PROJECT}/${SRC_MODULE}.cpp.obj > ${SRC_MODULE}.s
COMMENT "Invoking: G++ Disassemble ( CMakeFiles/${MAINFILE}.dir/${SRC_MODULE}.cpp.obj)")
endforeach()
endif()


# Adding gdb command - TBD
#add_custom_target(gdb DEPENDS ${MAINFILE}.elf)
Expand All @@ -462,4 +498,4 @@ endforeach()

SET(DCMAKE_EXPORT_COMPILE_COMMANDS ON)

#message( FATAL_ERROR "You can not do this at all, CMake will exit." )
#message( FATAL_ERROR "You can not do this at all, CMake will exit." )
21 changes: 21 additions & 0 deletions sw/applications/example_cpp/MyClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

extern "C" {
#include <stdio.h>
#include <stdlib.h>
}

#include "MyClass.hpp"

MyClass::MyClass(int initialValue) : value(initialValue) {}

void MyClass::setValue(int newValue) {
value = newValue;
}

int MyClass::getValue() {
return value*5;
}

void MyClass::printValue() {
printf("Value: %d\n\r", value);
}
15 changes: 15 additions & 0 deletions sw/applications/example_cpp/MyClass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef MYCLASS_HPP
#define MYCLASS_HPP

class MyClass {
public:
MyClass(int initialValue); // Constructor
void setValue(int newValue); // Setter
int getValue(); // Getter
void printValue(); // Method to print the value

private:
int value;
};

#endif // MYCLASS_HPP
27 changes: 0 additions & 27 deletions sw/applications/example_cpp/main.c

This file was deleted.

50 changes: 50 additions & 0 deletions sw/applications/example_cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 EPFL
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Juan Sapriza <[email protected]>
*/



extern "C" {
#include <stdio.h>
#include <stdlib.h>
}

#include "MyClass.hpp"
#include "core_v_mini_mcu.h"
#include "x-heep.h"

#if TARGET_SIM && PRINTF_IN_SIM
#define PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
#elif PRINTF_IN_FPGA && !TARGET_SIM
#define PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define PRINTF(...)
#endif

int main()
{
MyClass myObject(10); // Create an object with initial value 10
myObject.printValue(); // Print the initial value

myObject.setValue(20); // Change the value to 20
myObject.printValue(); // Print the updated value

int value = myObject.getValue(); // Get the value
PRINTF("Retrieved Value: %d\n\r" ,value); // Print the retrieved value

return value == 20*5 ? EXIT_SUCCESS : EXIT_FAILURE;
}
43 changes: 0 additions & 43 deletions sw/applications/example_cpp/test_cpp.cpp

This file was deleted.

4 changes: 0 additions & 4 deletions sw/applications/example_cpp/test_cpp.h

This file was deleted.

Loading

0 comments on commit 66c3084

Please sign in to comment.