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