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