Merge pull request #12064 from seiko2plus:coreUnvintrinArithm2
[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     if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
136       add_extra_compiler_option(-Wno-missing-field-initializers)  # GCC 4.x emits warnings about {}, fixed in GCC 5+
137     endif()
138   endif()
139   add_extra_compiler_option(-fdiagnostics-show-option)
140
141   # The -Wno-long-long is required in 64bit systems when including system headers.
142   if(X86_64)
143     add_extra_compiler_option(-Wno-long-long)
144   endif()
145
146   # We need pthread's
147   if(UNIX AND NOT ANDROID AND NOT (APPLE AND CV_CLANG)) # TODO
148     add_extra_compiler_option(-pthread)
149   endif()
150
151   if(CV_CLANG)
152     add_extra_compiler_option(-Qunused-arguments)
153   endif()
154
155   if(OPENCV_WARNINGS_ARE_ERRORS)
156     add_extra_compiler_option(-Werror)
157   endif()
158
159   if(APPLE)
160     add_extra_compiler_option(-Wno-semicolon-before-method-body)
161   endif()
162
163   # Other optimizations
164   if(ENABLE_OMIT_FRAME_POINTER)
165     add_extra_compiler_option(-fomit-frame-pointer)
166   elseif(DEFINED ENABLE_OMIT_FRAME_POINTER)
167     add_extra_compiler_option(-fno-omit-frame-pointer)
168   endif()
169   if(ENABLE_FAST_MATH)
170     add_extra_compiler_option(-ffast-math)
171   endif()
172
173   # Profiling?
174   if(ENABLE_PROFILING)
175     add_extra_compiler_option("-pg -g")
176     # turn off incompatible options
177     foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
178                   OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
179       string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
180       string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
181       string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
182     endforeach()
183   elseif(NOT ((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT MSVC)
184     # Remove unreferenced functions: function level linking
185     add_extra_compiler_option(-ffunction-sections)
186     add_extra_compiler_option(-fdata-sections)
187     if(NOT APPLE AND NOT OPENCV_SKIP_GC_SECTIONS)
188       set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
189     endif()
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     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
196   endif()
197
198   if(ENABLE_INSTRUMENTATION)
199     if(NOT HAVE_CXX11)
200       message(WARNING "ENABLE_INSTRUMENTATION requires C++11 support")
201     endif()
202     set(WITH_VTK OFF) # There are issues with VTK 6.0
203   endif()
204
205   if(ENABLE_LTO)
206     add_extra_compiler_option(-flto)
207   endif()
208   if(ENABLE_THIN_LTO)
209     add_extra_compiler_option(-flto=thin)
210   endif()
211
212   set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
213   if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG} " MATCHES "-O")
214     set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
215   endif()
216   set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
217
218   if(BUILD_WITH_DEBUG_INFO)
219     # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
220     # '-g' is equal to '-g2', '-g1' produces minimal information, enough for making backtraces
221     ocv_update(OPENCV_DEBUG_OPTION "-g1")
222     if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE}" MATCHES " -g")
223       set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} ${OPENCV_DEBUG_OPTION}")
224     endif()
225   endif()
226 endif()
227
228 if(MSVC)
229   #TODO Code refactoring is required to resolve security warnings
230   #if(NOT ENABLE_BUILD_HARDENING)
231     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
232   #endif()
233
234   if(BUILD_WITH_DEBUG_INFO)
235     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
236     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
237     set(OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE} /debug")
238   endif()
239
240   # Remove unreferenced functions: function level linking
241   set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
242   if(NOT MSVC_VERSION LESS 1400)
243     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
244   endif()
245
246   if(OPENCV_WARNINGS_ARE_ERRORS)
247     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
248   endif()
249
250   if(ENABLE_LTO)
251     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /GL")
252     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /LTCG")
253   endif()
254
255   if(NOT MSVC_VERSION LESS 1800 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
256     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
257     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
258   endif()
259 endif()
260
261 # Adding additional using directory for WindowsPhone 8.0 to get Windows.winmd properly
262 if(WINRT_PHONE AND WINRT_8_0)
263   set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
264 endif()
265
266 include(cmake/OpenCVCompilerOptimizations.cmake)
267 if(COMMAND ocv_compiler_optimization_options)
268   ocv_compiler_optimization_options()
269 endif()
270 if(COMMAND ocv_compiler_optimization_options_finalize)
271   ocv_compiler_optimization_options_finalize()
272 endif()
273
274 # set default visibility to hidden
275 if((CV_GCC OR CV_CLANG)
276     AND NOT MSVC
277     AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
278     AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fvisibility")
279   add_extra_compiler_option(-fvisibility=hidden)
280   add_extra_compiler_option(-fvisibility-inlines-hidden)
281 endif()
282
283 # combine all "extra" options
284 if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
285   set(CMAKE_C_FLAGS           "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
286   set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
287   set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
288   set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
289   set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
290   set(CMAKE_C_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
291   set(CMAKE_EXE_LINKER_FLAGS         "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
292   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
293   set(CMAKE_EXE_LINKER_FLAGS_DEBUG   "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
294   set(CMAKE_SHARED_LINKER_FLAGS         "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS}")
295   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE}")
296   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG   "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_DEBUG}")
297 endif()
298
299 if(MSVC)
300   if(NOT ENABLE_NOISY_WARNINGS)
301     if(MSVC_VERSION EQUAL 1400)
302       ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
303     endif()
304   endif()
305
306   foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
307     string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
308   endforeach()
309
310   # Enable 'extern "C"' and asynchronous (division by zero, access violation) exceptions
311   if(NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG)
312     foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
313       string(REGEX REPLACE " /EH[^ ]* " " " ${flags} " ${${flags}}")
314     endforeach()
315     if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH")
316       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
317     endif()
318   endif()
319
320   if(NOT ENABLE_NOISY_WARNINGS)
321     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
322     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
323     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
324     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
325     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
326     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
327   endif()
328
329   if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
330     foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
331       string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
332     endforeach()
333     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
334   endif()
335 endif()
336
337 if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
338   link_directories("/usr/local/lib")
339 endif()
340
341 if(ENABLE_BUILD_HARDENING)
342   include(${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake)
343 endif()
344
345 if(MSVC)
346   include(cmake/OpenCVCRTLinkage.cmake)
347   add_definitions(-D_VARIADIC_MAX=10)
348 endif()
349
350 # Enable compiler options for OpenCV modules/apps/samples only (ignore 3rdparty)
351 macro(ocv_add_modules_compiler_options)
352   if(MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION)
353     foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
354       string(REPLACE "/W3" "/W4" ${flags} "${${flags}}")
355     endforeach()
356   endif()
357 endmacro()