Skip to content

Commit

Permalink
lib/newlib: revert treatment of libc files as system includes
Browse files Browse the repository at this point in the history
The solution from zephyrproject-rtos#14312 of using -isystem to prioritize the position of
the libc directory bypasses the effect of -ffreestanding with respect to
libc symbols expected to be present in a non-hosted environment.

Further, it breaks C++ with the ARM Embedded toolchain as the system
fails to find the right file with #include_next.

Use a more fine-grained solution that explicitly includes the underlying
newlib header required for <inttypes.h> support before moving on to
include the next available one, whether system or non-system.

Closes zephyrproject-rtos#17564

Signed-off-by: Peter Bigot <[email protected]>
  • Loading branch information
pabigot authored and galak committed Aug 20, 2019
1 parent bd72ea1 commit 96c1b05
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/libc/newlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
zephyr_library()
zephyr_library_sources(libc-hooks.c)

# Zephyr normally uses -ffreestanding, which with current GNU toolchains
# means that the flag macros used by newlib 3.x <inttypes.h> to signal
# support for PRI.64 macros are not present. To make them available we
# need to hook into the include path before the system files and
# explicitly include the newlib header that provides those macros.
zephyr_include_directories(include)

# LIBC_*_DIR may or may not have been set by the toolchain. E.g. when
# using ZEPHYR_TOOLCHAIN_VARIANT=cross-compile it will be either up to the
# toolchain to know where it's libc implementation is, or if it is
# unable to, it will be up to the user to specify LIBC_*_DIR vars to
# point to a newlib implementation.
# point to a newlib implementation. Note that this does not change the
# directory order if LIBC_INCLUDE_DIR is already a system header
# directory.

# We need to make sure this is included before the standard system
# header include path's since we build with -ffreestanding and need
# our libc headers to be picked instead of the toolchain's ffreestanding
# headers.
if(LIBC_INCLUDE_DIR)
zephyr_system_include_directories(${LIBC_INCLUDE_DIR})
zephyr_include_directories(${LIBC_INCLUDE_DIR})
endif()

if(LIBC_LIBRARY_DIR)
Expand Down
30 changes: 30 additions & 0 deletions lib/libc/newlib/include/stdint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_
#define ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_

/* Work around -ffreestanding absence of defines required to support
* PRI.64 macros in <inttypes.h> by including the newlib header that
* provides the flag macros.
*/

#include <newlib.h>

#ifdef __NEWLIB__
/* Has this header. Older versions do it in <stdint.h>. */
#include <sys/_stdint.h>
#endif /* __NEWLIB__ */

/* This should work on GCC and clang.
*
* If we need to support a toolchain without #include_next the CMake
* infrastructure should be used to identify it and provide an
* alternative solution.
*/
#include_next <stdint.h>

#endif /* ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_ */

0 comments on commit 96c1b05

Please sign in to comment.