Skip to content

Commit 04baf84

Browse files
JiafeiPancfriedt
authored andcommitted
cmake: toolchain: cross_compile: check newlib and picolibc
Add support to check whether Cross Compile toolchain support newlib and picolibc or not. Signed-off-by: Jiafei Pan <[email protected]>
1 parent 8ae4195 commit 04baf84

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cmake/toolchain/cross-compile/generic.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,41 @@ if((NOT (DEFINED CROSS_COMPILE)) AND (DEFINED ENV{CROSS_COMPILE}))
2121
set(CROSS_COMPILE $ENV{CROSS_COMPILE})
2222
endif()
2323

24+
if((NOT (DEFINED CROSS_COMPILE_TOOLCHAIN_PATH)) AND (DEFINED ENV{CROSS_COMPILE_TOOLCHAIN_PATH}))
25+
set(CROSS_COMPILE_TOOLCHAIN_PATH $ENV{CROSS_COMPILE_TOOLCHAIN_PATH})
26+
endif()
27+
2428
set( CROSS_COMPILE ${CROSS_COMPILE} CACHE PATH "")
2529
assert(CROSS_COMPILE "CROSS_COMPILE is not set")
2630

31+
set(CROSS_COMPILE_TOOLCHAIN_PATH ${CROSS_COMPILE_TOOLCHAIN_PATH} CACHE PATH "Cross Compile toolchain directory")
32+
2733
set(COMPILER gcc)
2834
set(LINKER ld)
2935
set(BINTOOLS gnu)
3036

37+
# CROSS_COMPILE is flexible, meaning that it can in principle always support newlib or picolibc.
38+
# This is not decided by CROSS_COMPILE itself, but depends on libraries distributed with the installation.
39+
# Also newlib or picolibc may be created as add-ons. Thus always stating that CROSS_COMPILE does not have
40+
# newlib or picolibc would be wrong. Same with stating that CROSS_COMPILE has newlib or Picolibc.
41+
# The best assumption for TOOLCHAIN_HAS_<NEWLIB|PICOLIBC> is to check for the presence of
42+
# '_newlib_version.h' / 'picolibc' and have the default value set accordingly.
43+
# This provides a best effort mechanism to allow developers to have the newlib C / Picolibc library
44+
# selection available in Kconfig.
45+
# Developers can manually indicate library support with '-DTOOLCHAIN_HAS_<NEWLIB|PICOLIBC>=<ON|OFF>'
46+
47+
# Support for newlib is indicated by the presence of '_newlib_version.h' in the toolchain path.
48+
if(NOT CROSS_COMPILE_TOOLCHAIN_PATH STREQUAL "")
49+
file(GLOB_RECURSE newlib_header ${CROSS_COMPILE_TOOLCHAIN_PATH}/_newlib_version.h)
50+
if(newlib_header)
51+
set(TOOLCHAIN_HAS_NEWLIB ON CACHE BOOL "True if toolchain supports newlib")
52+
endif()
53+
54+
# Support for picolibc is indicated by the presence of 'picolibc.h' in the toolchain path.
55+
file(GLOB_RECURSE picolibc_header ${CROSS_COMPILE_TOOLCHAIN_PATH}/picolibc.h)
56+
if(picolibc_header)
57+
set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc")
58+
endif()
59+
endif()
60+
3161
message(STATUS "Found toolchain: cross-compile (${CROSS_COMPILE})")

0 commit comments

Comments
 (0)