Skip to content

[libc] Stop depending on .cpp files libcxx_shared_headers library. #133999

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

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

vonosmas
Copy link
Contributor

@vonosmas vonosmas commented Apr 1, 2025

Fix two instances of libcxx_shared_headers depending on .cpp files (in Bazel build):

  • Don't depend on exit syscall in LIBC_ASSERT implementation. This dependency is not used, since LIBC_ASSERT always uses system <assert.h> in the overlay mode, which is the only mode supported by Bazel.
  • Don't depend on libc_errno in str-to-float and str-to-integer conversions. We only need the ERANGE value, which can be obtained from the proxy header instead.

Fix two instances of libcxx_shared_headers depending on .cpp files (in
Bazel build):

* Don't depend on exit syscall in LIBC_ASSERT implementation. This
  dependency is not used, since LIBC_ASSERT always uses system
  <assert.h> in the overlay mode, which is the only mode supported by
  Bazel.
* Don't depend on libc_errno in str-to-float and str-to-integer
  conversions. We only need the ERANGE value, which can be obtained
  from the proxy header instead.
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Apr 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 1, 2025

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

Changes

Fix two instances of libcxx_shared_headers depending on .cpp files (in Bazel build):

  • Don't depend on exit syscall in LIBC_ASSERT implementation. This dependency is not used, since LIBC_ASSERT always uses system <assert.h> in the overlay mode, which is the only mode supported by Bazel.
  • Don't depend on libc_errno in str-to-float and str-to-integer conversions. We only need the ERANGE value, which can be obtained from the proxy header instead.

Full diff: https://github.com/llvm/llvm-project/pull/133999.diff

