Skip to content

Commit

Permalink
Merge pull request #44 from MissouriMRDT/hotfix/cmake-cleanup
Browse files Browse the repository at this point in the history
CMake Cleanup and Change C++ Version to C++20
  • Loading branch information
ClayJay3 authored Oct 7, 2024
2 parents 13aa010 + 109fe69 commit a498312
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
cd /opt/RoveComm_CPP/
mkdir build
cd build
cmake ..
cmake -DBUILD_TESTS_MODE=ON ..
make
- name: Run Unit Tests
Expand Down
209 changes: 126 additions & 83 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
cmake_minimum_required(VERSION 3.24.3)

## C++ Version
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

## C++ Compiler Flags.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -std=c++20")

## Project Name and Software Version Number
project(RoveComm_CPP VERSION 3.00.00)
project(RoveComm_CPP VERSION 3.00.00 LANGUAGES CXX)

## Include Required CMake Packages
if(NOT CPack_CMake_INCLUDED)
Expand All @@ -27,7 +26,21 @@ if(NOT CTest_CMake_INCLUDED)
include(CTest)
endif()

# Check if the project is standalone or within another project.
####################################################################################################################
## Options ##
####################################################################################################################

## Enable or Disable cross-compile for windows on linux.
option(BUILD_WIN "Windows Cross-Compile Mode" OFF)

## Enable or Disable Tests Mode
option(BUILD_TESTS_MODE "Enable Tests Mode" OFF)

####################################################################################################################
## Configuration Based on Options ##
####################################################################################################################

## Check if the project is standalone or within another project.
if(NOT DEFINED __ROVECOMM_LIBRARY_MODE__)
if(PROJECT_IS_TOP_LEVEL)
set(__ROVECOMM_LIBRARY_MODE__ 0)
Expand All @@ -36,8 +49,7 @@ if(NOT DEFINED __ROVECOMM_LIBRARY_MODE__)
endif()
endif()

# Check if we are on Windows or Linux or for windows mode if cross-compiling.
option(BUILD_WIN "Windows Cross-Compile Mode" OFF)
## Check if we are on Windows or Linux or for windows mode if cross-compiling.
if(NOT DEFINED __ROVECOMM_WINDOWS_MODE__)
if(WIN32 OR BUILD_WIN)
set(__ROVECOMM_WINDOWS_MODE__ 1)
Expand All @@ -47,11 +59,20 @@ if(NOT DEFINED __ROVECOMM_WINDOWS_MODE__)
endif()
endif()

## Build Unit and Integration Tests
if (BUILD_TESTS_MODE)
enable_testing()
endif()

message("-- RoveComm Build Mode: ${__ROVECOMM_LIBRARY_MODE__}")
add_definitions(-D__ROVECOMM_LIBRARY_MODE__=${__ROVECOMM_LIBRARY_MODE__})
message("-- RoveComm Windows Mode: ${__ROVECOMM_WINDOWS_MODE__}")
add_definitions(-D__ROVECOMM_WINDOWS_MODE__=${__ROVECOMM_WINDOWS_MODE__})

####################################################################################################################
## Cross-Compile Mode ##
####################################################################################################################

## Detect if we are cross-compiling or should compile for windows.
if(__ROVECOMM_WINDOWS_MODE__)
message("-- RoveComm_CPP -- Cross-compiling for Windows on Linux")
Expand All @@ -77,42 +98,41 @@ if(__ROVECOMM_WINDOWS_MODE__)
## Find Eigen3.
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

## ExternalProject configuration for pulling and building GTest
ExternalProject_Add(GTest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2 # Specify the GTest version tag or commit
PREFIX ${CMAKE_BINARY_DIR}/gtest
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/gtest_install
-DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc-posix
-DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++-posix
-DCMAKE_SYSTEM_NAME=Windows
)

## Create an imported target for GTest
add_library(GTest::gtest IMPORTED INTERFACE)
add_library(GTest::gtest_main IMPORTED INTERFACE)

## Specify where to find GTest once they're built
ExternalProject_Get_Property(GTest install_dir)
set(GTEST_INCLUDE_DIR ${install_dir}_install/include)
set(GTEST_LIB_DIR ${install_dir}_install/lib)

## Must create empty install dirs.
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/gtest_install/include)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/gtest_install/lib)

