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