1*bcb5dc79SHONG Yifan# Copyright 2024 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"""Rules to filter files from a directory.""" 15*bcb5dc79SHONG Yifan 16*bcb5dc79SHONG Yifanload(":providers.bzl", "DirectoryInfo") 17*bcb5dc79SHONG Yifan 18*bcb5dc79SHONG Yifandef _directory_glob_impl(ctx): 19*bcb5dc79SHONG Yifan directory = ctx.attr.directory[DirectoryInfo] 20*bcb5dc79SHONG Yifan srcs = directory.glob( 21*bcb5dc79SHONG Yifan ctx.attr.srcs, 22*bcb5dc79SHONG Yifan exclude = ctx.attr.exclude, 23*bcb5dc79SHONG Yifan allow_empty = ctx.attr.allow_empty, 24*bcb5dc79SHONG Yifan ) 25*bcb5dc79SHONG Yifan data = directory.glob( 26*bcb5dc79SHONG Yifan ctx.attr.data, 27*bcb5dc79SHONG Yifan exclude = ctx.attr.exclude, 28*bcb5dc79SHONG Yifan allow_empty = ctx.attr.allow_empty, 29*bcb5dc79SHONG Yifan ) 30*bcb5dc79SHONG Yifan 31*bcb5dc79SHONG Yifan return DefaultInfo( 32*bcb5dc79SHONG Yifan files = srcs, 33*bcb5dc79SHONG Yifan runfiles = ctx.runfiles(transitive_files = depset(transitive = [srcs, data])), 34*bcb5dc79SHONG Yifan ) 35*bcb5dc79SHONG Yifan 36*bcb5dc79SHONG Yifandirectory_glob = rule( 37*bcb5dc79SHONG Yifan implementation = _directory_glob_impl, 38*bcb5dc79SHONG Yifan attrs = { 39*bcb5dc79SHONG Yifan "allow_empty": attr.bool( 40*bcb5dc79SHONG Yifan doc = "If true, allows globs to not match anything.", 41*bcb5dc79SHONG Yifan ), 42*bcb5dc79SHONG Yifan "data": attr.string_list( 43*bcb5dc79SHONG Yifan doc = """A list of globs to files within the directory to put in the runfiles. 44*bcb5dc79SHONG Yifan 45*bcb5dc79SHONG YifanFor example, `data = ["foo/**"]` would collect all files contained within `<directory>/foo` into the 46*bcb5dc79SHONG Yifanrunfiles.""", 47*bcb5dc79SHONG Yifan ), 48*bcb5dc79SHONG Yifan "directory": attr.label(providers = [DirectoryInfo], mandatory = True), 49*bcb5dc79SHONG Yifan "exclude": attr.string_list( 50*bcb5dc79SHONG Yifan doc = "A list of globs to files within the directory to exclude from the files and runfiles.", 51*bcb5dc79SHONG Yifan ), 52*bcb5dc79SHONG Yifan "srcs": attr.string_list( 53*bcb5dc79SHONG Yifan doc = """A list of globs to files within the directory to put in the files. 54*bcb5dc79SHONG Yifan 55*bcb5dc79SHONG YifanFor example, `srcs = ["foo/**"]` would collect the file at `<directory>/foo` into the 56*bcb5dc79SHONG Yifanfiles.""", 57*bcb5dc79SHONG Yifan ), 58*bcb5dc79SHONG Yifan }, 59*bcb5dc79SHONG Yifan doc = """globs files from a directory by relative path. 60*bcb5dc79SHONG Yifan 61*bcb5dc79SHONG YifanUsage: 62*bcb5dc79SHONG Yifan 63*bcb5dc79SHONG Yifan``` 64*bcb5dc79SHONG Yifandirectory_glob( 65*bcb5dc79SHONG Yifan name = "foo", 66*bcb5dc79SHONG Yifan directory = ":directory", 67*bcb5dc79SHONG Yifan srcs = ["foo/bar"], 68*bcb5dc79SHONG Yifan data = ["foo/**"], 69*bcb5dc79SHONG Yifan exclude = ["foo/**/*.h"] 70*bcb5dc79SHONG Yifan) 71*bcb5dc79SHONG Yifan``` 72*bcb5dc79SHONG Yifan""", 73*bcb5dc79SHONG Yifan) 74