xref: /aosp_15_r20/build/bazel/rules/droiddoc/droiddoc_exported_dir.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
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"""Soong's droiddoc_exported_dir equivalent"""
16
17load("@bazel_skylib//lib:paths.bzl", "paths")
18
19DroiddocExportedDirInfo = provider(
20    "Info needed to identify the root dir and files for droid doc",
21    fields = {
22        "dir": "Common root directory for files",
23        "srcs": "Files",
24    },
25)
26
27def _droiddoc_exported_dir_rule_impl(ctx):
28    dir = paths.join(ctx.label.package, paths.normalize(ctx.attr.dir))
29    dir = paths.normalize(dir)
30
31    def validate(s):
32        if s.owner.workspace_name != ctx.label.workspace_name:
33            fail("File [{}] is under a different workspace [{}]".format(s.short_path, s.owner.workspace_name))
34        if not s.short_path.startswith(dir + "/"):
35            fail("File [{}] is not under [{}]".format(s.short_path, dir))
36
37    for s in ctx.files.srcs:
38        validate(s)
39
40    srcs = ctx.files.srcs
41    return [
42        DroiddocExportedDirInfo(
43            dir = dir,
44            srcs = srcs,
45        ),
46    ]
47
48_droiddoc_exported_dir = rule(
49    implementation = _droiddoc_exported_dir_rule_impl,
50    attrs = {
51        "dir": attr.string(),
52        "srcs": attr.label_list(allow_empty = False, allow_files = True, mandatory = True),
53    },
54)
55
56def droiddoc_exported_dir(name, srcs, **kwargs):
57    _droiddoc_exported_dir(
58        name = name,
59        srcs = srcs,
60        **kwargs
61    )
62