1
if(MINGW OR (X86 AND UNIX AND NOT APPLE))
2
# mingw compiler is known to produce unstable SSE code with -O3 hence we are trying to use -O2 instead
3
if(CMAKE_COMPILER_IS_GNUCXX)
4
foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
5
string(REPLACE "-O3" "-O2" ${flags} "${${flags}}")
9
if(CMAKE_COMPILER_IS_GNUCC)
10
foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
11
string(REPLACE "-O3" "-O2" ${flags} "${${flags}}")
17
string(REGEX REPLACE "^ *| * $" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
18
string(REGEX REPLACE "^ *| * $" "" CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT}")
19
if(CMAKE_CXX_FLAGS STREQUAL CMAKE_CXX_FLAGS_INIT)
20
# override cmake default exception handling option
21
string(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
22
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "Flags used by the compiler during all build types." FORCE)
26
set(OPENCV_EXTRA_FLAGS "")
27
set(OPENCV_EXTRA_C_FLAGS "")
28
set(OPENCV_EXTRA_CXX_FLAGS "")
29
set(OPENCV_EXTRA_FLAGS_RELEASE "")
30
set(OPENCV_EXTRA_FLAGS_DEBUG "")
31
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "")
32
set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "")
33
set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "")
35
macro(add_extra_compiler_option option)
37
set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
39
ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
41
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
44
ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
46
set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
50
# Gets environment variable and puts its value to the corresponding preprocessor definition
51
# Useful for WINRT that has no access to environment variables
52
macro(add_env_definitions option)
53
set(value $ENV{${option}})
54
if("${value}" STREQUAL "")
55
message(WARNING "${option} environment variable is empty. Please set it to appropriate location to get correct results")
57
string(REPLACE "\\" "\\\\" value ${value})
59
add_definitions("-D${option}=\"${value}\"")
62
# OpenCV fails some tests when 'char' is 'unsigned' by default
63
add_extra_compiler_option(-fsigned-char)
66
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
67
# here we are trying to workaround the problem
68
add_extra_compiler_option(-mstackrealign)
69
if(NOT HAVE_CXX_MSTACKREALIGN)
70
add_extra_compiler_option(-mpreferred-stack-boundary=2)
74
if(CMAKE_COMPILER_IS_GNUCXX)
75
# High level of warnings.
76
add_extra_compiler_option(-W)
77
add_extra_compiler_option(-Wall)
78
add_extra_compiler_option(-Werror=return-type)
79
add_extra_compiler_option(-Werror=non-virtual-dtor)
80
add_extra_compiler_option(-Werror=address)
81
add_extra_compiler_option(-Werror=sequence-point)
82
add_extra_compiler_option(-Wformat)
83
add_extra_compiler_option(-Werror=format-security -Wformat)
84
add_extra_compiler_option(-Wmissing-declarations)
85
add_extra_compiler_option(-Wmissing-prototypes)
86
add_extra_compiler_option(-Wstrict-prototypes)
87
add_extra_compiler_option(-Wundef)
88
add_extra_compiler_option(-Winit-self)
89
add_extra_compiler_option(-Wpointer-arith)
90
add_extra_compiler_option(-Wshadow)
91
add_extra_compiler_option(-Wsign-promo)
93
if(ENABLE_NOISY_WARNINGS)
94
add_extra_compiler_option(-Wcast-align)
95
add_extra_compiler_option(-Wstrict-aliasing=2)
97
add_extra_compiler_option(-Wno-narrowing)
98
add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
99
add_extra_compiler_option(-Wno-unnamed-type-template-args)
101
add_extra_compiler_option(-fdiagnostics-show-option)
103
# The -Wno-long-long is required in 64bit systems when including sytem headers.
105
add_extra_compiler_option(-Wno-long-long)
109
if(UNIX AND NOT ANDROID AND NOT (APPLE AND CMAKE_COMPILER_IS_CLANGCXX))
110
add_extra_compiler_option(-pthread)
113
if(CMAKE_COMPILER_IS_CLANGCXX)
114
add_extra_compiler_option(-Qunused-arguments)
117
if(OPENCV_WARNINGS_ARE_ERRORS)
118
add_extra_compiler_option(-Werror)
121
if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
122
add_extra_compiler_option(-march=i686)
126
add_extra_compiler_option(-Wno-semicolon-before-method-body)
129
# Other optimizations
130
if(ENABLE_OMIT_FRAME_POINTER)
131
add_extra_compiler_option(-fomit-frame-pointer)
133
add_extra_compiler_option(-fno-omit-frame-pointer)
136
add_extra_compiler_option(-ffast-math)
139
add_extra_compiler_option("-mcpu=G3 -mtune=G5")
142
add_extra_compiler_option(-msse)
145
add_extra_compiler_option(-msse2)
146
elseif(X86 OR X86_64)
147
add_extra_compiler_option(-mno-sse2)
150
add_extra_compiler_option("-mfpu=neon")
152
if(ENABLE_VFPV3 AND NOT ENABLE_NEON)
153
add_extra_compiler_option("-mfpu=vfpv3")
156
# SSE3 and further should be disabled under MingW because it generates compiler errors
159
add_extra_compiler_option(-mavx)
160
elseif(X86 OR X86_64)
161
add_extra_compiler_option(-mno-avx)
164
add_extra_compiler_option(-mavx2)
167
add_extra_compiler_option(-mfma)
171
# GCC depresses SSEx instructions when -mavx is used. Instead, it generates new AVX instructions or AVX equivalence for all SSEx instructions when needed.
172
if(NOT OPENCV_EXTRA_CXX_FLAGS MATCHES "-mavx")
174
add_extra_compiler_option(-msse3)
175
elseif(X86 OR X86_64)
176
add_extra_compiler_option(-mno-sse3)
180
add_extra_compiler_option(-mssse3)
181
elseif(X86 OR X86_64)
182
add_extra_compiler_option(-mno-ssse3)
186
add_extra_compiler_option(-msse4.1)
187
elseif(X86 OR X86_64)
188
add_extra_compiler_option(-mno-sse4.1)
192
add_extra_compiler_option(-msse4.2)
193
elseif(X86 OR X86_64)
194
add_extra_compiler_option(-mno-sse4.2)
198
add_extra_compiler_option(-mpopcnt)
204
if(NOT APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 4)
205
if(OPENCV_EXTRA_CXX_FLAGS MATCHES "-m(sse2|avx)")
206
add_extra_compiler_option(-mfpmath=sse)# !! important - be on the same wave with x64 compilers
208
add_extra_compiler_option(-mfpmath=387)
215
add_extra_compiler_option("-pg -g")
216
# turn off incompatible options
217
foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
218
OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
219
string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
220
string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
222
elseif(NOT APPLE AND NOT ANDROID)
223
# Remove unreferenced functions: function level linking
224
add_extra_compiler_option(-ffunction-sections)
228
set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} --coverage")
229
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --coverage")
232
set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
233
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0 -DDEBUG -D_DEBUG")
237
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
238
# 64-bit portability warnings, in MSVC80
240
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
243
if(BUILD_WITH_DEBUG_INFO)
244
set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
247
# Remove unreferenced functions: function level linking
248
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
249
if(NOT MSVC_VERSION LESS 1400)
250
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
252
if(BUILD_WITH_DEBUG_INFO)
253
set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
256
if(ENABLE_AVX2 AND NOT MSVC_VERSION LESS 1800)
257
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX2")
259
if(ENABLE_AVX AND NOT MSVC_VERSION LESS 1600 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
260
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX")
263
if(ENABLE_SSE4_1 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
264
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE4.1")
267
if(ENABLE_SSE3 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
268
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE3")
272
# 64-bit MSVC compiler uses SSE/SSE2 by default
273
if(ENABLE_SSE2 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
274
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE2")
276
if(ENABLE_SSE AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
277
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE")
281
if(ENABLE_SSE OR ENABLE_SSE2 OR ENABLE_SSE3 OR ENABLE_SSE4_1 OR ENABLE_AVX OR ENABLE_AVX2)
282
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Oi")
286
if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND ENABLE_SSE2)
287
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /fp:fast") # !! important - be on the same wave with x64 compilers
291
if(OPENCV_WARNINGS_ARE_ERRORS)
292
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
296
if(MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
297
set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
298
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
301
# Adding additional using directory for WindowsPhone 8.0 to get Windows.winmd properly
302
if(WINRT_PHONE AND WINRT_8_0)
303
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
306
# Extra link libs if the user selects building static libs:
307
if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
308
# Android does not need these settings because they are already set by toolchain file
309
set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} stdc++)
310
set(OPENCV_EXTRA_FLAGS "-fPIC ${OPENCV_EXTRA_FLAGS}")
313
# Add user supplied extra options (optimization, etc...)
314
# ==========================================================
315
set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS}" CACHE INTERNAL "Extra compiler options")
316
set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS}" CACHE INTERNAL "Extra compiler options for C sources")
317
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS}" CACHE INTERNAL "Extra compiler options for C++ sources")
318
set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build")
319
set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG}" CACHE INTERNAL "Extra compiler options for Debug build")
320
set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS}" CACHE INTERNAL "Extra linker flags")
321
set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build")
322
set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Extra linker flags for Debug build")
324
# set default visibility to hidden
325
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_OPENCV_GCC_VERSION_NUM GREATER 399)
326
add_extra_compiler_option(-fvisibility=hidden)
327
add_extra_compiler_option(-fvisibility-inlines-hidden)
330
#combine all "extra" options
331
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
332
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
333
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
334
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
335
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
336
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
337
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
338
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
339
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
342
# avoid warnings from MSVC about overriding the /W* option
343
# we replace /W3 with /W4 only for C++ files,
344
# since all the 3rd-party libraries OpenCV uses are in C,
345
# and we do not care about their warnings.
346
string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
347
string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
348
string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
350
if(NOT ENABLE_NOISY_WARNINGS AND MSVC_VERSION EQUAL 1400)
351
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
354
# allow extern "C" functions throw exceptions
355
foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
356
string(REPLACE "/EHsc-" "/EHs" ${flags} "${${flags}}")
357
string(REPLACE "/EHsc" "/EHs" ${flags} "${${flags}}")
359
string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
362
if(NOT ENABLE_NOISY_WARNINGS)
363
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
364
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
365
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
366
ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'