CMakeLists.txt 3.1 KB

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