This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Allow checking for newlib without using _NEWLIB_VERSION.
AbandonedPublic

Authored by abidh on Oct 8 2020, 6:10 AM.

Details

Reviewers
ldionne
Group Reviewers
Restricted Project
Summary

Existing code in libc++ uses _NEWLIB_VERSION to check for the presence
of newlib. But this macro is only set when a library header has been
included before that brings in its definition. In some cases, this macro
can't be used as a library header has not been included before it use.

This patch adds a CMake variable and corresponding macro that is used
to check for the presence of newlib in the following 2 cases:

  1. The __config header is the generally the first header included. So

_NEWLIB_VERSION used in it will not be set. We check for
_LIBCPP_HAS_NEWLIB_LIBC and include newlib.h that will make sure that
_NEWLIB_VERSION is correctly defined.

  1. The nasty_macros.h also has similar issue so _NEWLIB_VERSION can't be

used in it. We check for _LIBCPP_HAS_NEWLIB_LIBC to stub out a
problematic macro for newlib.

The new variable for newlib is quite similar to existing variable for MUSL.

Diff Detail

Event Timeline

abidh created this revision.Oct 8 2020, 6:10 AM
abidh requested review of this revision.Oct 8 2020, 6:10 AM
ldionne requested changes to this revision.Oct 21 2020, 5:28 AM

I see a couple issues with this:

  1. I don't want to add a new CMake option just to detect an underlying libc. We should be able to detect it through other means. Actually why do we even need _LIBCPP_HAS_NEWLIB_LIBC?
  2. We should have testers for this configuration. Newlib is not currently supported (well.. not officially at least), and we need some kind of testing if we pretend we're supporting it.
This revision now requires changes to proceed.Oct 21 2020, 5:28 AM
abidh added a comment.Oct 22 2020, 3:07 AM

Thanks for the review.

I see a couple issues with this:

Actually why do we even need _LIBCPP_HAS_NEWLIB_LIBC?

Consider example of __config, it already has some code conditional on _NEWLIB_VERSION. But this macro will always be undefined in this file as nothing is bringing in its definition. Note that _NEWLIB_VERSION is not defined by the compiler but it needs newlib.h to be included either directly or indirectly through some other library header. This is what _LIBCPP_HAS_NEWLIB_LIBC was trying to do. Detect newlib where we could not use _NEWLIB_VERSION.

  1. I don't want to add a new CMake option just to detect an underlying libc. We should be able to detect it through other means.

One alternate I could think of what to use -include newlib.h in my compiler flags. That works but the problem with nasty_macros.h still remains unless I use an override of configure_compile_flags_header_includes in my customized config file to skip the check for nasty_macros.h. This can work but it will require all newlib users to handle these things in their own config files.

  1. We should have testers for this configuration. Newlib is not currently supported (well.. not officially at least), and we need some kind of testing if we pretend we're supporting it.

You mentioned in https://reviews.llvm.org/D88825 that you will write some instruction. I will be happy to setup one.

abidh abandoned this revision.Aug 4 2021, 8:36 AM