Merge pull request #12516 from seiko2plus:changeUnvMultiply16
[platform/upstream/opencv.git] / cmake / OpenCVCompilerOptions.cmake
1 if("${CMAKE_CXX_COMPILER};${CMAKE_C_COMPILER};${CMAKE_CXX_COMPILER_LAUNCHER}" MATCHES "ccache")
2   set(CMAKE_COMPILER_IS_CCACHE 1)  # TODO: FIXIT Avoid setting of CMAKE_ variables
3   set(OPENCV_COMPILER_IS_CCACHE 1)
4 endif()
5 function(access_CMAKE_COMPILER_IS_CCACHE)
6   if(NOT OPENCV_SUPPRESS_DEPRECATIONS)
7     message(WARNING "DEPRECATED: CMAKE_COMPILER_IS_CCACHE is replaced to OPENCV_COMPILER_IS_CCACHE.")
8   endif()
9 endfunction()
10 variable_watch(CMAKE_COMPILER_IS_CCACHE access_CMAKE_COMPILER_IS_CCACHE)
11 if(ENABLE_CCACHE AND NOT OPENCV_COMPILER_IS_CCACHE AND NOT CMAKE_GENERATOR MATCHES "Xcode")
12   # This works fine with Unix Makefiles and Ninja generators
13   find_host_program(CCACHE_PROGRAM ccache)
14   if(CCACHE_PROGRAM)
15     message(STATUS "Looking for ccache - found (${CCACHE_PROGRAM})")
16     get_property(__OLD_RULE_LAUNCH_COMPILE GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
17     if(__OLD_RULE_LAUNCH_COMPILE)
18       message(STATUS "Can't replace CMake compiler launcher")
19     else()
20       set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
21       # NOTE: Actually this check doesn't work as expected.
22       # "RULE_LAUNCH_COMPILE" is ignored by CMake during try_compile() step.
23       # ocv_check_compiler_flag(CXX "" IS_CCACHE_WORKS)
24       set(IS_CCACHE_WORKS 1)
25       if(IS_CCACHE_WORKS)
26         set(OPENCV_COMPILER_IS_CCACHE 1)
27       else()
28         message(STATUS "Unable to compile program with enabled ccache, reverting...")
29         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${__OLD_RULE_LAUNCH_COMPILE}")
30       endif()
31     endif()
32   else()
33     message(STATUS "Looking for ccache - not found")
34   endif()
35 endif()
36
37 if((CV_CLANG AND NOT CMAKE_GENERATOR MATCHES "Xcode")  # PCH has no support for Clang
38     OR OPENCV_COMPILER_IS_CCACHE
39 )
40   set(ENABLE_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE)
41 endif()
42
43 macro(add_extra_compiler_option option)
44   ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
45   if(${_varname})
46     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
47   endif()
48
49   ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
50   if(${_varname})
51     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
52   endif()
53 endmacro()
54
55 macro(add_extra_compiler_option_force option)
56   set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
57   set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
58 endmacro()
59
60
61 # Gets environment variable and puts its value to the corresponding preprocessor definition
62 # Useful for WINRT that has no access to environment variables
63 macro(add_env_definitions option)
64   set(value $ENV{${option}})
65   if("${value}" STREQUAL "")
66     message(WARNING "${option} environment variable is empty. Please set it to appropriate location to get correct results")
67   else()
68     string(REPLACE "\\" "\\\\" value ${value})
69   endif()
70   add_definitions("-D${option}=\"${value}\"")
71 endmacro()
72
73 if(NOT MSVC)
74   # OpenCV fails some tests when 'char' is 'unsigned' by default
75   add_extra_compiler_option(-fsigned-char)
76 endif()
77
78 if(CV_ICC AND NOT ENABLE_FAST_MATH)
79   if(MSVC)
80     add_extra_compiler_option("/fp:precise")
81   else()
82     add_extra_compiler_option("-fp-model precise")
83   endif()
84 endif()
85
86 if(CV_GCC OR CV_CLANG)
87   # High level of warnings.
88   add_extra_compiler_option(-W)
89   if (NOT MSVC)
90     # clang-cl interprets -Wall as MSVC would: -Weverything, which is more than
91     # we want.
92     add_extra_compiler_option(-Wall)
93   endif()
94   add_extra_compiler_option(-Werror=return-type)
95   add_extra_compiler_option(-Werror=non-virtual-dtor)
96   add_extra_compiler_option(-Werror=address)
97   add_extra_compiler_option(-Werror=sequence-point)
98   add_extra_compiler_option(-Wformat)
99   add_extra_compiler_option(-Werror=format-security -Wformat)
100   add_extra_compiler_option(-Wmissing-declarations)
101   add_extra_compiler_option(-Wmissing-prototypes)
102   add_extra_compiler_option(-Wstrict-prototypes)
103   add_extra_compiler_option(-Wundef)
104   add_extra_compiler_option(-Winit-self)
105   add_extra_compiler_option(-Wpointer-arith)
106   add_extra_compiler_option(-Wshadow)
107   add_extra_compiler_option(-Wsign-promo)
108   add_extra_compiler_option(-Wuninitialized)
109   add_extra_compiler_option(-Winit-self)
110   if(HAVE_CXX11)
111     if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT ENABLE_PRECOMPILED_HEADERS)
112       add_extra_compiler_option(-Wsuggest-override)
113     elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
114       add_extra_compiler_option(-Winconsistent-missing-override)
115     endif()
116   endif()
117
118   if(ENABLE_NOISY_WARNINGS)
119     add_extra_compiler_option(-Wcast-align)
120     add_extra_compiler_option(-Wstrict-aliasing=2)
121   else()
122     add_extra_compiler_option(-Wno-narrowing)
123     add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
124     add_extra_compiler_option(-Wno-unnamed-type-template-args)
125     add_extra_compiler_option(-Wno-comment)
126     if(NOT OPENCV_SKIP_IMPLICIT_FALLTHROUGH
127         AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES "implicit-fallthrough"
128         AND (CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
129     )
130       add_extra_compiler_option(-Wimplicit-fallthrough=3)
131     endif()
132     if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
133       add_extra_compiler_option(-Wno-strict-overflow) # Issue appears when compiling surf.cpp from opencv_contrib/modules/xfeatures2d
134     endif()
135   endif()
136   add_extra_compiler_option(-fdiagnostics-show-option)
137
138   # The -Wno-long-long is required in 64bit systems when including system headers.
139   if(X86_64)
140     add_extra_compiler_option(-Wno-long-long)
141   endif()
142
143   # We need pthread's
144   if(UNIX AND NOT ANDROID AND NOT (APPLE AND CV_CLANG)) # TODO
145     add_extra_compiler_option(-pthread)
146   endif()
147
148   if(CV_CLANG)
149     add_extra_compiler_option(-Qunused-arguments)
150   endif()
151
152   if(OPENCV_WARNINGS_ARE_ERRORS)
153     add_extra_compiler_option(-Werror)
154   endif()
155
156   if(APPLE)
157     add_extra_compiler_option(-Wno-semicolon-before-method-body)
158   endif()
159
160   # Other optimizations
161   if(ENABLE_OMIT_FRAME_POINTER)
162     add_extra_compiler_option(-fomit-frame-pointer)
163   elseif(DEFINED ENABLE_OMIT_FRAME_POINTER)
164     add_extra_compiler_option(-fno-omit-frame-pointer)
165   endif()
166   if(ENABLE_FAST_MATH)
167     add_extra_compiler_option(-ffast-math)
168   endif()
169
170   # Profiling?
171   if(ENABLE_PROFILING)
172     add_extra_compiler_option("-pg -g")
173     # turn off incompatible options
174     foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
175                   OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
176       string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
177       string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
178       string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
179     endforeach()
180   elseif(NOT ((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT MSVC)
181     # Remove unreferenced functions: function level linking
182     add_extra_compiler_option(-ffunction-sections)
183     add_extra_compiler_option(-fdata-sections)
184     if(NOT APPLE AND NOT OPENCV_SKIP_GC_SECTIONS)
185       set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
186     endif()
187   endif()
188
189   if(ENABLE_COVERAGE)
190     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} --coverage")
191     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --coverage")
192     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
193   endif()
194
195   if(ENABLE_INSTRUMENTATION)
196     if(NOT HAVE_CXX11)
197       message(WARNING "ENABLE_INSTRUMENTATION requires C++11 support")
198     endif()
199     set(WITH_VTK OFF) # There are issues with VTK 6.0
200   endif()
201
202   if(ENABLE_LTO)
203     add_extra_compiler_option(-flto)
204   endif()
205   if(ENABLE_THIN_LTO)
206     add_extra_compiler_option(-flto=thin)
207   endif()
208
209   set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
210   if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG} " MATCHES "-O")
211     set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
212   endif()
213   set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
214
215   if(BUILD_WITH_DEBUG_INFO)
216     # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
217     # '-g' is equal to '-g2', '-g1' produces minimal information, enough for making backtraces
218     ocv_update(OPENCV_DEBUG_OPTION "-g1")
219     if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE}" MATCHES " -g")
220       set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} ${OPENCV_DEBUG_OPTION}")
221     endif()
222   endif()
223 endif()
224
225 if(MSVC)
226   #TODO Code refactoring is required to resolve security warnings
227   #if(NOT ENABLE_BUILD_HARDENING)
228     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
229   #endif()
230
231   if(BUILD_WITH_DEBUG_INFO)
232     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
233     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
234     set(OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE} /debug")
235   endif()
236
237   # Remove unreferenced functions: function level linking
238   set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
239   if(NOT MSVC_VERSION LESS 1400)
240     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
241   endif()
242
243   if(OPENCV_WARNINGS_ARE_ERRORS)
244     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
245   endif()
246
247   if(ENABLE_LTO)
248     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /GL")
249     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /LTCG")
250   endif()
251
252   if(NOT MSVC_VERSION LESS 1800 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
253     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
254     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
255   endif()
256 endif()
257
258 # Adding additional using directory for WindowsPhone 8.0 to get Windows.winmd properly
259 if(WINRT_PHONE AND WINRT_8_0)
260   set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
261 endif()
262
263 include(cmake/OpenCVCompilerOptimizations.cmake)
264 if(COMMAND ocv_compiler_optimization_options)
265   ocv_compiler_optimization_options()
266 endif()
267 if(COMMAND ocv_compiler_optimization_options_finalize)
268   ocv_compiler_optimization_options_finalize()
269 endif()
270
271 # set default visibility to hidden
272 if((CV_GCC OR CV_CLANG)
273     AND NOT MSVC
274     AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
275     AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fvisibility")
276   add_extra_compiler_option(-fvisibility=hidden)
277   add_extra_compiler_option(-fvisibility-inlines-hidden)
278 endif()
279
280 # combine all "extra" options
281 if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
282   set(CMAKE_C_FLAGS           "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
283   set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
284   set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
285   set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
286   set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
287   set(CMAKE_C_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
288   set(CMAKE_EXE_LINKER_FLAGS         "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
289   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
290   set(CMAKE_EXE_LINKER_FLAGS_DEBUG   "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
291   set(CMAKE_SHARED_LINKER_FLAGS         "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS}")
292   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE}")
293   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG   "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_DEBUG}")
294 endif()
295
296 if(MSVC)
297   if(NOT ENABLE_NOISY_WARNINGS)
298     if(MSVC_VERSION EQUAL 1400)
299       ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
300     endif()
301   endif()
302
303   foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
304     string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
305   endforeach()
306
307   # Enable 'extern "C"' and asynchronous (division by zero, access violation) exceptions
308   if(NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG)
309     foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
310       string(REGEX REPLACE " /EH[^ ]* " " " ${flags} " ${${flags}}")
311     endforeach()
312     if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH")
313       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
314     endif()
315   endif()
316
317   if(NOT ENABLE_NOISY_WARNINGS)
318     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
319     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
320     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
321     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
322     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
323     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
324   endif()
325
326   if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
327     foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
328       string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
329     endforeach()
330     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
331   endif()
332 endif()
333
334 if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
335   link_directories("/usr/local/lib")
336 endif()
337
338 if(ENABLE_BUILD_HARDENING)
339   include(${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake)
340 endif()
341
342 if(MSVC)
343   include(cmake/OpenCVCRTLinkage.cmake)
344   add_definitions(-D_VARIADIC_MAX=10)
345 endif()
346
347 # Enable compiler options for OpenCV modules/apps/samples only (ignore 3rdparty)
348 macro(ocv_add_modules_compiler_options)
349   if(MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION)
350     foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
351       string(REPLACE "/W3" "/W4" ${flags} "${${flags}}")
352     endforeach()
353   endif()
354 endmacro()