From 7a2ad28f6c51d4de194114e70ece76d9632a56d8 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Fri, 5 Apr 2024 12:12:22 +0200 Subject: [PATCH 1/4] Use FILE_SET HEADERS with CMAKE_VERIFY_INTERFACE_HEADER_SETS too CMAKE_UNITY_BUILD and CMAKE_RELWITHDEBINFO_POSTFIX --- .clang-tidy | 2 +- CMakeLists.txt | 9 +- Makefile | 33 +++++--- cmake/presets/CMakeWorkflowPresets.json | 93 ++++++++++++++++----- fuzz_test/fuzz_tester.cpp | 7 +- my_header_lib/CMakeLists.txt | 16 +++- my_header_lib/include/my_header_lib/lib.hpp | 6 +- my_lib/CMakeLists.txt | 14 +++- my_lib/include/my_lib/lib.hpp | 6 +- my_lib/src/my_lib/lib.cpp | 2 + 10 files changed, 138 insertions(+), 50 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5b98248..b875c65 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -16,6 +16,6 @@ Checks: "*, misc-non-private-member-variables-in-classes, misc-include-cleaner, " -WarningsAsErrors: '' +WarningsAsErrors: '*' HeaderFilterRegex: '' FormatStyle: none diff --git a/CMakeLists.txt b/CMakeLists.txt index 11783ec..d8b4d3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,11 @@ -cmake_minimum_required(VERSION 3.21...3.28) +cmake_minimum_required(VERSION 3.23...3.29) # set a default CXX standard for the tools and targets that do not specify them. # If commented, the latest supported standard for your compiler is automatically set. -# set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 20) + +# This allows libraries from multiple build types to be put in the same directory without overwriting each other +set(CMAKE_RELWITHDEBINFO_POSTFIX d) # Set to `ON` if using C++20 modules set(CMAKE_CXX_SCAN_FOR_MODULES OFF) @@ -105,7 +108,7 @@ project_options( # ENABLE_PCH # PCH_HEADERS # WARNINGS_AS_ERRORS - # ENABLE_INCLUDE_WHAT_YOU_USE + ENABLE_INCLUDE_WHAT_YOU_USE # ENABLE_GCC_ANALYZER # ENABLE_BUILD_WITH_TIME_TRACE # ENABLE_UNITY diff --git a/Makefile b/Makefile index 1f9ca0e..efffa9d 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,16 @@ # Notes: # - list all the task under PHONY # - If getting missing separator error, try replacing spaces with tabs. -# - If using Visual Studio, either run the following commands inside the Visual Studio command prompt (vcvarsall) or remove the Ninja generator from the commands. -.PHONY: build test test_release test_install coverage docs format clean +# - If using Visual Studio, either run the following commands inside the Visual Studio command prompt (vcvarsall) +# or remove the Ninja generator from the commands. +.PHONY: all build test test_release test_install test_release_debug coverage docs format clean distclean build: release +all: release test_release test_install test_release_debug + release: - cmake --workflow --preset default + cmake --workflow --preset Release debug: cmake -S ./ -B ./build -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=Debug -DFEATURE_TESTS:BOOL=OFF @@ -19,16 +22,13 @@ test: cmake --workflow --preset developer test_release_debug: - cmake -S ./ -B ./build -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DFEATURE_TESTS:BOOL=ON - cmake --build ./build --config RelWithDebInfo - - (cd build/my_exe/test && ctest -C RelWithDebInfo --output-on-failure) - (cd build/my_header_lib/test && ctest -C RelWithDebInfo --output-on-failure) - (cd build/my_lib/test && ctest -C RelWithDebInfo --output-on-failure) + cmake --workflow --preset RelWithDebInfo -test_release: release +test_release: + cmake --workflow --preset gcc-release -test_install: release +test_install: + cmake --workflow --preset clang-release coverage: ifeq ($(OS), Windows_NT) @@ -39,8 +39,8 @@ else endif docs: - cmake -S ./ -B ./build -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=Debug -DFEATURE_DOCS:BOOL=ON -DFEATURE_TESTS:BOOL=OFF - cmake --build ./build --target doxygen-docs --config Debug + cmake -S ./ -B ./build/docs -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=Debug -DFEATURE_DOCS:BOOL=ON -DFEATURE_TESTS:BOOL=OFF + cmake --build ./build/docs --target doxygen-docs --config Debug format: ifeq ($(OS), Windows_NT) @@ -55,3 +55,10 @@ ifeq ($(OS), Windows_NT) else rm -rf ./build endif + +distclean: clean +ifeq ($(OS), Windows_NT) + pwsh -c 'function rmrf($$path) { if (test-path $$path) { rm -r -force $$path }}; rmrf ./install;' +else + rm -rf ./install +endif diff --git a/cmake/presets/CMakeWorkflowPresets.json b/cmake/presets/CMakeWorkflowPresets.json index 3ceb511..cc218db 100644 --- a/cmake/presets/CMakeWorkflowPresets.json +++ b/cmake/presets/CMakeWorkflowPresets.json @@ -14,15 +14,15 @@ "binaryDir": "${sourceDir}/build/${presetName}", "installDir": "${sourceDir}/install/${presetName}", "cacheVariables": { + "CMAKE_UNITY_BUILD": true, "FEATURE_DOCS": true } }, { - "name": "default", + "name": "Release", + "inherits": "conf-common", "displayName": "Default User Config", "description": "Default build using Ninja generator", - "generator": "Ninja", - "binaryDir": "${sourceDir}/build/default", "installDir": "${sourceDir}/install", "cacheVariables": { "CMAKE_PREFIX_PATH": { @@ -32,7 +32,7 @@ "CMAKE_CXX_STANDARD": "20", "CMAKE_BUILD_TYPE": "Release", "CMAKE_SKIP_INSTALL_RULES": false, - "BUILD_SHARED_LIBS": false, + "BUILD_SHARED_LIBS": true, "FEATURE_TESTS": false, "FEATURE_DOCS": false, "WARNINGS_AS_ERRORS": false @@ -45,34 +45,59 @@ }, { "name": "developer", - "inherits": "default", + "inherits": "Release", "displayName": "Ninja Multi-Config", - "description": "Default build using Ninja Multi-Config generator", + "description": "Debug build using Ninja Multi-Config generator", "generator": "Ninja Multi-Config", - "binaryDir": "${sourceDir}/build/developer", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "CMAKE_SKIP_INSTALL_RULES": true, "BUILD_SHARED_LIBS": true, - "FEATURE_FUZZ_TESTS": false, + "FEATURE_FUZZ_TESTS": true, "FEATURE_TESTS": true, "FEATURE_DOCS": false, - "WARNINGS_AS_ERRORS": "WARNINGS_AS_ERRORS" + "WARNINGS_AS_ERRORS": true + } + }, + { + "name": "RelWithDebInfo", + "inherits": "developer", + "description": "RelWithDebInfo build using Ninja Multi-Config generator", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "RelWithDebInfo", + "CMAKE_SKIP_INSTALL_RULES": false, + "BUILD_SHARED_LIBS": true, + "FEATURE_FUZZ_TESTS": false, + "FEATURE_TESTS": false, + "FEATURE_DOCS": false, + "WARNINGS_AS_ERRORS": false } } ], "buildPresets": [ { - "name": "default", - "configurePreset": "default" + "name": "Release", + "configurePreset": "Release" }, { "name": "developer", "configurePreset": "developer" }, { - "name": "install", - "configurePreset": "default", + "name": "RelWithDebInfo", + "configurePreset": "RelWithDebInfo" + }, + { + "name": "installRelease", + "configurePreset": "Release", + "targets": [ + "install" + ] + }, + { + "name": "installRelWithDebInfo", + "configurePreset": "RelWithDebInfo", + "configuration": "RelWithDebInfo", "targets": [ "install" ] @@ -94,8 +119,15 @@ ], "packagePresets": [ { - "name": "default", - "configurePreset": "default", + "name": "Release", + "configurePreset": "Release", + "generators": [ + "TGZ" + ] + }, + { + "name": "RelWithDebInfo", + "configurePreset": "RelWithDebInfo", "generators": [ "TGZ" ] @@ -103,23 +135,23 @@ ], "workflowPresets": [ { - "name": "default", + "name": "Release", "steps": [ { "type": "configure", - "name": "default" + "name": "Release" }, { "type": "build", - "name": "default" + "name": "Release" }, { "type": "build", - "name": "install" + "name": "installRelease" }, { "type": "package", - "name": "default" + "name": "Release" } ] }, @@ -139,6 +171,27 @@ "name": "developer" } ] + }, + { + "name": "RelWithDebInfo", + "steps": [ + { + "type": "configure", + "name": "RelWithDebInfo" + }, + { + "type": "build", + "name": "RelWithDebInfo" + }, + { + "type": "build", + "name": "installRelWithDebInfo" + }, + { + "type": "package", + "name": "RelWithDebInfo" + } + ] } ] } diff --git a/fuzz_test/fuzz_tester.cpp b/fuzz_test/fuzz_tester.cpp index 7c1eb82..289c5da 100644 --- a/fuzz_test/fuzz_tester.cpp +++ b/fuzz_test/fuzz_tester.cpp @@ -1,7 +1,8 @@ -#include +#include // for print, format_string -#include -#include +#include // for size_t +#include // for uint8_t +#include // for next [[nodiscard]] auto sum_values(const uint8_t *Data, size_t Size) { constexpr auto scale = 1000; diff --git a/my_header_lib/CMakeLists.txt b/my_header_lib/CMakeLists.txt index 24bb0f7..c9e4cff 100644 --- a/my_header_lib/CMakeLists.txt +++ b/my_header_lib/CMakeLists.txt @@ -1,9 +1,21 @@ +set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON) + add_library(my_header_lib INTERFACE) + +# cmake-format: off +target_sources( + my_header_lib INTERFACE + FILE_SET HEADERS + BASE_DIRS include + FILES include/my_header_lib/lib.hpp +) +# cmake-format: on + # link project_options/warnings target_link_libraries(my_header_lib INTERFACE my_project_options my_project_warnings) -# Includes -target_include_interface_directories(my_header_lib include) +# Includes not needed if using FILE_SET HEADERS +#XXX target_include_interface_directories(my_header_lib include) # Find dependencies: target_find_dependencies(my_header_lib INTERFACE_CONFIG fmt) diff --git a/my_header_lib/include/my_header_lib/lib.hpp b/my_header_lib/include/my_header_lib/lib.hpp index 74170c7..6f5152e 100644 --- a/my_header_lib/include/my_header_lib/lib.hpp +++ b/my_header_lib/include/my_header_lib/lib.hpp @@ -2,11 +2,11 @@ #include -inline int some_fun() { +[[nodiscard]] inline int some_fun() { fmt::print("Hello {} !", "world"); return 0; } -constexpr int some_constexpr_fun() { +[[nodiscard]] constexpr int some_constexpr_fun() { return 0; -} \ No newline at end of file +} diff --git a/my_lib/CMakeLists.txt b/my_lib/CMakeLists.txt index a0d6447..05bb9e4 100644 --- a/my_lib/CMakeLists.txt +++ b/my_lib/CMakeLists.txt @@ -1,9 +1,19 @@ add_library(my_lib "./src/my_lib/lib.cpp") + +# cmake-format: off +target_sources( + my_lib PUBLIC + FILE_SET HEADERS + BASE_DIRS include + FILES include/my_lib/lib.hpp +) +# cmake-format: on + # link project_options/warnings target_link_libraries(my_lib PRIVATE my_project_options my_project_warnings) -# Includes -target_include_interface_directories(my_lib include) +# Includes not needed if using FILE_SET HEADERS +#XXX target_include_interface_directories(my_lib include) # Find dependencies: target_find_dependencies(my_lib PRIVATE_CONFIG fmt) diff --git a/my_lib/include/my_lib/lib.hpp b/my_lib/include/my_lib/lib.hpp index 4d766c1..43cc0f6 100644 --- a/my_lib/include/my_lib/lib.hpp +++ b/my_lib/include/my_lib/lib.hpp @@ -1,7 +1,7 @@ #pragma once -int some_fun(); +[[nodiscard]] int some_fun(); -constexpr int some_constexpr_fun() { +[[nodiscard]] constexpr int some_constexpr_fun() { return 0; -} \ No newline at end of file +} diff --git a/my_lib/src/my_lib/lib.cpp b/my_lib/src/my_lib/lib.cpp index 84794f2..a0dfc51 100644 --- a/my_lib/src/my_lib/lib.cpp +++ b/my_lib/src/my_lib/lib.cpp @@ -1,3 +1,5 @@ +#include "my_lib/lib.hpp" + #include int some_fun() { From 77d4b970564050f35a393a62d2a79b2500aaa15f Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 6 Apr 2024 18:41:25 +0200 Subject: [PATCH 2/4] Modernize Makefile --- Makefile | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index efffa9d..7931b5e 100644 --- a/Makefile +++ b/Makefile @@ -5,19 +5,27 @@ # - If getting missing separator error, try replacing spaces with tabs. # - If using Visual Studio, either run the following commands inside the Visual Studio command prompt (vcvarsall) # or remove the Ninja generator from the commands. +# Standard stuff + +.SUFFIXES: + +MAKEFLAGS+= --no-builtin-rules +MAKEFLAGS+= --warn-undefined-variables + +OS?=$(shell uname) +export PROJECT_DIR?=$(shell basename $(CURDIR)) +export BUILD_DIR?=$(CURDIR)/build + .PHONY: all build test test_release test_install test_release_debug coverage docs format clean distclean build: release -all: release test_release test_install test_release_debug +all: test_release test_install test_release_debug coverage doc release: cmake --workflow --preset Release -debug: - cmake -S ./ -B ./build -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=Debug -DFEATURE_TESTS:BOOL=OFF - cmake --build ./build --config Debug - +debug: test test: cmake --workflow --preset developer @@ -35,12 +43,11 @@ ifeq ($(OS), Windows_NT) OpenCppCoverage.exe --export_type cobertura:coverage.xml --cover_children -- $(MAKE) test else $(MAKE) test - gcovr -j 1 --delete --root ./ --print-summary --xml-pretty --xml coverage.xml ./build --gcov-executable gcov + gcovr -j 1 --delete --root $(CURDIR) --print-summary --xml-pretty --xml coverage.xml $(BUILD_DIR)/developer --gcov-executable gcov endif docs: - cmake -S ./ -B ./build/docs -G "Ninja Multi-Config" -DCMAKE_BUILD_TYPE:STRING=Debug -DFEATURE_DOCS:BOOL=ON -DFEATURE_TESTS:BOOL=OFF - cmake --build ./build/docs --target doxygen-docs --config Debug + cmake --build $(BUILD_DIR)/developer --target doxygen-docs --config Coverage format: ifeq ($(OS), Windows_NT) @@ -51,9 +58,9 @@ endif clean: ifeq ($(OS), Windows_NT) - pwsh -c 'function rmrf($$path) { if (test-path $$path) { rm -r -force $$path }}; rmrf ./build;' + pwsh -c 'function rmrf($$path) { if (test-path $$path) { rm -r -force $$path }}; rmrf $(BUILD_DIR);' else - rm -rf ./build + rm -rf $(BUILD_DIR) endif distclean: clean @@ -62,3 +69,8 @@ ifeq ($(OS), Windows_NT) else rm -rf ./install endif + +# Anything we don't know how to build will use this rule. +# The command is a do-nothing command. +# +% :: ; From 839215dbb4535b5fc0616dffabb1393909f849d6 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 29 Apr 2024 21:13:01 +0200 Subject: [PATCH 3/4] Quickfix for clang-18 on OSX --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index b875c65..7dc7200 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,6 @@ --- Checks: "*, + -bugprone-chained-comparison, -abseil-*, -altera-*, -android-*, From 2fda05e9acc8747606057a961db8133da256a3a7 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 27 Apr 2025 21:43:11 +0200 Subject: [PATCH 4/4] Rebase and fix build on OSX --- .clang-tidy | 3 ++- CMakeLists.txt | 6 +++--- cmake/presets/CMakeWorkflowPresets.json | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 7dc7200..4b6edb3 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -15,7 +15,8 @@ Checks: "*, readability-avoid-const-params-in-decls, cppcoreguidelines-non-private-member-variables-in-classes, misc-non-private-member-variables-in-classes, - misc-include-cleaner, + -misc-include-cleaner, + -misc-use-internal-linkagecor " WarningsAsErrors: '*' HeaderFilterRegex: '' diff --git a/CMakeLists.txt b/CMakeLists.txt index d8b4d3c..131ee69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ -cmake_minimum_required(VERSION 3.23...3.29) +cmake_minimum_required(VERSION 3.23...4.0) # set a default CXX standard for the tools and targets that do not specify them. # If commented, the latest supported standard for your compiler is automatically set. -set(CMAKE_CXX_STANDARD 20) +# set(CMAKE_CXX_STANDARD 20) # This allows libraries from multiple build types to be put in the same directory without overwriting each other set(CMAKE_RELWITHDEBINFO_POSTFIX d) @@ -108,7 +108,7 @@ project_options( # ENABLE_PCH # PCH_HEADERS # WARNINGS_AS_ERRORS - ENABLE_INCLUDE_WHAT_YOU_USE + # ENABLE_INCLUDE_WHAT_YOU_USE # ENABLE_GCC_ANALYZER # ENABLE_BUILD_WITH_TIME_TRACE # ENABLE_UNITY diff --git a/cmake/presets/CMakeWorkflowPresets.json b/cmake/presets/CMakeWorkflowPresets.json index cc218db..4e5bf8d 100644 --- a/cmake/presets/CMakeWorkflowPresets.json +++ b/cmake/presets/CMakeWorkflowPresets.json @@ -14,7 +14,7 @@ "binaryDir": "${sourceDir}/build/${presetName}", "installDir": "${sourceDir}/install/${presetName}", "cacheVariables": { - "CMAKE_UNITY_BUILD": true, + "CMAKE_CXX_STANDARD": "20", "FEATURE_DOCS": true } }, @@ -29,7 +29,8 @@ "type": "path", "value": "${sourceDir}/install" }, - "CMAKE_CXX_STANDARD": "20", + "CMAKE_UNITY_BUILD": true, + "CMAKE_CXX_STANDARD": "23", "CMAKE_BUILD_TYPE": "Release", "CMAKE_SKIP_INSTALL_RULES": false, "BUILD_SHARED_LIBS": true, @@ -50,6 +51,7 @@ "description": "Debug build using Ninja Multi-Config generator", "generator": "Ninja Multi-Config", "cacheVariables": { + "CMAKE_UNITY_BUILD": false, "CMAKE_BUILD_TYPE": "Debug", "CMAKE_SKIP_INSTALL_RULES": true, "BUILD_SHARED_LIBS": true,