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_any_true name option_list)
52    foreach(option ${option_list})
53      if (${option})
54        set(${name} 1 PARENT_SCOPE)
55        return()
56      endif()
57    endforeach()
58    set(${name} 0 PARENT_SCOPE)
59  endfunction()
60
61  function (picolibc_option_false name option)
62    if(${option})
63      set(${name} 0 PARENT_SCOPE)
64    else()
65      set(${name} 1 PARENT_SCOPE)
66    endif()
67  endfunction()
68
69  function (picolibc_option_val name val)
70    set(${name} ${val} PARENT_SCOPE)
71  endfunction()
72
73  # Map Zephyr options to Picolibc options
74
75  picolibc_option_true(FAST_STRCMP CONFIG_PICOLIBC_FAST_STRCMP)
76  picolibc_option_true("_WANT_IO_C99_FORMATS" CONFIG_PICOLIBC_IO_C99_FORMATS)
77  picolibc_option_true("_WANT_MINIMAL_IO_LONG_LONG" CONFIG_PICOLIBC_IO_MINIMAL_LONG_LONG)
78  picolibc_option_false("_WANT_IO_PERCENT_B" CONFIG_PICOLIBC_IO_PERCENT_B)
79  picolibc_option_true("_WANT_IO_POS_ARGS" CONFIG_PICOLIBC_IO_POS_ARGS)
80  picolibc_option_true("_WANT_IO_LONG_DOUBLE" CONFIG_PICOLIBC_IO_LONG_DOUBLE)
81  picolibc_option_true("_PRINTF_SMALL_ULTOA" CONFIG_PICOLIBC_IO_SMALL_ULTOA)
82  picolibc_option_true("_FORMAT_DEFAULT_DOUBLE" CONFIG_PICOLIBC_IO_FLOAT)
83  picolibc_option_val("_FORMAT_DEFAULT_FLOAT" 0)
84  picolibc_option_true("_FORMAT_DEFAULT_LONG_LONG" CONFIG_PICOLIBC_IO_LONG_LONG)
85  picolibc_option_true("_FORMAT_DEFAULT_INTEGER" CONFIG_PICOLIBC_IO_INTEGER)
86  picolibc_option_true("_FORMAT_DEFAULT_MINIMAL" CONFIG_PICOLIBC_IO_MINIMAL)
87  picolibc_option_true("_IO_FLOAT_EXACT" CONFIG_PICOLIBC_IO_FLOAT_EXACT)
88  picolibc_option_true("__HAVE_LOCALE_INFO__" CONFIG_PICOLIBC_LOCALE_INFO)
89  picolibc_option_true("__HAVE_LOCALE_INFO_EXTENDED__" CONFIG_PICOLIBC_LOCALE_EXTENDED_INFO)
90  picolibc_option_true("_MB_CAPABLE" CONFIG_PICOLIBC_MULTIBYTE)
91  picolibc_option_false("__SINGLE_THREAD__" CONFIG_PICOLIBC_MULTITHREAD)
92  picolibc_option_true("PICOLIBC_TLS" CONFIG_THREAD_LOCAL_STORAGE)
93  picolibc_option_true("NEWLIB_GLOBAL_ERRNO" CONFIG_PICOLIBC_GLOBAL_ERRNO)
94  picolibc_option_any_true(PREFER_SIZE_OVER_SPEED "CONFIG_MIN_SIZE_OPTIMIZATIONS;CONFIG_SIZE_OPTIMIZATIONS")
95  picolibc_option_true("_ASSERT_VERBOSE" CONFIG_PICOLIBC_ASSERT_VERBOSE)
96
97  if(NOT DEFINED CONFIG_THREAD_LOCAL_STORAGE)
98    set(__PICOLIBC_ERRNO_FUNCTION z_errno_wrap)
99  endif()
100
101  # Fetch zephyr compile flags from interface
102  get_property(PICOLIBC_EXTRA_COMPILE_OPTIONS TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_OPTIONS)
103
104  # Tell Zephyr about the module built picolibc library to link against.
105  # We need to construct the absolute path to picolibc in same fashion as CMake
106  # defines the library file name and location, because a generator expression
107  # cannot be used as the CMake linker rule definition doesn't supports them.
108  set_property(TARGET linker PROPERTY c_library
109      ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}c${CMAKE_STATIC_LIBRARY_SUFFIX}
110  )
111  zephyr_system_include_directories($<TARGET_PROPERTY:c,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>)
112
113  # Provide an alias target for zephyr to use
114  add_custom_target(PicolibcBuild)
115  add_dependencies(PicolibcBuild c)
116endif()
117