|
| 1 | +"""A library rule and macro for grouping Starlark sources. |
| 2 | +
|
| 3 | +Drop-in replacement for bzl_library in bazel_skylib, with exceptions: |
| 4 | +- We support .bzl and .star extensions, while bzl_library accepts .bzl and .scl. |
| 5 | +""" |
| 6 | + |
| 7 | +load("@bazel_skylib//:bzl_library.bzl", "StarlarkLibraryInfo") |
| 8 | + |
| 9 | +def _bzl_library_impl(ctx): |
| 10 | + deps_files = [x.files for x in ctx.attr.deps] |
| 11 | + all_files = depset(ctx.files.srcs, order = "postorder", transitive = deps_files) |
| 12 | + if not ctx.files.srcs and not deps_files: |
| 13 | + fail("bzl_library rule '%s' has no srcs or deps" % ctx.label) |
| 14 | + |
| 15 | + return [ |
| 16 | + # All dependent files should be listed in both `files` and in `runfiles`; |
| 17 | + # this ensures that a `bzl_library` can be referenced as `data` from |
| 18 | + # a separate program, or from `tools` of a genrule(). |
| 19 | + DefaultInfo( |
| 20 | + files = all_files, |
| 21 | + runfiles = ctx.runfiles(transitive_files = all_files), |
| 22 | + ), |
| 23 | + |
| 24 | + # Interop with @bazel_skylib//:bzl_library |
| 25 | + StarlarkLibraryInfo( |
| 26 | + srcs = ctx.files.srcs, |
| 27 | + transitive_srcs = all_files, |
| 28 | + ), |
| 29 | + ] |
| 30 | + |
| 31 | +bzl_library_rule = rule( |
| 32 | + implementation = _bzl_library_impl, |
| 33 | + attrs = { |
| 34 | + "srcs": attr.label_list( |
| 35 | + allow_files = [".bzl", ".star"], |
| 36 | + ), |
| 37 | + "deps": attr.label_list( |
| 38 | + allow_files = [".bzl", ".star"], |
| 39 | + ), |
| 40 | + }, |
| 41 | + doc = """Creates a logical collection of Starlark .bzl and .star files.""", |
| 42 | +) |
| 43 | + |
| 44 | +def bzl_library(name, srcs = [], deps = [], **kwargs): |
| 45 | + """Wrapper for bzl_library. |
| 46 | +
|
| 47 | + Args: |
| 48 | + name: name |
| 49 | +
|
| 50 | + srcs: List of `.bzl` and `.star` files that are processed to create this target. |
| 51 | + deps: List of other `bzl_library` or `filegroup` targets that are required by the Starlark files listed in `srcs`. |
| 52 | + **kwargs: additional arguments for the bzl_library rule. |
| 53 | + """ |
| 54 | + |
| 55 | + # buildifier: disable=unused-variable |
| 56 | + _ = kwargs.pop("compatible_with", None) |
| 57 | + _ = kwargs.pop("exec_compatible_with", None) |
| 58 | + _ = kwargs.pop("features", None) |
| 59 | + _ = kwargs.pop("target_compatible_with", None) |
| 60 | + bzl_library_rule( |
| 61 | + name = name, |
| 62 | + srcs = srcs, |
| 63 | + deps = deps, |
| 64 | + compatible_with = [], |
| 65 | + exec_compatible_with = [], |
| 66 | + features = [], |
| 67 | + target_compatible_with = [], |
| 68 | + **kwargs |
| 69 | + ) |
| 70 | + |
| 71 | + # validate that public API docs have correct deps, by running the doc extractor over them. |
| 72 | + # TODO(alexeagle): it would be better to attach this as a validation action in the bzl_library_rule |
| 73 | + # but there's no tool available for that since the Java implementation code is only exposed as a |
| 74 | + # native Bazel rule. |
| 75 | + # See bazelbuild/bazel-skylib#568 |
| 76 | + if hasattr(native, "starlark_doc_extract") and "/private" not in native.package_name(): |
| 77 | + for i, src in enumerate(srcs): |
| 78 | + native.starlark_doc_extract( |
| 79 | + name = "{}.doc_extract{}".format(name, i if i > 0 else ""), |
| 80 | + src = src, |
| 81 | + deps = deps, |
| 82 | + testonly = True, |
| 83 | + visibility = ["//visibility:private"], |
| 84 | + ) |
0 commit comments