blob: 65fe12a8e1bd29541281b7ccb0de6160f7715800 [file] [log] [blame]
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001// Copyright 2018 Google Inc. All rights reserved.
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
15package xml
16
17import (
18 "android/soong/android"
Alix5918d642022-06-27 20:57:44 +000019 "android/soong/bazel"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070020 "android/soong/etc"
Jiyong Park5a8d1be2018-04-25 22:57:34 +090021
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24)
25
26// prebuilt_etc_xml installs an xml file under <partition>/etc/<subdir>.
27// It also optionally validates the xml file against the schema.
28
29var (
30 pctx = android.NewPackageContext("android/soong/xml")
31
32 xmllintDtd = pctx.AndroidStaticRule("xmllint-dtd",
33 blueprint.RuleParams{
34 Command: `$XmlLintCmd --dtdvalid $dtd $in > /dev/null && touch -a $out`,
35 CommandDeps: []string{"$XmlLintCmd"},
36 Restat: true,
37 },
38 "dtd")
39
40 xmllintXsd = pctx.AndroidStaticRule("xmllint-xsd",
41 blueprint.RuleParams{
42 Command: `$XmlLintCmd --schema $xsd $in > /dev/null && touch -a $out`,
43 CommandDeps: []string{"$XmlLintCmd"},
44 Restat: true,
45 },
46 "xsd")
47
48 xmllintMinimal = pctx.AndroidStaticRule("xmllint-minimal",
49 blueprint.RuleParams{
50 Command: `$XmlLintCmd $in > /dev/null && touch -a $out`,
51 CommandDeps: []string{"$XmlLintCmd"},
52 Restat: true,
53 })
54)
55
56func init() {
Paul Duffin94b69602021-03-18 01:32:07 +000057 registerXmlBuildComponents(android.InitRegistrationContext)
Jiyong Park5a8d1be2018-04-25 22:57:34 +090058 pctx.HostBinToolVariable("XmlLintCmd", "xmllint")
59}
60
Paul Duffin94b69602021-03-18 01:32:07 +000061func registerXmlBuildComponents(ctx android.RegistrationContext) {
62 ctx.RegisterModuleType("prebuilt_etc_xml", PrebuiltEtcXmlFactory)
63}
64
Jiyong Park5a8d1be2018-04-25 22:57:34 +090065type prebuiltEtcXmlProperties struct {
66 // Optional DTD that will be used to validate the xml file.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Schema *string `android:"path"`
Jiyong Park5a8d1be2018-04-25 22:57:34 +090068}
69
70type prebuiltEtcXml struct {
Alix5918d642022-06-27 20:57:44 +000071 android.BazelModuleBase
72
Jaewoong Jung4b79e982020-06-01 10:45:49 -070073 etc.PrebuiltEtc
Jiyong Park5a8d1be2018-04-25 22:57:34 +090074
75 properties prebuiltEtcXmlProperties
76}
77
78func (p *prebuiltEtcXml) timestampFilePath(ctx android.ModuleContext) android.WritablePath {
79 return android.PathForModuleOut(ctx, p.PrebuiltEtc.SourceFilePath(ctx).Base()+"-timestamp")
80}
81
Jiyong Park5a8d1be2018-04-25 22:57:34 +090082func (p *prebuiltEtcXml) GenerateAndroidBuildActions(ctx android.ModuleContext) {
83 p.PrebuiltEtc.GenerateAndroidBuildActions(ctx)
84
85 if p.properties.Schema != nil {
Colin Cross8a497952019-03-05 22:25:09 -080086 schema := android.PathForModuleSrc(ctx, proptools.String(p.properties.Schema))
Jiyong Park5a8d1be2018-04-25 22:57:34 +090087
88 switch schema.Ext() {
89 case ".dtd":
90 ctx.Build(pctx, android.BuildParams{
91 Rule: xmllintDtd,
92 Description: "xmllint-dtd",
93 Input: p.PrebuiltEtc.SourceFilePath(ctx),
94 Output: p.timestampFilePath(ctx),
95 Implicit: schema,
96 Args: map[string]string{
97 "dtd": schema.String(),
98 },
99 })
100 break
101 case ".xsd":
102 ctx.Build(pctx, android.BuildParams{
103 Rule: xmllintXsd,
104 Description: "xmllint-xsd",
105 Input: p.PrebuiltEtc.SourceFilePath(ctx),
106 Output: p.timestampFilePath(ctx),
107 Implicit: schema,
108 Args: map[string]string{
109 "xsd": schema.String(),
110 },
111 })
112 break
113 default:
114 ctx.PropertyErrorf("schema", "not supported extension: %q", schema.Ext())
115 }
116 } else {
117 // when schema is not specified, just check if the xml is well-formed
118 ctx.Build(pctx, android.BuildParams{
119 Rule: xmllintMinimal,
120 Description: "xmllint-minimal",
121 Input: p.PrebuiltEtc.SourceFilePath(ctx),
122 Output: p.timestampFilePath(ctx),
123 })
124 }
125
126 p.SetAdditionalDependencies([]android.Path{p.timestampFilePath(ctx)})
127}
128
129func PrebuiltEtcXmlFactory() android.Module {
130 module := &prebuiltEtcXml{}
131 module.AddProperties(&module.properties)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700132 etc.InitPrebuiltEtcModule(&module.PrebuiltEtc, "etc")
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900133 // This module is device-only
Jooyung Hana0171822019-07-22 15:48:36 +0900134 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Alix5918d642022-06-27 20:57:44 +0000135 android.InitBazelModule(module)
Jiyong Park5a8d1be2018-04-25 22:57:34 +0900136 return module
137}
Alix5918d642022-06-27 20:57:44 +0000138
139type bazelPrebuiltEtcXmlAttributes struct {
140 Src bazel.LabelAttribute
141 Filename bazel.LabelAttribute
142 Dir string
143 Installable bazel.BoolAttribute
144 Filename_from_src bazel.BoolAttribute
145 Schema *string
146}
147
Chris Parsons637458d2023-09-19 20:09:00 +0000148func (p *prebuiltEtcXml) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Liz Kammerd5d12d02023-09-05 09:18:50 -0400149 baseAttrs, convertible := p.PrebuiltEtc.Bp2buildHelper(ctx)
150
151 if !convertible {
152 return
153 }
Alix5918d642022-06-27 20:57:44 +0000154
155 var schema *string
156 if p.properties.Schema != nil {
157 schema = p.properties.Schema
158 }
159
160 attrs := &bazelPrebuiltEtcXmlAttributes{
161 Src: baseAttrs.Src,
162 Filename: baseAttrs.Filename,
163 Dir: baseAttrs.Dir,
164 Installable: baseAttrs.Installable,
165 Filename_from_src: baseAttrs.Filename_from_src,
166 Schema: schema,
167 }
168
169 props := bazel.BazelTargetModuleProperties{
170 Rule_class: "prebuilt_xml",
171 Bzl_load_location: "//build/bazel/rules/prebuilt_xml.bzl",
172 }
173
174 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: p.Name()}, attrs)
175}