Skip to content

Commit 8d38c74

Browse files
committed
[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 #134780.
1 parent 9d33b92 commit 8d38c74

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

utils/bazel/WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ maybe(
182182
url = "https://github.com/wjakob/nanobind/archive/refs/tags/v2.4.0.tar.gz",
183183
)
184184

185+
maybe(
186+
http_archive,
187+
name = "pyyaml",
188+
build_file = "@llvm-raw//utils/bazel/third_party_build:pyyaml.BUILD",
189+
sha256 = "f0a35d7f282a6d6b1a4f3f3965ef5c124e30ed27a0088efb97c0977268fd671f",
190+
strip_prefix = "pyyaml-5.1/lib3",
191+
url = "https://github.com/yaml/pyyaml/archive/refs/tags/5.1.zip",
192+
)
193+
185194
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
186195

187196
py_repositories()

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load("@rules_python//python:defs.bzl", "py_binary")
99
load(
1010
":libc_build_rules.bzl",
1111
"libc_function",
12+
"libc_generated_header",
1213
"libc_header_library",
1314
"libc_math_function",
1415
"libc_support_library",
@@ -88,6 +89,14 @@ py_binary(
8889
srcs = glob(["utils/hdrgen/hdrgen/**/*.py"]),
8990
imports = ["utils/hdrgen"],
9091
main = "utils/hdrgen/hdrgen/main.py",
92+
deps = ["@pyyaml//:yaml"],
93+
)
94+
95+
libc_generated_header(
96+
name = "stdbit_public_header",
97+
hdr = "include/stdbit.h",
98+
other_srcs = ["include/stdbit.h.def"],
99+
yaml_template = "include/stdbit.yaml",
91100
)
92101

93102
# Library containing all headers that can be transitively included by generated llvm-libc

utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,29 @@ def libc_header_library(name, hdrs, deps = [], **kwargs):
248248
**kwargs
249249
)
250250

251+
def libc_generated_header(name, hdr, yaml_template, other_srcs = []):
252+
"""Generates a libc header file from YAML template.
253+
254+
Args:
255+
name: Name of the genrule target.
256+
hdr: Path of the header file to generate.
257+
yaml_template: Path of the YAML template file.
258+
other_srcs: Other files required to generate the header, if any.
259+
"""
260+
hdrgen = "//libc:hdrgen"
261+
cmd = "$(location {hdrgen}) $(location {yaml}) -o $@".format(
262+
hdrgen = hdrgen,
263+
yaml = yaml_template,
264+
)
265+
266+
native.genrule(
267+
name = name,
268+
outs = [hdr],
269+
srcs = [yaml_template] + other_srcs,
270+
cmd = cmd,
271+
tools = [hdrgen],
272+
)
273+
251274
def libc_math_function(
252275
name,
253276
additional_deps = None):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
load("@rules_python//python:defs.bzl", "py_library")
6+
7+
package(
8+
default_visibility = ["//visibility:public"],
9+
# BSD/MIT-like license (for PyYAML)
10+
licenses = ["notice"],
11+
)
12+
13+
py_library(
14+
name = "yaml",
15+
srcs = glob(["yaml/*.py"]),
16+
)

0 commit comments

Comments
 (0)