1#
2# SPDX-License-Identifier: BSD-3-Clause
3#
4# Copyright © 2022 Keith Packard
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above
14#    copyright notice, this list of conditions and the following
15#    disclaimer in the documentation and/or other materials provided
16#    with the distribution.
17#
18# 3. Neither the name of the copyright holder nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33# OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35
36# Zephyr-specific hooks for picolibc
37
38if(CONFIG_PICOLIBC_USE_MODULE)
39  zephyr_include_directories(${PROJECT_BINARY_DIR}/picolibc/include)
40
41  zephyr_compile_definitions(__LINUX_ERRNO_EXTENSIONS__)
42
43  function (picolibc_option_true name option)
44    if(${option})
45      set(${name} 1 PARENT_SCOPE)
46    else()
47      set(${name} 0 PARENT_SCOPE)
48    endif()
49  endfunction()
50
51  function (picolibc_option_false name option)
52    if(${option})
53      set(${name} 0 PARENT_SCOPE)
54    else()
55      set(${name} 1 PARENT_SCOPE)
56    endif()
57  endfunction()
58
59  # Map Zephyr options to Picolibc options
60
61  picolibc_option_true(FAST_STRCMP CONFIG_PICOLIBC_FAST_STRCMP)
62  picolibc_option_true("_WANT_IO_C99_FORMATS" CONFIG_PICOLIBC_IO_C99_FORMATS)
63  picolibc_option_true("_WANT_IO_LONG_LONG" CONFIG_PICOLIBC_IO_LONG_LONG)
64  picolibc_option_false("_WANT_IO_PERCENT_B" CONFIG_PICOLIBC_IO_PERCENT_B)
65  picolibc_option_true("FORMAT_DEFAULT_DOUBLE" CONFIG_PICOLIBC_IO_FLOAT)
66  picolibc_option_false("FORMAT_DEFAULT_INTEGER" CONFIG_PICOLIBC_IO_FLOAT)
67  set(FORMAT_DEFAULT_FLOAT 0)
68  picolibc_option_true("_IO_FLOAT_EXACT" CONFIG_PICOLIBC_IO_FLOAT_EXACT)
69  picolibc_option_true("__HAVE_LOCALE_INFO__" CONFIG_PICOLIBC_LOCALE_INFO)
70  picolibc_option_true("__HAVE_LOCALE_INFO_EXTENDED__" CONFIG_PICOLIBC_LOCALE_EXTENDED_INFO)
71  picolibc_option_true("_MB_CAPABLE" CONFIG_PICOLIBC_MULTIBYTE)
72  picolibc_option_false("__SINGLE_THREAD__" CONFIG_PICOLIBC_MULTITHREAD)
73  picolibc_option_true("PICOLIBC_TLS" CONFIG_THREAD_LOCAL_STORAGE)
74  picolibc_option_true("NEWLIB_GLOBAL_ERRNO" CONFIG_PICOLIBC_GLOBAL_ERRNO)
75  picolibc_option_true(PREFER_SIZE_OVER_SPEED CONFIG_SIZE_OPTIMIZATIONS)
76
77  if(NOT DEFINED CONFIG_THREAD_LOCAL_STORAGE)
78    set(__PICOLIBC_ERRNO_FUNCTION z_errno_wrap)
79  endif()
80
81  # Fetch zephyr compile flags from interface
82  get_property(PICOLIBC_EXTRA_COMPILE_OPTIONS TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_OPTIONS)
83
84  # Enable POSIX APIs
85
86  zephyr_compile_options(-D_POSIX_C_SOURCE=200809)
87
88  # Link to C library and libgcc (on non-armclang toolchains)
89
90  if(${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "armclang")
91    zephyr_link_libraries(c)
92  else()
93    zephyr_link_libraries(c -lgcc)
94  endif()
95
96  # Provide an alias target for zephyr to use
97
98  add_custom_target(PicolibcBuild)
99  add_dependencies(PicolibcBuild c)
100endif()
101