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. 62 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 ``` 106""", 107) 108