Skip to content

Conversation

@vonosmas
Copy link
Contributor

Create a proper way to build header-only libraries for llvm-libc code sharing. Use it to group headers that can be shared with libcxx for std::from_chars() implementation.

It mostly works, though the macro needs to be updated to enforce that no .cpp files are listed in dependencies (it's not the case now) - see PR #133126.

Create a proper way to build header-only libraries for llvm-libc code
sharing. Use it to group headers that can be shared with libcxx for
std::from_chars() implementation.

It mostly works, though the macro needs to be updated to enforce that no
.cpp files are listed in dependencies (it's not the case now) - see PR llvm#133126.
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Mar 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

Changes

Create a proper way to build header-only libraries for llvm-libc code sharing. Use it to group headers that can be shared with libcxx for std::from_chars() implementation.

It mostly works, though the macro needs to be updated to enforce that no .cpp files are listed in dependencies (it's not the case now) - see PR #133126.


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

2 Files Affected:

  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+17)
  • (modified) utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl (+36)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 89847fb888007..77aa75362c71d 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -9,6 +9,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
 load(
     ":libc_build_rules.bzl",
     "libc_function",
+    "libc_header_library",
     "libc_math_function",
     "libc_support_library",
 )
@@ -1589,6 +1590,7 @@ 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(
@@ -1603,6 +1605,21 @@ libc_support_library(
     ],
 )
 
+libc_header_library(
+    name = "libcxx_shared_headers",
+    hdrs = [
+        "shared/fp_bits.h",
+        "shared/str_to_float.h",
+        "shared/str_to_integer.h",
+    ],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_fp_bits",
+        ":__support_str_to_float",
+        ":__support_str_to_integer",
+    ],
+)
+
 ############################### errno ########################################
 
 libc_support_library(
diff --git a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
index a94e35a003149..4e73170be1e81 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -165,6 +165,42 @@ def libc_release_library(
         **kwargs
     )
 
+def libc_header_library(name, hdrs, deps = [], **kwargs):
+    """Creates a header-only library to share libc functionality.
+
+    Args:
+      name: Name of the cc_library target.
+      hdrs: List of headers to be shared.
+      deps: The list of libc_support_library dependencies if any.
+      **kwargs: All other attributes relevant for the cc_library rule.
+    """
+
+    # Combine sources from dependencies to create a single cc_library target.
+    native.filegroup(
+        name = name + "_hdr_deps",
+        srcs = [dep + "_srcs" for dep in deps],
+    )
+    native.cc_library(
+        name = name + "_textual_hdr_library",
+        textual_hdrs = [dep + "_textual_hdrs" for dep in deps],
+    )
+    native.cc_library(
+        name = name,
+        # Technically speaking, we should put _hdr_deps in srcs, as they are
+        # not a part of this cc_library interface. However, we keep it here to
+        # workaround the presence of .cpp files in _hdr_deps - we need to
+        # fix that and enforce their absence, since libc_header_library
+        # should be header-only and not produce any object files.
+        # See PR #133126 which tracks it.
+        hdrs = hdrs + [":" + name + "_hdr_deps"],
+        deps = [":" + name + "_textual_hdr_library"],
+        # copts don't really matter, since it's a header-only library, but we
+        # need proper -I flags for header validation, which are specified in
+        # libc_common_copts().
+        copts = libc_common_copts(),
+        **kwargs
+    )
+
 def libc_math_function(
         name,
         additional_deps = None):

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

@vonosmas vonosmas merged commit d724bab into llvm:main Mar 26, 2025
12 checks passed
@vonosmas vonosmas deleted the bazel-header-library branch March 26, 2025 21:10
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.

3 participants