RFC: Framework for compiler agnostic compile options, linker agnostic link options
As CMake supports a variety of compilers, I think it would be beneficial to have compiler agnostic options which can be set.
This is similar to this
set_target_properties(my-target PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE) # /GL on MSVC -flto on Clang and GCC
set_target_properties(my-target PROPERTIES CXX_STANDARD 17) # /std:c++17 on MSVC -std=c++17 on Clang and GCC
Proposal
# warnings_as_errors -> /WX on MSVC -Werror on Clang and GCC
# disable_warnings_unused_variable -> /wd4189 on MSVC -Wno-unused-varibale on Clang and GCC
# color_diagnostics_on -> -fdiagnostics-color on Clang and GCC, ignored on MSVC (we might throw an one time warning)
target_compile_options(my-target PRIVATE [warnings_as_errors] [disable_warnings_unused_variable] [color_diagnostics_on])
We can define this in a json file
"warnings_as_errors":{
"MSVC" : "/WX",
"Clang" : "-Werror"
...
}
We write one standard file with the most widely used options(huge undertaking) say in CMAKE_compiler_flags.json
and let the user add his/her own flags in compiler_flags.json
. This file will be in a local repository.
"color_diagnostics_on":{
"Clang" : "-fdiagnostics-color -fansi-virtual-escape-sequences"
"GCC" : "-fdiagnostics-color"
}
Note above we override [color_diagnostics_on]
for Clang.
When CMake runs the configure we can check if each of the flags work and report an error or ignore them(ignore the flags as well). As new options are being introduced in newer versions of the compiler, a version check can be introduced to ignore the newer options for older versions of the compiler.
Similarly this can be done for linker flags as well.