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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.26)

include(cmake/PreventInSourceBuilds.cmake)
include(cmake/Features.cmake)

project(
Template-CPP
Expand All @@ -10,7 +11,8 @@ project(
LANGUAGES CXX
)

include(cmake/Variables.cmake)
include(cmake/CompilerWarnings.cmake)
include(cmake/LinkerFlags.cmake)

if(FEATURE_TESTS)
enable_testing()
Expand Down
2 changes: 2 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
116 changes: 116 additions & 0 deletions cmake/CompilerWarnings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
function(set_compiler_warnings target)
if(MSVC)
if(CMAKE_CXX_COMPILER_ID STREQUAL "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(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
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 STREQUAL "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()
8 changes: 8 additions & 0 deletions cmake/Features.cmake
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions cmake/LinkerFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function(set_linker_flags target)
if (MSVC)
set(PROJECT_LINK_OPTIONS /machine:x64 /guard:cf)
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()

target_link_options(${target} PRIVATE ${PROJECT_LINK_OPTIONS})
endfunction()
34 changes: 0 additions & 34 deletions cmake/Platform/Common/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 0 additions & 2 deletions cmake/Platform/Linux/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"cpp-standard",
"ninja",
"gcc",
"flags-gcc-clang",
"vcpkg"
]
},
Expand All @@ -33,7 +32,6 @@
"cpp-standard",
"ninja",
"clang++",
"flags-gcc-clang",
"vcpkg"
]
}
Expand Down
2 changes: 0 additions & 2 deletions cmake/Platform/Mac/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"cpp-standard",
"ninja",
"clang++",
"flags-appleclang",
"vcpkg"
]
},
Expand All @@ -33,7 +32,6 @@
"cpp-standard",
"xcode",
"clang++",
"flags-appleclang",
"vcpkg"
]
}
Expand Down
4 changes: 0 additions & 4 deletions cmake/Platform/Windows/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"cpp-standard",
"vs2022",
"msvc",
"flags-msvc",
"vcpkg"
],
"architecture": {
Expand All @@ -41,7 +40,6 @@
"cpp-standard",
"vs2022",
"clang-cl",
"flags-llvm",
"vcpkg"
],
"architecture": {
Expand All @@ -63,7 +61,6 @@
"cpp-standard",
"ninja",
"msvc",
"flags-msvc",
"vcpkg"
],
"architecture": {
Expand All @@ -85,7 +82,6 @@
"cpp-standard",
"ninja",
"clang-cl",
"flags-llvm",
"vcpkg"
],
"architecture": {
Expand Down
2 changes: 0 additions & 2 deletions cmake/PreventInSourceBuilds.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# ---- In-source guard ----

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(
FATAL_ERROR
Expand Down
5 changes: 0 additions & 5 deletions cmake/Variables.cmake

This file was deleted.

3 changes: 2 additions & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ add_library(Core
src/Core/Lib.cpp
)
target_include_directories(Core PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/library/include>")
target_compile_features(Core PUBLIC cxx_std_20)
set_compiler_warnings(Core)
set_linker_flags(Core)

include(GenerateExportHeader)
generate_export_header(Core
Expand Down
1 change: 0 additions & 1 deletion library/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----

Expand Down
Loading