Skip to content

Commit

Permalink
Merge pull request #40 from NeoResearch/38_fix_tests
Browse files Browse the repository at this point in the history
Fix unit tests build
  • Loading branch information
igormcoelho authored Dec 2, 2022
2 parents 6608f7f + 25258a0 commit c810658
Show file tree
Hide file tree
Showing 35 changed files with 1,889 additions and 218 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ vendor/
*.dot
*.png
*.o
#*.pb.cc
#*.pb.h
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- cd ${TRAVIS_BUILD_DIR}/spec/cpp
- mkdir build
- cd build
- cmake -Dtest=ON -DCOVERAGE=1 ..
- cmake -DBUILD_TESTS=ON -DCOVERAGE=1 ..
- make
- GTEST_COLOR=1 ctest --extra-verbose

Expand All @@ -66,7 +66,7 @@ jobs:
# - cd ${TRAVIS_BUILD_DIR}/spec/cpp
# - mkdir build
# - cd build
# - cmake -Dtest=ON ..
# - cmake -DBUILD_TESTS=ON ..
# - make
# - GTEST_COLOR=1 ctest --extra-verbose

Expand Down
76 changes: 14 additions & 62 deletions spec/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,36 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/build")

project(${PROJECT_NAME} VERSION ${PROJECT_VERSION})

# https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/
# https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html
add_definitions(-std=c++17)
#add_definitions(-fconcepts)
add_definitions(-fpermissive)
add_definitions(-fasynchronous-unwind-tables)
if (EXISTS /usr/bin/gcc-9)
if (EXISTS /usr/bin/g++-9)
set(CMAKE_C_COMPILER /usr/bin/gcc-9)
set(CMAKE_CXX_COMPILER /usr/bin/g++-9)
endif()
endif()

#add_definitions(-fno-rtti)
add_definitions(-pedantic-errors)
add_definitions(-pipe)

add_definitions(-W)
add_definitions(-Wall)
add_definitions(-Wconversion)
add_definitions(-Wcast-align)
add_definitions(-Wextra)
#add_definitions(-Werror)
add_definitions(-Werror=format-security)
add_definitions(-Werror=implicit-function-declaration)
add_definitions(-Wpedantic)
add_definitions(-Wno-long-long)
add_definitions(-Wno-missing-braces)
add_definitions(-Wno-parentheses)
add_definitions(-Wno-switch)
add_definitions(-Wno-unused-parameter)
add_definitions(-Wold-style-cast)
add_definitions(-Wpointer-arith)
add_definitions(-Wshadow)
#add_definitions(-Wswitch-default)
#add_definitions(-Wswitch-enum)
add_definitions(-Wcast-qual)
#add_definitions(-Wundef)
add_definitions(-Wunused)
add_definitions(-Wunreachable-code)
add_definitions(-Wunused-function)
add_definitions(-Wunused-variable)
add_definitions(-Wwrite-strings)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(Definitions REQUIRED)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes")
ADD_MY_DEFINITIONS()

#set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS "${CMAKE_CXX_SOURCE_FILE_EXTENSIONS};hpp;HPP")
#message(STATUS "Ignoring extensios ${CMAKE_CXX_IGNORE_EXTENSIONS}")
#list(REMOVE_ITEM CMAKE_CXX_IGNORE_EXTENSIONS HPP hpp)
#message(STATUS "Ignoring extensios ${CMAKE_CXX_IGNORE_EXTENSIONS}")

string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
if (build_type STREQUAL "debug")
message(STATUS "--- Debug build ---")
add_definitions(-g)
add_definitions(-grecord-gcc-switches)
# add_definitions(-D_GLIBCXX_DEBUG) # Not sure why it's breaking the mainRPC.cpp build
# add_definitions(-D_GLIBCXX_DEBUG_PEDANTIC) # Not sure why it's breaking the mainRPC.cpp build
add_definitions(-D_FORTIFY_SOURCE=2)
add_definitions(-D_GLIBCXX_ASSERTIONS)
add_definitions(-ggdb)
add_definitions(-O0)
add_definitions(-fstack-protector-all)

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
else ()
message(STATUS "--- Release build ---")
add_definitions(-O3)
# add_definitions(-Ofast)
add_definitions(-finline-functions)
add_definitions(-funroll-loops)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -frecursive")
endif ()

#set(PROTOBUF_GENERATED_PATH src CACHE STRING proto_gen)
set(GRPC_PLUGIN_CPP /usr/local/bin/grpc_cpp_plugin CACHE STRING grpc_bin)

option(BUILD_TESTS "Build test programs" ON)
message(STATUS "BUILD_TESTS = ${BUILD_TESTS}")

option(BUILD_TESTS "Build test programs" ON)
set(CMAKE_CXX_CPPCHECK "cppcheck")