5 Files Affected:

  • (modified) libc/src/__support/CMakeLists.txt (+2-2)
  • (modified) libc/src/__support/libc_assert.h (+1-1)
  • (modified) libc/src/__support/str_to_float.h (+1-1)
  • (modified) libc/src/__support/str_to_integer.h (+1-1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+3-23)
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 17f03a6b6c4a0..f92499fdbf451 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -165,7 +165,7 @@ add_header_library(
   DEPENDS
     .ctype_utils
     .str_to_num_result
-    libc.src.errno.errno
+    libc.hdr.errno_macros
     libc.src.__support.CPP.limits
     libc.src.__support.CPP.type_traits
     libc.src.__support.common
@@ -217,6 +217,7 @@ add_header_library(
     .str_to_integer
     .str_to_num_result
     .uint128
+    libc.hdr.errno_macros
     libc.src.__support.common
     libc.src.__support.CPP.bit
     libc.src.__support.CPP.limits
@@ -226,7 +227,6 @@ add_header_library(
     libc.src.__support.macros.config
     libc.src.__support.macros.null_check
     libc.src.__support.macros.optimization
-    libc.src.errno.errno
 )
 
 add_header_library(
diff --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h
index 3db179ff67212..ada1795ccb80a 100644
--- a/libc/src/__support/libc_assert.h
+++ b/libc/src/__support/libc_assert.h
@@ -9,7 +9,6 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_LIBC_ASSERT_H
 #define LLVM_LIBC_SRC___SUPPORT_LIBC_ASSERT_H
 
-#include "src/__support/macros/config.h"
 #if defined(LIBC_COPT_USE_C_ASSERT) || !defined(LIBC_FULL_BUILD)
 
 // The build is configured to just use the public <assert.h> API
@@ -25,6 +24,7 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/integer_to_string.h"
 #include "src/__support/macros/attributes.h"   // For LIBC_INLINE
+#include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // For LIBC_UNLIKELY
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index 48c88309c58e9..0748e1cb8a8b4 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
 #define LLVM_LIBC_SRC___SUPPORT_STR_TO_FLOAT_H
 
+#include "hdr/errno_macros.h" // For ERANGE
 #include "src/__support/CPP/bit.h"
 #include "src/__support/CPP/limits.h"
 #include "src/__support/CPP/optional.h"
@@ -31,7 +32,6 @@
 #include "src/__support/str_to_integer.h"
 #include "src/__support/str_to_num_result.h"
 #include "src/__support/uint128.h"
-#include "src/errno/libc_errno.h" // For ERANGE
 
 #include <stdint.h>
 
diff --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h
index 9212ad25d0820..76a99a8948941 100644
--- a/libc/src/__support/str_to_integer.h
+++ b/libc/src/__support/str_to_integer.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_STR_TO_INTEGER_H
 #define LLVM_LIBC_SRC___SUPPORT_STR_TO_INTEGER_H
 
+#include "hdr/errno_macros.h" // For ERANGE
 #include "src/__support/CPP/limits.h"
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/CPP/type_traits/make_unsigned.h"
@@ -24,7 +25,6 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/str_to_num_result.h"
 #include "src/__support/uint128.h"
-#include "src/errno/libc_errno.h" // For ERANGE
 
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 77aa75362c71d..4d264b955cf66 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -841,12 +841,6 @@ libc_support_library(
 libc_support_library(
     name = "__support_libc_assert",
     hdrs = ["src/__support/libc_assert.h"],
-    deps = [
-        ":__support_integer_to_string",
-        ":__support_macros_attributes",
-        ":__support_osutil_exit",
-        ":__support_osutil_io",
-    ],
 )
 
 libc_support_library(
@@ -868,7 +862,7 @@ libc_support_library(
         ":__support_ctype_utils",
         ":__support_str_to_num_result",
         ":__support_uint128",
-        ":errno",
+        ":hdr_errno_macros",
     ],
 )
 
@@ -892,7 +886,7 @@ libc_support_library(
         ":__support_str_to_integer",
         ":__support_str_to_num_result",
         ":__support_uint128",
-        ":errno",
+        ":hdr_errno_macros",
     ],
 )
 
@@ -1590,21 +1584,6 @@ libc_support_library(
 
 ########################## externally shared targets ###########################
 
-# TODO: Remove this once downstream users are migrated to libcxx_shared_headers.
-libc_support_library(
-    name = "libc_external_common",
-    hdrs = glob(
-        ["shared/*.h"],
-        exclude = ["shared/rpc_server.h"],
-    ),
-    deps = [
-        ":__support_common",
-        ":__support_fputil_fp_bits",
-        ":__support_str_to_float",
-        ":__support_str_to_integer",
-    ],
-)
-
 libc_header_library(
     name = "libcxx_shared_headers",
     hdrs = [
@@ -1911,6 +1890,7 @@ libc_support_library(
         ":__support_fputil_fma",
         ":__support_fputil_multiply_add",
         ":__support_fputil_polyeval",
+        ":__support_integer_literals",
     ],
 )
 

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the cleanup

@vonosmas vonosmas merged commit 07504af into llvm:main Apr 1, 2025
19 checks passed
@vonosmas vonosmas deleted the libc-bazel-hih branch April 1, 2025 23:23
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request Apr 2, 2025
…llvm#133999)

Fix two instances of libcxx_shared_headers depending on .cpp files (in
Bazel build):

* Don't depend on exit syscall in LIBC_ASSERT implementation. This
dependency is not used, since LIBC_ASSERT always uses system <assert.h>
in the overlay mode, which is the only mode supported by Bazel.
* Don't depend on libc_errno in str-to-float and str-to-integer
conversions. We only need the ERANGE value, which can be obtained from
the proxy header instead.
aheejin added a commit to aheejin/emscripten that referenced this pull request May 15, 2025
aheejin added a commit to aheejin/emscripten that referenced this pull request May 16, 2025
LLVM 20 release contains a bug that it one of the headers in
`libc/src/__support` directory includes a header from `libc/src/errno`,
which is not one of the header directories that are meant to be
included:
https://github.com/llvm/llvm-project/blob/ec28b8f9cc7f2ac187d8a617a6d08d5e56f9120e/libc/src/__support/str_to_float.h#L34

I was told
(llvm/llvm-project#91651 (comment))
this was a bug that was fixed in
llvm/llvm-project#133999. Whether this fix will
be backported to later version of LLVM 20 release is unclear. To avoid
importing files from `libc/src`, this applies the fix here as well.
aheejin added a commit to aheejin/emscripten that referenced this pull request May 16, 2025
LLVM 20 release contains a bug that it one of the headers in
`libc/src/__support` directory includes a header from `libc/src/errno`,
which is not one of the header directories that are meant to be
included:
https://github.com/llvm/llvm-project/blob/ec28b8f9cc7f2ac187d8a617a6d08d5e56f9120e/libc/src/__support/str_to_float.h#L34

I was told
(llvm/llvm-project#91651 (comment))
this was a bug that was fixed in
llvm/llvm-project#133999. Whether this fix will
be backported to later version of LLVM 20 release is unclear. To avoid
importing files from `libc/src`, this applies the fix here as well.
aheejin added a commit to emscripten-core/emscripten that referenced this pull request May 21, 2025
This updates libcxx and libcxxabi to LLVM 20.1.4:
https://github.com/llvm/llvm-project/releases/tag/llvmorg-20.1.4

Before going into each additional change of the PR, these are some
noteworthy changes from this LLVM release:

- Freezing C++03 headers
- RFC:
https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc/77319
  - PRs:
    - llvm/llvm-project#108999
    - llvm/llvm-project#109000
    - llvm/llvm-project#109001
    - llvm/llvm-project#109002
    - 
This copies libc++ headers of the last LLVM 19 release into a separate
directory
(https://github.com/llvm/llvm-project/tree/main/libcxx/include/__cxx03)
and redirects all C++03 workflow there. The motivation is not to fix
C++03 related changes unless they are critical bugs, and simplifies the
main headers. So the main headers are like
  ```
  #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
  #  include <__cxx03/algorithm>
  #else
  ... main header content ...
  ```

As you can see it looks like we can avoid opting into this by not
defining `_LIBCPP_USE_FROZEN_CXX03_HEADERS` at the moment. I think their
eventual goal is to remove C++03 support from the main headers but it
hasn't seem to have happened yet and I don't know what their timeline
for that is.

Adding that `__cxx03` directory increases the header size by 10MB. We
can get away not using them by not defining
`_LIBCPP_USE_FROZEN_CXX03_HEADERS` in this release, so this update does
not include that directory.

- Sharing of headers between libc++ and libc (Project Hand in Hand)
- RFC:
https://discourse.llvm.org/t/rfc-project-hand-in-hand-llvm-libc-libc-code-sharing/77701
- PR: llvm/llvm-project#91651 (More are likely
to come)

This tries to share some libc headers from libcxx. libcxx's source files
can depend on headers from `libc/shared`, `libc/src/__support`,
`libc/include/llvm-libc-macros`, and `libc/include/llvm-libc-types`. And
it turns out libcxx can also depend on `libc/hdr`, even though the
directory is not in the diagram in the RFC.

These headers can be only included from `libcxx/src` and not
`libcxx/include`, to prevent libcxx's API from changing. So these
headers don't need to be copied into `cache/`.

- Locale API reimplementation
  It doesn't seem to have an RFC, but the PRs are:
  - llvm/llvm-project#113737
  - llvm/llvm-project#115176
  - llvm/llvm-project#115752
  - llvm/llvm-project#122489

It looks this aims provide a new way to define locale API for each
platform. Currently Apple, FreeBSD, MSVCRT, and Fuschia are using the
new API and the others are still using the old one:
https://github.com/llvm/llvm-project/blob/ec28b8f9cc7f2ac187d8a617a6d08d5e56f9120e/libcxx/include/__locale_dir/locale_base_api.h#L116-L138
For our purpose, adding `if defined(__EMSCRIPTEN__)` entry to the list
of "old" list seems to work for the moment, but we may need to move to
the new way eventually later.

---

Additional changes:

- Copy `vendor/llvm/default_assertion_handler.in` to
`__assertion_handler`:
aa53648
Our previous `__assertion_handler` was copied from
`libcxx/vendor/llvm/default_assertion_handler.in`. This updates our
`__assertion_handler` to the new
`libcxx/vendor/llvm/default_assertion_handler.in`. For what this file
is, see the PR description of #22994, with which the file was added.

- Remove `libcxx/include/__cxx03` directory:
d646f6b
As I described in "Freezing C++ headers" above, C++03 headers were
copied to `include/cxx03` to be "frozen". But by not defining
`_LIBCPP_USE_FROZEN_CXX03_HEADERS` we can still use the main headers.
This new `__cxx03` header directory is almost 10M and we are not using
it in this update, this deletes it.

- Define more variables in `__config_site`:
9912236
libcxx decides to almost all configuration macros to be defined. So
before it tested whether a macro was defined/undefined, and now it
assumes it is defined and tests whether its value is 1/0.

Before llvm/llvm-project#112094
`_config_site.in` used to use `#cmakedefine`, which only defined
variables when they were enabled, but now it uses `#cmakedefine01`,
which always defines variables and assigns 1 or 0 depending on whether
the feature is enabled.

So this change adds `#define` to each variable that `_config_site.in`
has an entry of. The value assigned is 1 if it was defined in our
previous Emscripten environment and 0 if not.

- Fix Emscripten's locale:
2ae01b0
Before, the code was like
https://github.com/emscripten-core/emscripten/blob/dc1abd514b1bade135a01a4453a9ff6def0793b6/system/lib/libcxx/include/__locale_dir/locale_base_api.h#L12-L30
But now they are divided into two parts, one using the new API and the
other using old. See "Locale API reimplementation" above.

https://github.com/llvm/llvm-project/blob/ec28b8f9cc7f2ac187d8a617a6d08d5e56f9120e/libcxx/include/__locale_dir/locale_base_api.h#L116-L138
This adds Emscripten in the beginning of the "old" API list. (This has
to be the beginning; see #23414)

- Import libc headers used by libcxx:
12a4ee4,
12a4ee4
and
43c8ce4
This imports a part of libc headers into `system/lib/llvm-libc`. The
imported directories are:
  - `libc/shared`
  - `libc/src/__support`
  - `libc/include/llvm-libc-macros`
  - `libc/include/llvm-libc-types`
  - `libc/hdr`
  See "sharing of headers between libc+ and libc" above for details.
This also applies llvm/llvm-project#133999,
which is a bugfix that has not be backported, which fixes the bug of
including from a wrong directory.

- `std::uncaught_exception` -> `std::uncaught_exceptions`:
1bf4e78
`std::uncaught_exception` has been deprecated in C++17, so it generates
a warning (which we treat as an error). Replaced it with
`std::uncaught_exceptions`, which returns the number of uncaught
exceptions (but can still be used as a boolean in the test).

- Remove `ryu_constants.h` and `ryu_long_double_constants.h` from libc:
5767ac4
These are not used from libcxx and `ryu_long_double_constants.h` is huge
(12M).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants