Skip to content

Commit 96c1b05

Browse files
pabigotgalak
authored andcommitted
lib/newlib: revert treatment of libc files as system includes
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]>
1 parent bd72ea1 commit 96c1b05

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

lib/libc/newlib/CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
zephyr_library()
44
zephyr_library_sources(libc-hooks.c)
55

6+
# Zephyr normally uses -ffreestanding, which with current GNU toolchains
7+
# means that the flag macros used by newlib 3.x <inttypes.h> to signal
8+
# support for PRI.64 macros are not present. To make them available we
9+
# need to hook into the include path before the system files and
10+
# explicitly include the newlib header that provides those macros.
11+
zephyr_include_directories(include)
12+
613
# LIBC_*_DIR may or may not have been set by the toolchain. E.g. when
714
# using ZEPHYR_TOOLCHAIN_VARIANT=cross-compile it will be either up to the
815
# toolchain to know where it's libc implementation is, or if it is
916
# unable to, it will be up to the user to specify LIBC_*_DIR vars to
10-
# point to a newlib implementation.
17+
# point to a newlib implementation. Note that this does not change the
18+
# directory order if LIBC_INCLUDE_DIR is already a system header
19+
# directory.
1120

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

2025
if(LIBC_LIBRARY_DIR)

lib/libc/newlib/include/stdint.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_
8+
#define ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_
9+
10+
/* Work around -ffreestanding absence of defines required to support
11+
* PRI.64 macros in <inttypes.h> by including the newlib header that
12+
* provides the flag macros.
13+
*/
14+
15+
#include <newlib.h>
16+
17+
#ifdef __NEWLIB__
18+
/* Has this header. Older versions do it in <stdint.h>. */
19+
#include <sys/_stdint.h>
20+
#endif /* __NEWLIB__ */
21+
22+
/* This should work on GCC and clang.
23+
*
24+
* If we need to support a toolchain without #include_next the CMake
25+
* infrastructure should be used to identify it and provide an
26+
* alternative solution.
27+
*/
28+
#include_next <stdint.h>
29+
30+
#endif /* ZEPHYR_LIB_LIBC_NEWLIB_INCLUDE_STDINT_H_ */

0 commit comments

Comments
 (0)