Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/standalone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}

- name: configure
run: cmake -Bbuild -D${PROJECT_NAME}_ENABLE_STANDALONE=1 -DCMAKE_BUILD_TYPE=$BUILD_TYPE
run: cmake -Bbuild -D${PROJECT_NAME}_BUILD_EXECUTABLE=1 -DCMAKE_BUILD_TYPE=$BUILD_TYPE

- name: build
run: cmake --build build -j4

- name: run
run: ./build/bin/${APP_NAME}
run: ./build/bin/$APP_NAME
29 changes: 14 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ endif()
# Make sure that custom modules are found
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# ######################################################################################################################
# Define the Project Name and Description
# ######################################################################################################################
project(
modern_cpp_project
VERSION 1.0.0
LANGUAGES CXX
DESCRIPTION "ModernCppProject is a project template for Modern C++"
)

#
include(CMakeDependentOption)
include(CheckCXXCompilerFlag)
include(CheckTypeSize)
include(CMakePrintHelpers)
#

include(cmake/Info.cmake)
# ######################################################################################################################
# Define the Project Name and Description
# ######################################################################################################################
project(${MAIN_PROJECT_NAME} VERSION 1.0.0 LANGUAGES CXX DESCRIPTION ${MAIN_PROJECT_DESCRIPTION})

