Skip to content

Commit 5f82ada

Browse files
committed
Improve compiler flags.
1 parent 29e420e commit 5f82ada

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

cmake/CompileOptions.cmake

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ if(WIN32)
7474
)
7575
endif ()
7676

77+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR MAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
78+
set(DEFAULT_COMPILE_DEFINITIONS
79+
${DEFAULT_COMPILE_DEFINITIONS}
80+
DEBUG
81+
)
82+
else()
83+
set(DEFAULT_COMPILE_DEFINITIONS
84+
${DEFAULT_COMPILE_DEFINITIONS}
85+
NDEBUG
86+
)
87+
endif()
88+
7789
#
7890
# Compile options
7991
#
@@ -91,34 +103,41 @@ if(WIN32)
91103
#add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
92104

93105
# Release
94-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
95-
# TODO: Review debug optimization
96-
#add_compile_options(/GL) # Enable debugging information
106+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
107+
# Disable optimizations
108+
add_compile_options(/Od)
109+
110+
# Multithread MSV CTR
111+
add_compile_options(/MDd)
112+
97113
##add_compile_options(/LTCG) # Enable debugging information
98114
else()
99115
add_compile_options(/GS) # Buffer Security Check
100116
add_compile_options(/GF) # Enable read-only string pooling
101-
#add_compile_options(/GW) # Enable read-only string pooling
117+
add_compile_options(/GW) # Enable read-only string pooling
118+
119+
# Multithread MSV CTR
120+
add_compile_options(/MD)
121+
122+
# Enable optimizations
123+
add_compile_options(/O2)
124+
add_compile_options(/Ob)
125+
add_compile_options(/Og)
126+
add_compile_options(/Oi)
127+
add_compile_options(/Oy)
102128
endif()
103129
endif()
104130

105131
if (PROJECT_OS_FAMILY MATCHES "unix")
106132

107133
if(APPLE)
108134
# We cannot enable "stack-protector-strong" On OS X due to a bug in clang compiler (current version 7.0.2)
109-
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
110-
add_compile_options(-fstack-protector)
111-
endif()
112135

113136
# Enable threads in OS X
114137
add_compile_options(-pthread)
115138

116139
# clang options only
117140
add_compile_options(-Wreturn-stack-address)
118-
else()
119-
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
120-
add_compile_options(-fstack-protector-strong)
121-
endif()
122141
endif()
123142

124143
if(PROJECT_OS_LINUX)
@@ -128,6 +147,14 @@ if (PROJECT_OS_FAMILY MATCHES "unix")
128147

129148
# All warnings that are not explicitly disabled are reported as errors
130149
#add_compile_options(-Werror)
150+
add_compile_options(-Wall)
151+
add_compile_options(-Wextra)
152+
153+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
154+
add_compile_options(-g)
155+
else()
156+
add_compile_options(-O3)
157+
endif()
131158

132159
# Sanitizers
133160
if(OPTION_BUILD_SANITIZER AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo"))
@@ -153,7 +180,7 @@ endif()
153180
set(DEFAULT_LINKER_OPTIONS)
154181

155182
# Use pthreads on mingw and linux
156-
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU" OR "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
183+
if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
157184
set(DEFAULT_LINKER_OPTIONS
158185
-pthread
159186
)

cmake/SecurityFlags.cmake

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
3030
endif()
3131

3232
# Detect stack protector
33-
check_c_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_C_FLAG)
33+
check_c_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_C_FLAG)
3434

35-
if(STACK_PROTECTOR_C_FLAG)
36-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
35+
if(STACK_PROTECTOR_STRONG_C_FLAG)
36+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong")
37+
38+
# use ssp-buffer-size if it is supported
39+
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
40+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
41+
endif()
42+
else()
43+
check_c_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
44+
45+
if(STACK_PROTECTOR_C_FLAG)
46+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
3747

3848
# use ssp-buffer-size if it is supported
3949
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
4050
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
4151
endif()
42-
52+
endif()
4353
endif()
4454

4555
# Detect fortify source
4656
check_c_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_C_FLAG)
4757

4858
if(FORTIFY_SOURCE_C_FLAG)
49-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -D_FORTIFY_SOURCE=2")
59+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
5060
endif()
5161

5262
endif()
@@ -64,24 +74,33 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
6474
endif()
6575

6676
# Detect stack protector
67-
check_cxx_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
77+
check_cxx_compiler_flag_stack_smashing("-fstack-protector-strong" STACK_PROTECTOR_STRONG_CXX_FLAG)
78+
79+
if(STACK_PROTECTOR_STRONG_CXX_FLAG)
80+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-strong")
6881

69-
if(STACK_PROTECTOR_CXX_FLAG)
70-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
82+
# use ssp-buffer-size if it is supported
83+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
84+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --param ssp-buffer-size=4")
85+
endif()
86+
else()
87+
check_cxx_compiler_flag_stack_smashing("-fstack-protector" STACK_PROTECTOR_CXX_FLAG)
88+
89+
if(STACK_PROTECTOR_CXX_FLAG)
90+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
7191

7292
# use ssp-buffer-size if it is supported
7393
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
74-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --param ssp-buffer-size=4")
94+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --param ssp-buffer-size=4")
7595
endif()
76-
96+
endif()
7797
endif()
7898

7999
# Detect fortify source
80100
check_cxx_compiler_flag("-D_FORTIFY_SOURCE=2" FORTIFY_SOURCE_CXX_FLAG)
81101

82102
if(FORTIFY_SOURCE_CXX_FLAG)
83-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -D_FORTIFY_SOURCE=2")
103+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -D_FORTIFY_SOURCE=2")
84104
endif()
85105

86106
endif()
87-

source/ports/py_port/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
7575
#
7676

7777
# Set SWIG flags
78-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
78+
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR MAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
7979
set(PY_PORT_DEBUG_FLAGS "-DDEBUG")
8080
else()
8181
set(PY_PORT_DEBUG_FLAGS "-DNDEBUG")

0 commit comments

Comments
 (0)