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)
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")
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)
16 set(CMAKE_COMPILER_IS_CCACHE 1)
18 message(STATUS "Unable to compile program with enabled ccache, reverting...")
19 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${__OLD_RULE_LAUNCH_COMPILE}")
23 message(STATUS "Looking for ccache - not found")
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)
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)
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 "")
51 macro(add_extra_compiler_option option)
52 ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
54 set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
57 ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
59 set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
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}")
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")
76 string(REPLACE "\\" "\\\\" value ${value})
78 add_definitions("-D${option}=\"${value}\"")
81 # OpenCV fails some tests when 'char' is 'unsigned' by default
82 add_extra_compiler_option(-fsigned-char)
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)
93 if(CV_ICC AND NOT ENABLE_FAST_MATH)
95 add_extra_compiler_option("/fp:precise")
97 add_extra_compiler_option("-fp-model precise")
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)
122 if(ENABLE_NOISY_WARNINGS)
123 add_extra_compiler_option(-Wcast-align)
124 add_extra_compiler_option(-Wstrict-aliasing=2)
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
135 add_extra_compiler_option(-fdiagnostics-show-option)
137 # The -Wno-long-long is required in 64bit systems when including sytem headers.
139 add_extra_compiler_option(-Wno-long-long)
143 if(UNIX AND NOT ANDROID AND NOT (APPLE AND CMAKE_COMPILER_IS_CLANGCXX)) # TODO
144 add_extra_compiler_option(-pthread)
147 if(CMAKE_COMPILER_IS_CLANGCXX)
148 add_extra_compiler_option(-Qunused-arguments)
151 if(OPENCV_WARNINGS_ARE_ERRORS)
152 add_extra_compiler_option(-Werror)
155 if(X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE)
156 add_extra_compiler_option(-march=i686)
160 add_extra_compiler_option(-Wno-semicolon-before-method-body)
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)
170 add_extra_compiler_option(-ffast-math)
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}}")
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")
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")
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
204 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} " 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")
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")
224 # 64-bit portability warnings, in MSVC80
226 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64")
229 if(BUILD_WITH_DEBUG_INFO)
230 set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
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")
238 if(BUILD_WITH_DEBUG_INFO)
239 set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
242 if(OPENCV_WARNINGS_ARE_ERRORS)
243 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
247 set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /GL")
248 set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} /LTCG")
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")
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)")
263 include(cmake/OpenCVCompilerOptimizations.cmake)
265 if(COMMAND ocv_compiler_optimization_options)
266 ocv_compiler_optimization_options()
269 if(COMMAND ocv_compiler_optimization_options_finalize)
270 ocv_compiler_optimization_options_finalize()
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")
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)
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}")
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}")
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)
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}}")
323 string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
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'
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}}")
339 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
343 if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
344 link_directories("/usr/local/lib")
348 if(ENABLE_BUILD_HARDENING)
349 include(${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake)