From 9fda812aacf4de461e0509d83a9ec7f630523de5 Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Wed, 26 Feb 2025 20:18:08 +0300 Subject: [PATCH 1/6] initial commit --- CMakeLists.txt | 2 + app/CMakeLists.txt | 2 + cmake/CompilerWarnings.cmake | 116 +++++++++++++++++++++++ cmake/LinkerFlags.cmake | 9 ++ cmake/Platform/Common/CMakePresets.json | 34 ------- cmake/Platform/Linux/CMakePresets.json | 2 - cmake/Platform/Mac/CMakePresets.json | 2 - cmake/Platform/Windows/CMakePresets.json | 4 - library/CMakeLists.txt | 3 +- 9 files changed, 131 insertions(+), 43 deletions(-) create mode 100644 cmake/CompilerWarnings.cmake create mode 100644 cmake/LinkerFlags.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 394257b..0b92ce0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ project( ) include(cmake/Variables.cmake) +include(cmake/CompilerWarnings.cmake) +include(cmake/LinkerFlags.cmake) if(FEATURE_TESTS) enable_testing() diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 1cb1340..11e6d10 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -2,4 +2,6 @@ add_executable(Application App.cpp) target_link_libraries(Application PRIVATE Core) +set_compiler_warnings(Application) +set_linker_flags(Application) install(TARGETS Application) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake new file mode 100644 index 0000000..9771bc6 --- /dev/null +++ b/cmake/CompilerWarnings.cmake @@ -0,0 +1,116 @@ +function(set_compiler_warnings target) + if (MSVC) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(PROJECT_WARNINGS + -Wall + -Wextra + -Wshadow + -Wpedantic + -Wunused + -Wformat=2 + -Wnull-dereference + -Wnon-virtual-dtor + -Woverloaded-virtual + -Wold-style-cast + -Wno-missing-prototypes + -Wno-switch-enum + -Wno-c++98-compat + -Wno-c++98-compat-pedantic + /EHsc) + else() + # Note that all the flags after /W4 are required for MSVC to conform to the language standard + set(PROJECT_WARNINGS + /guard:cf + /utf-8 + /diagnostics:caret + /sdl + /w14165 + /w44242 + /w44254 + /w44263 + /w34265 + /w34287 + /w44296 + /w44388 + /w44464 + /w14545 + /w14546 + /w14547 + /w14549 + /w14555 + /w34619 + /w34640 + /w24826 + /w14905 + /w14906 + /w14928 + /w45038 + /W4 + /permissive- + /volatile:iso + /Zc:inline + /Zc:preprocessor + /Zc:enumTypes + /Zc:lambda + /Zc:__cplusplus + /Zc:externConstexpr + /Zc:throwingNew + /EHsc) + endif() + elseif (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang") + set(PROJECT_WARNINGS + -fstack-protector-strong + -Wall + -Wextra + -Wpedantic + -Wconversion + -Wsign-conversion + -Wcast-qual + -Wformat=2 + -Wundef + -Werror=float-equal + -Wshadow + -Wcast-align + -Wunused + -Wnull-dereference + -Wdouble-promotion + -Wimplicit-fallthrough + -Wextra-semi + -Woverloaded-virtual + -Wnon-virtual-dtor + -Wold-style-cast + ) + elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(PROJECT_WARNINGS + -U_FORTIFY_SOURCE + -D_FORTIFY_SOURCE=3 + -D_GLIBCXX_ASSERTIONS=1 + -fstack-protector-strong + -fcf-protection=full + -fstack-clash-protection + -Wall + -Wextra + -Wpedantic + -Wconversion + -Wsign-conversion + -Wcast-qual + -Wformat=2 + -Wundef + -Werror=float-equal + -Wshadow + -Wcast-align + -Wunused + -Wnull-dereference + -Wdouble-promotion + -Wimplicit-fallthrough + -Wextra-semi + -Woverloaded-virtual + -Wnon-virtual-dtor + -Wold-style-cast + ) + else() + message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'") + endif() + + target_compile_options(${target} PRIVATE ${PROJECT_WARNINGS}) +endfunction() diff --git a/cmake/LinkerFlags.cmake b/cmake/LinkerFlags.cmake new file mode 100644 index 0000000..7b92679 --- /dev/null +++ b/cmake/LinkerFlags.cmake @@ -0,0 +1,9 @@ +function(set_linker_flags target) + if (MSVC) + set(PROJECT_LINK_OPTIONS /machine:x64 /guard:cf) + elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(PROJECT_LINK_OPTIONS -Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen) + endif() + + target_link_options(${target} PRIVATE ${PROJECT_LINK_OPTIONS}) +endfunction() diff --git a/cmake/Platform/Common/CMakePresets.json b/cmake/Platform/Common/CMakePresets.json index d6f58cc..ba87574 100644 --- a/cmake/Platform/Common/CMakePresets.json +++ b/cmake/Platform/Common/CMakePresets.json @@ -159,40 +159,6 @@ "cacheVariables": { "CMAKE_CXX_COMPILER": "clang++" } - }, - { - "name": "flags-msvc", - "description": "Note that all the flags after /W4 are required for MSVC to conform to the language standard", - "hidden": true, - "cacheVariables": { - "CMAKE_CXX_FLAGS": "/sdl /guard:cf /utf-8 /diagnostics:caret /sdl /w14165 /w44242 /w44254 /w44263 /w34265 /w34287 /w44296 /w44388 /w44464 /w14545 /w14546 /w14547 /w14549 /w14555 /w34619 /w34640 /w24826 /w14905 /w14906 /w14928 /w45038 /W4 /permissive- /volatile:iso /Zc:inline /Zc:preprocessor /Zc:enumTypes /Zc:lambda /Zc:__cplusplus /Zc:externConstexpr /Zc:throwingNew /EHsc", - "CMAKE_EXE_LINKER_FLAGS": "/machine:x64 /guard:cf", - "CMAKE_SHARED_LINKER_FLAGS": "/machine:x64 /guard:cf" - } - }, - { - "name": "flags-llvm", - "hidden": true, - "cacheVariables": { - "CMAKE_CXX_FLAGS": "-Wall -Wextra -Wshadow -Wpedantic -Wunused -Wformat=2 -Wnull-dereference -Wnon-virtual-dtor -Woverloaded-virtual -Wold-style-cast -Wno-missing-prototypes -Wno-switch-enum -Wno-c++98-compat -Wno-c++98-compat-pedantic /EHsc" - } - }, - { - "name": "flags-gcc-clang", - "description": "These flags are supported by both GCC and Clang", - "hidden": true, - "cacheVariables": { - "CMAKE_CXX_FLAGS": "-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -D_GLIBCXX_ASSERTIONS=1 -fstack-protector-strong -fcf-protection=full -fstack-clash-protection -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wimplicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wold-style-cast", - "CMAKE_EXE_LINKER_FLAGS": "-Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen", - "CMAKE_SHARED_LINKER_FLAGS": "-Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen" - } - }, - { - "name": "flags-appleclang", - "hidden": true, - "cacheVariables": { - "CMAKE_CXX_FLAGS": "-fstack-protector-strong -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wformat=2 -Wundef -Werror=float-equal -Wshadow -Wcast-align -Wunused -Wnull-dereference -Wdouble-promotion -Wimplicit-fallthrough -Wextra-semi -Woverloaded-virtual -Wnon-virtual-dtor -Wold-style-cast" - } } ], "buildPresets": [ diff --git a/cmake/Platform/Linux/CMakePresets.json b/cmake/Platform/Linux/CMakePresets.json index 9aa0c14..6a67b89 100644 --- a/cmake/Platform/Linux/CMakePresets.json +++ b/cmake/Platform/Linux/CMakePresets.json @@ -19,7 +19,6 @@ "cpp-standard", "ninja", "gcc", - "flags-gcc-clang", "vcpkg" ] }, @@ -33,7 +32,6 @@ "cpp-standard", "ninja", "clang++", - "flags-gcc-clang", "vcpkg" ] } diff --git a/cmake/Platform/Mac/CMakePresets.json b/cmake/Platform/Mac/CMakePresets.json index e1437c8..6229e85 100644 --- a/cmake/Platform/Mac/CMakePresets.json +++ b/cmake/Platform/Mac/CMakePresets.json @@ -19,7 +19,6 @@ "cpp-standard", "ninja", "clang++", - "flags-appleclang", "vcpkg" ] }, @@ -33,7 +32,6 @@ "cpp-standard", "xcode", "clang++", - "flags-appleclang", "vcpkg" ] } diff --git a/cmake/Platform/Windows/CMakePresets.json b/cmake/Platform/Windows/CMakePresets.json index 512c224..1678d0b 100644 --- a/cmake/Platform/Windows/CMakePresets.json +++ b/cmake/Platform/Windows/CMakePresets.json @@ -19,7 +19,6 @@ "cpp-standard", "vs2022", "msvc", - "flags-msvc", "vcpkg" ], "architecture": { @@ -41,7 +40,6 @@ "cpp-standard", "vs2022", "clang-cl", - "flags-llvm", "vcpkg" ], "architecture": { @@ -63,7 +61,6 @@ "cpp-standard", "ninja", "msvc", - "flags-msvc", "vcpkg" ], "architecture": { @@ -85,7 +82,6 @@ "cpp-standard", "ninja", "clang-cl", - "flags-llvm", "vcpkg" ], "architecture": { diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 1cf000d..7d1ab09 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -5,7 +5,8 @@ add_library(Core src/Core/Lib.cpp ) target_include_directories(Core PUBLIC "$") -target_compile_features(Core PUBLIC cxx_std_20) +set_compiler_warnings(Core) +set_linker_flags(Core) include(GenerateExportHeader) generate_export_header(Core From 5f6bb17c215dd1f9a072db9fc49ea85e129689d5 Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Wed, 26 Feb 2025 20:27:28 +0300 Subject: [PATCH 2/6] change comp func --- cmake/CompilerWarnings.cmake | 6 +++--- cmake/LinkerFlags.cmake | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index 9771bc6..da8ad09 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -1,6 +1,6 @@ function(set_compiler_warnings target) if (MSVC) - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(PROJECT_WARNINGS -Wall -Wextra @@ -57,7 +57,7 @@ function(set_compiler_warnings target) /Zc:throwingNew /EHsc) endif() - elseif (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang") + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") set(PROJECT_WARNINGS -fstack-protector-strong -Wall @@ -80,7 +80,7 @@ function(set_compiler_warnings target) -Wnon-virtual-dtor -Wold-style-cast ) - elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(PROJECT_WARNINGS -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 diff --git a/cmake/LinkerFlags.cmake b/cmake/LinkerFlags.cmake index 7b92679..c0cea7b 100644 --- a/cmake/LinkerFlags.cmake +++ b/cmake/LinkerFlags.cmake @@ -1,7 +1,7 @@ function(set_linker_flags target) if (MSVC) set(PROJECT_LINK_OPTIONS /machine:x64 /guard:cf) - elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(PROJECT_LINK_OPTIONS -Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen) endif() From f3dc0d742ee35933943e983a8d3c845f70b9b42f Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Wed, 26 Feb 2025 20:41:13 +0300 Subject: [PATCH 3/6] test --- cmake/CompilerWarnings.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index da8ad09..2510965 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -112,5 +112,6 @@ function(set_compiler_warnings target) message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'") endif() + message(STATUS "Detected CXX Compiler ID: ${CMAKE_CXX_COMPILER_ID}") target_compile_options(${target} PRIVATE ${PROJECT_WARNINGS}) endfunction() From 27d6dcb00375e400a1848d5e98968d7c544ef58d Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Wed, 26 Feb 2025 20:49:20 +0300 Subject: [PATCH 4/6] test --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b92ce0..9249b9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,9 @@ cmake_minimum_required(VERSION 3.26) +if(POLICY CMP0025) + cmake_policy(SET CMP0025 NEW) +endif() + include(cmake/PreventInSourceBuilds.cmake) project( From 3f1ac11e0903854a4142e732dc893e6c6ddf2f48 Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Wed, 26 Feb 2025 21:02:42 +0300 Subject: [PATCH 5/6] test --- CMakeLists.txt | 4 ---- cmake/CompilerWarnings.cmake | 3 +-- cmake/LinkerFlags.cmake | 2 +- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9249b9b..0b92ce0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,5 @@ cmake_minimum_required(VERSION 3.26) -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() - include(cmake/PreventInSourceBuilds.cmake) project( diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index 2510965..bf2dde0 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -57,7 +57,7 @@ function(set_compiler_warnings target) /Zc:throwingNew /EHsc) endif() - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + elseif (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(PROJECT_WARNINGS -fstack-protector-strong -Wall @@ -112,6 +112,5 @@ function(set_compiler_warnings target) message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'") endif() - message(STATUS "Detected CXX Compiler ID: ${CMAKE_CXX_COMPILER_ID}") target_compile_options(${target} PRIVATE ${PROJECT_WARNINGS}) endfunction() diff --git a/cmake/LinkerFlags.cmake b/cmake/LinkerFlags.cmake index c0cea7b..ec54a9c 100644 --- a/cmake/LinkerFlags.cmake +++ b/cmake/LinkerFlags.cmake @@ -1,7 +1,7 @@ function(set_linker_flags target) if (MSVC) set(PROJECT_LINK_OPTIONS /machine:x64 /guard:cf) - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + elseif (NOT APPLE AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) set(PROJECT_LINK_OPTIONS -Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen) endif() From fb16c2b592c6596c97a2e4abfe90d2a42d424f5d Mon Sep 17 00:00:00 2001 From: Emre Kovanci Date: Thu, 27 Feb 2025 15:08:33 +0300 Subject: [PATCH 6/6] minimal changes --- CMakeLists.txt | 2 +- cmake/CompilerWarnings.cmake | 8 ++++---- cmake/Features.cmake | 8 ++++++++ cmake/PreventInSourceBuilds.cmake | 2 -- cmake/Variables.cmake | 5 ----- library/test/CMakeLists.txt | 1 - 6 files changed, 13 insertions(+), 13 deletions(-) create mode 100644 cmake/Features.cmake delete mode 100644 cmake/Variables.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b92ce0..4f0c4e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.26) include(cmake/PreventInSourceBuilds.cmake) +include(cmake/Features.cmake) project( Template-CPP @@ -10,7 +11,6 @@ project( LANGUAGES CXX ) -include(cmake/Variables.cmake) include(cmake/CompilerWarnings.cmake) include(cmake/LinkerFlags.cmake) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index bf2dde0..b4dfc66 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -1,6 +1,6 @@ function(set_compiler_warnings target) - if (MSVC) - if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if(MSVC) + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(PROJECT_WARNINGS -Wall -Wextra @@ -57,7 +57,7 @@ function(set_compiler_warnings target) /Zc:throwingNew /EHsc) endif() - elseif (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + elseif(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(PROJECT_WARNINGS -fstack-protector-strong -Wall @@ -80,7 +80,7 @@ function(set_compiler_warnings target) -Wnon-virtual-dtor -Wold-style-cast ) - elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(PROJECT_WARNINGS -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 diff --git a/cmake/Features.cmake b/cmake/Features.cmake new file mode 100644 index 0000000..26b8611 --- /dev/null +++ b/cmake/Features.cmake @@ -0,0 +1,8 @@ +option(FEATURE_TESTS "Enable the tests" OFF) +if(FEATURE_TESTS) + list(APPEND VCPKG_MANIFEST_FEATURES "tests") +endif() + +if(PROJECT_IS_TOP_LEVEL) + option(BUILD_SHARED_LIBS "Build shared libs." OFF) +endif() diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake index 9ac3aef..7562057 100644 --- a/cmake/PreventInSourceBuilds.cmake +++ b/cmake/PreventInSourceBuilds.cmake @@ -1,5 +1,3 @@ -# ---- In-source guard ---- - if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) message( FATAL_ERROR diff --git a/cmake/Variables.cmake b/cmake/Variables.cmake deleted file mode 100644 index b8356b0..0000000 --- a/cmake/Variables.cmake +++ /dev/null @@ -1,5 +0,0 @@ -# ---- Developer mode ---- - -if(PROJECT_IS_TOP_LEVEL) - option(BUILD_SHARED_LIBS "Build shared libs." OFF) -endif() diff --git a/library/test/CMakeLists.txt b/library/test/CMakeLists.txt index 90e015c..11e7ede 100644 --- a/library/test/CMakeLists.txt +++ b/library/test/CMakeLists.txt @@ -7,7 +7,6 @@ set(CMAKE_CATCH_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST) add_executable(CoreTests Tests.cpp) target_link_libraries(CoreTests PRIVATE Core Catch2::Catch2WithMain) -target_compile_features(CoreTests PRIVATE cxx_std_20) # ---- Tests ----