include_directories(src)
add_subdirectory(src)
if (test)
if (BUILD_TESTS)
message(STATUS "Building the tests")
enable_testing()
SET(COVERAGE OFF CACHE BOOL "Coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
Expand All @@ -96,4 +46,6 @@ if (test)
#find_package(GTest REQUIRED)
#include_directories(${GTEST_INCLUDE_DIRS})
add_subdirectory(tests)
else()
message(STATUS "Ignoring the tests")
endif()
105 changes: 105 additions & 0 deletions spec/cpp/cmake/FindDefinitions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
MACRO(ADD_MY_DEFINITIONS)
message(STATUS "Adding definitiions")
# https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc/
# https://caiorss.github.io/C-Cpp-Notes/compiler-flags-options.html
add_definitions(-std=c++17)
#add_definitions(-fconcepts)
add_definitions(-fpermissive)
add_definitions(-fasynchronous-unwind-tables)

#add_definitions(-fno-rtti)
add_definitions(-pedantic-errors)
add_definitions(-pipe)

add_definitions(-W)
add_definitions(-Wall)
add_definitions(-Wconversion)
add_definitions(-Wcast-align)
add_definitions(-Wextra)
#add_definitions(-Werror)
add_definitions(-Werror=format-security)
add_definitions(-Werror=implicit-function-declaration)
add_definitions(-Wpedantic)
add_definitions(-Wno-long-long)
add_definitions(-Wno-missing-braces)
add_definitions(-Wno-parentheses)
add_definitions(-Wno-switch)
add_definitions(-Wno-unused-parameter)
add_definitions(-Wold-style-cast)
add_definitions(-Wpointer-arith)
add_definitions(-Wshadow)
#add_definitions(-Wswitch-default)
#add_definitions(-Wswitch-enum)
add_definitions(-Wcast-qual)
#add_definitions(-Wundef)
add_definitions(-Wunused)
add_definitions(-Wunreachable-code)
add_definitions(-Wunused-function)
add_definitions(-Wunused-variable)
add_definitions(-Wwrite-strings)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes")

string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
if (build_type STREQUAL "debug")
message(STATUS "--- Debug build ---")
add_definitions(-g)
add_definitions(-grecord-gcc-switches)
# add_definitions(-D_GLIBCXX_DEBUG) # Not sure why it's breaking the mainRPC.cpp build
# add_definitions(-D_GLIBCXX_DEBUG_PEDANTIC) # Not sure why it's breaking the mainRPC.cpp build
add_definitions(-D_FORTIFY_SOURCE=2)
add_definitions(-D_GLIBCXX_ASSERTIONS)
add_definitions(-ggdb)
add_definitions(-O0)
add_definitions(-fstack-protector-all)

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
else ()
message(STATUS "--- Release build ---")
add_definitions(-O3)
# add_definitions(-Ofast)
add_definitions(-finline-functions)
add_definitions(-funroll-loops)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -frecursive")
endif ()
ENDMACRO()

MACRO(ADD_TEST_DEFINITIONS)
message(STATUS "Adding test definitions")

string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
if (build_type STREQUAL "debug")
message(STATUS "--- Debug build ---")
message(STATUS "Adding sanitizer")
add_definitions(-fsanitize=address)
add_definitions(-fsanitize=alignment)
add_definitions(-fsanitize=bool)
add_definitions(-fsanitize=bounds)
add_definitions(-fsanitize=bounds-strict)
add_definitions(-fsanitize=builtin)
add_definitions(-fsanitize=enum)
add_definitions(-fsanitize=float-divide-by-zero)
add_definitions(-fsanitize=float-cast-overflow)
add_definitions(-fsanitize=integer-divide-by-zero)
add_definitions(-fsanitize=leak)
# add_definitions(-fsanitize=kernel-address)
add_definitions(-fsanitize=nonnull-attribute)
add_definitions(-fsanitize=null)
add_definitions(-fsanitize=object-size)
add_definitions(-fsanitize=pointer-compare)
add_definitions(-fsanitize=pointer-overflow)
add_definitions(-fsanitize=pointer-subtract)
add_definitions(-fsanitize=return)
add_definitions(-fsanitize=returns-nonnull-attribute)
add_definitions(-fsanitize=signed-integer-overflow)
add_definitions(-fsanitize=shift)
add_definitions(-fsanitize=shift-base)
add_definitions(-fsanitize=shift-exponent)
# add_definitions(-fsanitize=thread)
add_definitions(-fsanitize=undefined)
add_definitions(-fsanitize=unreachable)
add_definitions(-fsanitize=vla-bound)
add_definitions(-fsanitize=vptr)
endif ()
ENDMACRO()
10 changes: 10 additions & 0 deletions spec/cpp/cmake/FindHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MACRO(SUBDIRLIST result curdir)
FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
SET(dirlist "")
FOREACH (child ${children})
IF (IS_DIRECTORY ${curdir}/${child} AND EXISTS ${curdir}/${child}/CMakeLists.txt)
LIST(APPEND dirlist ${child})
ENDIF ()
ENDFOREACH ()
SET(${result} ${dirlist})
ENDMACRO()
50 changes: 50 additions & 0 deletions spec/cpp/cmake/FindProtobufHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#================================================================#
# Protobuf code generation
#================================================================#
find_package(Protobuf REQUIRED)

#include_directories(protos)

#file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} PROTOMODEL_PATH)
#file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${PROTOBUF_GENERATED_PATH} PROTOBINDING_PATH)

MACRO(GENERATE_PROTO proto)
file(TO_NATIVE_PATH ${proto} proto_native)
message(STATUS "Generating Protobuf ${proto}")
execute_process(
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
# --proto_path=${PROTOMODEL_PATH}
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}
# --cpp_out=${PROTOBINDING_PATH} ${proto_native}
--cpp_out=${CMAKE_CURRENT_SOURCE_DIR} ${proto_native}
RESULT_VARIABLE rv
)

