1# Copyright 2017 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Skylib module containing a library rule for aggregating rules files."""
16
17StarlarkLibraryInfo = provider(
18    "Information on contained Starlark rules.",
19    fields = {
20        "srcs": "Top level rules files.",
21        "transitive_srcs": "Transitive closure of rules files required for " +
22                           "interpretation of the srcs",
23    },
24)
25
26def _bzl_library_impl(ctx):
27    deps_files = [x.files for x in ctx.attr.deps]
28    all_files = depset(ctx.files.srcs, order = "postorder", transitive = deps_files)
29    return [
30        # All dependent files should be listed in both `files` and in `runfiles`;
31        # this ensures that a `bzl_library` can be referenced as `data` from
32        # a separate program, or from `tools` of a genrule().
33        DefaultInfo(
34            files = all_files,
35            runfiles = ctx.runfiles(transitive_files = all_files),
36        ),
37
38        # We also define our own provider struct, for aggregation and testing.
39        StarlarkLibraryInfo(
40            srcs = ctx.files.srcs,
41            transitive_srcs = all_files,
42        ),
43    ]
44
45bzl_library = rule(
46    implementation = _bzl_library_impl,
47    attrs = {
48        "srcs": attr.label_list(
49            allow_files = [".bzl"],
50            doc = "List of `.bzl` files that are processed to create this target.",
51        ),
52        "deps": attr.label_list(
53            allow_files = [".bzl"],
54            providers = [
55                [StarlarkLibraryInfo],
56            ],
57            doc = """List of other `bzl_library` targets that are required by the
58Starlark files listed in `srcs`.""",
59        ),
60    },
61    doc = """Creates a logical collection of Starlark .bzl files.
62Example:
63  Suppose your project has the following structure:
64  ```
65  [workspace]/
66      WORKSPACE
67      BUILD
68      checkstyle/
69          BUILD
70          checkstyle.bzl
71      lua/
72          BUILD
73          lua.bzl
74          luarocks.bzl
75  ```
76  In this case, you can have `bzl_library` targets in `checkstyle/BUILD` and
77  `lua/BUILD`:
78  `checkstyle/BUILD`:
79  ```python
80  load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
81  bzl_library(
82      name = "checkstyle-rules",
83      srcs = ["checkstyle.bzl"],
84  )
85  ```
86  `lua/BUILD`:
87  ```python
88  load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
89  bzl_library(
90      name = "lua-rules",
91      srcs = [
92          "lua.bzl",
93          "luarocks.bzl",
94      ],
95  )
96  ```
97""",
98)
99