diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a7b3efe1e5d7..8f654bf224b8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -200,7 +200,8 @@ elseif(CONFIG_SPEED_OPTIMIZATIONS) elseif(CONFIG_SIZE_OPTIMIZATIONS) set(OPTIMIZATION_FLAG ${OPTIMIZE_FOR_SIZE_FLAG}) # Default in kconfig else() - assert(0 "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr") + message(FATAL_ERROR + "Unreachable code. Expected optimization level to have been chosen. See Kconfig.zephyr") endif() if(NOT CONFIG_ARCH_IS_SET) @@ -247,7 +248,8 @@ if(CONFIG_CPP) set(STD_CPP_DIALECT_FLAGS $) list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20}) else() - assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.") + message(FATAL_ERROR + "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.") endif() set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE) @@ -955,7 +957,28 @@ if(CONFIG_USERSPACE) endif() get_property(CSTD GLOBAL PROPERTY CSTD) -set_ifndef(CSTD c99) +if(NOT DEFINED CSTD) + if(CONFIG_STD_C23) + set(CSTD c2x) + elseif(CONFIG_STD_C17) + set(CSTD c17) + elseif(CONFIG_STD_C11) + set(CSTD c11) + elseif(CONFIG_STD_C99) + set(CSTD c99) + elseif(CONFIG_STD_C90) + set(CSTD c90) + else() + message(FATAL_ERROR "Unreachable code. Expected C standard to have been chosen.") + endif() + + if(CONFIG_GNU_C_EXTENSIONS) + string(REPLACE "c" "gnu" CSTD "${CSTD}") + endif() +else() + message(DEPRECATION + "Global CSTD property is deprecated, see Kconfig.zephyr for C Standard options.") +endif() # @Intent: Obtain compiler specific flag for specifying the c standard zephyr_compile_options( diff --git a/Kconfig.zephyr b/Kconfig.zephyr index 15a365d6defae..edb9a7959303d 100644 --- a/Kconfig.zephyr +++ b/Kconfig.zephyr @@ -338,6 +338,84 @@ endmenu menu "Compiler Options" +config REQUIRES_STD_C99 + bool + help + Hidden option to select compiler support C99 standard or higher. + +config REQUIRES_STD_C11 + bool + select REQUIRES_STD_C99 + help + Hidden option to select compiler support C11 standard or higher. + +config REQUIRES_STD_C17 + bool + select REQUIRES_STD_C11 + help + Hidden option to select compiler support C17 standard or higher. + +config REQUIRES_STD_C23 + bool + select REQUIRES_STD_C17 + help + Hidden option to select compiler support C23 standard or higher. + +choice STD_C + prompt "C Standard" + default STD_C23 if REQUIRES_STD_C23 + default STD_C17 if REQUIRES_STD_C17 + default STD_C11 if REQUIRES_STD_C11 + default STD_C99 + help + C Standards. + +config STD_C90 + bool "C90" + depends on !REQUIRES_STD_C99 + help + 1989 C standard as completed in 1989 and ratified by ISO/IEC + as ISO/IEC 9899:1990. This version is known as "ANSI C". + +config STD_C99 + bool "C99" + depends on !REQUIRES_STD_C11 + help + 1999 C standard. + +config STD_C11 + bool "C11" + depends on !REQUIRES_STD_C17 + help + 2011 C standard. + +config STD_C17 + bool "C17" + depends on !REQUIRES_STD_C23 + help + 2017 C standard, addresses defects in C11 without introducing + new language features. + +config STD_C23 + bool "C23" + help + 2023 C standard. + +endchoice + +config TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS + bool + default y if "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr" + help + Hidden option to signal that toolchain supports GNU Extensions. + +config GNU_C_EXTENSIONS + bool "GNU C Extensions" + depends on TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS + help + Enable GNU C Extensions. GNU C provides several language features + not found in ISO standard C. + config CODING_GUIDELINE_CHECK bool "Enforce coding guideline rules" help diff --git a/arch/Kconfig b/arch/Kconfig index a800d220b581e..b9e40d8ec20fa 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -144,6 +144,12 @@ config ARCH_POSIX select BARRIER_OPERATIONS_BUILTIN # POSIX arch based targets get their memory cleared on entry by the host OS select SKIP_BSS_CLEAR + # Override the C standard used for compilation to C 2011 + # This is due to some tests using _Static_assert which is a 2011 feature, but + # otherwise relying on compilers supporting it also when set to C99. + # This was in general ok, but with some host compilers and C library versions + # it led to problems. So we override it to 2011 for the native targets. + select REQUIRES_STD_C11 help POSIX (native) architecture diff --git a/arch/posix/CMakeLists.txt b/arch/posix/CMakeLists.txt index 7ce45f9ad9b5c..bc06cecac2766 100644 --- a/arch/posix/CMakeLists.txt +++ b/arch/posix/CMakeLists.txt @@ -196,11 +196,4 @@ if(NOT ${LLVM_SANITIZERS_ARG} STREQUAL "") target_compile_options(native_simulator INTERFACE ${LLVM_SANITIZERS_ARG}) endif() -# Override the C standard used for compilation to C 2011 -# This is due to some tests using _Static_assert which is a 2011 feature, but -# otherwise relying on compilers supporting it also when set to C99. -# This was in general ok, but with some host compilers and C library versions -# it led to problems. So we override it to 2011 for the native targets. -set_property(GLOBAL PROPERTY CSTD c11) - add_subdirectory(core) diff --git a/cmake/compiler/arcmwdt/compiler_flags.cmake b/cmake/compiler/arcmwdt/compiler_flags.cmake index 5383016795b90..5d2be413dc8fe 100644 --- a/cmake/compiler/arcmwdt/compiler_flags.cmake +++ b/cmake/compiler/arcmwdt/compiler_flags.cmake @@ -1,5 +1,3 @@ -set_property(GLOBAL PROPERTY CSTD gnu99) - # List the warnings that are not supported for C++ compilations list(APPEND CXX_EXCLUDED_OPTIONS -Werror=implicit-int diff --git a/cmake/toolchain/arcmwdt/Kconfig b/cmake/toolchain/arcmwdt/Kconfig index c2ef35d3c795c..3f70536b36745 100644 --- a/cmake/toolchain/arcmwdt/Kconfig +++ b/cmake/toolchain/arcmwdt/Kconfig @@ -4,3 +4,7 @@ config TOOLCHAIN_ARCMWDT_SUPPORTS_THREAD_LOCAL_STORAGE def_bool y select TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE + +config TOOLCHAIN_ARCMWDT_SUPPORTS_GNU_EXTENSIONS + def_bool y + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS diff --git a/cmake/toolchain/armclang/Kconfig b/cmake/toolchain/armclang/Kconfig index 19b7bcca5150d..27be295057ca9 100644 --- a/cmake/toolchain/armclang/Kconfig +++ b/cmake/toolchain/armclang/Kconfig @@ -23,3 +23,7 @@ config ARMCLANG_STD_LIBC not provided by ARM Compiler standard libraries. endchoice + +config TOOLCHAIN_ARMCLANG_SUPPORTS_GNU_EXTENSIONS + def_bool y + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS diff --git a/cmake/toolchain/cross-compile/Kconfig b/cmake/toolchain/cross-compile/Kconfig index d2da9acbd8d35..1f2fbd37d1f3b 100644 --- a/cmake/toolchain/cross-compile/Kconfig +++ b/cmake/toolchain/cross-compile/Kconfig @@ -7,3 +7,10 @@ config TOOLCHAIN_CROSS_COMPILE_SUPPORTS_THREAD_LOCAL_STORAGE help Set this if the cross-compile toolchain being used for the build supports thread local storage. + +config TOOLCHAIN_CROSS_COMPILE_SUPPORTS_GNU_EXTENSIONS + bool "Cross-compile toolchain supports GNU Extensions" + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS + help + Set this if the cross-compile toolchain being used for the build + supports GNU Extensions. diff --git a/cmake/toolchain/gnuarmemb/Kconfig b/cmake/toolchain/gnuarmemb/Kconfig index 702d503ddd94b..9d34449f4c7b5 100644 --- a/cmake/toolchain/gnuarmemb/Kconfig +++ b/cmake/toolchain/gnuarmemb/Kconfig @@ -7,3 +7,7 @@ config TOOLCHAIN_GNUARMEMB def_bool y select HAS_NEWLIB_LIBC_NANO select NEWLIB_LIBC_SUPPORTED + +config TOOLCHAIN_GNUARMEMB_SUPPORTS_GNU_EXTENSIONS + def_bool y + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS diff --git a/cmake/toolchain/host/Kconfig b/cmake/toolchain/host/Kconfig new file mode 100644 index 0000000000000..81958491352db --- /dev/null +++ b/cmake/toolchain/host/Kconfig @@ -0,0 +1,6 @@ +# Copyright (c) 2024 Basalte bv +# SPDX-License-Identifier: Apache-2.0 + +config TOOLCHAIN_HOST_SUPPORTS_GNU_EXTENSIONS + def_bool y + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS diff --git a/cmake/toolchain/llvm/Kconfig b/cmake/toolchain/llvm/Kconfig index 0e2e3e85863fd..1886f50cfe51d 100644 --- a/cmake/toolchain/llvm/Kconfig +++ b/cmake/toolchain/llvm/Kconfig @@ -22,3 +22,7 @@ config TOOLCHAIN_LLVM_SUPPORTS_THREAD_LOCAL_STORAGE depends on RISCV def_bool y select TOOLCHAIN_SUPPORTS_THREAD_LOCAL_STORAGE + +config TOOLCHAIN_LLVM_SUPPORTS_GNU_EXTENSIONS + def_bool y + select TOOLCHAIN_SUPPORTS_GNU_EXTENSIONS diff --git a/doc/releases/release-notes-3.7.rst b/doc/releases/release-notes-3.7.rst index 3719823b0631a..3f168b10651b8 100644 --- a/doc/releases/release-notes-3.7.rst +++ b/doc/releases/release-notes-3.7.rst @@ -93,6 +93,12 @@ Build system and Infrastructure * CI-enabled blackbox tests were added in order to verify correctness of the vast majority of Twister flags. +* Compiler + + * Deprecated the global CSTD cmake property in favor of the :kconfig:option:`CONFIG_STD_C` + choice to select the C Standard version. Additionally subsystems can select a minimum + required C Standard version, with for example :kconfig:option:`CONFIG_REQUIRES_STD_C11`. + Drivers and Sensors ******************* diff --git a/tests/subsys/bindesc/definition/testcase.yaml b/tests/subsys/bindesc/definition/testcase.yaml index 6e1884ee0b01f..1d51ee82fcc51 100644 --- a/tests/subsys/bindesc/definition/testcase.yaml +++ b/tests/subsys/bindesc/definition/testcase.yaml @@ -22,14 +22,23 @@ tests: - qemu_riscv32e - qemu_riscv64 bindesc.define.c99: - extra_args: CSTD="c99" + extra_configs: + - CONFIG_STD_C99=y bindesc.define.c11: - extra_args: CSTD="c11" + extra_configs: + - CONFIG_STD_C11=y bindesc.define.c17: - extra_args: CSTD="c17" + extra_configs: + - CONFIG_STD_C17=y bindesc.define.gnu99: - extra_args: CSTD="gnu99" + extra_configs: + - CONFIG_STD_C99=y + - CONFIG_GNU_C_EXTENSIONS=y bindesc.define.gnu11: - extra_args: CSTD="gnu11" + extra_configs: + - CONFIG_STD_C11=y + - CONFIG_GNU_C_EXTENSIONS=y bindesc.define.gnu17: - extra_args: CSTD="gnu17" + extra_configs: + - CONFIG_STD_C17=y + - CONFIG_GNU_C_EXTENSIONS=y