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}")
51 # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
52 # here we are trying to workaround the problem
53 add_extra_compiler_option(-mstackrealign)
54 if(NOT HAVE_CXX_MSTACKREALIGN)
55 add_extra_compiler_option(-mpreferred-stack-boundary=2)
59 if(CMAKE_COMPILER_IS_GNUCXX)
60 # High level of warnings.
61 add_extra_compiler_option(-W)
62 add_extra_compiler_option(-Wall)
63 add_extra_compiler_option(-Werror=return-type)
64 #add_extra_compiler_option(-Werror=non-virtual-dtor)
65 add_extra_compiler_option(-Werror=address)
66 add_extra_compiler_option(-Werror=sequence-point)
67 add_extra_compiler_option(-Wformat)
68 add_extra_compiler_option(-Werror=format-security -Wformat)
69 add_extra_compiler_option(-Wmissing-declarations)
70 add_extra_compiler_option(-Wmissing-prototypes)
71 add_extra_compiler_option(-Wstrict-prototypes)
72 add_extra_compiler_option(-Wundef)
73 add_extra_compiler_option(-Winit-self)
74 add_extra_compiler_option(-Wpointer-arith)
75 add_extra_compiler_option(-Wshadow)
76 add_extra_compiler_option(-Wsign-promo)
78 if(ENABLE_NOISY_WARNINGS)
79 add_extra_compiler_option(-Wcast-align)
80 add_extra_compiler_option(-Wstrict-aliasing=2)
82 add_extra_compiler_option(-Wno-narrowing)
83 add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
84 add_extra_compiler_option(-Wno-unnamed-type-template-args)
86 add_extra_compiler_option(-fdiagnostics-show-option)
88 # The -Wno-long-long is required in 64bit systems when including sytem headers.
90 add_extra_compiler_option(-Wno-long-long)
94 if(UNIX AND NOT ANDROID)
95 add_extra_compiler_option(-pthread)
98 if(OPENCV_WARNINGS_ARE_ERRORS)
99 add_extra_compiler_option(-Werror)
102 if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
103 add_extra_compiler_option(-march=i686)
106 # Other optimizations
107 if(ENABLE_OMIT_FRAME_POINTER)
108 add_extra_compiler_option(-fomit-frame-pointer)
110 add_extra_compiler_option(-fno-omit-frame-pointer)
113 add_extra_compiler_option(-ffast-math)
116 add_extra_compiler_option("-mcpu=G3 -mtune=G5")
119 add_extra_compiler_option(-msse)
122 add_extra_compiler_option(-msse2)
125 # SSE3 and further should be disabled under MingW because it generates compiler errors
128 add_extra_compiler_option(-mavx)
131 # GCC depresses SSEx instructions when -mavx is used. Instead, it generates new AVX instructions or AVX equivalence for all SSEx instructions when needed.
132 if(NOT OPENCV_EXTRA_CXX_FLAGS MATCHES "-mavx")
134 add_extra_compiler_option(-msse3)
138 add_extra_compiler_option(-mssse3)
142 add_extra_compiler_option(-msse4.1)
146 add_extra_compiler_option(-msse4.2)
152 if(NOT APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 4)
153 if(OPENCV_EXTRA_CXX_FLAGS MATCHES "-m(sse2|avx)")
154 add_extra_compiler_option(-mfpmath=sse)# !! important - be on the same wave with x64 compilers
156 add_extra_compiler_option(-mfpmath=387)
163 add_extra_compiler_option("-pg -g")
164 # turn off incompatible options
165 foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
166 OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
167 string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
168 string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
170 elseif(NOT APPLE AND NOT ANDROID)
171 # Remove unreferenced functions: function level linking
172 add_extra_compiler_option(-ffunction-sections)
175 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
176 set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0 -DDEBUG -D_DEBUG")
177 if(BUILD_WITH_DEBUG_INFO)
178 set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -ggdb3")
183 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
184 # 64-bit portability warnings, in MSVC80
186 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
189 if(BUILD_WITH_DEBUG_INFO)
190 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
193 # Remove unreferenced functions: function level linking
194 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
195 if(NOT MSVC_VERSION LESS 1400)
196 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
198 if(BUILD_WITH_DEBUG_INFO)
199 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
202 if(ENABLE_AVX AND NOT MSVC_VERSION LESS 1600)
203 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX")
206 if(ENABLE_SSE4_1 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
207 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE4.1")
210 if(ENABLE_SSE3 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
211 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE3")
215 # 64-bit MSVC compiler uses SSE/SSE2 by default
216 if(ENABLE_SSE2 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
217 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE2")
219 if(ENABLE_SSE AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
220 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE")
224 if(ENABLE_SSE OR ENABLE_SSE2 OR ENABLE_SSE3 OR ENABLE_SSE4_1 OR ENABLE_AVX)
225 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Oi")
229 if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND ENABLE_SSE2)
230 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /fp:fast") # !! important - be on the same wave with x64 compilers
235 # Extra link libs if the user selects building static libs:
236 if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
237 # Android does not need these settings because they are already set by toolchain file
238 set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} stdc++)
239 set(OPENCV_EXTRA_FLAGS "-fPIC ${OPENCV_EXTRA_FLAGS}")
242 # Add user supplied extra options (optimization, etc...)
243 # ==========================================================
244 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS}" CACHE INTERNAL "Extra compiler options")
245 set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS}" CACHE INTERNAL "Extra compiler options for C sources")
246 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS}" CACHE INTERNAL "Extra compiler options for C++ sources")
247 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build")
248 set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG}" CACHE INTERNAL "Extra compiler options for Debug build")
249 set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS}" CACHE INTERNAL "Extra linker flags")
250 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build")
251 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Extra linker flags for Debug build")
253 #combine all "extra" options
254 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
255 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
256 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
257 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
258 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
259 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
260 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
261 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
262 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
265 # avoid warnings from MSVC about overriding the /W* option
266 # we replace /W3 with /W4 only for C++ files,
267 # since all the 3rd-party libraries OpenCV uses are in C,
268 # and we do not care about their warnings.
269 string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
270 string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
271 string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
273 if(NOT ENABLE_NOISY_WARNINGS AND MSVC_VERSION EQUAL 1400)
274 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
277 # allow extern "C" functions throw exceptions
278 foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
279 string(REPLACE "/EHsc-" "/EHs" ${flags} "${${flags}}")
280 string(REPLACE "/EHsc" "/EHs" ${flags} "${${flags}}")
282 string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
285 if(NOT ENABLE_NOISY_WARNINGS)
286 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") #class 'std::XXX' needs to have dll-interface to be used by clients of YYY