Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/rp2_common/pico_clib_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,43 @@ if (NOT TARGET pico_clib_interface)

if (NOT PICO_CLIB)
# PICO_CMAKE_CONFIG: PICO_CLIB, The C library to use e.g. newlib/picolibc/llvm_libc, type=string, default=based on PICO_COMPILER, group=build, docref=cmake-toolchain-config
set(PICO_CLIB newlib)
endif()
set(CLIB_DETECT_CODE "
#include <stdio.h>
#ifdef __PICOLIBC__
#define USING_PICOLIBC 1
#elif defined(__NEWLIB__)
#define USING_NEWLIB 1
#endif

#if (picolibc && !USING_PICOLIBC) || (newlib && !USING_NEWLIB)
#error not using wanted c library
#endif
")

# Create a temporary file for the source
set(CLIB_DETECT_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/clib_check.c")
file(WRITE "${CLIB_DETECT_SOURCE}" "${CLIB_DETECT_CODE}")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
foreach(CLIB picolibc newlib)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think order should be "newlib picolibc", because newlib was default before

Comment thread
kilograham marked this conversation as resolved.
Outdated
# Attempt to compile the source file
try_compile(CLIB_MATCHED
"${CMAKE_CURRENT_BINARY_DIR}" # Directory for temporary build files
"${CLIB_DETECT_SOURCE}" # Source file to compile
COMPILE_DEFINITIONS "-D${CLIB}=1"
)
if (CLIB_MATCHED)
set(PICO_CLIB ${CLIB})
break()
endif()
endforeach ()
if (NOT PICO_CLIB)
message("C library not detected; defaulting to newlib")
set(PICO_CLIB newlib)
endif()
file(REMOVE "${CLIB_DETECT_SOURCE}")
set(PICO_CLIB "${PICO_CLIB}" CACHE INTERNAL "")
endif()
message("C library type is ${PICO_CLIB}")
target_link_libraries(pico_clib_interface INTERFACE pico_${PICO_CLIB}_interface)
endif()
Loading