CMakeLists of libraries should only specify usage requirements.
For example if your library requires at least C++11 standard at build time & consume time:
target_compile_features(mylib PUBLIC cxx_std_11)
With this compile feature, by default CMake takes care to inject proper compilation flags so that this target is compiled with a C++ standard greater or equal than C++11 (it depends on compiler and its version).
Do not hardcode CMAKE_CXX_STANDARD in CMakeLists of a library, because it prevents users to compile with a different compatible C++ standard (and it's not uncommon to want to compile all libraries of a dependency graph with the same C++ standard).
Then if you want to build this lib with a very specific standard, you can inject CMAKE_CXX_STANDARD externally during CMake configuration:
cmake -S <source_dir> -B <build_dir> -DCMAKE_CXX_STANDARD=20
=============================================
And now that you have clarified your question:
add_library(mylib1 ...)
# only build lib2 if compiler supports C++20 standard
if("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
add_library(mylib2 ...)
target_compile_features(mylib2 PUBLIC cxx_std_2O)
endif()
I would suggest to display a summary at the end of your configuration, because it can be quite surprising for users to silently disable the build of several libs.