Merge pull request #576 from bitwangyaoyao:2.4_customMat2
[profile/ivi/opencv.git] / cmake / OpenCVCompilerOptions.cmake
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}}")
6     endforeach()
7   endif()
8
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}}")
12     endforeach()
13   endif()
14 endif()
15
16 if(MSVC)
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)
23   endif()
24 endif()
25
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 "")
34
35 macro(add_extra_compiler_option option)
36   if(CMAKE_BUILD_TYPE)
37     set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
38   endif()
39   ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
40   if(${_varname})
41     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
42   endif()
43
44   ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
45   if(${_varname})
46     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
47   endif()
48 endmacro()
49
50 if(MINGW)
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)
56   endif()
57 endif()
58
59 if(OPENCV_CAN_BREAK_BINARY_COMPATIBILITY)
60   add_definitions(-DOPENCV_CAN_BREAK_BINARY_COMPATIBILITY)
61 endif()
62
63 if(CMAKE_COMPILER_IS_GNUCXX)
64   # High level of warnings.
65   add_extra_compiler_option(-W)
66   add_extra_compiler_option(-Wall)
67   add_extra_compiler_option(-Werror=return-type)
68   if(OPENCV_CAN_BREAK_BINARY_COMPATIBILITY)
69     add_extra_compiler_option(-Werror=non-virtual-dtor)
70   endif()
71   add_extra_compiler_option(-Werror=address)
72   add_extra_compiler_option(-Werror=sequence-point)
73   add_extra_compiler_option(-Wformat)
74   add_extra_compiler_option(-Werror=format-security -Wformat)
75   add_extra_compiler_option(-Wmissing-declarations)
76   add_extra_compiler_option(-Wmissing-prototypes)
77   add_extra_compiler_option(-Wstrict-prototypes)
78   add_extra_compiler_option(-Wundef)
79   add_extra_compiler_option(-Winit-self)
80   add_extra_compiler_option(-Wpointer-arith)
81   add_extra_compiler_option(-Wshadow)
82   add_extra_compiler_option(-Wsign-promo)
83
84   if(ENABLE_NOISY_WARNINGS)
85     add_extra_compiler_option(-Wcast-align)
86     add_extra_compiler_option(-Wstrict-aliasing=2)
87   else()
88     add_extra_compiler_option(-Wno-narrowing)
89     add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
90     add_extra_compiler_option(-Wno-unnamed-type-template-args)
91   endif()
92   add_extra_compiler_option(-fdiagnostics-show-option)
93
94   # The -Wno-long-long is required in 64bit systems when including sytem headers.
95   if(X86_64)
96     add_extra_compiler_option(-Wno-long-long)
97   endif()
98
99   # We need pthread's
100   if(UNIX AND NOT ANDROID)
101     add_extra_compiler_option(-pthread)
102   endif()
103
104   if(OPENCV_WARNINGS_ARE_ERRORS)
105     add_extra_compiler_option(-Werror)
106   endif()
107
108   if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
109     add_extra_compiler_option(-march=i686)
110   endif()
111
112   # Other optimizations
113   if(ENABLE_OMIT_FRAME_POINTER)
114     add_extra_compiler_option(-fomit-frame-pointer)
115   else()
116     add_extra_compiler_option(-fno-omit-frame-pointer)
117   endif()
118   if(ENABLE_FAST_MATH)
119     add_extra_compiler_option(-ffast-math)
120   endif()
121   if(ENABLE_POWERPC)
122     add_extra_compiler_option("-mcpu=G3 -mtune=G5")
123   endif()
124   if(ENABLE_SSE)
125     add_extra_compiler_option(-msse)
126   endif()
127   if(ENABLE_SSE2)
128     add_extra_compiler_option(-msse2)
129   endif()
130
131   # SSE3 and further should be disabled under MingW because it generates compiler errors
132   if(NOT MINGW)
133     if(ENABLE_AVX)
134       add_extra_compiler_option(-mavx)
135     endif()
136
137     # GCC depresses SSEx instructions when -mavx is used. Instead, it generates new AVX instructions or AVX equivalence for all SSEx instructions when needed.
138     if(NOT OPENCV_EXTRA_CXX_FLAGS MATCHES "-mavx")
139       if(ENABLE_SSE3)
140         add_extra_compiler_option(-msse3)
141       endif()
142
143       if(ENABLE_SSSE3)
144         add_extra_compiler_option(-mssse3)
145       endif()
146
147       if(ENABLE_SSE41)
148         add_extra_compiler_option(-msse4.1)
149       endif()
150
151       if(ENABLE_SSE42)
152         add_extra_compiler_option(-msse4.2)
153       endif()
154     endif()
155   endif(NOT MINGW)
156
157   if(X86 OR X86_64)
158     if(NOT APPLE AND CMAKE_SIZEOF_VOID_P EQUAL 4)
159       if(OPENCV_EXTRA_CXX_FLAGS MATCHES "-m(sse2|avx)")
160         add_extra_compiler_option(-mfpmath=sse)# !! important - be on the same wave with x64 compilers
161       else()
162         add_extra_compiler_option(-mfpmath=387)
163       endif()
164     endif()
165   endif()
166
167   # Profiling?
168   if(ENABLE_PROFILING)
169     add_extra_compiler_option("-pg -g")
170     # turn off incompatible options
171     foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
172                   OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
173       string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
174       string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
175     endforeach()
176   elseif(NOT APPLE AND NOT ANDROID)
177     # Remove unreferenced functions: function level linking
178     add_extra_compiler_option(-ffunction-sections)
179   endif()
180
181   set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
182   set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0 -DDEBUG -D_DEBUG")
183   if(BUILD_WITH_DEBUG_INFO)
184     set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -ggdb3")
185   endif()
186 endif()
187
188 if(MSVC)
189   set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
190   # 64-bit portability warnings, in MSVC80
191   if(MSVC80)
192     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
193   endif()
194
195   if(BUILD_WITH_DEBUG_INFO)
196     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
197   endif()
198
199   # Remove unreferenced functions: function level linking
200   set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
201   if(NOT MSVC_VERSION LESS 1400)
202     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
203   endif()
204   if(BUILD_WITH_DEBUG_INFO)
205     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
206   endif()
207
208   if(ENABLE_AVX AND NOT MSVC_VERSION LESS 1600)
209     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:AVX")
210   endif()
211
212   if(ENABLE_SSE4_1 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
213     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE4.1")
214   endif()
215
216   if(ENABLE_SSE3 AND CV_ICC AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
217     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE3")
218   endif()
219
220   if(NOT MSVC64)
221     # 64-bit MSVC compiler uses SSE/SSE2 by default
222     if(ENABLE_SSE2 AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
223       set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE2")
224     endif()
225     if(ENABLE_SSE AND NOT OPENCV_EXTRA_FLAGS MATCHES "/arch:")
226       set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /arch:SSE")
227     endif()
228   endif()
229
230   if(ENABLE_SSE OR ENABLE_SSE2 OR ENABLE_SSE3 OR ENABLE_SSE4_1 OR ENABLE_AVX)
231     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Oi")
232   endif()
233
234   if(X86 OR X86_64)
235     if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND ENABLE_SSE2)
236       set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /fp:fast") # !! important - be on the same wave with x64 compilers
237     endif()
238   endif()
239 endif()
240
241 # Extra link libs if the user selects building static libs:
242 if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
243   # Android does not need these settings because they are already set by toolchain file
244   set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} stdc++)
245   set(OPENCV_EXTRA_FLAGS "-fPIC ${OPENCV_EXTRA_FLAGS}")
246 endif()
247
248 # Add user supplied extra options (optimization, etc...)
249 # ==========================================================
250 set(OPENCV_EXTRA_FLAGS         "${OPENCV_EXTRA_FLAGS}"         CACHE INTERNAL "Extra compiler options")
251 set(OPENCV_EXTRA_C_FLAGS       "${OPENCV_EXTRA_C_FLAGS}"       CACHE INTERNAL "Extra compiler options for C sources")
252 set(OPENCV_EXTRA_CXX_FLAGS     "${OPENCV_EXTRA_CXX_FLAGS}"     CACHE INTERNAL "Extra compiler options for C++ sources")
253 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build")
254 set(OPENCV_EXTRA_FLAGS_DEBUG   "${OPENCV_EXTRA_FLAGS_DEBUG}"   CACHE INTERNAL "Extra compiler options for Debug build")
255 set(OPENCV_EXTRA_EXE_LINKER_FLAGS         "${OPENCV_EXTRA_EXE_LINKER_FLAGS}"         CACHE INTERNAL "Extra linker flags")
256 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build")
257 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG   "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}"   CACHE INTERNAL "Extra linker flags for Debug build")
258
259 #combine all "extra" options
260 set(CMAKE_C_FLAGS           "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
261 set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
262 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
263 set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
264 set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
265 set(CMAKE_C_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
266 set(CMAKE_EXE_LINKER_FLAGS         "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
267 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
268 set(CMAKE_EXE_LINKER_FLAGS_DEBUG   "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
269
270 if(MSVC)
271   # avoid warnings from MSVC about overriding the /W* option
272   # we replace /W3 with /W4 only for C++ files,
273   # since all the 3rd-party libraries OpenCV uses are in C,
274   # and we do not care about their warnings.
275   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS}")
276   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
277   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG}")
278
279   if(NOT ENABLE_NOISY_WARNINGS AND MSVC_VERSION EQUAL 1400)
280     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
281   endif()
282
283   # allow extern "C" functions throw exceptions
284   foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
285     string(REPLACE "/EHsc-" "/EHs" ${flags} "${${flags}}")
286     string(REPLACE "/EHsc"  "/EHs" ${flags} "${${flags}}")
287
288     string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
289   endforeach()
290
291   if(NOT ENABLE_NOISY_WARNINGS)
292     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") #class 'std::XXX' needs to have dll-interface to be used by clients of YYY
293   endif()
294 endif()