## Set the imported target properties for GTest
set_target_properties(GTest::gtest PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES ${GTEST_LIB_DIR}/libgtest.a
)
set_target_properties(GTest::gtest_main PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES ${GTEST_LIB_DIR}/libgtest_main.a
)

## Enable to CTest and Google Test Frameworks
enable_testing()
if (BUILD_TESTS_MODE)
## ExternalProject configuration for pulling and building GTest
ExternalProject_Add(GTest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2 # Specify the GTest version tag or commit
PREFIX ${CMAKE_BINARY_DIR}/gtest
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/gtest_install
-DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc-posix
-DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++-posix
-DCMAKE_SYSTEM_NAME=Windows
)

## Create an imported target for GTest
add_library(GTest::gtest IMPORTED INTERFACE)
add_library(GTest::gtest_main IMPORTED INTERFACE)

## Specify where to find GTest once they're built
ExternalProject_Get_Property(GTest install_dir)
set(GTEST_INCLUDE_DIR ${install_dir}_install/include)
set(GTEST_LIB_DIR ${install_dir}_install/lib)

## Must create empty install dirs.
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/gtest_install/include)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/gtest_install/lib)

## Set the imported target properties for GTest
set_target_properties(GTest::gtest PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES ${GTEST_LIB_DIR}/libgtest.a
)
set_target_properties(GTest::gtest_main PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES ${GTEST_LIB_DIR}/libgtest_main.a
)
endif()
else()
message("-- RoveComm_CPP -- LIBRARY MODE")
endif()
Expand All @@ -122,24 +142,25 @@ else()
if(PROJECT_IS_TOP_LEVEL)
message("-- RoveComm_CPP -- STAND ALONE MODE")

## Find Google Test
find_package(GTest CONFIG REQUIRED)
include(GoogleTest)
add_library(GTest::GTest INTERFACE IMPORTED)
target_link_libraries(GTest::GTest INTERFACE gtest_main)
if (BUILD_TESTS_MODE)
## Find Google Test
find_package(GTest CONFIG REQUIRED)
include(GoogleTest)
add_library(GTest::GTest INTERFACE IMPORTED)
target_link_libraries(GTest::GTest INTERFACE gtest_main)
endif()

## Find Eigen3.
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

## Enable to CTest and Google Test Frameworks
enable_testing()
else()
message("-- RoveComm_CPP -- LIBRARY MODE")
endif()
endif()

## Set ThreadPool Variables
add_compile_definitions(BS_THREAD_POOL_ENABLE_PAUSE=1)

####################################################################################################################
## CPack Setup ##
####################################################################################################################

## Define Project Name and Version Number for CPack
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
Expand All @@ -150,6 +171,13 @@ set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/data/CPACK/logo.ico")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
set(CPACK_GENERATOR "STGZ")

####################################################################################################################
## Dependencies and Libraries ##
####################################################################################################################

## Set ThreadPool Variables
add_compile_definitions(BS_THREAD_POOL_ENABLE_PAUSE=1)

## Search Project Directories for Header and Source Files
file(GLOB_RECURSE RoveComm_INCLUDE CONFIGURE_DEPENDS "src/RoveComm/*.h")
file(GLOB_RECURSE RoveComm_SRC CONFIGURE_DEPENDS "src/RoveComm/*.cpp")
Expand All @@ -158,7 +186,7 @@ file(GLOB_RECURSE RoveComm_SRC CONFIGURE_DEPENDS "src/RoveComm/*.cpp")
add_library(${PROJECT_NAME} STATIC ${RoveComm_INCLUDE} ${RoveComm_SRC})

## Configure the directories to search for header files.
target_include_directories(${PROJECT_NAME} PRIVATE src/RoveComm)
target_include_directories(${PROJECT_NAME} PUBLIC src/RoveComm)

## Set the version property.
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
Expand All @@ -172,6 +200,9 @@ set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${PROJECT_VERSION_MAJ
## For access to standard installation directory variables (CMAKE_INSTALL_xDIR).
include(GNUInstallDirs)

####################################################################################################################
## Installation ##
####################################################################################################################
## Set library shared object and API header file to install.
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand All @@ -198,7 +229,13 @@ else()
)
endif()



if(PROJECT_IS_TOP_LEVEL)
####################################################################################################################
## Build Executable ##
####################################################################################################################

## Find all main source files
file(GLOB Main_SRC CONFIGURE_DEPENDS "src/*.cpp")

