1# SPDX-License-Identifier: Apache-2.0
2
3zephyr_library()
4zephyr_library_sources(libc-hooks.c)
5
6# Zephyr normally uses -ffreestanding, which with current GNU toolchains
7# means that the flag macros used by newlib 3.x <inttypes.h> to signal
8# support for PRI.64 macros are not present.  To make them available we
9# need to hook into the include path before the system files and
10# explicitly include the newlib header that provides those macros.
11zephyr_include_directories(include)
12
13# LIBC_*_DIR may or may not have been set by the toolchain. E.g. when
14# using ZEPHYR_TOOLCHAIN_VARIANT=cross-compile it will be either up to the
15# toolchain to know where it's libc implementation is, or if it is
16# unable to, it will be up to the user to specify LIBC_*_DIR vars to
17# point to a newlib implementation.  Note that this does not change the
18# directory order if LIBC_INCLUDE_DIR is already a system header
19# directory.
20
21if(LIBC_INCLUDE_DIR)
22  zephyr_include_directories(${LIBC_INCLUDE_DIR})
23endif()
24
25if(LIBC_LIBRARY_DIR)
26  set(LIBC_LIBRARY_DIR_FLAG -L${LIBC_LIBRARY_DIR})
27endif()
28
29# define __LINUX_ERRNO_EXTENSIONS__ so we get errno defines like -ESHUTDOWN
30# used by the network stack
31zephyr_compile_definitions(__LINUX_ERRNO_EXTENSIONS__)
32
33# We are using
34# - ${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}c
35# - ${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}gcc
36# - c
37# in code below.
38# This is needed because when linking with newlib on aarch64, then libgcc has a
39# link dependency to libc (strchr), but libc also has dependencies to libgcc.
40#
41# CMake is capable of handling circular link dependencies for CMake defined
42# static libraries, which can be further controlled using LINK_INTERFACE_MULTIPLICITY.
43# However, libc and libgcc are not regular CMake libraries, and is seen as linker
44# flags by CMake, and thus symbol de-duplications will be performed.
45# CMake link options cannot be used, as that will place those libs first on the
46# linker invocation. -Wl,--start-group is problematic as the placement of -lc
47# and -lgcc is not guaranteed in case later libraries are also using
48# -lc / -libbgcc as interface linker flags.
49#
50# Thus, we resort to use `${CMAKE_C_LINKER_WRAPPER_FLAG}${CMAKE_LINK_LIBRARY_FLAG}`
51# as this ensures the uniqueness and thus avoids symbol de-duplication which means
52# libc will be followed by libgcc, which is finally followed by libc again.
53
54list(JOIN CMAKE_C_LINKER_WRAPPER_FLAG "" linker_wrapper_string)
55
56zephyr_link_libraries(
57  m
58  "${linker_wrapper_string}${CMAKE_LINK_LIBRARY_FLAG}c"
59  ${LIBC_LIBRARY_DIR_FLAG} # NB: Optional
60  $<$<BOOL:${CONFIG_NEWLIB_LIBC_FLOAT_PRINTF}>:-u_printf_float>
61  $<$<BOOL:${CONFIG_NEWLIB_LIBC_FLOAT_SCANF}>:-u_scanf_float>
62  # Lib C depends on libgcc. e.g. libc.a(lib_a-fvwrite.o) references __aeabi_idiv
63  "${linker_wrapper_string}${CMAKE_LINK_LIBRARY_FLAG}gcc"
64  c
65  )
66
67if(CONFIG_NEWLIB_LIBC_NANO)
68  zephyr_link_libraries(
69    -specs=nano.specs
70    )
71  zephyr_compile_options(
72    -specs=nano.specs
73    )
74endif()
75