Merge pull request #10629 from GlueCrow:ocl_mog2_test_fix
[platform/upstream/opencv.git] / cmake / OpenCVCompilerOptions.cmake
1 if(ENABLE_CCACHE AND NOT CMAKE_COMPILER_IS_CCACHE)
2   # This works fine with Unix Makefiles and Ninja generators
3   find_host_program(CCACHE_PROGRAM ccache)
4   if(CCACHE_PROGRAM)
5     message(STATUS "Looking for ccache - found (${CCACHE_PROGRAM})")
6     get_property(__OLD_RULE_LAUNCH_COMPILE GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
7     if(__OLD_RULE_LAUNCH_COMPILE)
8       message(STATUS "Can't replace CMake compiler launcher")
9     else()
10       set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
11       # NOTE: Actually this check doesn't work as expected.
12       # "RULE_LAUNCH_COMPILE" is ignored by CMake during try_compile() step.
13       # ocv_check_compiler_flag(CXX "" IS_CCACHE_WORKS)
14       set(IS_CCACHE_WORKS 1)
15       if(IS_CCACHE_WORKS)
16         set(CMAKE_COMPILER_IS_CCACHE 1)
17       else()
18         message(STATUS "Unable to compile program with enabled ccache, reverting...")
19         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${__OLD_RULE_LAUNCH_COMPILE}")
20       endif()
21     endif()
22   else()
23     message(STATUS "Looking for ccache - not found")
24   endif()
25 endif()
26
27 if((CMAKE_COMPILER_IS_CLANGCXX OR CMAKE_COMPILER_IS_CLANGCC OR CMAKE_COMPILER_IS_CCACHE) AND NOT CMAKE_GENERATOR MATCHES "Xcode")
28   set(ENABLE_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE)
29 endif()
30
31 if(MSVC)
32   string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
33   string(STRIP "${CMAKE_CXX_FLAGS_INIT}" CMAKE_CXX_FLAGS_INIT)
34   if(CMAKE_CXX_FLAGS STREQUAL CMAKE_CXX_FLAGS_INIT)
35     # override cmake default exception handling option
36     string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
37     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
38     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}"  CACHE STRING "Flags used by the compiler during all build types." FORCE)
39   endif()
40 endif()
41
42 set(OPENCV_EXTRA_FLAGS "")
43 set(OPENCV_EXTRA_C_FLAGS "")
44 set(OPENCV_EXTRA_CXX_FLAGS "")
45 set(OPENCV_EXTRA_FLAGS_RELEASE "")
46 set(OPENCV_EXTRA_FLAGS_DEBUG "")
47 set(OPENCV_EXTRA_EXE_LINKER_FLAGS "")
48 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "")
49 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "")
50
51 macro(add_extra_compiler_option option)
52   ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
53   if(${_varname})
54     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
55   endif()
56
57   ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
58   if(${_varname})
59     set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
60   endif()
61 endmacro()
62
63 macro(add_extra_compiler_option_force option)
64   set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
65   set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
66 endmacro()
67
68
69 # Gets environment variable and puts its value to the corresponding preprocessor definition
70 # Useful for WINRT that has no access to environment variables
71 macro(add_env_definitions option)
72   set(value $ENV{${option}})
73   if("${value}" STREQUAL "")
74     message(WARNING "${option} environment variable is empty. Please set it to appropriate location to get correct results")
75   else()
76     string(REPLACE "\\" "\\\\" value ${value})
77   endif()
78   add_definitions("-D${option}=\"${value}\"")
79 endmacro()
80
81 # OpenCV fails some tests when 'char' is 'unsigned' by default
82 add_extra_compiler_option(-fsigned-char)
83
84 if(MINGW)
85   # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838
86   # here we are trying to workaround the problem
87   add_extra_compiler_option(-mstackrealign)
88   if(NOT HAVE_CXX_MSTACKREALIGN)
89     add_extra_compiler_option(-mpreferred-stack-boundary=2)
90   endif()
91 endif()
92
93 if(CV_ICC AND NOT ENABLE_FAST_MATH)
94   if(MSVC)
95     add_extra_compiler_option("/fp:precise")
96   else()
97     add_extra_compiler_option("-fp-model precise")
98   endif()
99 endif()
100
101 if(CMAKE_COMPILER_IS_GNUCXX)
102   # High level of warnings.
103   add_extra_compiler_option(-W)
104   add_extra_compiler_option(-Wall)
105   add_extra_compiler_option(-Werror=return-type)
106   add_extra_compiler_option(-Werror=non-virtual-dtor)
107   add_extra_compiler_option(-Werror=address)
108   add_extra_compiler_option(-Werror=sequence-point)
109   add_extra_compiler_option(-Wformat)
110   add_extra_compiler_option(-Werror=format-security -Wformat)
111   add_extra_compiler_option(-Wmissing-declarations)
112   add_extra_compiler_option(-Wmissing-prototypes)
113   add_extra_compiler_option(-Wstrict-prototypes)
114   add_extra_compiler_option(-Wundef)
115   add_extra_compiler_option(-Winit-self)
116   add_extra_compiler_option(-Wpointer-arith)
117   add_extra_compiler_option(-Wshadow)
118   add_extra_compiler_option(-Wsign-promo)
119   add_extra_compiler_option(-Wuninitialized)
120   add_extra_compiler_option(-Winit-self)
121
122   if(ENABLE_NOISY_WARNINGS)
123     add_extra_compiler_option(-Wcast-align)
124     add_extra_compiler_option(-Wstrict-aliasing=2)
125   else()
126     add_extra_compiler_option(-Wno-narrowing)
127     add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
128     add_extra_compiler_option(-Wno-unnamed-type-template-args)
129     add_extra_compiler_option(-Wno-comment)
130     add_extra_compiler_option(-Wno-implicit-fallthrough)
131     if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 7.2.0)
132       add_extra_compiler_option(-Wno-strict-overflow) # Issue is fixed in GCC 7.2.1
133     endif()
134   endif()
135   add_extra_compiler_option(-fdiagnostics-show-option)
136
137   # The -Wno-long-long is required in 64bit systems when including sytem headers.
138   if(X86_64)
139     add_extra_compiler_option(-Wno-long-long)
140   endif()
141
142   # We need pthread's
143   if(UNIX AND NOT ANDROID AND NOT (APPLE AND CMAKE_COMPILER_IS_CLANGCXX)) # TODO
144     add_extra_compiler_option(-pthread)
145   endif()
146
147   if(CMAKE_COMPILER_IS_CLANGCXX)
148     add_extra_compiler_option(-Qunused-arguments)
149   endif()
150
151   if(OPENCV_WARNINGS_ARE_ERRORS)
152     add_extra_compiler_option(-Werror)
153   endif()
154
155   if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
156     add_extra_compiler_option(-march=i686)
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))
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     set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --std=c++11")
200     set(WITH_VTK OFF) # There are issues with VTK 6.0
201   endif()
202
203   if(ENABLE_LTO)
204     add_extra_compiler_option(-flto)
205   endif()
206
207   if(ENABLE_THIN_LTO)
208     add_extra_compiler_option(-flto=thin)
209   endif()
210
211   set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
212   if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} " MATCHES "-O")
213     set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
214   endif()
215   set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
216 endif()
217
218 if(MSVC)
219   #TODO Code refactoring is required to resolve security warnings
220   #if(NOT ENABLE_BUILD_HARDENING)
221     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
222   #endif()
223
224   # 64-bit portability warnings, in MSVC80
225   if(MSVC80)
226     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
227   endif()
228
229   if(BUILD_WITH_DEBUG_INFO)
230     set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
231   endif()
232
233   # Remove unreferenced functions: function level linking
234   set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
235   if(NOT MSVC_VERSION LESS 1400)
236     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
237   endif()
238   if(BUILD_WITH_DEBUG_INFO)
239     set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
240   endif()
241
242   if(OPENCV_WARNINGS_ARE_ERRORS)
243     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
244   endif()
245
246   if(ENABLE_LTO)
247     set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /GL")
248     set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} /LTCG")
249   endif()
250
251 endif()
252
253 if(MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
254   set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
255   set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
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
265 if(COMMAND ocv_compiler_optimization_options)
266   ocv_compiler_optimization_options()
267 endif()
268
269 if(COMMAND ocv_compiler_optimization_options_finalize)
270   ocv_compiler_optimization_options_finalize()
271 endif()
272
273 # Add user supplied extra options (optimization, etc...)
274 # ==========================================================
275 set(OPENCV_EXTRA_FLAGS         "${OPENCV_EXTRA_FLAGS}"         CACHE INTERNAL "Extra compiler options")
276 set(OPENCV_EXTRA_C_FLAGS       "${OPENCV_EXTRA_C_FLAGS}"       CACHE INTERNAL "Extra compiler options for C sources")
277 set(OPENCV_EXTRA_CXX_FLAGS     "${OPENCV_EXTRA_CXX_FLAGS}"     CACHE INTERNAL "Extra compiler options for C++ sources")
278 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build")
279 set(OPENCV_EXTRA_FLAGS_DEBUG   "${OPENCV_EXTRA_FLAGS_DEBUG}"   CACHE INTERNAL "Extra compiler options for Debug build")
280 set(OPENCV_EXTRA_EXE_LINKER_FLAGS         "${OPENCV_EXTRA_EXE_LINKER_FLAGS}"         CACHE INTERNAL "Extra linker flags")
281 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build")
282 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG   "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}"   CACHE INTERNAL "Extra linker flags for Debug build")
283
284 # set default visibility to hidden
285 if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
286     AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
287     AND NOT CMAKE_CXX_FLAGS MATCHES "-fvisibility")
288   add_extra_compiler_option(-fvisibility=hidden)
289   add_extra_compiler_option(-fvisibility-inlines-hidden)
290 endif()
291
292 #combine all "extra" options
293 set(CMAKE_C_FLAGS           "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
294 set(CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
295 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
296 set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
297 set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
298 set(CMAKE_C_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
299 set(CMAKE_EXE_LINKER_FLAGS         "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
300 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
301 set(CMAKE_EXE_LINKER_FLAGS_DEBUG   "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
302
303 if(MSVC)
304   # avoid warnings from MSVC about overriding the /W* option
305   # we replace /W3 with /W4 only for C++ files,
306   # since all the 3rd-party libraries OpenCV uses are in C,
307   # and we do not care about their warnings.
308   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS         "${CMAKE_CXX_FLAGS}")
309   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
310   string(REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS_DEBUG}")
311
312   if(NOT ENABLE_NOISY_WARNINGS)
313     if(MSVC_VERSION EQUAL 1400)
314       ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
315     endif()
316   endif()
317
318   # allow extern "C" functions throw exceptions
319   foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
320     string(REPLACE "/EHsc-" "/EHs" ${flags} "${${flags}}")
321     string(REPLACE "/EHsc"  "/EHs" ${flags} "${${flags}}")
322
323     string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
324   endforeach()
325
326   if(NOT ENABLE_NOISY_WARNINGS)
327     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
328     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
329     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
330     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
331     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
332     ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
333   endif()
334
335   if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
336     foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
337       string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
338     endforeach()
339     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
340   endif()
341 endif()
342
343 if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
344   link_directories("/usr/local/lib")
345 endif()
346
347
348 if(ENABLE_BUILD_HARDENING)
349   include(${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake)
350 endif()