• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2022 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
15load(":prebuilt_file.bzl", "PrebuiltFileInfo")
16
17def _prebuilt_xml_impl(ctx):
18    schema = ctx.file.schema
19
20    if len(ctx.files.src) != 1:
21        fail("src for %s is expected to be singular, but is of len %s\n", ctx.label.name, len(ctx.files.src))
22
23    src = ctx.files.src[0]
24
25    args = ctx.actions.args()
26    inputs = [src]
27
28    if schema != None:
29        if schema.extension == "dtd":
30            args.add("--dtdvalid", schema.path)
31        elif schema.extension == "xsd":
32            args.add("--schema", schema.path)
33        inputs.append(schema)
34
35    args.add(src)
36    args.add(">")
37    args.add("/dev/null")
38    args.add("&&")
39    args.add("touch")
40    args.add("-a")
41
42    validation_output = ctx.actions.declare_file(ctx.attr.name + ".validation")
43    args.add(validation_output.path)
44
45    ctx.actions.run(
46        outputs = [validation_output],
47        inputs = inputs,
48        executable = ctx.executable._xml_validation_tool,
49        arguments = [args],
50        mnemonic = "XMLValidation",
51    )
52
53    filename = ""
54
55    if ctx.attr.filename_from_src and ctx.attr.filename != "":
56        fail("filename is set. filename_from_src cannot be true")
57    elif ctx.attr.filename != "":
58        filename = ctx.attr.filename
59    elif ctx.attr.filename_from_src:
60        filename = src
61    else:
62        filename = ctx.attrs.name
63
64    return [
65        PrebuiltFileInfo(
66            src = src,
67            dir = "etc/xml",
68            filename = filename,
69        ),
70        DefaultInfo(files = depset([src])),
71        OutputGroupInfo(_validation = depset([validation_output])),
72    ]
73
74prebuilt_xml = rule(
75    doc = """
76    prebuilt_etc_xml installs an xml file under <partition>/etc/<subdir>.
77    It also optionally validates the xml file against the schema.
78    """,
79    implementation = _prebuilt_xml_impl,
80    attrs = {
81        "src": attr.label(
82            mandatory = True,
83            allow_files = True,
84            # TODO(b/217908237): reenable allow_single_file
85            # allow_single_file = True,
86        ),
87        "schema": attr.label(
88            allow_single_file = [".dtd", ".xsd"],
89            doc = "Optional DTD or XSD that will be used to validate the xml file",
90        ),
91        "filename": attr.string(doc = "Optional name for the installed file"),
92        "filename_from_src": attr.bool(
93            doc = "Optional. When filename is not provided and" +
94                  "filename_from_src is true, name for the installed file" +
95                  "will be set from src",
96        ),
97        "_xml_validation_tool": attr.label(
98            default = "//external/libxml2:xmllint",
99            executable = True,
100            cfg = "exec",
101        ),
102    },
103)
104