[libcxx-commits] [PATCH] D89040: [libcxx] Allow checking for newlib without using _NEWLIB_VERSION.

Hafiz Abid Qadeer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 8 06:10:16 PDT 2020


abidh created this revision.
abidh added reviewers: libc++, ldionne.
abidh added a project: libc++.
Herald added subscribers: libcxx-commits, dexonsmith, mgorny.
abidh requested review of this revision.

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.

2. 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D89040

Files:
  libcxx/CMakeLists.txt
  libcxx/include/__config
  libcxx/include/__config_site.in
  libcxx/test/support/nasty_macros.h


Index: libcxx/test/support/nasty_macros.h
===================================================================
--- libcxx/test/support/nasty_macros.h
+++ libcxx/test/support/nasty_macros.h
@@ -60,7 +60,11 @@
 #endif
 
 #define __output NASTY_MACRO
+// The newlib uses __input in stdlib.h, so don't define it when newlib is
+// in use.
+#ifndef _LIBCPP_HAS_NEWLIB_LIBC
 #define __input NASTY_MACRO
+#endif
 
 #define __acquire NASTY_MACRO
 #define __release NASTY_MACRO
Index: libcxx/include/__config_site.in
===================================================================
--- libcxx/include/__config_site.in
+++ libcxx/include/__config_site.in
@@ -21,6 +21,7 @@
 #cmakedefine _LIBCPP_HAS_NO_MONOTONIC_CLOCK
 #cmakedefine _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS
 #cmakedefine _LIBCPP_HAS_MUSL_LIBC
+#cmakedefine _LIBCPP_HAS_NEWLIB_LIBC
 #cmakedefine _LIBCPP_HAS_THREAD_API_PTHREAD
 #cmakedefine _LIBCPP_HAS_THREAD_API_EXTERNAL
 #cmakedefine _LIBCPP_HAS_THREAD_API_WIN32
Index: libcxx/include/__config
===================================================================
--- libcxx/include/__config
+++ libcxx/include/__config
@@ -327,6 +327,11 @@
 #  define _LIBCPP_USING_DEV_RANDOM
 #endif
 
+// Include newlib.h to get _NEWLIB_VERSION
+#if defined(_LIBCPP_HAS_NEWLIB_LIBC)
+#  include <newlib.h>
+#endif
+
 #if !defined(_LIBCPP_LITTLE_ENDIAN) && !defined(_LIBCPP_BIG_ENDIAN)
 #  include <endian.h>
 #  if __BYTE_ORDER == __LITTLE_ENDIAN
Index: libcxx/CMakeLists.txt
===================================================================
--- libcxx/CMakeLists.txt
+++ libcxx/CMakeLists.txt
@@ -242,6 +242,7 @@
   "Build libc++ with support for a monotonic clock.
    This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON)
 option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
+option(LIBCXX_HAS_NEWLIB_LIBC "Build libc++ with support for the Newlib C library" OFF)
 option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
 option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 thread API" OFF)
 option(LIBCXX_HAS_EXTERNAL_THREAD_API
@@ -845,6 +846,7 @@
 config_define_if(LIBCXX_HAS_WIN32_THREAD_API _LIBCPP_HAS_THREAD_API_WIN32)
 config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
 config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC)
+config_define_if(LIBCXX_HAS_NEWLIB_LIBC _LIBCPP_HAS_NEWLIB_LIBC)
 config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
 config_define_if(LIBCXX_ENABLE_PARALLEL_ALGORITHMS _LIBCPP_HAS_PARALLEL_ALGORITHMS)
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89040.296939.patch
Type: text/x-patch
Size: 2603 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201008/bfa4181a/attachment.bin>


More information about the libcxx-commits mailing list