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 if not ctx.files.srcs and not deps_files: 30 fail("bzl_library rule '%s' has no srcs or deps" % ctx.label) 31 32 return [ 33 # All dependent files should be listed in both `files` and in `runfiles`; 34 # this ensures that a `bzl_library` can be referenced as `data` from 35 # a separate program, or from `tools` of a genrule(). 36 DefaultInfo( 37 files = all_files, 38 runfiles = ctx.runfiles(transitive_files = all_files), 39 ), 40 41 # We also define our own provider struct, for aggregation and testing. 42 StarlarkLibraryInfo( 43 srcs = ctx.files.srcs, 44 transitive_srcs = all_files, 45 ), 46 ] 47 48bzl_library = rule( 49 implementation = _bzl_library_impl, 50 attrs = { 51 "srcs": attr.label_list( 52 allow_files = [".bzl", ".scl"], 53 doc = "List of `.bzl` and `.scl` files that are processed to create this target.", 54 ), 55 "deps": attr.label_list( 56 allow_files = [".bzl", ".scl"], 57 doc = """List of other `bzl_library` or `filegroup` targets that are required by the 58Starlark files listed in `srcs`.""", 59 ), 60 }, 61 doc = """Creates a logical collection of Starlark .bzl and .scl 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