Compiling OpenCV with contrib modules has been very problematic for me. Here are some solutions I've found to various compilation errors.
Problem:
CMake Error at cmake/OpenCVCompilerOptimizations.cmake:611 (message):
Compiler doesn't support baseline optimization flags:
Call Stack (most recent call first):
cmake/OpenCVCompilerOptions.cmake:327 (ocv_compiler_optimization_options)
CMakeLists.txt:646 (include)