option(${PROJECT_NAME}_USE_ALT_NAMES "Use alternative names for the project, such as naming the include directory all lowercase." ON)
if(${PROJECT_NAME}_USE_ALT_NAMES)
Expand All @@ -48,7 +46,7 @@ include(cmake/CompilerWarnings.cmake)
if(PROJECT_IS_TOP_LEVEL)
# Put the libraries and binaries that get built into directories at the top of
# the build tree rather than in hard-to-find leaf directories. This simplifies
# manual testing and the use of the build tre.
# manual testing and the use of the build tree.
set(MAINFOLDER ${PROJECT_SOURCE_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down Expand Up @@ -180,18 +178,19 @@ if (PROJECT_IS_TOP_LEVEL AND ${PROJECT_NAME_UPPERCASE}_ENABLE_TESTING)
add_subdirectory(test)
endif()

if (PROJECT_IS_TOP_LEVEL AND ${PROJECT_NAME_UPPERCASE}_ENABLE_STANDALONE)
if (PROJECT_IS_TOP_LEVEL AND ${PROJECT_NAME_UPPERCASE}_BUILD_EXECUTABLE)
add_subdirectory(standalone)
endif()


# ######################################################################################################################
# INFO
# ######################################################################################################################
if(PROJECT_IS_TOP_LEVEL)
message(STATUS "CMake ${CMAKE_VERSION} successfully configured ${PROJECT_NAME} using ${CMAKE_GENERATOR} generator")
message(STATUS "${PROJECT_NAME} package version: ${CMAKE_PROJECT_VERSION}")
message(STATUS "${PROJECT_NAME} package dependencies: ${${PROJECT_NAME}_DEPENDENCIES}")
message(STATUS "${PROJECT_NAME} shared libraries: ${${PROJECT_NAME_UPPERCASE}_BUILD_SHARED_LIBS}")
message(STATUS "[${PROJECT_NAME_LOWERCASE}] package version: ${CMAKE_PROJECT_VERSION}")
message(STATUS "[${PROJECT_NAME_LOWERCASE}] package dependencies: ${${PROJECT_NAME}_DEPENDENCIES}")
message(STATUS "[${PROJECT_NAME_LOWERCASE}] shared libraries: ${${PROJECT_NAME_UPPERCASE}_BUILD_SHARED_LIBS}")
if(${BUILD_SHARED_LIBS})
message(STATUS "Building dynamic libraries")
else()
Expand Down
6 changes: 6 additions & 0 deletions cmake/Info.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Note: update this to your new project's name and version
set(MAIN_PROJECT_NAME modern_cpp_project)
set(MAIN_PROJECT_DESCRIPTION "ModernCppProject is a project template for Modern C++")
set(MAIN_CXX_VERSION 23)
set(STANDALONE_PROJECT_NAME Standalone)
set(TEST_PROJECT_NAME TestRunner)
7 changes: 5 additions & 2 deletions cmake/StandardSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ option(${PROJECT_NAME_UPPERCASE}_ENABLE_CCACHE "Enable the usage of Ccache, in o
option(${PROJECT_NAME_UPPERCASE}_VERBOSE_OUTPUT "Enable verbose output, allowing for a better understanding of each step taken." ON)
option(${PROJECT_NAME_UPPERCASE}_GENERATE_EXPORT_HEADER "Create a `project_export.h` file containing all exported symbols." OFF)
option(${PROJECT_NAME_UPPERCASE}_THREAD_PREFER_PTHREAD "prefer pthread library on system with multiple thread libraries available." ON)
option(${PROJECT_NAME_UPPERCASE}_WARNINGS_AS_ERRORS, "Make all warnings into errors." OFF)
option(${PROJECT_NAME_UPPERCASE}_ENABLE_STANDALONE, "Build and run the standalone target" ON)
option(${PROJECT_NAME_UPPERCASE}_WARNINGS_AS_ERRORS "Make all warnings into errors." OFF)
option(${PROJECT_NAME_UPPERCASE}_BUILD_EXECUTABLE "Build and run a standalone executable target" OFF)
# ---------------------------------------------------------------------------
# Packing options
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -69,6 +69,9 @@ if (UNIX)
add_compile_options("$<$<CONFIG:DEBUG>:-D_DEBUG>")
endif (UNIX)

if(MSVC)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()

###############################################################################
# RPATH business
Expand Down
16 changes: 6 additions & 10 deletions standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
cmake_minimum_required(VERSION 3.22)

if (${CMAKE_PROJECT_NAME})
set(MODULE_NAME ${CMAKE_PROJECT_NAME})
else()
set(MODULE_NAME modern_cpp_project)
endif()
include(../cmake/Info.cmake)

project(Standalone LANGUAGES CXX)
project(${STANDALONE_PROJECT_NAME} LANGUAGES CXX)

# --- Import tools ----

Expand All @@ -28,9 +24,9 @@ CPMAddPackage(
)

if(STANDALONE_USE_INSTALLED_VERSION)
find_package(${MODULE_NAME} REQUIRED)
find_package(${MAIN_PROJECT_NAME} REQUIRED)
elseif(PROJECT_IS_TOP_LEVEL)
CPMAddPackage(NAME ${MODULE_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
CPMAddPackage(NAME ${MAIN_PROJECT_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
endif()

# ---- Create standalone executable ----
Expand All @@ -39,6 +35,6 @@ file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)

add_executable(${PROJECT_NAME} ${sources})

set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 23 OUTPUT_NAME ${MODULE_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 23 OUTPUT_NAME ${MAIN_PROJECT_NAME})

target_link_libraries(${PROJECT_NAME} ${MODULE_NAME} cxxopts)
target_link_libraries(${PROJECT_NAME} ${MAIN_PROJECT_NAME}::${MAIN_PROJECT_NAME} cxxopts)
42 changes: 18 additions & 24 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
cmake_minimum_required(VERSION 3.22)

if (${CMAKE_PROJECT_NAME})
set(MODULE_NAME ${CMAKE_PROJECT_NAME})
else()
set(MODULE_NAME modern_cpp_project)
endif()

set(TEST_SUITE ${MODULE_NAME}TestRunner)
project(${TEST_SUITE} LANGUAGES CXX)
include (../cmake/Info.cmake)
project(${TEST_PROJECT_NAME} LANGUAGES CXX)

message(STATUS "Started ${TEST_SUITE} Test Suite for ${CMAKE_PROJECT_NAME}...\n")
message(STATUS "Started ${PROJECT_NAME} Test Suite for ${CMAKE_PROJECT_NAME}...\n")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


Expand All @@ -33,9 +27,9 @@ CPMAddPackage(
CPMAddPackage("gh:TheLartians/[email protected]")

if(TEST_USE_INSTALLED_VERSION)
find_package(${MODULE_NAME} REQUIRED)
find_package(${MAIN_PROJECT_NAME} REQUIRED)
elseif(PROJECT_IS_TOP_LEVEL)
CPMAddPackage(NAME ${MODULE_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
CPMAddPackage(NAME ${MAIN_PROJECT_NAME} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
# needed to generate test target
enable_testing()
endif()
Expand All @@ -44,27 +38,27 @@ endif()
# Adding unit test files
# ######################################################################################################################
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
verbose_message("Adding ${TEST_SOURCES} to ${TEST_SUITE} test suite.")
verbose_message("Adding ${TEST_SOURCES} to ${PROJECT_NAME} test suite.")

# ######################################################################################################################
# Create binary
# ######################################################################################################################
add_executable(${TEST_SUITE} ${TEST_SOURCES})
target_compile_features(${TEST_SUITE} PRIVATE cxx_std_23)
target_link_libraries(${TEST_SUITE} doctest::doctest ${MODULE_NAME})
add_executable(${PROJECT_NAME} ${TEST_SOURCES})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_23)
target_link_libraries(${PROJECT_NAME} doctest::doctest ${MAIN_PROJECT_NAME}::${MAIN_PROJECT_NAME})

# ######################################################################################################################
# Enable compiler warnings
# ######################################################################################################################
if(NOT TEST_USE_INSTALLED_VERSION)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(${MODULE_NAME} PUBLIC -Wall -Wpedantic -Wextra -Werror)
target_compile_options(${MAIN_PROJECT_NAME} PUBLIC -Wall -Wpedantic -Wextra -Werror)
elseif(MSVC)
target_compile_options(${MODULE_NAME} PUBLIC /W4 /WX)
target_compile_definitions(${TEST_SUITE} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
target_compile_options(${MAIN_PROJECT_NAME} PUBLIC /W4 /WX)
target_compile_definitions(${PROJECT_NAME} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
endif()

target_compile_definitions(${TEST_SUITE} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
target_compile_definitions(${PROJECT_NAME} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
endif()

# ######################################################################################################################
Expand All @@ -79,19 +73,19 @@ if(NOT doctest_SOURCE_DIR)
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
else()
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
doctest_discover_tests(${TEST_SUITE})
doctest_discover_tests(${PROJECT_NAME})
endif()



# ######################################################################################################################
# Setup code coverage if enabled
# ######################################################################################################################
if (${MODULE_NAME}_ENABLE_CODE_COVERAGE OR ENABLE_CODE_COVERAGE)
if (${MAIN_PROJECT_NAME}_ENABLE_CODE_COVERAGE OR ENABLE_CODE_COVERAGE)
# ---- code coverage ----
target_compile_options(${MODULE_NAME} PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(${MODULE_NAME} PUBLIC -fprofile-arcs -ftest-coverage)
target_compile_options(${MAIN_PROJECT_NAME} PUBLIC -O0 -g -fprofile-arcs -ftest-coverage)
target_link_options(${MAIN_PROJECT_NAME} PUBLIC -fprofile-arcs -ftest-coverage)
endif()


verbose_message("Finished adding unit tests for ${MODULE_NAME}.")
verbose_message("Finished adding unit tests for ${MAIN_PROJECT_NAME}.")