@ -28,26 +28,6 @@ if((CMAKE_COMPILER_IS_CLANGCXX OR CMAKE_COMPILER_IS_CLANGCC OR CMAKE_COMPILER_IS
set ( ENABLE_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE )
endif ( )
if ( MSVC )
string ( STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS )
string ( STRIP "${CMAKE_CXX_FLAGS_INIT}" CMAKE_CXX_FLAGS_INIT )
if ( CMAKE_CXX_FLAGS STREQUAL CMAKE_CXX_FLAGS_INIT )
# o v e r r i d e c m a k e d e f a u l t e x c e p t i o n h a n d l i n g o p t i o n
string ( REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "Flags used by the compiler during all build types." FORCE )
endif ( )
endif ( )
set ( OPENCV_EXTRA_FLAGS "" )
set ( OPENCV_EXTRA_C_FLAGS "" )
set ( OPENCV_EXTRA_CXX_FLAGS "" )
set ( OPENCV_EXTRA_FLAGS_RELEASE "" )
set ( OPENCV_EXTRA_FLAGS_DEBUG "" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS "" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "" )
macro ( add_extra_compiler_option option )
ocv_check_flag_support ( CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}" )
if ( ${ _varname } )
@ -78,16 +58,9 @@ macro(add_env_definitions option)
add_definitions ( "-D${option}=\" ${ value } \"")
endmacro ( )
# O p e n C V f a i l s s o m e t e s t s w h e n ' c h a r ' i s ' u n s i g n e d ' b y d e f a u l t
add_extra_compiler_option ( -fsigned-char )
if ( MINGW )
# h t t p : / / g c c . g n u . o r g / b u g z i l l a / s h o w _ b u g . c g i ? i d = 4 0 8 3 8
# h e r e w e a r e t r y i n g t o w o r k a r o u n d t h e p r o b l e m
add_extra_compiler_option ( -mstackrealign )
if ( NOT HAVE_CXX_MSTACKREALIGN )
add_extra_compiler_option ( -mpreferred-stack-boundary=2 )
endif ( )
if ( NOT MSVC )
# O p e n C V f a i l s s o m e t e s t s w h e n ' c h a r ' i s ' u n s i g n e d ' b y d e f a u l t
add_extra_compiler_option ( -fsigned-char )
endif ( )
if ( CV_ICC AND NOT ENABLE_FAST_MATH )
@ -152,10 +125,6 @@ if(CMAKE_COMPILER_IS_GNUCXX)
add_extra_compiler_option ( -Werror )
endif ( )
if ( X86 AND NOT MINGW64 AND NOT X86_64 AND NOT APPLE )
add_extra_compiler_option ( -march=i686 )
endif ( )
if ( APPLE )
add_extra_compiler_option ( -Wno-semicolon-before-method-body )
endif ( )
@ -196,23 +165,30 @@ if(CMAKE_COMPILER_IS_GNUCXX)
endif ( )
if ( ENABLE_INSTRUMENTATION )
set ( OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --std=c++11" )
if ( NOT HAVE_CXX11 )
message ( WARNING "ENABLE_INSTRUMENTATION requires C++11 support" )
endif ( )
set ( WITH_VTK OFF ) # T h e r e a r e i s s u e s w i t h V T K 6 . 0
endif ( )
if ( ENABLE_LTO )
add_extra_compiler_option ( -flto )
endif ( )
if ( ENABLE_THIN_LTO )
add_extra_compiler_option ( -flto=thin )
endif ( )
set ( OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG" )
if ( NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} " MATCHES "-O" )
if ( NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG} " MATCHES "-O" )
set ( OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0" )
endif ( )
set ( OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG" )
if ( BUILD_WITH_DEBUG_INFO )
if ( NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -g" )
set ( OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} -g" )
endif ( )
endif ( )
endif ( )
if ( MSVC )
@ -221,11 +197,6 @@ if(MSVC)
set ( OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS" )
# endif ( )
# 6 4 - b i t p o r t a b i l i t y w a r n i n g s , i n MSVC80
if ( MSVC80 )
set ( OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Wp64" )
endif ( )
if ( BUILD_WITH_DEBUG_INFO )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug" )
endif ( )
@ -248,11 +219,10 @@ if(MSVC)
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /LTCG" )
endif ( )
endif ( )
if ( MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio" )
set ( OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS" )
set ( OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS" )
if ( NOT MSVC_VERSION LESS 1800 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio" )
set ( OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS" )
set ( OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS" )
endif ( )
endif ( )
# A d d i n g a d d i t i o n a l u s i n g d i r e c t o r y f o r W i n d o w s P h o n e 8 . 0 t o g e t W i n d o w s . w i n m d p r o p e r l y
@ -261,68 +231,55 @@ if(WINRT_PHONE AND WINRT_8_0)
endif ( )
include ( cmake/OpenCVCompilerOptimizations.cmake )
if ( COMMAND ocv_compiler_optimization_options )
ocv_compiler_optimization_options ( )
endif ( )
if ( COMMAND ocv_compiler_optimization_options_finalize )
ocv_compiler_optimization_options_finalize ( )
endif ( )
# A d d u s e r s u p p l i e d e x t r a options ( optimization, etc... )
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
set ( OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS}" CACHE INTERNAL "Extra compiler options" )
set ( OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS}" CACHE INTERNAL "Extra compiler options for C sources" )
set ( OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS}" CACHE INTERNAL "Extra compiler options for C++ sources" )
set ( OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE}" CACHE INTERNAL "Extra compiler options for Release build" )
set ( OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG}" CACHE INTERNAL "Extra compiler options for Debug build" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS}" CACHE INTERNAL "Extra linker flags" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" CACHE INTERNAL "Extra linker flags for Release build" )
set ( OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG "${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}" CACHE INTERNAL "Extra linker flags for Debug build" )
# s e t d e f a u l t v i s i b i l i t y t o h i d d e n
if ( ( CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
A N D N O T O P E N C V _ S K I P _ V I S I B I L I T Y _ H I D D E N
A N D N O T C M A K E _ C X X _ F L A G S M A T C H E S " - f v i s i b i l i t y " )
A N D N O T " $ { C M A K E _ C X X _ F L A G S } $ { O P E N C V _ E X T R A _ F L A G S } $ { O P E N C V _ E X T R A _ C X X _ F L A G S } " M A T C H E S " - f v i s i b i l i t y " )
add_extra_compiler_option ( -fvisibility=hidden )
add_extra_compiler_option ( -fvisibility-inlines-hidden )
endif ( )
# c o m b i n e a l l " e x t r a " o p t i o n s
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" )
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}" )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}" )
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}" )
set ( CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}" )
# c o m b i n e a l l " e x t r a " o p t i o n s
if ( NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" )
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}" )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}" )
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}" )
set ( CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}" )
set ( CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}" )
endif ( )
if ( MSVC )
# a v o i d w a r n i n g s f r o m MSVC a b o u t o v e r r i d i n g t h e / W * o p t i o n
# w e r e p l a c e / W 3 w i t h / W 4 o n l y f o r C + + f i l e s ,
# s i n c e a l l t h e 3 r d - p a r t y l i b r a r i e s O p e n C V u s e s a r e i n C ,
# a n d w e d o n o t c a r e a b o u t t h e i r w a r n i n g s .
string ( REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
string ( REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" )
string ( REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" )
if ( NOT ENABLE_NOISY_WARNINGS )
if ( MSVC_VERSION EQUAL 1400 )
ocv_warnings_disable ( CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267 )
endif ( )
endif ( )
# a l l o w e x t e r n " C " f u n c t i o n s t h r o w e x c e p t i o n s
foreach ( flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
string ( REPLACE "/EHsc-" "/EHs" ${ flags } "${${flags}}" )
string ( REPLACE "/EHsc" "/EHs" ${ flags } "${${flags}}" )
string ( REPLACE "/Zm1000" "" ${ flags } "${${flags}}" )
endforeach ( )
# E n a b l e ' e x t e r n " C " ' a n d asynchronous ( division by zero, access violation ) e x c e p t i o n s
if ( NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG )
foreach ( flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
string ( REGEX REPLACE " /EH[^ ]* " " " ${ flags } " ${${flags}}" )
endforeach ( )
if ( NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa" )
endif ( )
endif ( )
if ( NOT ENABLE_NOISY_WARNINGS )
ocv_warnings_disable ( CMAKE_CXX_FLAGS /wd4127 ) # c o n d i t i o n a l e x p r e s s i o n i s c o n s t a n t
ocv_warnings_disable ( CMAKE_CXX_FLAGS /wd4251 ) # c l a s s ' s t d : : X X X ' n e e d s t o h a v e d l l - i n t e r f a c e t o b e u s e d b y c l i e n t s o f Y Y Y
@ -344,7 +301,20 @@ if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/
link_directories ( "/usr/local/lib" )
endif ( )
if ( ENABLE_BUILD_HARDENING )
include ( ${ CMAKE_CURRENT_LIST_DIR } /OpenCVCompilerDefenses.cmake )
endif ( )
if ( MSVC )
include ( cmake/OpenCVCRTLinkage.cmake )
add_definitions ( -D_VARIADIC_MAX=10 )
endif ( )
# E n a b l e c o m p i l e r o p t i o n s f o r O p e n C V m o d u l e s / a p p s / s a m p l e s only ( ignore 3rdparty )
macro ( ocv_add_modules_compiler_options )
if ( MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION )
foreach ( flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
string ( REPLACE "/W3" "/W4" ${ flags } "${${flags}}" )
endforeach ( )
endif ( )
endmacro ( )