[llvm] [libc][bazel] Support generating public libc headers in Bazel builds. (PR #141256)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 23 09:58:36 PDT 2025
https://github.com/jtstogel created https://github.com/llvm/llvm-project/pull/141256
This requires adding a new dependency on PyYAML so that Bazel is able to run the `hdrgen` tool hermetically.
See https://github.com/llvm/llvm-project/issues/134780.
>From b639607d115cfc5c8bd3c44956fa8ff49b97923c Mon Sep 17 00:00:00 2001
From: Jackson Stogel <jtstogel at google.com>
Date: Thu, 22 May 2025 22:43:05 +0000
Subject: [PATCH] [libc][bazel] Support generating public libc headers in Bazel
builds.
This requires adding a new dependency on PyYAML so that Bazel is able
to run the `hdrgen` tool hermetically.
See https://github.com/llvm/llvm-project/issues/134780.
---
utils/bazel/WORKSPACE | 9 +
.../llvm-project-overlay/libc/BUILD.bazel | 491 ++++++++++++++++++
.../libc/libc_build_rules.bzl | 23 +
utils/bazel/third_party_build/pyyaml.BUILD | 16 +
4 files changed, 539 insertions(+)
create mode 100644 utils/bazel/third_party_build/pyyaml.BUILD
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..821e5bb27c992 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,496 @@ py_binary(
srcs = glob(["utils/hdrgen/hdrgen/**/*.py"]),
imports = ["utils/hdrgen"],
main = "utils/hdrgen/hdrgen/main.py",
+ deps = ["@pyyaml//:yaml"],
+)
+
+libc_generated_header(
+ name = "ctype_public_header",
+ hdr = "include/ctype.h",
+ yaml_template = "include/ctype.yaml",
+)
+
+libc_generated_header(
+ name = "dirent_public_header",
+ hdr = "include/dirent.h",
+ other_srcs = ["include/dirent.h.def"],
+ yaml_template = "include/dirent.yaml",
+)
+
+libc_generated_header(
+ name = "fcntl_public_header",
+ hdr = "include/fcntl.h",
+ other_srcs = ["include/fcntl.h.def"],
+ yaml_template = "include/fcntl.yaml",
+)
+
+libc_generated_header(
+ name = "dlfcn_public_header",
+ hdr = "include/dlfcn.h",
+ other_srcs = ["include/dlfcn.h.def"],
+ yaml_template = "include/dlfcn.yaml",
+)
+
+libc_generated_header(
+ name = "endian_public_header",
+ hdr = "include/endian.h",
+ other_srcs = ["include/endian.h.def"],
+ yaml_template = "include/endian.yaml",
+)
+
+libc_generated_header(
+ name = "features_public_header",
+ hdr = "include/features.h",
+ other_srcs = ["include/features.h.def"],
+ yaml_template = "include/features.yaml",
+)
+
+libc_generated_header(
+ name = "fenv_public_header",
+ hdr = "include/fenv.h",
+ yaml_template = "include/fenv.yaml",
+)
+
+libc_generated_header(
+ name = "inttypes_public_header",
+ hdr = "include/inttypes.h",
+ other_srcs = ["include/inttypes.h.def"],
+ yaml_template = "include/inttypes.yaml",
+)
+
+libc_generated_header(
+ name = "float_public_header",
+ hdr = "include/float.h",
+ other_srcs = ["include/float.h.def"],
+ yaml_template = "include/float.yaml",
+)
+
+libc_generated_header(
+ name = "stdint_public_header",
+ hdr = "include/stdint.h",
+ other_srcs = ["include/stdint.h.def"],
+ yaml_template = "include/stdint.yaml",
+)
+
+libc_generated_header(
+ name = "limits_public_header",
+ hdr = "include/limits.h",
+ other_srcs = ["include/limits.h.def"],
+ yaml_template = "include/limits.yaml",
+)
+
+libc_generated_header(
+ name = "malloc_public_header",
+ hdr = "include/malloc.h",
+ other_srcs = ["include/stdlib-malloc.yaml"],
+ yaml_template = "include/malloc.yaml",
+)
+
+libc_generated_header(
+ name = "math_public_header",
+ hdr = "include/math.h",
+ other_srcs = ["include/math.h.def"],
+ yaml_template = "include/math.yaml",
+)
+
+libc_generated_header(
+ name = "stdfix_public_header",
+ hdr = "include/stdfix.h",
+ other_srcs = ["include/stdfix.h.def"],
+ yaml_template = "include/stdfix.yaml",
+)
+
+libc_generated_header(
+ name = "sysexits_public_header",
+ hdr = "include/sysexits.h",
+ other_srcs = ["include/sysexits.h.def"],
+ yaml_template = "include/sysexits.yaml",
+)
+
+libc_generated_header(
+ name = "assert_public_header",
+ hdr = "include/assert.h",
+ other_srcs = ["include/assert.h.def"],
+ yaml_template = "include/assert.yaml",
+)
+
+libc_generated_header(
+ name = "complex_public_header",
+ hdr = "include/complex.h",
+ other_srcs = ["include/complex.h.def"],
+ yaml_template = "include/complex.yaml",
+)
+
+libc_generated_header(
+ name = "setjmp_public_header",
+ hdr = "include/setjmp.h",
+ other_srcs = ["include/setjmp.h.def"],
+ yaml_template = "include/setjmp.yaml",
+)
+
+libc_generated_header(
+ name = "string_public_header",
+ hdr = "include/string.h",
+ other_srcs = ["include/string.h.def"],
+ yaml_template = "include/string.yaml",
+)
+
+libc_generated_header(
+ name = "strings_public_header",
+ hdr = "include/strings.h",
+ other_srcs = ["include/strings.h.def"],
+ yaml_template = "include/strings.yaml",
+)
+
+libc_generated_header(
+ name = "search_public_header",
+ hdr = "include/search.h",
+ other_srcs = ["include/search.h.def"],
+ yaml_template = "include/search.yaml",
+)
+
+libc_generated_header(
+ name = "time_public_header",
+ hdr = "include/time.h",
+ other_srcs = ["include/time.h.def"],
+ yaml_template = "include/time.yaml",
+)
+
+libc_generated_header(
+ name = "threads_public_header",
+ hdr = "include/threads.h",
+ other_srcs = ["include/threads.h.def"],
+ yaml_template = "include/threads.yaml",
+)
+
+libc_generated_header(
+ name = "errno_public_header",
+ hdr = "include/errno.h",
+ other_srcs = ["include/errno.h.def"],
+ yaml_template = "include/errno.yaml",
+)
+
+libc_generated_header(
+ name = "signal_public_header",
+ hdr = "include/signal.h",
+ other_srcs = ["include/signal.h.def"],
+ yaml_template = "include/signal.yaml",
+)
+
+libc_generated_header(
+ name = "stdbit_public_header",
+ hdr = "include/stdbit.h",
+ other_srcs = ["include/stdbit.h.def"],
+ yaml_template = "include/stdbit.yaml",
+)
+
+libc_generated_header(
+ name = "stdckdint_public_header",
+ hdr = "include/stdckdint.h",
+ other_srcs = ["include/stdckdint.h.def"],
+ yaml_template = "include/stdckdint.yaml",
+)
+
+libc_generated_header(
+ name = "stdio_public_header",
+ hdr = "include/stdio.h",
+ other_srcs = ["include/stdio.h.def"],
+ yaml_template = "include/stdio.yaml",
+)
+
+libc_generated_header(
+ name = "stdlib_public_header",
+ hdr = "include/stdlib.h",
+ other_srcs = [
+ "include/stdlib.h.def",
+ "include/stdlib-malloc.yaml",
+ ],
+ yaml_template = "include/stdlib.yaml",
+)
+
+libc_generated_header(
+ name = "unistd_public_header",
+ hdr = "include/unistd.h",
+ other_srcs = ["include/unistd.h.def"],
+ yaml_template = "include/unistd.yaml",
+)
+
+libc_generated_header(
+ name = "pthread_public_header",
+ hdr = "include/pthread.h",
+ other_srcs = ["include/pthread.h.def"],
+ yaml_template = "include/pthread.yaml",
+)
+
+libc_generated_header(
+ name = "sched_public_header",
+ hdr = "include/sched.h",
+ other_srcs = ["include/sched.h.def"],
+ yaml_template = "include/sched.yaml",
+)
+
+libc_generated_header(
+ name = "spawn_public_header",
+ hdr = "include/spawn.h",
+ other_srcs = ["include/spawn.h.def"],
+ yaml_template = "include/spawn.yaml",
+)
+
+libc_generated_header(
+ name = "link_public_header",
+ hdr = "include/link.h",
+ yaml_template = "include/link.yaml",
+)
+
+libc_generated_header(
+ name = "elf_public_header",
+ hdr = "include/elf.h",
+ other_srcs = ["include/elf.h.def"],
+ yaml_template = "include/elf.yaml",
+)
+
+libc_generated_header(
+ name = "termios_public_header",
+ hdr = "include/termios.h",
+ other_srcs = ["include/termios.h.def"],
+ yaml_template = "include/termios.yaml",
+)
+
+libc_generated_header(
+ name = "uchar_public_header",
+ hdr = "include/uchar.h",
+ other_srcs = ["include/uchar.h.def"],
+ yaml_template = "include/uchar.yaml",
+)
+
+libc_generated_header(
+ name = "wchar_public_header",
+ hdr = "include/wchar.h",
+ other_srcs = ["include/wchar.h.def"],
+ yaml_template = "include/wchar.yaml",
+)
+
+libc_generated_header(
+ name = "locale_public_header",
+ hdr = "include/locale.h",
+ other_srcs = ["include/locale.h.def"],
+ yaml_template = "include/locale.yaml",
+)
+
+libc_generated_header(
+ name = "poll_public_header",
+ hdr = "include/poll.h",
+ other_srcs = ["include/poll.h.def"],
+ yaml_template = "include/poll.yaml",
+)
+
+libc_generated_header(
+ name = "uefi_public_header",
+ hdr = "include/Uefi.h",
+ yaml_template = "include/Uefi.yaml",
+)
+
+libc_generated_header(
+ name = "arpa_inet_public_header",
+ hdr = "include/arpa/inet.h",
+ other_srcs = ["include/arpa/inet.h.def"],
+ yaml_template = "include/arpa/inet.yaml",
+)
+
+libc_generated_header(
+ name = "sys_auxv_public_header",
+ hdr = "include/sys/auxv.h",
+ other_srcs = ["include/sys/auxv.h.def"],
+ yaml_template = "include/sys/auxv.yaml",
+)
+
+libc_generated_header(
+ name = "sys_epoll_public_header",
+ hdr = "include/sys/epoll.h",
+ other_srcs = ["include/sys/epoll.h.def"],
+ yaml_template = "include/sys/epoll.yaml",
+)
+
+libc_generated_header(
+ name = "sys_ioctl_public_header",
+ hdr = "include/sys/ioctl.h",
+ other_srcs = ["include/sys/ioctl.h.def"],
+ yaml_template = "include/sys/ioctl.yaml",
+)
+
+libc_generated_header(
+ name = "sys_mman_public_header",
+ hdr = "include/sys/mman.h",
+ other_srcs = ["include/sys/mman.h.def"],
+ yaml_template = "include/sys/mman.yaml",
+)
+
+libc_generated_header(
+ name = "sys_prctl_public_header",
+ hdr = "include/sys/prctl.h",
+ other_srcs = ["include/sys/prctl.h.def"],
+ yaml_template = "include/sys/prctl.yaml",
+)
+
+libc_generated_header(
+ name = "sys_random_public_header",
+ hdr = "include/sys/random.h",
+ other_srcs = ["include/sys/random.h.def"],
+ yaml_template = "include/sys/random.yaml",
+)
+
+libc_generated_header(
+ name = "sys_resource_public_header",
+ hdr = "include/sys/resource.h",
+ other_srcs = ["include/sys/resource.h.def"],
+ yaml_template = "include/sys/resource.yaml",
+)
+
+libc_generated_header(
+ name = "sys_stat_public_header",
+ hdr = "include/sys/stat.h",
+ other_srcs = ["include/sys/stat.h.def"],
+ yaml_template = "include/sys/stat.yaml",
+)
+
+libc_generated_header(
+ name = "sys_select_public_header",
+ hdr = "include/sys/select.h",
+ other_srcs = ["include/sys/select.h.def"],
+ yaml_template = "include/sys/select.yaml",
+)
+
+libc_generated_header(
+ name = "sys_sendfile_public_header",
+ hdr = "include/sys/sendfile.h",
+ other_srcs = ["include/sys/sendfile.h.def"],
+ yaml_template = "include/sys/sendfile.yaml",
+)
+
+libc_generated_header(
+ name = "sys_socket_public_header",
+ hdr = "include/sys/socket.h",
+ other_srcs = ["include/sys/socket.h.def"],
+ yaml_template = "include/sys/socket.yaml",
+)
+
+libc_generated_header(
+ name = "sys_statvfs_public_header",
+ hdr = "include/sys/statvfs.h",
+ other_srcs = ["include/sys/statvfs.h.def"],
+ yaml_template = "include/sys/statvfs.yaml",
+)
+
+libc_generated_header(
+ name = "sys_syscall_public_header",
+ hdr = "include/sys/syscall.h",
+ other_srcs = ["include/sys/syscall.h.def"],
+ yaml_template = "include/sys/syscall.yaml",
+)
+
+libc_generated_header(
+ name = "sys_time_public_header",
+ hdr = "include/sys/time.h",
+ other_srcs = ["include/sys/time.h.def"],
+ yaml_template = "include/sys/time.yaml",
+)
+
+libc_generated_header(
+ name = "sys_types_public_header",
+ hdr = "include/sys/types.h",
+ other_srcs = ["include/sys/types.h.def"],
+ yaml_template = "include/sys/types.yaml",
+)
+
+libc_generated_header(
+ name = "sys_utsname_public_header",
+ hdr = "include/sys/utsname.h",
+ other_srcs = ["include/sys/utsname.h.def"],
+ yaml_template = "include/sys/utsname.yaml",
+)
+
+libc_generated_header(
+ name = "sys_uio_public_header",
+ hdr = "include/sys/uio.h",
+ other_srcs = ["include/sys/uio.h.def"],
+ yaml_template = "include/sys/uio.yaml",
+)
+
+libc_generated_header(
+ name = "sys_wait_public_header",
+ hdr = "include/sys/wait.h",
+ other_srcs = ["include/sys/wait.h.def"],
+ yaml_template = "include/sys/wait.yaml",
+)
+
+# Library containing all public header files.
+libc_support_library(
+ name = "public_headers",
+ textual_hdrs = [
+ ":ctype_public_header",
+ ":dirent_public_header",
+ ":fcntl_public_header",
+ ":dlfcn_public_header",
+ ":endian_public_header",
+ ":features_public_header",
+ ":fenv_public_header",
+ ":inttypes_public_header",
+ ":float_public_header",
+ ":stdint_public_header",
+ ":limits_public_header",
+ ":malloc_public_header",
+ ":math_public_header",
+ ":stdfix_public_header",
+ ":sysexits_public_header",
+ ":assert_public_header",
+ ":complex_public_header",
+ ":setjmp_public_header",
+ ":string_public_header",
+ ":strings_public_header",
+ ":search_public_header",
+ ":time_public_header",
+ ":threads_public_header",
+ ":errno_public_header",
+ ":signal_public_header",
+ ":stdbit_public_header",
+ ":stdckdint_public_header",
+ ":stdio_public_header",
+ ":stdlib_public_header",
+ ":unistd_public_header",
+ ":pthread_public_header",
+ ":sched_public_header",
+ ":spawn_public_header",
+ ":link_public_header",
+ ":elf_public_header",
+ ":termios_public_header",
+ ":uchar_public_header",
+ ":wchar_public_header",
+ ":locale_public_header",
+ ":poll_public_header",
+ ":uefi_public_header",
+ ":arpa_inet_public_header",
+ ":sys_auxv_public_header",
+ ":sys_epoll_public_header",
+ ":sys_ioctl_public_header",
+ ":sys_mman_public_header",
+ ":sys_prctl_public_header",
+ ":sys_random_public_header",
+ ":sys_resource_public_header",
+ ":sys_stat_public_header",
+ ":sys_select_public_header",
+ ":sys_sendfile_public_header",
+ ":sys_socket_public_header",
+ ":sys_statvfs_public_header",
+ ":sys_syscall_public_header",
+ ":sys_time_public_header",
+ ":sys_types_public_header",
+ ":sys_utsname_public_header",
+ ":sys_uio_public_header",
+ ":sys_wait_public_header",
+ ],
+ deps = [
+ ":public_headers_deps",
+ ],
)
# 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"]),
+)
More information about the llvm-commits
mailing list