Skip to content

Commit d3bcdec

Browse files
committed
[libc][bazel] Support generating public libc headers in Bazel builds.
Running `hdrgen` in Bazel hermetically requires adding a new dependency on PyYAML. This PR uses PyYAML version 5.1 due to keep in line with the docs: https://github.com/llvm/llvm-project/blob/b878e0d11874a898bbaa1daf58007dfd232005f2/libc/docs/dev/header_generation.rst?plain=1#L22 See #134780. Generated headers are placed in a `staging/` directory so that they have the opportunity to be treated differently from non-generated headers. This is a follow-up to #141256, which was reverted in #143001 because it caused downstream failures when bootstrapping builds.
1 parent 6b9e6f0 commit d3bcdec

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

utils/bazel/WORKSPACE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ maybe(
164164
url = "https://github.com/pybind/pybind11/archive/v2.10.3.zip",
165165
)
166166

167+
maybe(
168+
http_archive,
169+
name = "pyyaml",
170+
build_file = "@llvm-raw//utils/bazel/third_party_build:pyyaml.BUILD",
171+
sha256 = "f0a35d7f282a6d6b1a4f3f3965ef5c124e30ed27a0088efb97c0977268fd671f",
172+
strip_prefix = "pyyaml-5.1/lib3",
173+
url = "https://github.com/yaml/pyyaml/archive/refs/tags/5.1.zip",
174+
)
175+
167176
maybe(
168177
http_archive,
169178
name = "robin_map",

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 = "include_stdbit_h",
97+
hdr = "staging/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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,36 @@ 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+
if not hdr.startswith("staging/"):
267+
fail(
268+
"Generated headers should be placed in a 'staging/' directory " +
269+
"so that they can be treated differently from constant source files " +
270+
"when bootstrapping builds.",
271+
)
272+
273+
native.genrule(
274+
name = name,
275+
outs = [hdr],
276+
srcs = [yaml_template] + other_srcs,
277+
cmd = cmd,
278+
tools = [hdrgen],
279+
)
280+
251281
def libc_math_function(
252282
name,
253283
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)