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