c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 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 | |
| 17 | StarlarkLibraryInfo = 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 | |
| 26 | def _bzl_library_impl(ctx): |
Laurent Le Brun | 1c5ed07 | 2020-02-03 16:45:44 +0100 | [diff] [blame] | 27 | deps_files = [x.files for x in ctx.attr.deps] |
c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 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, |
bttk | 3b30154 | 2018-11-14 09:52:55 -0800 | [diff] [blame] | 35 | runfiles = ctx.runfiles(transitive_files = all_files), |
c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 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 | |
| 45 | bzl_library = rule( |
| 46 | implementation = _bzl_library_impl, |
| 47 | attrs = { |
| 48 | "srcs": attr.label_list( |
| 49 | allow_files = [".bzl"], |
Thomas Van Lenten | d7c5518 | 2018-11-21 13:04:25 -0500 | [diff] [blame] | 50 | doc = "List of `.bzl` files that are processed to create this target.", |
c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 51 | ), |
| 52 | "deps": attr.label_list( |
| 53 | allow_files = [".bzl"], |
| 54 | providers = [ |
| 55 | [StarlarkLibraryInfo], |
| 56 | ], |
Thomas Van Lenten | d7c5518 | 2018-11-21 13:04:25 -0500 | [diff] [blame] | 57 | doc = """List of other `bzl_library` targets that are required by the |
| 58 | Starlark files listed in `srcs`.""", |
c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 59 | ), |
| 60 | }, |
Thomas Van Lenten | d7c5518 | 2018-11-21 13:04:25 -0500 | [diff] [blame] | 61 | doc = """Creates a logical collection of Starlark .bzl files. |
c-parsons | 6e2d7e4 | 2018-09-28 09:09:18 -0400 | [diff] [blame] | 62 | |
| 63 | Example: |
| 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 Lenten | d7c5518 | 2018-11-21 13:04:25 -0500 | [diff] [blame] | 106 | """, |
| 107 | ) |