Skip to content

Commit 6173013

Browse files
tejlmandnashif
authored andcommitted
cmake: CMake compile features support
Fixes: #36558 #32577 This commit introduces CMAKE_C_COMPILE_FEATURES and CMAKE_CXX_COMPILE_FEATURES. This allows users to use the `target_compile_features()` in their own code. In Zephyr, the CMAKE_C/CXX_COMPILE_FEATURES are defined based on the compiler and the Kconfig / CSTD setting. Doing so ensures that a user compiling Zephyr with c99 and specifies `target_compile_features(<target> ... c_std_11)` will get an error. And similar if building Zephyr with C++ support and c++11, but testing for `target_compile_features(<target> ... cxx_std_17)`. For example in the C++ case, the user must ensure that Zephyr is compiled with C++17, that is: CPLUSPLUS=y and STD_CPP17=y, in which case the CMAKE_CXX_COMPILE_FEATURES will contain support for C++17 and thus the `target_compile_features(<target> ... cxx_std_17)` will succeed. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 5455bf4 commit 6173013

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,29 @@ if(CONFIG_CPLUSPLUS)
168168
# Kconfig choice ensures only one of these CONFIG_STD_CPP* is set.
169169
if(CONFIG_STD_CPP98)
170170
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp98>)
171+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp98})
171172
elseif(CONFIG_STD_CPP11)
172173
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp11>) # Default in kconfig
174+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp11})
173175
elseif(CONFIG_STD_CPP14)
174176
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp14>)
177+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp14})
175178
elseif(CONFIG_STD_CPP17)
176179
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp17>)
180+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp17})
177181
elseif(CONFIG_STD_CPP2A)
178182
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2a>)
183+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
179184
elseif(CONFIG_STD_CPP20)
180185
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp20>)
186+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
181187
elseif(CONFIG_STD_CPP2B)
182188
set(STD_CPP_DIALECT_FLAGS $<TARGET_PROPERTY:compiler-cpp,dialect_cpp2b>)
189+
list(APPEND CMAKE_CXX_COMPILE_FEATURES ${compile_features_cpp20})
183190
else()
184191
assert(0 "Unreachable code. Expected C++ standard to have been chosen. See Kconfig.zephyr.")
185192
endif()
193+
set(CMAKE_CXX_COMPILE_FEATURES ${CMAKE_CXX_COMPILE_FEATURES} PARENT_SCOPE)
186194

187195
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:${STD_CPP_DIALECT_FLAGS}>)
188196
endif()
@@ -979,6 +987,7 @@ set_ifndef(CSTD c99)
979987
zephyr_compile_options(
980988
$<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,cstd>${CSTD}>
981989
)
990+
set(CMAKE_C_COMPILE_FEATURES ${compile_features_${CSTD}} PARENT_SCOPE)
982991

983992
# @Intent: Configure linker scripts, i.e. generate linker scripts with variables substituted
984993
toolchain_ld_configure_files()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
set(c23id c2x gnu2x)
2+
set(c17id c17 c18 gnu17 gnu18 "iso9899:2017" "iso9899:2018")
3+
set(c11id c11 gnu11 "iso9899:2011")
4+
set(c99id c99 gnu99 "iso9899:1999")
5+
set(c90id c89 c90 gnu89 gnu90 "iso9899:1990" "iso9899:199409")
6+
7+
set(compile_features_list)
8+
9+
# For each id value above a compile_features_${idval} with a list of supported
10+
# `c_std_XX` values are created for easy lookup.
11+
# For example, the settings
12+
# - `compile_feature_c99` will contain `c_std_90;c_std_99`
13+
# - `compile_feature_iso9899:2011` will contain `c_std_90;c_std_99;c_std_11`
14+
# that can then be used to set CMAKE_C_COMPILE_FEATURES accordingly.
15+
foreach(standard 90 99 11 17 23)
16+
list(APPEND compile_features_list c_std_${standard})
17+
foreach(id ${c${standard}id})
18+
set(compile_features_${id} ${compile_features_list})
19+
endforeach()
20+
endforeach()
21+
22+
set(compile_features_cpp98 cxx_std_98)
23+
set(compile_features_cpp11 cxx_std_11 ${compile_features_cpp98})
24+
set(compile_features_cpp14 cxx_std_14 ${compile_features_cpp11})
25+
set(compile_features_cpp17 cxx_std_17 ${compile_features_cpp14})
26+
set(compile_features_cpp20 cxx_std_20 ${compile_features_cpp17})

cmake/target_toolchain_flags.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ set(TOOLCHAIN_SIGNATURE ${CMAKE_C_COMPILER_MD5_SUM})
2424
string(MD5 COMPILER_SIGNATURE ${CMAKE_C_COMPILER}_${CMAKE_C_COMPILER_ID}_${CMAKE_C_COMPILER_VERSION})
2525
set(TOOLCHAIN_SIGNATURE ${TOOLCHAIN_SIGNATURE}_${COMPILER_SIGNATURE})
2626

27+
# Load the compile features file which will provide compile features lists for
28+
# various C / CXX language dialects that can then be exported based on current
29+
# Zephyr Kconfig settings or the CSTD global property.
30+
include(${CMAKE_CURRENT_LIST_DIR}/compiler/compiler_features.cmake)
31+
2732
# Loading of templates are strictly not needed as they does not set any
2833
# properties.
2934
# They purely provides an overview as well as a starting point for supporting

0 commit comments

Comments
 (0)