blob: 1b59440fa65bf514d51f1b01781fe4595edf1678 [file] [log] [blame]
c-parsons6e2d7e42018-09-28 09:09:18 -04001# 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):
Laurent Le Brun1c5ed072020-02-03 16:45:44 +010027 deps_files = [x.files for x in ctx.attr.deps]
c-parsons6e2d7e42018-09-28 09:09:18 -040028 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,
bttk3b301542018-11-14 09:52:55 -080035 runfiles = ctx.runfiles(transitive_files = all_files),
c-parsons6e2d7e42018-09-28 09:09:18 -040036 ),
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"],
Thomas Van Lentend7c55182018-11-21 13:04:25 -050050 doc = "List of `.bzl` files that are processed to create this target.",
c-parsons6e2d7e42018-09-28 09:09:18 -040051 ),
52 "deps": attr.label_list(
53 allow_files = [".bzl"],
54 providers = [
55 [StarlarkLibraryInfo],
56 ],
Thomas Van Lentend7c55182018-11-21 13:04:25 -050057 doc = """List of other `bzl_library` targets that are required by the
58Starlark files listed in `srcs`.""",
c-parsons6e2d7e42018-09-28 09:09:18 -040059 ),
60 },
Thomas Van Lentend7c55182018-11-21 13:04:25 -050061 doc = """Creates a logical collection of Starlark .bzl files.
c-parsons6e2d7e42018-09-28 09:09:18 -040062
63Example:
64 Suppose your project has the following structure:
65
66 ```
67 [workspace]/
68 WORKSPACE
69 BUILD
70 checkstyle/
71 BUILD
72 checkstyle.bzl
73 lua/
74 BUILD
75 lua.bzl
76 luarocks.bzl
77 ```
78
79 In this case, you can have `bzl_library` targets in `checkstyle/BUILD` and
80 `lua/BUILD`:
81
82 `checkstyle/BUILD`:
83
84 ```python
85 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
86
87 bzl_library(
88 name = "checkstyle-rules",
89 srcs = ["checkstyle.bzl"],
90 )
91 ```
92
93 `lua/BUILD`:
94
95 ```python
96 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
97
98 bzl_library(
99 name = "lua-rules",
100 srcs = [
101 "lua.bzl",
102 "luarocks.bzl",
103 ],
104 )
105 ```
Thomas Van Lentend7c55182018-11-21 13:04:25 -0500106""",
107)