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)
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.")
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)
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")
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)
26 set(OPENCV_COMPILER_IS_CCACHE 1)
28 message(STATUS "Unable to compile program with enabled ccache, reverting...")
29 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${__OLD_RULE_LAUNCH_COMPILE}")
33 message(STATUS "Looking for ccache - not found")
37 if((CV_CLANG AND NOT CMAKE_GENERATOR MATCHES "Xcode") # PCH has no support for Clang
38 OR OPENCV_COMPILER_IS_CCACHE
40 set(ENABLE_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE)
43 macro(add_extra_compiler_option option)
44 ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
46 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
49 ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
51 set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
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}")
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")
68 string(REPLACE "\\" "\\\\" value ${value})
70 add_definitions("-D${option}=\"${value}\"")
74 # OpenCV fails some tests when 'char' is 'unsigned' by default
75 add_extra_compiler_option(-fsigned-char)
78 if(CV_ICC AND NOT ENABLE_FAST_MATH)
80 add_extra_compiler_option("/fp:precise")
82 add_extra_compiler_option("-fp-model precise")
86 if(CV_GCC OR CV_CLANG)
87 # High level of warnings.
88 add_extra_compiler_option(-W)
90 # clang-cl interprets -Wall as MSVC would: -Weverything, which is more than
92 add_extra_compiler_option(-Wall)
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)
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)
118 if(ENABLE_NOISY_WARNINGS)
119 add_extra_compiler_option(-Wcast-align)
120 add_extra_compiler_option(-Wstrict-aliasing=2)
122 add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
123 add_extra_compiler_option(-Wno-unnamed-type-template-args)
124 add_extra_compiler_option(-Wno-comment)
125 if(NOT OPENCV_SKIP_IMPLICIT_FALLTHROUGH
126 AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES "implicit-fallthrough"
127 AND (CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
129 add_extra_compiler_option(-Wimplicit-fallthrough=3)
131 if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
132 add_extra_compiler_option(-Wno-strict-overflow) # Issue appears when compiling surf.cpp from opencv_contrib/modules/xfeatures2d
134 if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
135 add_extra_compiler_option(-Wno-missing-field-initializers) # GCC 4.x emits warnings about {}, fixed in GCC 5+
138 add_extra_compiler_option(-fdiagnostics-show-option)
140 # The -Wno-long-long is required in 64bit systems when including system headers.
142 add_extra_compiler_option(-Wno-long-long)
146 if(UNIX AND NOT ANDROID AND NOT (APPLE AND CV_CLANG)) # TODO
147 add_extra_compiler_option(-pthread)
151 add_extra_compiler_option(-Qunused-arguments)
154 if(OPENCV_WARNINGS_ARE_ERRORS)
155 add_extra_compiler_option(-Werror)
159 add_extra_compiler_option(-Wno-semicolon-before-method-body)
162 # Other optimizations
163 if(ENABLE_OMIT_FRAME_POINTER)
164 add_extra_compiler_option(-fomit-frame-pointer)
165 elseif(DEFINED ENABLE_OMIT_FRAME_POINTER)
166 add_extra_compiler_option(-fno-omit-frame-pointer)
169 add_extra_compiler_option(-ffast-math)
174 add_extra_compiler_option("-pg -g")
175 # turn off incompatible options
176 foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
177 OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
178 string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
179 string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
180 string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
182 elseif(NOT ((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT MSVC)
183 # Remove unreferenced functions: function level linking
184 add_extra_compiler_option(-ffunction-sections)
185 add_extra_compiler_option(-fdata-sections)
186 if(NOT APPLE AND NOT OPENCV_SKIP_GC_SECTIONS)
187 set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
192 set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} --coverage")
193 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --coverage")
194 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
197 if(ENABLE_INSTRUMENTATION)
199 message(WARNING "ENABLE_INSTRUMENTATION requires C++11 support")
201 set(WITH_VTK OFF) # There are issues with VTK 6.0
205 add_extra_compiler_option(-flto)
208 add_extra_compiler_option(-flto=thin)
211 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
212 if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG} " MATCHES "-O")
213 set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
215 set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
217 if(BUILD_WITH_DEBUG_INFO)
218 # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
219 # '-g' is equal to '-g2', '-g1' produces minimal information, enough for making backtraces
220 ocv_update(OPENCV_DEBUG_OPTION "-g1")
221 if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE}" MATCHES " -g")
222 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} ${OPENCV_DEBUG_OPTION}")
228 #TODO Code refactoring is required to resolve security warnings
229 #if(NOT ENABLE_BUILD_HARDENING)
230 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
233 if(BUILD_WITH_DEBUG_INFO)
234 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
235 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
236 set(OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE} /debug")
239 # Remove unreferenced functions: function level linking
240 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
241 if(NOT MSVC_VERSION LESS 1400)
242 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
245 if(OPENCV_WARNINGS_ARE_ERRORS)
246 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
250 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /GL")
251 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /LTCG")
254 if(NOT MSVC_VERSION LESS 1800 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
255 set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
256 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
260 # Adding additional using directory for WindowsPhone 8.0 to get Windows.winmd properly
261 if(WINRT_PHONE AND WINRT_8_0)
262 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
265 include(cmake/OpenCVCompilerOptimizations.cmake)
266 if(COMMAND ocv_compiler_optimization_options)
267 ocv_compiler_optimization_options()
269 if(COMMAND ocv_compiler_optimization_options_finalize)
270 ocv_compiler_optimization_options_finalize()
273 # set default visibility to hidden
274 if((CV_GCC OR CV_CLANG)
276 AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
277 AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fvisibility")
278 add_extra_compiler_option(-fvisibility=hidden)
279 add_extra_compiler_option(-fvisibility-inlines-hidden)
282 # workaround gcc bug for aligned ld/st
283 # https://github.com/opencv/opencv/issues/13211
284 if((PPC64LE AND NOT CMAKE_CROSSCOMPILING) OR OPENCV_FORCE_COMPILER_CHECK_VSX_ALIGNED)
285 ocv_check_runtime_flag("${CPU_BASELINE_FLAGS}" "OPENCV_CHECK_VSX_ALIGNED" "${OpenCV_SOURCE_DIR}/cmake/checks/runtime/cpu_vsx_aligned.cpp")
286 if(NOT OPENCV_CHECK_VSX_ALIGNED)
287 add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ALIGNED)
291 # combine all "extra" options
292 if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
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 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS}")
303 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE}")
304 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_DEBUG}")
308 if(NOT ENABLE_NOISY_WARNINGS)
309 if(MSVC_VERSION EQUAL 1400)
310 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
314 foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
315 string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
318 # Enable 'extern "C"' and asynchronous (division by zero, access violation) exceptions
319 if(NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG)
320 foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
321 string(REGEX REPLACE " /EH[^ ]* " " " ${flags} " ${${flags}}")
323 if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH")
324 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
328 if(NOT ENABLE_NOISY_WARNINGS)
329 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
330 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
331 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
332 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
333 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
334 ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
337 if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
338 foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
339 string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
341 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
345 if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
346 link_directories("/usr/local/lib")
349 if(ENABLE_BUILD_HARDENING)
350 include(${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake)
354 include(cmake/OpenCVCRTLinkage.cmake)
355 add_definitions(-D_VARIADIC_MAX=10)
358 # Enable compiler options for OpenCV modules/apps/samples only (ignore 3rdparty)
359 macro(ocv_add_modules_compiler_options)
360 if(MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION)
361 foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
362 string(REPLACE "/W3" "/W4" ${flags} "${${flags}}")