-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcompiler_settings.cmake
155 lines (132 loc) · 5.85 KB
/
compiler_settings.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Compiler recognition
set(COMPILER_MSVC 0)
set(COMPILER_GCC 0)
set(COMPILER_CLANG 0)
# ToDo: extend as necessary and remove common assumptions
if(MSVC)
set(COMPILER_MSVC 1)
else()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(COMPILER_CLANG 1)
else()
set(COMPILER_GCC 1)
endif()
set(USE_GCC_FLAGS 1)
endif()
function(set_compiler_flags target)
if(NOT MSVC)
set_gcc_flags()
target_compile_options(${target} PRIVATE "${AWS_COMPILER_FLAGS}")
string(REPLACE ";" " " _TMP "${AWS_COMPILER_FLAGS}")
set(PKG_CONFIG_CFLAGS "${_TMP}" CACHE INTERNAL "C++ compiler flags which affect the ABI")
endif()
endfunction()
function(set_compiler_warnings target)
if(NOT MSVC)
set_gcc_warnings()
target_compile_options(${target} PRIVATE "${AWS_COMPILER_WARNINGS}")
endif()
endfunction()
macro(set_gcc_flags)
list(APPEND AWS_COMPILER_FLAGS "-fno-exceptions" "-std=c++${CPP_STANDARD}")
if(NOT BUILD_SHARED_LIBS)
list(APPEND AWS_COMPILER_FLAGS "-fPIC")
endif()
if(NOT ENABLE_RTTI)
list(APPEND AWS_COMPILER_FLAGS "-fno-rtti")
endif()
if(MINIMIZE_SIZE AND COMPILER_GCC)
list(APPEND AWS_COMPILER_FLAGS "-s")
endif()
if(NOT BUILD_SHARED_LIBS AND NOT ENABLE_VIRTUAL_OPERATIONS)
list(APPEND AWS_COMPILER_FLAGS "-ffunction-sections;-fdata-sections")
endif()
endmacro()
macro(set_gcc_warnings)
list(APPEND AWS_COMPILER_WARNINGS "-Wall" "-Werror" "-pedantic" "-Wextra")
if(COMPILER_CLANG)
if(PLATFORM_ANDROID)
# when using clang with libc and API lower than 21 we need to include Android support headers and ignore the gnu-include-next warning.
if(ANDROID_STL MATCHES "libc" AND ANDROID_NATIVE_API_LEVEL_NUM LESS "21")
# NDK lower than 12 doesn't support ignoring the gnu-include-next warning so we need to disable pedantic mode.
if(NDK_RELEASE_NUMBER LESS "12000")
string(REGEX REPLACE "-pedantic" "" AWS_COMPILER_WARNINGS "${AWS_COMPILER_WARNINGS}")
else()
list(APPEND AWS_COMPILER_WARNINGS "-Wno-gnu-include-next")
endif()
endif()
endif()
endif()
# when using gcc we need to ignore warnings for missing initalizers as it was identified as a bug in gcc
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
list(APPEND AWS_COMPILER_WARNINGS "-Wno-missing-field-initializers")
endif()
endmacro()
macro(set_msvc_flags)
if(MSVC)
# enable parallel builds
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
# some of the clients are exceeding the 16-bit code section limit when building x64 debug, so use /bigobj when we build
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
# do not assume charset
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
if(NOT ENABLE_RTTI)
string(REGEX REPLACE "/GR " " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
endif()
# special windows build options:
# debug info: pdbs with dlls, embedded in static libs
# release optimizations to purely focus on size, override debug info settings as necessary
if(BUILD_SHARED_LIBS)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
else()
if(CMAKE_CXX_FLAGS MATCHES "/Zi")
string(REGEX REPLACE "/Zi" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()
if(CMAKE_CXX_FLAGS_DEBUG MATCHES "/Zi")
message(STATUS "Clearing pdb setting")
string(REGEX REPLACE "/Zi" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()
# put Z7 in config-specific flags so we can strip from release if we're concerned about size
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Z7")
endif()
if(MINIMIZE_SIZE)
# strip debug info from release
string(REGEX REPLACE "/Z[a-zA-Z0-9]" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "/DEBUG" "" CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}")
# strip optimization settings and replace with
string(REGEX REPLACE "/O[a-zA-Z0-9]*" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
# pure size flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O1 /Ob0 /Os")
endif()
endif()
endmacro()
macro(set_msvc_warnings)
if(MSVC)
#remove bogus errors at generation time if these variables haven't been manually set
if(NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;MinSizeRel;RelWithDebInfo")
endif()
if(NOT CMAKE_CXX_FLAGS_DEBUGOPT)
set(CMAKE_CXX_FLAGS_DEBUGOPT "")
endif()
if(NOT CMAKE_EXE_LINKER_FLAGS_DEBUGOPT)
set(CMAKE_EXE_LINKER_FLAGS_DEBUGOPT "")
endif()
if(NOT CMAKE_SHARED_LINKER_FLAGS_DEBUGOPT)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUGOPT "")
endif()
# warnings as errors, max warning level (4)
if(NOT CMAKE_CXX_FLAGS MATCHES "/WX")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
endif()
# taken from http://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
endif(MSVC)
endmacro()