add-compile-options use for alternatives

add_compile_options(/Ob2) on windows will cause msvc to complain on every file that you’re overriding the string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "${_MD}${_Zi} /O2 /Ob1 /DNDEBUG") in the base cmake config.

CMake seems to see /Ob1 and /Ob2 as unrelated flags not different values passed to the same flag (/Ob=1 vs /Ob=2)?

While this particular flavor is ms-unique it seems like other compilers have something equivalent with “–no-…” for example, or varying “-g” flags etc.

So what’s the sane and portable way to do it? this feels … wrong:

foreach (lang in C CXX)
foreach (rel in RELEASE RELWITHDEBINFO)
string (REGEX REPLACE “/Ob[02]?” ““ CMAKE_${lang}_FLAGS_${rel} “${CMAKE_${lang}_FLAGS_${rel}}”
endforeach ()
endforeach ()
add_compile_options($<IF:$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>,/Ob2,/Ob1>)