Prompt
Establish a consistent pattern for feature flags and dependency management in configuration files:
- Use
WITH_*orOPENCV_ENABLE_*variables to represent user intentions (features to enable if available) - Perform proper detection with
find_package()and preferablytry_compile()to validate dependencies - Set
HAVE_*variables based on actual availability check results - Always guard feature usage in code with
HAVE_*checks - Prefer target-based dependencies over simple flag checks
- Make compile definitions PUBLIC when they affect interface headers
Example:
# User intention
ocv_option(OPENCV_ENABLE_EGL_INTEROP "Build with EGL interoperability support" ON)
# Actual detection (in main CMakeLists.txt)
if(OPENCV_ENABLE_EGL_INTEROP)
find_package(EGL)
if(EGL_FOUND)
set(HAVE_EGL_INTEROP ON)
endif()
endif()
# Usage in module CMakeLists.txt
if(HAVE_EGL_INTEROP)
# Prefer target-based dependencies
target_link_libraries(${the_module} PUBLIC ocv.3rdparty.egl)
# Make definitions PUBLIC if used in headers
target_compile_definitions(${the_module} PUBLIC HAVE_EGL_INTEROP)
endif()
This convention ensures all dependencies are properly detected during configuration stage, not build stage, preventing build failures due to missing dependencies. It clearly separates user intent from actual availability and promotes modern CMake practices.