execute_process(
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
# --proto_path=${PROTOMODEL_PATH}
--proto_path=${CMAKE_CURRENT_SOURCE_DIR}
# --grpc_out=${PROTOBINDING_PATH}
--grpc_out=${CMAKE_CURRENT_SOURCE_DIR}
--plugin=protoc-gen-grpc=${GRPC_PLUGIN_CPP} ${proto_native}
RESULT_VARIABLE rv
)
ENDMACRO()

MACRO(target_link_libraries_proto name)
# find_package(gRPC CONFIG REQUIRED)
# find_package(Protobuf REQUIRED)
target_link_libraries(
${name}
protobuf
pthread
# gRPC::grpc++
grpc++
grpc++_reflection
dl
m
rt
${_GRPC_GRPCPP_UNSECURE}
${_PROTOBUF_LIBPROTOBUF}
)
ENDMACRO()
42 changes: 42 additions & 0 deletions spec/cpp/cmake/FindTestHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
MACRO(ADD_TEST_ALL_FILES asan)
get_filename_component(test_name ${CMAKE_CURRENT_SOURCE_DIR} NAME)

if (${asan} STREQUAL "ON")
message(STATUS "Adding address sanitizer to ${test_name}")
ADD_TEST_DEFINITIONS()
else ()
message(STATUS "No address sanitizer to ${test_name}")
endif ()
message(STATUS "Creating test ${test_name}")

file(GLOB_RECURSE test_sources *)
list(FILTER test_sources INCLUDE REGEX "cpp$")

set(lib_name "${PROJECT_NAME}_${PROJECT_VERSION}")

add_executable(
"${test_name}_test"
${test_sources}
)

target_link_libraries(
"${test_name}_test"
${lib_name}
gtest
gtest_main
${CMAKE_THREAD_LIBS_INIT}
)

if (${asan} STREQUAL "ON")
target_link_libraries("${test_name}_test" asan)
endif ()

target_link_libraries("${test_name}_test" ${lib_name})
include_directories(${CMAKE_SOURCE_DIR}/src)

message(STATUS "Adding test: ${test_name}_test")
add_test(NAME "${test_name}_test_caller" COMMAND "${test_name}_test")

target_compile_options("${test_name}_test" PRIVATE --coverage)
target_link_libraries("${test_name}_test" --coverage)
ENDMACRO()
29 changes: 14 additions & 15 deletions spec/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
find_package(ProtobufHelper REQUIRED)

file(GLOB_RECURSE project_sources *)
list(FILTER project_sources EXCLUDE REGEX "main.*\\.cpp$")
list(FILTER project_sources INCLUDE REGEX "pp$")
list(FILTER project_sources EXCLUDE REGEX "main.*\\.cc$")
list(FILTER project_sources EXCLUDE REGEX "\\.txt$")
list(FILTER project_sources EXCLUDE REGEX "\\.h$")
list(FILTER project_sources EXCLUDE REGEX "\\.gitignore$")
list(FILTER project_sources EXCLUDE REGEX "Makefile$")
list(FILTER project_sources EXCLUDE REGEX "\\.proto$")
#list(FILTER project_sources INCLUDE REGEX "pp$")

set(lib_name "${PROJECT_NAME}_${PROJECT_VERSION}")
message(STATUS "${lib_name} - ${project_sources}")
add_library(${lib_name} ${project_sources} utils/Pointer.hpp)
add_library(${lib_name} ${project_sources})
target_link_libraries_proto(${lib_name})
#set_source_files_properties(${project_sources} PROPERTIES LANGUAGE CXX)
set_target_properties(${lib_name} PROPERTIES LINKER_LANGUAGE CXX)

add_executable(app_test main.cpp ${project_sources})

target_link_libraries_proto(app_test)
target_link_libraries(
app_test
${lib_name}
protobuf
grpc++
grpc++_reflection
dl
pthread
)

add_subdirectory(p2p)
add_subdirectory(bftevents-grpc)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L/usr/local/lib")
Expand All @@ -31,14 +37,7 @@ add_executable(
${project_sources}
)

target_link_libraries(
app_RPCtest
protobuf
pthread
grpc++
grpc++_reflection
dl
)
target_link_libraries_proto(app_RPCtest)

if (build_type STREQUAL "debug")
# Fix asan
Expand Down
3 changes: 0 additions & 3 deletions spec/cpp/src/bftevents-grpc/.gitignore

This file was deleted.

Loading

0 comments on commit c810658

Please sign in to comment.