xref: /aosp_15_r20/external/bazel-skylib/rules/select_file.bzl (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan# Copyright 2020 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"""
16*bcb5dc79SHONG Yifanselect_file() build rule implementation.
17*bcb5dc79SHONG Yifan
18*bcb5dc79SHONG YifanSelects a single file from the outputs of a target by given relative path.
19*bcb5dc79SHONG Yifan"""
20*bcb5dc79SHONG Yifan
21*bcb5dc79SHONG Yifandef _impl(ctx):
22*bcb5dc79SHONG Yifan    if ctx.attr.subpath and len(ctx.attr.subpath) == 0:
23*bcb5dc79SHONG Yifan        fail("Subpath can not be empty.")
24*bcb5dc79SHONG Yifan
25*bcb5dc79SHONG Yifan    out = None
26*bcb5dc79SHONG Yifan    canonical = ctx.attr.subpath.replace("\\", "/")
27*bcb5dc79SHONG Yifan    for file_ in ctx.attr.srcs.files.to_list():
28*bcb5dc79SHONG Yifan        if file_.path.replace("\\", "/").endswith(canonical):
29*bcb5dc79SHONG Yifan            out = file_
30*bcb5dc79SHONG Yifan            break
31*bcb5dc79SHONG Yifan    if not out:
32*bcb5dc79SHONG Yifan        files_str = ",\n".join([
33*bcb5dc79SHONG Yifan            str(f.path)
34*bcb5dc79SHONG Yifan            for f in ctx.attr.srcs.files.to_list()
35*bcb5dc79SHONG Yifan        ])
36*bcb5dc79SHONG Yifan        fail("Can not find specified file in [%s]" % files_str)
37*bcb5dc79SHONG Yifan    return [DefaultInfo(files = depset([out]))]
38*bcb5dc79SHONG Yifan
39*bcb5dc79SHONG Yifanselect_file = rule(
40*bcb5dc79SHONG Yifan    implementation = _impl,
41*bcb5dc79SHONG Yifan    doc = "Selects a single file from the outputs of a target by given relative path",
42*bcb5dc79SHONG Yifan    attrs = {
43*bcb5dc79SHONG Yifan        "srcs": attr.label(
44*bcb5dc79SHONG Yifan            allow_files = True,
45*bcb5dc79SHONG Yifan            mandatory = True,
46*bcb5dc79SHONG Yifan            doc = "The target producing the file among other outputs",
47*bcb5dc79SHONG Yifan        ),
48*bcb5dc79SHONG Yifan        "subpath": attr.string(
49*bcb5dc79SHONG Yifan            mandatory = True,
50*bcb5dc79SHONG Yifan            doc = "Relative path to the file",
51*bcb5dc79SHONG Yifan        ),
52*bcb5dc79SHONG Yifan    },
53*bcb5dc79SHONG Yifan)
54