default language extensions are inconsistent between compilers and property LANG_EXTENSIONS has no effect without setting LANG_STANDARD
Description of the Problem
Use of standard/extended language is inconsistent and depends on the compiler default, as representative examples gcc and iar are chosen (iar pending improved support from !991 (merged))
The current state
# Nothing
gcc: use default language standard with extensions
iar: use default language standard without extensions
set(CMAKE_C_STANDARD 99)
gcc: use C99 language standard with extensions
iar: use C99 language standard with extensions
set(CMAKE_C_EXTENSIONS Off)
gcc: use default language standard with extensions
iar: use default language standard without extensions
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS Off)
gcc: use C99 language standard without extensions
iar: use C99 language standard without extensions
Expected Results (honoring that CMake uses extensions as default)
# Nothing
gcc: use default language standard with extensions
iar: use default language standard with extensions
set(CMAKE_C_STANDARD 99)
gcc: use C99 language standard with extensions
iar: use C99 language standard with extensions
set(CMAKE_C_EXTENSIONS Off)
gcc: use default language standard without extensions
iar: use default language standard without extensions
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS Off)
gcc: use C99 language standard without extensions
iar: use C99 language standard without extensions
Discussion
The primary issue is that not all compilers enable extensions by default and don't allow reversing an argument once it is set. As expressed in the examples above, everything is fine if a suitable language standard is defined. There are usecases where defining a language standard is not wanted, and the default should be consistent across compilers (CMake default is extensions enabled).
A possible way to fix this issue would be to provide an CMAKE_<LANG>_<EXTTYPE>_COMPILE_OPTION
variable in addition to the CMAKE_<LANG><VER>_<EXTTYPE>_COMPILE_OPTION
variables. Quite possibly this could be automatically deduced from existing variables.
# valid for GNU C/C++, and all other compilers using extensions by default
set(CMAKE_${lang}_STANDARD_COMPILE_OPTION "CMAKE_${lang}${CMAKE_${lang}_STANDARD_DEFAULT}_STANDARD_COMPILE_OPTION")
# valid for IAR C/C++, and all other compilers NOT using extensions by default
set(CMAKE_${lang}_EXTENSION_COMPILE_OPTION "CMAKE_${lang}${CMAKE_${lang}_STANDARD_DEFAULT}_EXTENSION_COMPILE_OPTION")
logic for adding flags would need to be changed in this ways:
if has LANG_STANDARD set
do as before and use variable CMAKE_<LANG><VER>_<EXTTYPE>_COMPILE_OPTION
else
use variable CMAKE_<LANG>_<EXTTYPE>_COMPILE_OPTION
endif
Since the CMAKE_<LANG>_<EXTTYPE>_COMPILE_OPTION
variable could be set in an almost automatic manner, a variation would be to set them automatically, the only information necessary is whether the compiler uses extensions by default or not.
If a compiler uses extensions by default, the variable CMAKE_<LANG>_EXTENSION_DEFAULT
could be set (to the same value as CMAKE_<LANG>_STANDARD_DEFAULT
), otherwise left empty. If set, then this would also complete the set of EXTENSION
Variables to be identical to the STANDARD
variants