Skip to content

Commit d22df68

Browse files
authored
Merge pull request #10 from emrekovanci/comp_link_flags
Comp link flags
2 parents f279b20 + fb16c2b commit d22df68

File tree

13 files changed

+140
-52
lines changed

13 files changed

+140
-52
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.26)
22

33
include(cmake/PreventInSourceBuilds.cmake)
4+
include(cmake/Features.cmake)
45

56
project(
67
Template-CPP
@@ -10,7 +11,8 @@ project(
1011
LANGUAGES CXX
1112
)
1213

13-
include(cmake/Variables.cmake)
14+
include(cmake/CompilerWarnings.cmake)
15+
include(cmake/LinkerFlags.cmake)
1416

1517
if(FEATURE_TESTS)
1618
enable_testing()

app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
add_executable(Application App.cpp)
44
target_link_libraries(Application PRIVATE Core)
5+
set_compiler_warnings(Application)
6+
set_linker_flags(Application)
57
install(TARGETS Application)

cmake/CompilerWarnings.cmake

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
function(set_compiler_warnings target)
2+
if(MSVC)
3+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
4+
set(PROJECT_WARNINGS
5+
-Wall
6+
-Wextra
7+
-Wshadow
8+
-Wpedantic
9+
-Wunused
10+
-Wformat=2
11+
-Wnull-dereference
12+
-Wnon-virtual-dtor
13+
-Woverloaded-virtual
14+
-Wold-style-cast
15+
-Wno-missing-prototypes
16+
-Wno-switch-enum
17+
-Wno-c++98-compat
18+
-Wno-c++98-compat-pedantic
19+
/EHsc)
20+
else()
21+
# Note that all the flags after /W4 are required for MSVC to conform to the language standard
22+
set(PROJECT_WARNINGS
23+
/guard:cf
24+
/utf-8
25+
/diagnostics:caret
26+
/sdl
27+
/w14165
28+
/w44242
29+
/w44254
30+
/w44263
31+
/w34265
32+
/w34287
33+
/w44296
34+
/w44388
35+
/w44464
36+
/w14545
37+
/w14546
38+
/w14547
39+
/w14549
40+
/w14555
41+
/w34619
42+
/w34640
43+
/w24826
44+
/w14905
45+
/w14906
46+
/w14928
47+
/w45038
48+
/W4
49+
/permissive-
50+
/volatile:iso
51+
/Zc:inline
52+
/Zc:preprocessor
53+
/Zc:enumTypes
54+
/Zc:lambda
55+
/Zc:__cplusplus
56+
/Zc:externConstexpr
57+
/Zc:throwingNew
58+
/EHsc)
59+
endif()
60+
elseif(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
61+
set(PROJECT_WARNINGS
62+
-fstack-protector-strong
63+
-Wall
64+
-Wextra
65+
-Wpedantic
66+
-Wconversion
67+
-Wsign-conversion
68+
-Wcast-qual
69+
-Wformat=2
70+
-Wundef
71+
-Werror=float-equal
72+
-Wshadow
73+
-Wcast-align
74+
-Wunused
75+
-Wnull-dereference
76+
-Wdouble-promotion
77+
-Wimplicit-fallthrough
78+
-Wextra-semi
79+
-Woverloaded-virtual
80+
-Wnon-virtual-dtor
81+
-Wold-style-cast
82+
)
83+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
84+
set(PROJECT_WARNINGS
85+
-U_FORTIFY_SOURCE
86+
-D_FORTIFY_SOURCE=3
87+
-D_GLIBCXX_ASSERTIONS=1
88+
-fstack-protector-strong
89+
-fcf-protection=full
90+
-fstack-clash-protection
91+
-Wall
92+
-Wextra
93+
-Wpedantic
94+
-Wconversion
95+
-Wsign-conversion
96+
-Wcast-qual
97+
-Wformat=2
98+
-Wundef
99+
-Werror=float-equal
100+
-Wshadow
101+
-Wcast-align
102+
-Wunused
103+
-Wnull-dereference
104+
-Wdouble-promotion
105+
-Wimplicit-fallthrough
106+
-Wextra-semi
107+
-Woverloaded-virtual
108+
-Wnon-virtual-dtor
109+
-Wold-style-cast
110+
)
111+
else()
112+
message(AUTHOR_WARNING "No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'")
113+
endif()
114+
115+
target_compile_options(${target} PRIVATE ${PROJECT_WARNINGS})
116+
endfunction()

cmake/Features.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
option(FEATURE_TESTS "Enable the tests" OFF)
2+
if(FEATURE_TESTS)
3+
list(APPEND VCPKG_MANIFEST_FEATURES "tests")
4+
endif()
5+
6+
if(PROJECT_IS_TOP_LEVEL)
7+
option(BUILD_SHARED_LIBS "Build shared libs." OFF)
8+
endif()

cmake/LinkerFlags.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function(set_linker_flags target)
2+
if (MSVC)
3+
set(PROJECT_LINK_OPTIONS /machine:x64 /guard:cf)
4+
elseif (NOT APPLE AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
5+
set(PROJECT_LINK_OPTIONS -Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen)
6+
endif()
7+
8+
target_link_options(${target} PRIVATE ${PROJECT_LINK_OPTIONS})
9+
endfunction()

cmake/Platform/Common/CMakePresets.json

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -159,40 +159,6 @@
159159
"cacheVariables": {
160160
"CMAKE_CXX_COMPILER": "clang++"
161161
}
162-
},
163-
{
164-
"name": "flags-msvc",
165-
"description": "Note that all the flags after /W4 are required for MSVC to conform to the language standard",
166-
"hidden": true,
167-
"cacheVariables": {
168-
"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",
169-
"CMAKE_EXE_LINKER_FLAGS": "/machine:x64 /guard:cf",
170-
"CMAKE_SHARED_LINKER_FLAGS": "/machine:x64 /guard:cf"
171-
}
172-
},
173-
{
174-
"name": "flags-llvm",
175-
"hidden": true,
176-
"cacheVariables": {
177-
"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"
178-
}
179-
},
180-
{
181-
"name": "flags-gcc-clang",
182-
"description": "These flags are supported by both GCC and Clang",
183-
"hidden": true,
184-
"cacheVariables": {
185-
"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",
186-
"CMAKE_EXE_LINKER_FLAGS": "-Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen",
187-
"CMAKE_SHARED_LINKER_FLAGS": "-Wl,--allow-shlib-undefined,--as-needed,-z,noexecstack,-z,relro,-z,now,-z,nodlopen"
188-
}
189-
},
190-
{
191-
"name": "flags-appleclang",
192-
"hidden": true,
193-
"cacheVariables": {
194-
"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"
195-
}
196162
}
197163
],
198164
"buildPresets": [

cmake/Platform/Linux/CMakePresets.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"cpp-standard",
2020
"ninja",
2121
"gcc",
22-
"flags-gcc-clang",
2322
"vcpkg"
2423
]
2524
},
@@ -33,7 +32,6 @@
3332
"cpp-standard",
3433
"ninja",
3534
"clang++",
36-
"flags-gcc-clang",
3735
"vcpkg"
3836
]
3937
}

