OpenCV only defines two configurations, Debug and Release. Trying to build a dependent project in RelWithDebInfo causes a link error on MSVC. A solution to that is to set MAP_IMPORTED_CONFIG_RELWITHDEBINFO to RELEASE.

However, when building it via ExternalProject_Add I don’t know how to do an equivalent thing. Can it be done, and how?

You need to do that on the consuming side (wherever the find_package(OpenCV) is). I suppose you could patch the CMake package after installation, but that sounds finicky to get right. It also doesn’t work for anyone else using your OpenCV-consuming project with their own OpenCV build.