Description
Background:
- Today the Zephyr coding guidelines A.4 & A.5 specify what functions can be used in the kernel and in-tree.
1.1) Most of these functions are part of the base C11 standard, two are (POSIX.1-2008) extensions. - Some developers have the understanding that this requires the used C libraries to expose these functions without the need for the user files (C / cmake) to define any
SOURCE
macros. Picolibc and minimallibc do this. Newlib, the integration with the host C library for the native targets, or other libraries out there don't. - Checkpatch today has a simple check of A.4 and A.5 by warning on definitions of
SOURCE
macros in .c files. This has two problems: 4.1) it does not detect definitions in build files. 4.2) It blocks legitimate uses of these macros.
This RFC does NOT try to discuss what should be in 1).
If focuses on 2)
Clarification needed
Has there been any kind of authoritative decision on 2) (i.e. TSC vote), or is this just an understanding reached in some PR discussion by a subset of developers?
Discussion on 2)
Today PicolibC ensures that these 2 extra functions prototypes are defined by treating Zephyr specially, with extra Zephyr specific ifdefs to detect and expose them. Newlib does not, and one cannot expect other C libraries the user may use to provide these.
The expectation that these 2 are exposed, sets a weird requirement on the C library integration
(See #67040 (comment) , #67040 (comment) , #67040 (comment) )
Under the assumption that 2) is the way forward, a simple way to work around this issue for C libraries without special Zephyr handling is to define globally the _POSIX_C_SOURCE
macro. This is what PicolibC did before, and what #67040 proposed doing for Newlib.
There is the minor disadvantage of doing this, that we can cause this macro to be redefined, causing a redefinition warning, either in tree or in users code.
So far the only issue with assuming that 2) is not the way forward, is that the _POSIX_C_SOURCE
macro needs to be defined where those 2 functions are used. As such there is no drawback of this beyond a) _POSIX_C_SOURCE
needs to be defined in those files , and b) That checkpatch is triggered if that is done in the C source files themselves.
a) is something one would need to do with another OS or C library, so something users should expect and technically correct. Avoiding to do this due to that checkpatch check seems the wrong reason, as checkpatch detection of this rules violation is anyhow both very limited and overeager.
Proposal
- C libraries are not expected to expose the extra functions defined in A.4 and A.5 by default
- Source code in tree defines the required
_POSIX_C_SOURCE
macro locally (in the C file or build for that file/library) where it uses these extra functions. - Checkpatch detection of the
_POSIX_C_SOURCE
macro usage in .c files is fixed (i.e. does not warn for this use when only these 2 functions are used). - To avoid people assuming the oposite, coding guidelines rules A.4 & A.5 would state that C libraries may require
_POSIX_C_SOURCE
be set for those 2 functions prototypes to be visible.
Benefit of the proposal:
- We stop requiring extra integration and adding complexity around integration of C libraries. We just do what we would be expected to do and is technically correct. This would simplify the integration of other C libraries both in and out of tree, and avoid issues derived from the workarounds that extra integration requires (There is no nice way of solving this issue without modifying the C library source).
- PicolibC can stop treating Zephyr specially if desired (i.e. remove
_ZEPHYR_VISIBLE
) - Checkpatch check for this rule needs refining anyhow, as today's detection is faulty both in false positives and false negatives.
References:
- https://docs.zephyrproject.org/latest/contribute/coding_guidelines/index.html#rule-a-4-c-standard-library-usage-restrictions-in-zephyr-kernel
- https://docs.zephyrproject.org/latest/contribute/coding_guidelines/index.html#rule-a-5-c-standard-library-usage-restrictions-in-zephyr-codebase
- Add coding guidelines on C standard library function usage #57598
- Remove explicit -D_POSIX_C_SOURCE global compiler options when building with picolibc #67040
- Several fixes around POSIX_SOURCE, remove global definitions of POSIX_SOURCE #67052
- Make picolibc the default C library for Zephyr #49922
- Specifically this part of the discussion Make picolibc the default C library for Zephyr #49922 (comment)
- scripts/checkpatch: Check for patches adding #defines for libc APIs #60487
- libc: newlib/picolibc: define prototypes for non-ansi C functions #57619
Additional notes
Please do not use this RFC to discuss what functions should be part of A.4 & A.5. Create other RFCs for that.
Edit: In proposal replaced "SOURCE macros" with "_POSIX_C_SOURCE macro" as this is the only one I really intended to discuss about here.
Metadata
Metadata
Assignees
Type
Projects
Status