cmake/Platform/Mac/CMakePresets.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"cpp-standard",
2020
"ninja",
2121
"clang++",
22-
"flags-appleclang",
2322
"vcpkg"
2423
]
2524
},
@@ -33,7 +32,6 @@
3332
"cpp-standard",
3433
"xcode",
3534
"clang++",
36-
"flags-appleclang",
3735
"vcpkg"
3836
]
3937
}

cmake/Platform/Windows/CMakePresets.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"cpp-standard",
2020
"vs2022",
2121
"msvc",
22-
"flags-msvc",
2322
"vcpkg"
2423
],
2524
"architecture": {
@@ -41,7 +40,6 @@
4140
"cpp-standard",
4241
"vs2022",
4342
"clang-cl",
44-
"flags-llvm",
4543
"vcpkg"
4644
],
4745
"architecture": {
@@ -63,7 +61,6 @@
6361
"cpp-standard",
6462
"ninja",
6563
"msvc",
66-
"flags-msvc",
6764
"vcpkg"
6865
],
6966
"architecture": {
@@ -85,7 +82,6 @@
8582
"cpp-standard",
8683
"ninja",
8784
"clang-cl",
88-
"flags-llvm",
8985
"vcpkg"
9086
],
9187
"architecture": {

cmake/PreventInSourceBuilds.cmake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# ---- In-source guard ----
2-
31
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
42
message(
53
FATAL_ERROR

0 commit comments

Comments
 (0)