Skip to content

[libc][bazel] Support generating public libc headers in Bazel builds. #141256

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jtstogel
Copy link

@jtstogel jtstogel commented May 23, 2025

This requires adding a new dependency on PyYAML so that Bazel is able to run the hdrgen tool hermetically. This PR uses PyYAML version 5.1 due to keep in line with the docs:

See #134780.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@jtstogel jtstogel force-pushed the add-generated-headers branch from b639607 to 9f8dbf8 Compare May 23, 2025 20:16
)

libc_generated_header(
name = "sys_wait_public_header",
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly looks good! I would suggest to have just a single public header in the first PR to minimize the diff, and split out adding generating logic vs. adding targets for all public headers into separate commits.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good, switched to just adding stdbit.h.

@jtstogel jtstogel force-pushed the add-generated-headers branch from 9f8dbf8 to 8d38c74 Compare May 27, 2025 19:57
@jtstogel jtstogel marked this pull request as ready for review May 27, 2025 19:57
@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels May 27, 2025
@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-libc

Author: None (jtstogel)

Changes

This requires adding a new dependency on PyYAML so that Bazel is able to run the hdrgen tool hermetically. This PR uses PyYAML version 5.1 due to keep in line with the docs:

See #134780.


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

4 Files Affected:

  • (modified) utils/bazel/WORKSPACE (+9)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+9)
  • (modified) utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl (+23)
  • (added) utils/bazel/third_party_build/pyyaml.BUILD (+16)
diff --git a/utils/bazel/WORKSPACE b/utils/bazel/WORKSPACE
index 84e65e6f67b5a..c21812ef2363b 100644
--- a/utils/bazel/WORKSPACE
+++ b/utils/bazel/WORKSPACE
@@ -182,6 +182,15 @@ maybe(
     url = "https://github.com/wjakob/nanobind/archive/refs/tags/v2.4.0.tar.gz",
 )
 
+maybe(
+    http_archive,
+    name = "pyyaml",
+    build_file = "@llvm-raw//utils/bazel/third_party_build:pyyaml.BUILD",
+    sha256 = "f0a35d7f282a6d6b1a4f3f3965ef5c124e30ed27a0088efb97c0977268fd671f",
+    strip_prefix = "pyyaml-5.1/lib3",
+    url = "https://github.com/yaml/pyyaml/archive/refs/tags/5.1.zip",
+)
+
 load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
 
 py_repositories()
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index d9a02a2fd85c5..ab386ae139d51 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_generated_header",
     "libc_header_library",
     "libc_math_function",
     "libc_support_library",
@@ -88,6 +89,14 @@ py_binary(
     srcs = glob(["utils/hdrgen/hdrgen/**/*.py"]),
     imports = ["utils/hdrgen"],
     main = "utils/hdrgen/hdrgen/main.py",
+    deps = ["@pyyaml//:yaml"],
+)
+
+libc_generated_header(
+    name = "stdbit_public_header",
+    hdr = "include/stdbit.h",
+    other_srcs = ["include/stdbit.h.def"],
+    yaml_template = "include/stdbit.yaml",
 )
 
 # Library containing all headers that can be transitively included by generated llvm-libc
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 7fe263ab08f71..9c9cbdcce756c 100644
--- a/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
@@ -248,6 +248,29 @@ def libc_header_library(name, hdrs, deps = [], **kwargs):
         **kwargs
     )
 
+def libc_generated_header(name, hdr, yaml_template, other_srcs = []):
+    """Generates a libc header file from YAML template.
+
+    Args:
+      name: Name of the genrule target.
+      hdr: Path of the header file to generate.
+      yaml_template: Path of the YAML template file.
+      other_srcs: Other files required to generate the header, if any.
+    """
+    hdrgen = "//libc:hdrgen"
+    cmd = "$(location {hdrgen}) $(location {yaml}) -o $@".format(
+        hdrgen = hdrgen,
+        yaml = yaml_template,
+    )
+
+    native.genrule(
+        name = name,
+        outs = [hdr],
+        srcs = [yaml_template] + other_srcs,
+        cmd = cmd,
+        tools = [hdrgen],
+    )
+
 def libc_math_function(
         name,
         additional_deps = None):
diff --git a/utils/bazel/third_party_build/pyyaml.BUILD b/utils/bazel/third_party_build/pyyaml.BUILD
new file mode 100644
index 0000000000000..78261a7f60d9b
--- /dev/null
+++ b/utils/bazel/third_party_build/pyyaml.BUILD
@@ -0,0 +1,16 @@
+# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+load("@rules_python//python:defs.bzl", "py_library")
+
+package(
+    default_visibility = ["//visibility:public"],
+    # BSD/MIT-like license (for PyYAML)
+    licenses = ["notice"],
+)
+
+py_library(
+    name = "yaml",
+    srcs = glob(["yaml/*.py"]),
+)

)

libc_generated_header(
name = "stdbit_public_header",
Copy link
Contributor

Choose a reason for hiding this comment

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

It may be a bike-shedding, but maybe something like include_stdbit_h would generalize better to generated directories.

Also, tagging @rupprecht for advice.

Copy link
Author

@jtstogel jtstogel May 28, 2025

Choose a reason for hiding this comment

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

Anything is good, switched the naming.

This requires adding a new dependency on PyYAML so that Bazel is able
to run the `hdrgen` tool hermetically.

See llvm#134780.
@jtstogel jtstogel force-pushed the add-generated-headers branch from 8d38c74 to 81e4d9f Compare May 28, 2025 17:40
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.

4 participants