From c96cd54d383a77b41e9a3c7a465b7ad5ebf38128 Mon Sep 17 00:00:00 2001 From: Oleksii Skidan Date: Mon, 28 Mar 2022 21:24:44 +0300 Subject: [PATCH 01/17] Add support for macOS X host to CMakePresets.json --- CMakePresets.json | 55 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 5a4f2ebf..24d22b0c 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -53,6 +53,17 @@ } } }, + { + "name": "conf-darwin-common", + "description": "Darwin (macOS) settings for AppleClang tooolchain", + "hidden": true, + "inherits": "conf-common", + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + } + }, { "name": "windows-msvc-debug-developer-mode", "displayName": "msvc Debug (Developer Mode)", @@ -75,7 +86,7 @@ "CMAKE_CXX_COMPILER": "cl", "CMAKE_BUILD_TYPE": "RelWithDebInfo", "ENABLE_DEVELOPER_MODE": "ON" - } + } }, { "name": "windows-msvc-debug-user-mode", @@ -87,7 +98,7 @@ "CMAKE_CXX_COMPILER": "cl", "CMAKE_BUILD_TYPE": "Debug", "ENABLE_DEVELOPER_MODE": "OFF" - } + } }, { "name": "windows-msvc-release-user-mode", @@ -99,7 +110,7 @@ "CMAKE_CXX_COMPILER": "cl", "CMAKE_BUILD_TYPE": "RelWithDebInfo", "ENABLE_DEVELOPER_MODE": "OFF" - } + } }, { "name": "windows-clang-debug", @@ -176,6 +187,28 @@ "CMAKE_CXX_COMPILER": "clang++", "CMAKE_BUILD_TYPE": "RelWithDebInfo" } + }, + { + "name": "darwin-clang-debug", + "displayName": "AppleClang Debug", + "description": "Target Darwin (macOS) with the AppleCalng compiler, debug build type", + "inherits": "conf-darwin-common", + "cacheVariables": { + "CMAKE_C_COMPILER": "clang", + "CMAKE_CXX_COMPILER": "clang++", + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "darwin-clang-release", + "displayName": "AppleClang Release", + "description": "Target Darwin (macOS) with the AppleCalng compiler, release build type", + "inherits": "conf-darwin-common", + "cacheVariables": { + "CMAKE_C_COMPILER": "clang", + "CMAKE_CXX_COMPILER": "clang++", + "CMAKE_BUILD_TYPE": "RelWithDebInfo" + } } ], "testPresets": [ @@ -246,6 +279,20 @@ "description": "Enable output and stop on failure", "inherits": "test-common", "configurePreset": "linux-clang-release" + }, + { + "name": "test-darwin-clang-debug", + "displayName": "Strict", + "description": "Enable output and stop on failure", + "inherits": "test-common", + "configurePreset": "darwin-clang-debug" + }, + { + "name": "test-darwin-clang-release", + "displayName": "Strict", + "description": "Enable output and stop on failure", + "inherits": "test-common", + "configurePreset": "darwin-clang-release" } ] -} +} \ No newline at end of file From 0f36bfe29cecc97d509da0ae227c43ff61c6b3fa Mon Sep 17 00:00:00 2001 From: Oleksii Skidan Date: Tue, 29 Mar 2022 19:39:32 +0300 Subject: [PATCH 02/17] Shared presets for Unix-like OSes. Currently the only supported Unix-like OSes are: - Linux; - Darwin-based OSes, such as macOS X. --- CMakePresets.json | 100 +++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 72 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 24d22b0c..5871489e 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -38,14 +38,17 @@ } }, { - "name": "conf-linux-common", - "description": "Linux settings for gcc and clang toolchains", + "name": "conf-unixlike-common", + "description": "Unix-like OS settings for gcc and clang toolchains", "hidden": true, "inherits": "conf-common", "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Linux" + "type": "inList", + "string": "${hostSystemName}", + "list": [ + "Linux", + "Darwin" + ] }, "vendor": { "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { @@ -53,17 +56,6 @@ } } }, - { - "name": "conf-darwin-common", - "description": "Darwin (macOS) settings for AppleClang tooolchain", - "hidden": true, - "inherits": "conf-common", - "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Darwin" - } - }, { "name": "windows-msvc-debug-developer-mode", "displayName": "msvc Debug (Developer Mode)", @@ -145,10 +137,10 @@ } }, { - "name": "linux-gcc-debug", + "name": "unixlike-gcc-debug", "displayName": "gcc Debug", - "description": "Target Linux with the gcc compiler, debug build type", - "inherits": "conf-linux-common", + "description": "Target Unix-like OS with the gcc compiler, debug build type", + "inherits": "conf-unixlike-common", "cacheVariables": { "CMAKE_C_COMPILER": "gcc", "CMAKE_CXX_COMPILER": "g++", @@ -156,10 +148,10 @@ } }, { - "name": "linux-gcc-release", + "name": "unixlike-gcc-release", "displayName": "gcc Release", - "description": "Target Linux with the gcc compiler, release build type", - "inherits": "conf-linux-common", + "description": "Target Unix-like OS with the gcc compiler, release build type", + "inherits": "conf-unixlike-common", "cacheVariables": { "CMAKE_C_COMPILER": "gcc", "CMAKE_CXX_COMPILER": "g++", @@ -167,10 +159,10 @@ } }, { - "name": "linux-clang-debug", + "name": "unixlike-clang-debug", "displayName": "clang Debug", - "description": "Target Linux with the clang compiler, debug build type", - "inherits": "conf-linux-common", + "description": "Target Unix-like OS with the clang compiler, debug build type", + "inherits": "conf-unixlike-common", "cacheVariables": { "CMAKE_C_COMPILER": "clang", "CMAKE_CXX_COMPILER": "clang++", @@ -178,32 +170,10 @@ } }, { - "name": "linux-clang-release", + "name": "unixlike-clang-release", "displayName": "clang Release", - "description": "Target Linux with the clang compiler, release build type", - "inherits": "conf-linux-common", - "cacheVariables": { - "CMAKE_C_COMPILER": "clang", - "CMAKE_CXX_COMPILER": "clang++", - "CMAKE_BUILD_TYPE": "RelWithDebInfo" - } - }, - { - "name": "darwin-clang-debug", - "displayName": "AppleClang Debug", - "description": "Target Darwin (macOS) with the AppleCalng compiler, debug build type", - "inherits": "conf-darwin-common", - "cacheVariables": { - "CMAKE_C_COMPILER": "clang", - "CMAKE_CXX_COMPILER": "clang++", - "CMAKE_BUILD_TYPE": "Debug" - } - }, - { - "name": "darwin-clang-release", - "displayName": "AppleClang Release", - "description": "Target Darwin (macOS) with the AppleCalng compiler, release build type", - "inherits": "conf-darwin-common", + "description": "Target Unix-like OS with the clang compiler, release build type", + "inherits": "conf-unixlike-common", "cacheVariables": { "CMAKE_C_COMPILER": "clang", "CMAKE_CXX_COMPILER": "clang++", @@ -253,46 +223,32 @@ "configurePreset": "windows-clang-release" }, { - "name": "test-linux-gcc-debug", - "displayName": "Strict", - "description": "Enable output and stop on failure", - "inherits": "test-common", - "configurePreset": "linux-gcc-debug" - }, - { - "name": "test-linux-gcc-release", - "displayName": "Strict", - "description": "Enable output and stop on failure", - "inherits": "test-common", - "configurePreset": "linux-gcc-release" - }, - { - "name": "test-linux-clang-debug", + "name": "test-unixlike-gcc-debug", "displayName": "Strict", "description": "Enable output and stop on failure", "inherits": "test-common", - "configurePreset": "linux-clang-debug" + "configurePreset": "unixlike-gcc-debug" }, { - "name": "test-linux-clang-release", + "name": "test-unixlike-gcc-release", "displayName": "Strict", "description": "Enable output and stop on failure", "inherits": "test-common", - "configurePreset": "linux-clang-release" + "configurePreset": "unixlike-gcc-release" }, { - "name": "test-darwin-clang-debug", + "name": "test-unixlike-clang-debug", "displayName": "Strict", "description": "Enable output and stop on failure", "inherits": "test-common", - "configurePreset": "darwin-clang-debug" + "configurePreset": "unixlike-clang-debug" }, { - "name": "test-darwin-clang-release", + "name": "test-unixlike-clang-release", "displayName": "Strict", "description": "Enable output and stop on failure", "inherits": "test-common", - "configurePreset": "darwin-clang-release" + "configurePreset": "unixlike-clang-release" } ] } \ No newline at end of file From e51378d638537aae2a3995f609860a2b13b3aac0 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 3 Apr 2022 17:48:27 +0200 Subject: [PATCH 03/17] upgrade to project_options v0.20.0 modernize the cmake build concept too: * Import your installed targets with an test to be sure it is usable. * Add support for PROJECT_IS_TOP_LEVEL cmake variable * Enable conan again as default --- CMakeLists.txt | 44 ++++++++++++++++++++++++++++++++++---------- src/CMakeLists.txt | 14 +++++--------- src/main.cpp | 3 ++- test/CMakeLists.txt | 28 ++++++++++++++++++++-------- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bb3bee9..f887d73d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.16...3.23) # Not ideal to use this global variable, but necessary to make sure # that tooling and projects use the same version @@ -20,11 +20,10 @@ set(ENABLE_DEVELOPER_MODE # Change this to false if you want to disable warnings_as_errors in developer mode set(OPT_WARNINGS_AS_ERRORS_DEVELOPER_DEFAULT TRUE) -# Add project_options v0.17.0 +# Add project_options v0.20.0 # https://github.com/cpp-best-practices/project_options include(FetchContent) -FetchContent_Declare(_project_options - URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.17.0.zip) +FetchContent_Declare(_project_options URL https://github.com/aminya/project_options/archive/refs/tags/v0.20.0.zip) FetchContent_MakeAvailable(_project_options) include(${_project_options_SOURCE_DIR}/Index.cmake) @@ -40,6 +39,18 @@ project( HOMEPAGE_URL "%%myurl%%" LANGUAGES CXX C) +# This variable is set by project() in CMake 3.21+ +string( + COMPARE EQUAL + "${CMAKE_SOURCE_DIR}" + "${PROJECT_SOURCE_DIR}" + PROJECT_IS_TOP_LEVEL) +if(PROJECT_IS_TOP_LEVEL) + # Consider the CTest module, which creates targets and options! + # Only needed if you want to enable submissions to a CDash server. + include(CTest) +endif() + set(GIT_SHA "Unknown" CACHE STRING "SHA this build was generated from") @@ -92,6 +103,7 @@ dynamic_project_options( PCH_HEADERS # This is a list of headers to pre-compile, here are some common ones + ENABLE_CONAN # CONAN_OPTIONS # Extra options to pass to conan # MSVC_WARNINGS # Override the defaults for the MSVC warnings # CLANG_WARNINGS # Override the defaults for the CLANG warnings @@ -100,6 +112,9 @@ dynamic_project_options( ) target_compile_features(project_options INTERFACE cxx_std_${CMAKE_CXX_STANDARD}) +# TODO: The INTERFACE library NAMESPACE ALIAS are missing! CK +add_library(myproject::project_options INTERFACE IMPORTED) +add_library(myproject::project_warnings INTERFACE IMPORTED) # configure files based on CMake configuration options add_subdirectory(configured_files) @@ -108,33 +123,42 @@ add_subdirectory(configured_files) add_subdirectory(src) # Adding the tests: -option(ENABLE_TESTING "Enable the tests" ON) +option(ENABLE_TESTING "Enable the tests" ${PROJECT_IS_TOP_LEVEL}) if(ENABLE_TESTING) enable_testing() - message("Building Tests. Be sure to check out test/constexpr_tests for constexpr -testing") + message(AUTHOR_WARNING "Building Tests. Be sure to check out test/constexpr_tests.cpp for constexpr testing") add_subdirectory(test) endif() option(ENABLE_FUZZING "Enable the fuzz tests" OFF) if(ENABLE_FUZZING) - message("Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html") + message(AUTHOR_WARNING "Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html") add_subdirectory(fuzz_test) endif() # If MSVC is being used, and ASAN is enabled, we need to set the debugger environment # so that it behaves well with MSVC's debugger, and we can run the target from visual studio if(MSVC) - get_all_targets(all_targets) + get_all_installable_targets(all_targets) + message("all_targets=${all_targets}") set_target_properties(${all_targets} PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=$(VC_ExecutablePath_x64);%PATH%") endif() # set the startup project for the "play" button in MSVC set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT intro) +if(CMAKE_SKIP_INSTALL_RULES) + return() +elseif(NOT PROJECT_IS_TOP_LEVEL) + return() +endif() + # Add other targets that you want installed here, be default we just package the one executable # we know we want to ship -package_project(TARGETS intro) +package_project(TARGETS intro project_options project_warnings + # FIXME: this does not work! CK + # PRIVATE_DEPENDENCIES_CONFIGURED project_options project_warnings +) # Experience shows that explicit package naming can help make it easier to sort # out potential ABI related issues before they start, while helping you diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 23208c22..2feabaae 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,16 +1,12 @@ -find_package(fmt CONFIG) -find_package(spdlog CONFIG) -find_package(docopt CONFIG) +find_package(fmt CONFIG REQUIRED) +find_package(spdlog CONFIG REQUIRED) +find_package(docopt CONFIG REQUIRED) # Generic test that uses conan libs add_executable(intro main.cpp) target_link_libraries( intro - PRIVATE project_options - project_warnings - docopt::docopt - fmt::fmt - spdlog::spdlog) + PUBLIC project_options project_warnings + PRIVATE docopt::docopt fmt::fmt spdlog::spdlog) target_include_directories(intro PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include") - diff --git a/src/main.cpp b/src/main.cpp index de9b1564..065668df 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ static constexpr auto USAGE = --drifting Drifting mine. )"; +// NOLINTNEXTLINE(bugprone-exception-escape) int main(int argc, const char **argv) { try { @@ -45,6 +46,6 @@ int main(int argc, const char **argv) fmt::print("Hello, from {}\n", "{fmt}"); } catch (const std::exception &e) { - fmt::print("Unhandled exception in main: {}", e.what()); + spdlog::error("Unhandled exception in main: {}", e.what()); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c6a32356..84c712d4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,25 +1,37 @@ +cmake_minimum_required(VERSION 3.15...3.23) + +project(CmakeConfigPackageTests LANGUAGES CXX) + +# ---- Test as standalone project the exported config package ---- + +if(PROJECT_IS_TOP_LEVEL OR TEST_INSTALLED_VERSION) + enable_testing() + + find_package(myproject CONFIG REQUIRED) # for intro, project_options, ... +endif() + +# ---- Dependencies ---- + find_package(Catch2 REQUIRED) -include(CTest) include(Catch) add_library(catch_main OBJECT catch_main.cpp) target_link_libraries(catch_main PUBLIC Catch2::Catch2) -target_link_libraries(catch_main PRIVATE project_options) +target_link_libraries(catch_main PRIVATE myproject::project_options) # Provide a simple smoke test to make sure that the CLI works and can display a --help message -add_test(NAME cli.has_help COMMAND intro --help) +add_test(NAME cli.has_help COMMAND myproject::intro --help) # Provide a test to verify that the version being reported from the application # matches the version given to CMake. This will be important once you package # your program. Real world shows that this is the kind of simple mistake that is easy # to make, but also easy to test for. -add_test(NAME cli.version_matches COMMAND intro --version) +add_test(NAME cli.version_matches COMMAND myproject::intro --version) set_tests_properties(cli.version_matches PROPERTIES PASS_REGULAR_EXPRESSION "${PROJECT_VERSION}") - add_executable(tests tests.cpp) -target_link_libraries(tests PRIVATE project_warnings project_options catch_main) +target_link_libraries(tests PRIVATE myproject::project_warnings myproject::project_options catch_main) # automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX # to whatever you want, or use different for different binaries @@ -38,7 +50,7 @@ catch_discover_tests( # Add a file containing a set of constexpr tests add_executable(constexpr_tests constexpr_tests.cpp) -target_link_libraries(constexpr_tests PRIVATE project_options project_warnings catch_main) +target_link_libraries(constexpr_tests PRIVATE myproject::project_options myproject::project_warnings catch_main) catch_discover_tests( constexpr_tests @@ -56,7 +68,7 @@ catch_discover_tests( # Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when # things go wrong with the constexpr testing add_executable(relaxed_constexpr_tests constexpr_tests.cpp) -target_link_libraries(relaxed_constexpr_tests PRIVATE project_options project_warnings catch_main) +target_link_libraries(relaxed_constexpr_tests PRIVATE myproject::project_options myproject::project_warnings catch_main) target_compile_definitions(relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE) catch_discover_tests( From e95411b2eca4088e7aff1c4c07d8183a7abc6b60 Mon Sep 17 00:00:00 2001 From: LtdSauce Date: Sun, 3 Apr 2022 21:27:26 +0200 Subject: [PATCH 04/17] Use an unsafe porject name in janitor for testing --- .github/workflows/template-janitor.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/template-janitor.yml b/.github/workflows/template-janitor.yml index 3efe5ce2..bd5ad350 100644 --- a/.github/workflows/template-janitor.yml +++ b/.github/workflows/template-janitor.yml @@ -61,10 +61,22 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Use testing variables if still a template + if: fromJson(steps.get_repo_meta.outputs.data).is_template == true + run: | + # This name is unsafe because it is not a valid C++ identifier + # ToDo put more unsafe symbols and maybe prefix with number/underscores? + echo "NEW_PROJECT=my-unsafe-project-name" >> $GITHUB_ENV + + - name: Add safe replacement variable versions + run: | + echo "ToDo: add the safe version!" + # Rename all cpp_starter_project occurences to current repository and remove this workflow - name: Insert new org and project run: | # rename the CMake project to match the github project + # ToDo use safe version here sed -i "s/myproject/${{ github.event.repository.name }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp # Update URL placeholders for project From 05e8d931bfafbb0360f7a1c1bf23d328e471d080 Mon Sep 17 00:00:00 2001 From: LtdSauce Date: Sun, 3 Apr 2022 21:41:18 +0200 Subject: [PATCH 05/17] Always use env variable for project name --- .github/workflows/template-janitor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-janitor.yml b/.github/workflows/template-janitor.yml index bd5ad350..42ab9092 100644 --- a/.github/workflows/template-janitor.yml +++ b/.github/workflows/template-janitor.yml @@ -77,7 +77,7 @@ jobs: run: | # rename the CMake project to match the github project # ToDo use safe version here - sed -i "s/myproject/${{ github.event.repository.name }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp + sed -i "s/myproject/${{ env.NEW_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp # Update URL placeholders for project sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt From f159cacc31703fd5bf21e81f05e1968c5b87c862 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 3 Apr 2022 22:14:07 +0200 Subject: [PATCH 06/17] fix CI tests TODO: the installed intro program is not found while test installed version --- test/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 84c712d4..65aeef23 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,7 +12,7 @@ endif() # ---- Dependencies ---- -find_package(Catch2 REQUIRED) +find_package(Catch2 CONFIG REQUIRED) include(Catch) @@ -21,13 +21,13 @@ target_link_libraries(catch_main PUBLIC Catch2::Catch2) target_link_libraries(catch_main PRIVATE myproject::project_options) # Provide a simple smoke test to make sure that the CLI works and can display a --help message -add_test(NAME cli.has_help COMMAND myproject::intro --help) +add_test(NAME cli.has_help COMMAND intro --help) # Provide a test to verify that the version being reported from the application # matches the version given to CMake. This will be important once you package # your program. Real world shows that this is the kind of simple mistake that is easy # to make, but also easy to test for. -add_test(NAME cli.version_matches COMMAND myproject::intro --version) +add_test(NAME cli.version_matches COMMAND intro --version) set_tests_properties(cli.version_matches PROPERTIES PASS_REGULAR_EXPRESSION "${PROJECT_VERSION}") add_executable(tests tests.cpp) From 9a5cb52272e7c8bcda0ad63bdcf641d361c8cac0 Mon Sep 17 00:00:00 2001 From: LtdSauce Date: Sun, 3 Apr 2022 21:46:49 +0200 Subject: [PATCH 07/17] Add valid c++ identifier project name variable and use it This commit replaces every hyphen with a underscore in the project name to create a safe project name that can be used in C++ identifiers. This is also used for the CMake project name. This commit also adds the dot as a forbidden symbol and replaces it with the underscore for a safe project name. --- .github/workflows/template-janitor.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/template-janitor.yml b/.github/workflows/template-janitor.yml index 42ab9092..f66cc4eb 100644 --- a/.github/workflows/template-janitor.yml +++ b/.github/workflows/template-janitor.yml @@ -65,19 +65,19 @@ jobs: if: fromJson(steps.get_repo_meta.outputs.data).is_template == true run: | # This name is unsafe because it is not a valid C++ identifier - # ToDo put more unsafe symbols and maybe prefix with number/underscores? - echo "NEW_PROJECT=my-unsafe-project-name" >> $GITHUB_ENV + echo "NEW_PROJECT=my-unsafe.project" >> $GITHUB_ENV - name: Add safe replacement variable versions run: | - echo "ToDo: add the safe version!" + # hyphens and dots in c++ identifiers are forbidden. Use underscores instead. + NEW_SAFE_PROJECT=$(echo ${{ env.NEW_PROJECT }} | sed "s/-/_/g" | sed "s/\./_/g" ) + echo "NEW_SAFE_PROJECT=$NEW_SAFE_PROJECT" >> $GITHUB_ENV # Rename all cpp_starter_project occurences to current repository and remove this workflow - name: Insert new org and project run: | # rename the CMake project to match the github project - # ToDo use safe version here - sed -i "s/myproject/${{ env.NEW_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp + sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp # Update URL placeholders for project sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt From a919c6683d62f8a0ebccebbd9c922d86df046e8f Mon Sep 17 00:00:00 2001 From: LtdSauce Date: Sun, 3 Apr 2022 22:41:45 +0200 Subject: [PATCH 08/17] Build the project during janitor to catch bugs This commit lets the janitor build the project as we might have broken the cpp/hpp files when replacing the names. This is somewhat done for regression testing as hyphens and dots in repository name broke the build. --- .github/workflows/template-janitor.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/template-janitor.yml b/.github/workflows/template-janitor.yml index f66cc4eb..c48265a6 100644 --- a/.github/workflows/template-janitor.yml +++ b/.github/workflows/template-janitor.yml @@ -132,7 +132,8 @@ jobs: - name: Test simple configuration to make sure nothing broke run: | cmake -S . -B ./build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE:STRING=${{ matrix.build_type }} -DENABLE_DEVELOPER_MODE:BOOL=${{ matrix.developer_mode }} -DOPT_ENABLE_COVERAGE:BOOL=OFF - + # Build it because we may have broken something in the cpp/hpp files + cmake --build build - uses: EndBug/add-and-commit@v4 # only commit and push if we are not a template project anymore! From 9203a2e1ada2444656f352d4fd17a3d3c183cbda Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 3 Apr 2022 22:54:37 +0200 Subject: [PATCH 09/17] be strictly paranoid for Template Janitor github action! --- test/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 65aeef23..a5e24bc6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,6 +8,11 @@ if(PROJECT_IS_TOP_LEVEL OR TEST_INSTALLED_VERSION) enable_testing() find_package(myproject CONFIG REQUIRED) # for intro, project_options, ... + + if(NOT TARGET myproject::project_options) + message(FATAL_ERROR "Requiered config package not found!") + return() # be strictly paranoid for Template Janitor github action! CK + endif() endif() # ---- Dependencies ---- From f3937e6f3bfe5cd9168af33a7be3a2cfa596f5e6 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 13 Apr 2022 17:37:51 -0600 Subject: [PATCH 10/17] Make sure tests have project names updated also by janitor --- .github/workflows/template-janitor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-janitor.yml b/.github/workflows/template-janitor.yml index c48265a6..61c9102d 100644 --- a/.github/workflows/template-janitor.yml +++ b/.github/workflows/template-janitor.yml @@ -77,7 +77,7 @@ jobs: - name: Insert new org and project run: | # rename the CMake project to match the github project - sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp + sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp test/CMakeLists.txt fuzz_test/CMakeLists.txt # Update URL placeholders for project sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt From 93968c5fd5e2f09ce7cfc70b10f053ad1eff0647 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 14 Apr 2022 07:59:39 -0300 Subject: [PATCH 11/17] Updates setup-cpp and adds missing tools - uses setup-cpp v0.10.0 - Adds clang-tidy and clang-format --- .gitlab-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cafb95fa..727a39ae 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,9 +20,9 @@ stages: apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1E9377A2BA9EF27F .setup_cpp: &setup_cpp | - curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.5.8/setup_cpp_linux" + curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.10.0/setup_cpp_linux" chmod +x setup_cpp_linux - ./setup_cpp_linux --compiler $compiler --cmake true --ninja true --conan true --ccache true + ./setup_cpp_linux --compiler $compiler --cmake true --ninja true --conan true --ccache true --clangtidy true --clangformat true --cppcheck true source ~/.profile .test: &test | @@ -46,4 +46,4 @@ test_linux_gcc: script: - *setup_linux - *setup_cpp - - *test \ No newline at end of file + - *test From 43842e921f04ec61718349d95c659a794baaf88d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 Apr 2022 11:04:22 -0600 Subject: [PATCH 12/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 860389e3..9e9ee3e8 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# cpp_boilerplate_project +# cmake_conan_boilerplate_template [![ci](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/cpp-best-practices/cpp_boilerplate_project/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cpp_boilerplate_project) [![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cpp_boilerplate_project)](https://lgtm.com/projects/g/cpp-best-practices/cpp_boilerplate_project/context:cpp) [![CodeQL](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/codeql-analysis.yml) -## About cpp_boilerplate_project +## About cmake_conan_boilerplate_template This is a C++ Best Practices GitHub template for getting up and running with C++ quickly. From b2b483e5a3e7c18896507756286cb003a111c146 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 Apr 2022 11:06:48 -0600 Subject: [PATCH 13/17] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1f7ce29..2278d16c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# cpp_starter_project +# gui_starter_template [![ci](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/cpp-best-practices/cpp_starter_project/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cpp_starter_project) [![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cpp_starter_project)](https://lgtm.com/projects/g/cpp-best-practices/cpp_starter_project/context:cpp) [![CodeQL](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/codeql-analysis.yml) -## About cpp_starter_project +## About gui_starter_template This is a C++ Best Practices GitHub template for getting up and running with C++ quickly. From 27e8034c5e40818cc700eaf14c6745d6de164b29 Mon Sep 17 00:00:00 2001 From: Template Janitor Date: Wed, 27 Apr 2022 17:22:41 +0000 Subject: [PATCH 14/17] Change Template Name --- .github/template/template_name | 2 +- .github/template/template_repository | 2 +- README.md | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/template/template_name b/.github/template/template_name index 4e886aed..26c1b5d7 100644 --- a/.github/template/template_name +++ b/.github/template/template_name @@ -1 +1 @@ -cpp_starter_project +gui_starter_template diff --git a/.github/template/template_repository b/.github/template/template_repository index 3d16d5e5..a28f72fb 100644 --- a/.github/template/template_repository +++ b/.github/template/template_repository @@ -1 +1 @@ -cpp-best-practices/cpp_starter_project +cpp-best-practices/gui_starter_template diff --git a/README.md b/README.md index 2278d16c..79fc1542 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # gui_starter_template -[![ci](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/ci.yml) -[![codecov](https://codecov.io/gh/cpp-best-practices/cpp_starter_project/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cpp_starter_project) -[![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cpp_starter_project)](https://lgtm.com/projects/g/cpp-best-practices/cpp_starter_project/context:cpp) -[![CodeQL](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_starter_project/actions/workflows/codeql-analysis.yml) +[![ci](https://github.com/cpp-best-practices/gui_starter_template/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/gui_starter_template/actions/workflows/ci.yml) +[![codecov](https://codecov.io/gh/cpp-best-practices/gui_starter_template/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/gui_starter_template) +[![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/gui_starter_template)](https://lgtm.com/projects/g/cpp-best-practices/gui_starter_template/context:cpp) +[![CodeQL](https://github.com/cpp-best-practices/gui_starter_template/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/gui_starter_template/actions/workflows/codeql-analysis.yml) ## About gui_starter_template @@ -28,16 +28,16 @@ It requires * conan * a compiler -For a simplified project, check out the [cpp_starter_project](https://github.com/cpp-best-practices/cpp_starter_project). +For a simplified project, check out the [gui_starter_template](https://github.com/cpp-best-practices/gui_starter_template). -Ths Boilerplate project will merge new features first, then they will be merged (as appropriate) into cpp_starter_project. +Ths Boilerplate project will merge new features first, then they will be merged (as appropriate) into gui_starter_template. ## Getting Started ### Use the Github template First, click the green `Use this template` button near the top of this page. -This will take you to Github's ['Generate Repository'](https://github.com/cpp-best-practices/cpp_starter_project/generate) page. +This will take you to Github's ['Generate Repository'](https://github.com/cpp-best-practices/gui_starter_template/generate) page. Fill in a repository name and short description, and click 'Create repository from template'. This will allow you to create a new repository in your Github account, prepopulated with the contents of this project. From bdc8d9578375f6c04b6cbb212de95d66574d4469 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 Apr 2022 12:19:33 -0600 Subject: [PATCH 15/17] Fix name too short warning --- src/sdl/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sdl/main.cpp b/src/sdl/main.cpp index 536a8300..56a94e8f 100644 --- a/src/sdl/main.cpp +++ b/src/sdl/main.cpp @@ -43,9 +43,9 @@ static void check_video_driver(const char *name) // required to give full function signature for linking on Windows int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) { - SDL_version v; - SDL_GetVersion(&v); - spdlog::info("SDL version {}.{}.{}", v.major, v.minor, v.patch); + SDL_version version; + SDL_GetVersion(&version); + spdlog::info("SDL version {}.{}.{}", version.major, version.minor, version.patch); check_video_driver("x11"); check_audio_driver("alsa"); check_audio_driver("pulseaudio"); From 235115d92a4909c4a1571dae202d11e6337868c4 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 Apr 2022 14:27:07 -0700 Subject: [PATCH 16/17] Change Project Name - Update Dependencies PR to update project name, but dependencies also needed to be updated to get CI passing again. --- .github/template/template_name | 2 +- .github/template/template_repository | 2 +- .github/workflows/ci.yml | 18 +++++++++--- README.md | 10 +++---- conanfile.txt | 7 ++--- src/CMakeLists.txt | 4 +-- src/main.cpp | 44 ++++++++++++---------------- 7 files changed, 44 insertions(+), 43 deletions(-) diff --git a/.github/template/template_name b/.github/template/template_name index 9f9b489e..b4857fcb 100644 --- a/.github/template/template_name +++ b/.github/template/template_name @@ -1 +1 @@ -cpp_boilerplate_project +cmake_conan_boilerplate_template diff --git a/.github/template/template_repository b/.github/template/template_repository index 622fb5a7..efbdeb18 100644 --- a/.github/template/template_repository +++ b/.github/template/template_repository @@ -1 +1 @@ -cpp-best-practices/cpp_boilerplate_project +cpp-best-practices/cmake_conan_boilerplate_template diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1ac78e3f..ff341bd5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,7 @@ env: CONAN_SYSREQUIRES_MODE: enabled CONAN_USER_HOME: "${{ github.workspace }}/conan-cache" CONAN_USER_HOME_SHORT: "${{ github.workspace }}/conan-cache/short" + CLANG_TIDY_VERSION: "13.0.0" jobs: Test: @@ -72,21 +73,25 @@ jobs: gcov_executable: gcov developer_mode: On + # Windows msvc builds - os: windows-2022 compiler: msvc generator: "Visual Studio 17 2022" build_type: Debug developer_mode: On + - os: windows-2022 compiler: msvc generator: "Visual Studio 17 2022" build_type: Release developer_mode: On + - os: windows-2022 compiler: msvc generator: "Visual Studio 17 2022" build_type: Debug developer_mode: Off + - os: windows-2022 compiler: msvc generator: "Visual Studio 17 2022" @@ -95,10 +100,14 @@ jobs: package_generator: ZIP - - - steps: + - name: Check for llvm version mismatches + if: ${{ contains(matrix.compiler, 'llvm') && !contains(matrix.compiler, env.CLANG_TIDY_VERSION) }} + uses: actions/github-script@v3 + with: + script: | + core.setFailed('There is a mismatch between configured llvm compiler and clang-tidy version chosen') + - uses: actions/checkout@v2 - name: Setup Cache @@ -120,7 +129,8 @@ jobs: conan: true vcpkg: false ccache: true - clangtidy: true + clangtidy: ${{ env.CLANG_TIDY_VERSION }} + cppcheck: true diff --git a/README.md b/README.md index 9e9ee3e8..8b9af443 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # cmake_conan_boilerplate_template -[![ci](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/ci.yml) -[![codecov](https://codecov.io/gh/cpp-best-practices/cpp_boilerplate_project/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cpp_boilerplate_project) -[![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cpp_boilerplate_project)](https://lgtm.com/projects/g/cpp-best-practices/cpp_boilerplate_project/context:cpp) -[![CodeQL](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/cpp_boilerplate_project/actions/workflows/codeql-analysis.yml) +[![ci](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template/actions/workflows/ci.yml/badge.svg)](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template/actions/workflows/ci.yml) +[![codecov](https://codecov.io/gh/cpp-best-practices/cmake_conan_boilerplate_template/branch/main/graph/badge.svg)](https://codecov.io/gh/cpp-best-practices/cmake_conan_boilerplate_template) +[![Language grade: C++](https://img.shields.io/lgtm/grade/cpp/github/cpp-best-practices/cmake_conan_boilerplate_template)](https://lgtm.com/projects/g/cpp-best-practices/cmake_conan_boilerplate_template/context:cpp) +[![CodeQL](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template/actions/workflows/codeql-analysis.yml) ## About cmake_conan_boilerplate_template @@ -36,7 +36,7 @@ Ths Boilerplate project will merge new features first, then they will be merged ### Use the Github template First, click the green `Use this template` button near the top of this page. -This will take you to Github's ['Generate Repository'](https://github.com/cpp-best-practices/cpp_boilerplate_project/generate) page. +This will take you to Github's ['Generate Repository'](https://github.com/cpp-best-practices/cmake_conan_boilerplate_template/generate) page. Fill in a repository name and short description, and click 'Create repository from template'. This will allow you to create a new repository in your Github account, prepopulated with the contents of this project. diff --git a/conanfile.txt b/conanfile.txt index 3e21bd08..a92cc590 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,10 +1,9 @@ # Docs at https://docs.conan.io/en/latest/reference/conanfile_txt.html [requires] -catch2/2.13.8 -docopt.cpp/0.6.3 -#fmt/8.1.1 -spdlog/1.9.2 +catch2/2.13.9 +cli11/2.2.0 +spdlog/1.10.0 [generators] cmake_find_package_multi diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2feabaae..ab05fe9f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,12 +1,12 @@ find_package(fmt CONFIG REQUIRED) find_package(spdlog CONFIG REQUIRED) -find_package(docopt CONFIG REQUIRED) +find_package(CLI11 CONFIG REQUIRED) # Generic test that uses conan libs add_executable(intro main.cpp) target_link_libraries( intro PUBLIC project_options project_warnings - PRIVATE docopt::docopt fmt::fmt spdlog::spdlog) + PRIVATE CLI11::CLI11 fmt::fmt spdlog::spdlog) target_include_directories(intro PRIVATE "${CMAKE_BINARY_DIR}/configured_files/include") diff --git a/src/main.cpp b/src/main.cpp index 065668df..9793e979 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ #include #include +#include -#include +#include #include // This file will be generated automatically when you run the CMake configuration step. @@ -9,42 +10,33 @@ // You can modify the source template at `configured_files/config.hpp.in`. #include -static constexpr auto USAGE = - R"(Naval Fate. - - Usage: - naval_fate ship new ... - naval_fate ship move [--speed=] - naval_fate ship shoot - naval_fate mine (set|remove) [--moored | --drifting] - naval_fate (-h | --help) - naval_fate --version - Options: - -h --help Show this screen. - --version Show version. - --speed= Speed in knots [default: 10]. - --moored Moored (anchored) mine. - --drifting Drifting mine. -)"; // NOLINTNEXTLINE(bugprone-exception-escape) int main(int argc, const char **argv) { try { - std::map args = docopt::docopt(USAGE, - { std::next(argv), std::next(argv, argc) }, - true,// show help if requested - fmt::format("{} {}", - myproject::cmake::project_name, - myproject::cmake::project_version));// version string, acquired from config.hpp via CMake + CLI::App app{ fmt::format("{} version {}", myproject::cmake::project_name, myproject::cmake::project_version) }; - for (auto const &arg : args) { std::cout << arg.first << "=" << arg.second << '\n'; } + std::optional message; + app.add_option("-m,--message", message, "A message to print back out"); + bool show_version = false; + app.add_flag("--version", show_version, "Show version information"); + CLI11_PARSE(app, argc, argv); + + if (show_version) { + fmt::print("{}\n", myproject::cmake::project_version); + return EXIT_SUCCESS; + } // Use the default logger (stdout, multi-threaded, colored) spdlog::info("Hello, {}!", "World"); - fmt::print("Hello, from {}\n", "{fmt}"); + if (message) { + fmt::print("Message: '{}'\n", *message); + } else { + fmt::print("No Message Provided :(\n"); + } } catch (const std::exception &e) { spdlog::error("Unhandled exception in main: {}", e.what()); } From 948679fe20f62385fc6d02f82787ae1bb600f116 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 Apr 2022 16:47:03 -0600 Subject: [PATCH 17/17] Disable difficult/unhelpful CppCheck warnings --- CMakeLists.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f887d73d..324628e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,7 +108,17 @@ dynamic_project_options( # MSVC_WARNINGS # Override the defaults for the MSVC warnings # CLANG_WARNINGS # Override the defaults for the CLANG warnings # GCC_WARNINGS # Override the defaults for the GCC warnings - # CPPCHECK_OPTIONS # Override the defaults for CppCheck + CPPCHECK_OPTIONS + --enable=style,performance,warning,portability + --inline-suppr + # We cannot act on a bug/missing feature of cppcheck + --suppress=cppcheckError + --suppress=internalAstError + # if a file does not have an internalAstError, we get an unmatchedSuppression error + --suppress=unmatchedSuppression + --suppress=passedByValue + --suppress=syntaxError + --inconclusive ) target_compile_features(project_options INTERFACE cxx_std_${CMAKE_CXX_STANDARD})