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 function (picolibc_option_val name val) 60 set(${name} ${val} PARENT_SCOPE) 61 endfunction() 62 63 # Map Zephyr options to Picolibc options 64 65 picolibc_option_true(FAST_STRCMP CONFIG_PICOLIBC_FAST_STRCMP) 66 picolibc_option_true("_WANT_IO_C99_FORMATS" CONFIG_PICOLIBC_IO_C99_FORMATS) 67 picolibc_option_true("_WANT_MINIMAL_IO_LONG_LONG" CONFIG_PICOLIBC_IO_MINIMAL_LONG_LONG) 68 picolibc_option_false("_WANT_IO_PERCENT_B" CONFIG_PICOLIBC_IO_PERCENT_B) 69 picolibc_option_true("_WANT_IO_POS_ARGS" CONFIG_PICOLIBC_IO_POS_ARGS) 70 picolibc_option_true("_WANT_IO_LONG_DOUBLE" CONFIG_PICOLIBC_IO_LONG_DOUBLE) 71 picolibc_option_true("_PRINTF_SMALL_ULTOA" CONFIG_PICOLIBC_IO_SMALL_ULTOA) 72 picolibc_option_true("_FORMAT_DEFAULT_DOUBLE" CONFIG_PICOLIBC_IO_FLOAT) 73 picolibc_option_val("_FORMAT_DEFAULT_FLOAT" 0) 74 picolibc_option_true("_FORMAT_DEFAULT_LONG_LONG" CONFIG_PICOLIBC_IO_LONG_LONG) 75 picolibc_option_true("_FORMAT_DEFAULT_INTEGER" CONFIG_PICOLIBC_IO_INTEGER) 76 picolibc_option_true("_FORMAT_DEFAULT_MINIMAL" CONFIG_PICOLIBC_IO_MINIMAL) 77 picolibc_option_true("_IO_FLOAT_EXACT" CONFIG_PICOLIBC_IO_FLOAT_EXACT) 78 picolibc_option_true("__HAVE_LOCALE_INFO__" CONFIG_PICOLIBC_LOCALE_INFO) 79 picolibc_option_true("__HAVE_LOCALE_INFO_EXTENDED__" CONFIG_PICOLIBC_LOCALE_EXTENDED_INFO) 80 picolibc_option_true("_MB_CAPABLE" CONFIG_PICOLIBC_MULTIBYTE) 81 picolibc_option_false("__SINGLE_THREAD__" CONFIG_PICOLIBC_MULTITHREAD) 82 picolibc_option_true("PICOLIBC_TLS" CONFIG_THREAD_LOCAL_STORAGE) 83 picolibc_option_true("NEWLIB_GLOBAL_ERRNO" CONFIG_PICOLIBC_GLOBAL_ERRNO) 84 picolibc_option_true(PREFER_SIZE_OVER_SPEED CONFIG_SIZE_OPTIMIZATIONS) 85 picolibc_option_true("_ASSERT_VERBOSE" CONFIG_PICOLIBC_ASSERT_VERBOSE) 86 87 if(NOT DEFINED CONFIG_THREAD_LOCAL_STORAGE) 88 set(__PICOLIBC_ERRNO_FUNCTION z_errno_wrap) 89 endif() 90 91 # Fetch zephyr compile flags from interface 92 get_property(PICOLIBC_EXTRA_COMPILE_OPTIONS TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_OPTIONS) 93 94 # Enable POSIX APIs 95 96 # Link to C library and libgcc (on non-armclang toolchains) 97 98 if(${ZEPHYR_TOOLCHAIN_VARIANT} STREQUAL "armclang") 99 zephyr_link_libraries(c) 100 else() 101 zephyr_link_libraries(c -lgcc) 102 endif() 103 104 # Provide an alias target for zephyr to use 105 106 add_custom_target(PicolibcBuild) 107 add_dependencies(PicolibcBuild c) 108endif() 109