Expand All @@ -222,37 +259,43 @@ if(PROJECT_IS_TOP_LEVEL)
target_link_libraries(${PROJECT_NAME}_App PRIVATE RoveComm_CPP)
endif()

## Find all test source files
file(GLOB UnitTests_SRC CONFIGURE_DEPENDS "tests/Unit/**/*.cc")
file(GLOB IntegrationTests_SRC CONFIGURE_DEPENDS "tests/Integration/**/*.cc")

## Count the number of tests found for each type
list(LENGTH UnitTests_SRC UnitTests_LEN)
list(LENGTH IntegrationTests_SRC IntegrationTests_LEN)

## Create Unit Tests if found
if (UnitTests_LEN GREATER 0)
add_executable(${PROJECT_NAME}_UnitTests "tests/Unit/main.cc" "tests/TestUtils.cc" ${UnitTests_SRC})
if (__ROVECOMM_WINDOWS_MODE__)
## Ensure GTest are built before project
add_dependencies(${PROJECT_NAME}_UnitTests GTest)
target_link_libraries(${PROJECT_NAME}_UnitTests RoveComm_CPP ws2_32 GTest::gtest GTest::gtest_main)
else()
target_link_libraries(${PROJECT_NAME}_UnitTests GTest::gtest GTest::gtest_main RoveComm_CPP)
####################################################################################################################
## Tests ##
####################################################################################################################

if (BUILD_TESTS_MODE)
## Find all test source files
file(GLOB UnitTests_SRC CONFIGURE_DEPENDS "tests/Unit/**/*.cc")
file(GLOB IntegrationTests_SRC CONFIGURE_DEPENDS "tests/Integration/**/*.cc")

## Count the number of tests found for each type
list(LENGTH UnitTests_SRC UnitTests_LEN)
list(LENGTH IntegrationTests_SRC IntegrationTests_LEN)

## Create Unit Tests if found
if (UnitTests_LEN GREATER 0)
add_executable(${PROJECT_NAME}_UnitTests "tests/Unit/main.cc" "tests/TestUtils.cc" ${UnitTests_SRC})
if (__ROVECOMM_WINDOWS_MODE__)
## Ensure GTest are built before project
add_dependencies(${PROJECT_NAME}_UnitTests GTest)
target_link_libraries(${PROJECT_NAME}_UnitTests RoveComm_CPP ws2_32 GTest::gtest GTest::gtest_main)
else()
target_link_libraries(${PROJECT_NAME}_UnitTests GTest::gtest GTest::gtest_main RoveComm_CPP)
endif()
add_test(Unit_Tests ${PROJECT_NAME}_UnitTests)
endif()
add_test(Unit_Tests ${PROJECT_NAME}_UnitTests)
endif()

## Create Integration Tests if found
if (IntegrationTests_LEN GREATER 0)
add_executable(${PROJECT_NAME}_IntegrationTests "tests/Unit/main.cc" "tests/TestUtils.cc" ${IntegrationTests_SRC})
if (__ROVECOMM_WINDOWS_MODE__)
## Ensure GTest are built before project
add_dependencies(${PROJECT_NAME}_IntegrationTests GTest)
target_link_libraries(${PROJECT_NAME}_IntegrationTests RoveComm_CPP ws2_32 GTest::gtest GTest::gtest_main)
else()
target_link_libraries(${PROJECT_NAME}_IntegrationTests GTest::gtest GTest::gtest_main RoveComm_CPP)
## Create Integration Tests if found
if (IntegrationTests_LEN GREATER 0)
add_executable(${PROJECT_NAME}_IntegrationTests "tests/Unit/main.cc" "tests/TestUtils.cc" ${IntegrationTests_SRC})
if (__ROVECOMM_WINDOWS_MODE__)
## Ensure GTest are built before project
add_dependencies(${PROJECT_NAME}_IntegrationTests GTest)
target_link_libraries(${PROJECT_NAME}_IntegrationTests RoveComm_CPP ws2_32 GTest::gtest GTest::gtest_main)
else()
target_link_libraries(${PROJECT_NAME}_IntegrationTests GTest::gtest GTest::gtest_main RoveComm_CPP)
endif()
add_test(Integration_Tests ${PROJECT_NAME}_IntegrationTests)
endif()
add_test(Integration_Tests ${PROJECT_NAME}_IntegrationTests)
endif()
endif()

0 comments on commit a498312

Please sign in to comment.