-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only use -ffreestanding with minimal C library #54638
Only use -ffreestanding with minimal C library #54638
Conversation
177ed1b
to
3a6f52d
Compare
3a6f52d
to
cc46680
Compare
cc46680
to
ea762b4
Compare
I've split this PR into two commits, the first adds COMPILER_FREESTANDING without changing current semantics, the second adopts the semantics proposed in the PR description. Happy to split this PR apart if that makes things easier in any way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although from a logical viewpoint it makes sense to first decide whether hosted or freestanding is to be used, then we also have a wish to have picolibc default C-library in future.
So probably we should either default COMPILER_FREESTANDING
to n
, or have a helper symbol, such as SUPPORTS_FREESTANDING
, so that a C-library, such as PICOLIBC can do:
config SUPPORTS_FREESTANDING
default n if PICOLIBC
and then have:
config COMPILER_FREESTANDING
bool "Build in a freestanding compiler mode"
depends on SUPPORTS_FREESTANDING
from user perspective, I think I prefer user should select freestanding mode first, and then can choose available C-libs, but am open to alternative.
In such case, default n
is probably best for freestanding mode.
Kconfig.zephyr
Outdated
@@ -327,6 +327,16 @@ config NATIVE_APPLICATION | |||
Build as a native application that can run on the host and using | |||
resources and libraries provided by the host. | |||
|
|||
config COMPILER_FREESTANDING | |||
bool "Build in a freestanding compiler mode" | |||
default y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a good choice ?
If COMPILER_FREESTANDING
is default y
, then it means that PICOLIBC
cannot be selected per default.
In order to use picolibs, users / samples must explicitly disable COMPILER_FREESTANDING
befora being able to select picolibc.
I don't think that is what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help here -- I don't know how to set things up so that freestanding is used unless you're using picolibc.
I'm not sure -- the only reason freestanding mode should ever be used in Zephyr is when the system cannot support hosted mode due to bugs or gaps in the implementation. Even the Linux kernel is built in hosted mode to gain all of the compiler benefits.
Oh, a helper symbol is probably a good idea, but I'd invert the sense -- 'SUPPORTS_HOSTED' -- so that the system can move away from freestanding mode when the underlying environment supports hosted mode.
I disagree -- I think freestanding mode is entirely a consequence of having bugs/gaps which prevent the use of hosted mode; the user shouldn't generally care if it has to be enabled. However, it's pretty clear I don't understand the Zephyr Kconfig system well enough to get the result I'm after -- having hosted mode be selected iff the environment supports it. |
I agree that hosted is the optimal choice, I was more referring to the following two scenarios:
unfortunately Kconfig doesn't allow for a real mutual exclusive functionality where both C-libraries and hosted/freestanding choices are available and the the other automatically becomes unavailable if the other is selected. Like this:
where
No problem, but we need to decide what choice the user should make first / always be able to make.
Do I understand this correctly, so that if the environment supports host, then hosted should always be used ?
possibility
it not so much about the Zephyr Kconfig, but more a limitation in Kconfig which doesn't support config loops that results in us having to decide which symbol is always available for configuration, and then the other symbol is available depending on the value of first symbol. |
@@ -327,6 +327,15 @@ config NATIVE_APPLICATION | |||
Build as a native application that can run on the host and using | |||
resources and libraries provided by the host. | |||
|
|||
config COMPILER_FREESTANDING | |||
bool "Build in a freestanding compiler mode" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we need this to be user selectable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so -- it's really just a consequence of an environment which is lacking a standard C library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So lets make this just bool
and drop the user selectable nature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User selection may still be desirable since dropping -ffreestanding
enables a few optimisations using the libc functions, which user may or may not want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it best to wait for someone to come ask for this to be user selectable. 99.99% of users are not going to know how to set it and I don't think we should expose it until someone comes with a real need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it best to wait for someone to come ask for this to be user selectable. 99.99% of users are not going to know how to set it and I don't think we should expose it until someone comes with a real need.
Why? Based on that reasoning, we should not have anything user configurable until someone comes with a need.
And I can already see the usefulness of having this user configurable (e.g. debugging a "mysterious" struct assignment failure to see whether it is due to the compiler making use of the libc memcpy
by toggling this config in the menuconfig).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as the defaults are correct, having this configurable by the user seems reasonable, it's something they "shouldn't" have to set, but it's also not something that Zephyr requires to be set in any particular way. This will also allow people to experiment with this flag, which might help them become more comfortable working in a 'hosted' environment.
Definitely. Hosted mode makes the compiler able to detect many common programming mistakes and generate better code. As I've said a few times, even the Linux kernel uses hosted mode to gain these benefits. Hosted mode only means you have a C library, nothing more. |
@keith-packard I noticed you updated the last commit to remove the I have a small proposal on how we can achieve the following in Kconfig without causing Kconfig loop and have both the +config COMPILER_HOSTED_FORCED
+ bool
+
+config COMPILER_HOSTED
+ bool
+ default y if !COMPILER_FREESTANDING
+
+config COMPILER_FREESTANDING
+ bool "Build in a freestanding compiler mode"
+ depends on !COMPILER_HOSTED_FORCED
+ help
+ <help text>
+ then various c-libraries can enforce freestanding or hosted mode. Like this:
This has the following behavior. A C library can force the use of freestanding mode by selecting If a c-library doesn't enforce a particular mode, then the I assume this is the behavior we want to achieve. |
We are way over-complicating this for no good reason. The solution should be as simple as adding |
Agree with the first half, I still disagree with making this user-configurable. |
if I understand @keith-packard correctly and this commit 92ae8bc
then freestanding mode should never be chosen for picolibc. If that is the case, we have three situations, forcing freestanding, forcing hosted, let user decide. The |
yeah, I just went and tried to build picolibc with -ffreestanding and there were compile warnings and some test suite failures. I'll treat those as bugs -- this "should" work, but it pretty clearly doesn't right now. |
Agreed. Picolibc should be able to build for both "hosted" and "freestanding" environments; there is no technical reason why |
I've fixed the bugs upstream and am working on a 1.8.1 (or maybe 1.9) release in the next month or so. However, picolibc CI isn't running tests in -ffreestanding mode currently. I need to add a meson configuration option to add -ffreestanding to the compile options to make this easy; otherwise it would require separate cross compilation control scripts. |
Looks like there are some minor clang warnings that come up about using |
With picolibc/picolibc#438 picolibc will get regular CI validation of |
That sounds a bit like a request for changes; I'll see if I can clean these up. |
ea762b4
to
c3e50b3
Compare
Here's what I've got: |
@keith-packard is this PR blocked by #54628 ? i.e. will CI fail here until we change the return type of |
c3e50b3
to
b1f31bc
Compare
Thanks for checking. Yes, I think it is blocked by that; with the other remaining PR merged, I think that's all we're waiting for. I've rebased to verify -- the clang build should fail on newlib and picolibc test cases. |
While the build here is failing with clang, that doesn't appear related to the main return type. Running the clang tests locally on native_posix, they all pass. That's not surprising as the native_posix architecture has it's own main entry point; any Zephyr main entry point is renamed via "preprocessor trickery". So, while this PR should pass CI, I think we should block on #54628 as anyone using clang on other architectures will be impacted. Help figuring out why the clang build here is failing would be appreciate; I re-tried the tests once and they still failed with mysterious errors. |
Add an explicit compiler configuration, COMPILER_FREESTANDING, which controls whether the compiler should operate in freestanding or hosted mode (according to the C and C++ language specifications. This depends on having a C library which conforms with the language specification, and the minimal C library does not. Have the minimal C library select COMPILER_FREESTANDING to continue using freestanding mode with that library. For other C libraries, leave this disabled by default while allowing users to enable it if they want to go back to the previous configuration. Signed-off-by: Keith Packard <[email protected]>
b1f31bc
to
55c0a5c
Compare
I've switched the minimal libc Kconfig entry to use |
ok, looks like the build errors have mysteriously resolved themselves. I think the code here is ready to go. |
As Zephyr applications are "hosted" according to the C language spec, we should be telling the compiler to perform error checking and optimizations based upon the hosted language specification instead of the freestanding one.
However, this depends on having a C library which conforms with the language specification, and the minimal C library does not. Continue to use freestanding mode when that library is in use.
See discussion: #54336 (comment)
Signed-off-by: Keith Packard [email protected]