From 3757618462c60f99d4b6a7ccb887b6eb0451aea4 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sat, 16 Sep 2023 15:36:58 -0300 Subject: [PATCH 1/7] Define main project name in a separate file --- CMakeLists.txt | 25 +++++++++++---------- cmake/Info.cmake | 6 ++++++ cmake/StandardSettings.cmake | 4 ++-- standalone/CMakeLists.txt | 16 ++++++-------- test/CMakeLists.txt | 42 ++++++++++++++++-------------------- 5 files changed, 44 insertions(+), 49 deletions(-) create mode 100644 cmake/Info.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 620d87b..ca77f32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -184,14 +182,15 @@ if (PROJECT_IS_TOP_LEVEL AND ${PROJECT_NAME_UPPERCASE}_ENABLE_STANDALONE) 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() diff --git a/cmake/Info.cmake b/cmake/Info.cmake new file mode 100644 index 0000000..d86c1f8 --- /dev/null +++ b/cmake/Info.cmake @@ -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 App) +set(TEST_PROJECT_NAME TestRunner) \ No newline at end of file diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 4adaad2..8f9f6ee 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -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}_ENABLE_STANDALONE "Build and run the standalone target" ON) # --------------------------------------------------------------------------- # Packing options # --------------------------------------------------------------------------- diff --git a/standalone/CMakeLists.txt b/standalone/CMakeLists.txt index 72fd7ca..4a1d0f0 100644 --- a/standalone/CMakeLists.txt +++ b/standalone/CMakeLists.txt @@ -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 ---- @@ -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 ---- @@ -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 ${PROJECT_NAME}) -target_link_libraries(${PROJECT_NAME} ${MODULE_NAME} cxxopts) +target_link_libraries(${PROJECT_NAME} ${MAIN_PROJECT_NAME}::${MAIN_PROJECT_NAME} cxxopts) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 323864a..8e1bb86 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) @@ -33,9 +27,9 @@ CPMAddPackage( CPMAddPackage("gh:TheLartians/Format.cmake@1.7.3") 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() @@ -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() # ###################################################################################################################### @@ -79,7 +73,7 @@ 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() @@ -87,11 +81,11 @@ 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}.") From 3a202d23593b652c94e1dfeb4d08c845d5fffe96 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sat, 16 Sep 2023 15:46:12 -0300 Subject: [PATCH 2/7] renamming.... --- cmake/Info.cmake | 2 +- standalone/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/Info.cmake b/cmake/Info.cmake index d86c1f8..dabb131 100644 --- a/cmake/Info.cmake +++ b/cmake/Info.cmake @@ -2,5 +2,5 @@ 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 App) +set(STANDALONE_PROJECT_NAME Standalone) set(TEST_PROJECT_NAME TestRunner) \ No newline at end of file diff --git a/standalone/CMakeLists.txt b/standalone/CMakeLists.txt index 4a1d0f0..e0521fc 100644 --- a/standalone/CMakeLists.txt +++ b/standalone/CMakeLists.txt @@ -35,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 ${PROJECT_NAME}) +set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 23 OUTPUT_NAME ${MAIN_PROJECT_NAME}) target_link_libraries(${PROJECT_NAME} ${MAIN_PROJECT_NAME}::${MAIN_PROJECT_NAME} cxxopts) From b25c859f90ea8b15a3955c1f059e73e0826057f3 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sat, 16 Sep 2023 15:59:20 -0300 Subject: [PATCH 3/7] Fixing... --- cmake/StandardSettings.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 8f9f6ee..3f4ae69 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -13,7 +13,7 @@ option(${PROJECT_NAME_UPPERCASE}_VERBOSE_OUTPUT "Enable verbose output, allowing 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}_ENABLE_STANDALONE "Build and run the standalone target" OFF) # --------------------------------------------------------------------------- # Packing options # --------------------------------------------------------------------------- From 8adfebf65787c10d50871c1fb939e2a22d670099 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sat, 16 Sep 2023 16:07:00 -0300 Subject: [PATCH 4/7] exporting symbols --- cmake/StandardSettings.cmake | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 3f4ae69..8437112 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -69,6 +69,9 @@ if (UNIX) add_compile_options("$<$:-D_DEBUG>") endif (UNIX) +if(MSVC) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) +endif() ############################################################################### # RPATH business From 0e0acdd1f1b763c781a98b99b060e272299f4cc2 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sun, 17 Sep 2023 13:06:32 -0300 Subject: [PATCH 5/7] For Windows, it is necessary to link with the MultiThreaded library. --- CMakeLists.txt | 2 +- cmake/StandardSettings.cmake | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca77f32..6de291d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -178,7 +178,7 @@ 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() diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 8437112..93fce84 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -13,7 +13,7 @@ option(${PROJECT_NAME_UPPERCASE}_VERBOSE_OUTPUT "Enable verbose output, allowing 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" OFF) +option(${PROJECT_NAME_UPPERCASE}_BUILD_EXECUTABLE "Build and run a standalone executable target" OFF) # --------------------------------------------------------------------------- # Packing options # --------------------------------------------------------------------------- @@ -70,7 +70,8 @@ if (UNIX) endif (UNIX) if(MSVC) - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + #set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() ############################################################################### From 6c36ff194a199a34972fd8b7d1d91db05a6fa17c Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sun, 17 Sep 2023 13:32:32 -0300 Subject: [PATCH 6/7] Windows linking problems --- .github/workflows/standalone.yml | 2 +- CMakeLists.txt | 2 +- cmake/StandardSettings.cmake | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/standalone.yml b/.github/workflows/standalone.yml index 38c5220..65966c9 100644 --- a/.github/workflows/standalone.yml +++ b/.github/workflows/standalone.yml @@ -36,4 +36,4 @@ jobs: run: cmake --build build -j4 - name: run - run: ./build/bin/${APP_NAME} + run: ./build/bin/$APP_NAME diff --git a/CMakeLists.txt b/CMakeLists.txt index 6de291d..b1e1b43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,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) diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 93fce84..a3d39c8 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -70,7 +70,7 @@ if (UNIX) endif (UNIX) if(MSVC) - #set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif() From bfec63b911e2b49b25904ca546a2a3f9e7f37ce9 Mon Sep 17 00:00:00 2001 From: Diego Saraiva Date: Sun, 17 Sep 2023 13:45:33 -0300 Subject: [PATCH 7/7] Updating vars --- .github/workflows/standalone.yml | 2 +- cmake/StandardSettings.cmake | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/standalone.yml b/.github/workflows/standalone.yml index 65966c9..e7e9401 100644 --- a/.github/workflows/standalone.yml +++ b/.github/workflows/standalone.yml @@ -30,7 +30,7 @@ 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 diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index a3d39c8..613713f 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -70,7 +70,6 @@ if (UNIX) endif (UNIX) if(MSVC) - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") endif()